public function fire(Illuminate\Queue\Jobs\Job $job, $data) { $check = Check::findOrFail($data); $headers = array(); $options = array(); if (!empty($check->username)) { $options['auth'] = array($check->username, $check->password); } try { $latencyStart = microtime(true); $response = Requests::get($check->url, $headers, $options); $latency = round((microtime(true) - $latencyStart) * 1000); } catch (Requests_Exception $e) { $latency = round((microtime(true) - $latencyStart) * 1000); if (CheckResult::create(array('check_id' => $check->id, 'status_code' => 0, 'success' => false, 'latency' => $latency, 'content' => trans('check.errors.resolve-host', array('host' => $check->url))))) { $job->delete(); } else { $job->release(); } return true; } $lastCheck = CheckResult::where('check_id', '=', $check->id)->orderBy('created_at', 'desc')->first(); if ($response->success) { if (isset($lastCheck) && !$lastCheck->success) { Mail::queue('email.check.online', array('id' => $check->id, 'title' => $check->title), function ($message) use($check) { $message->to($check->theUser->email)->subject(trans('check.job.email.online.subject', array('title' => $check->title))); }); } if (CheckResult::create(array('check_id' => $check->id, 'status_code' => $response->status_code, 'success' => true, 'latency' => $latency))) { $job->delete(); } else { $job->release(); } } else { if (isset($lastCheck) && $lastCheck->success) { Mail::queue('email.check.offline', array('id' => $check->id, 'title' => $check->title, 'statusCode' => $response->status_code), function ($message) use($check) { $message->to($check->theUser->email)->subject(trans('check.job.email.offline.subject', array('title' => $check->title))); }); } if (CheckResult::create(array('check_id' => $check->id, 'status_code' => $response->status_code, 'success' => false, 'content' => $response->body, 'headers' => json_encode($response->headers), 'latency' => $latency))) { $job->delete(); } else { $job->release(); } } }
/** * Run the user seeds. * * @return void */ public function run() { DB::table('checks')->delete(); $check = Check::create(array('url' => 'http://www.example.com', 'user_id' => 1, 'company_id' => 1, 'port' => 80, 'interval' => 5)); $start = \Carbon\Carbon::now()->subDays(3); $now = \Carbon\Carbon::now(); while ($start->diffInSeconds($now, false) > 0) { $success = rand(0, 100) != 0; CheckResult::create(array('check_id' => $check->id, 'status_code' => $success ? 200 : 500, 'latency' => $success ? rand(100, 4000) : rand(10000, 60000), 'success' => $success, 'created_at' => $start)); $start->addMinutes(5); } $check = Check::create(array('url' => 'http://www.example.org', 'user_id' => 2, 'company_id' => 2, 'port' => 8080, 'interval' => 15)); $start = \Carbon\Carbon::now()->subDays(1); $now = \Carbon\Carbon::now(); while ($start->diffInSeconds($now, false) > 0) { $success = rand(0, 100) != 0; CheckResult::create(array('check_id' => $check->id, 'status_code' => $success ? 200 : 500, 'latency' => $success ? rand(100, 4000) : rand(10000, 60000), 'success' => $success, 'created_at' => $start)); $start->addMinutes(15); } }