/** * Execute the console command. * * @return mixed */ public function fire() { $checks = Check::where('paused', '=', 0)->get(); foreach ($checks as $check) { $hasChecked = CheckResult::where('check_id', '=', $check->id)->where('created_at', '>', date('Y-m-d H:i:s', time() - $check->interval * 60))->count(); if ($hasChecked == 0) { Queue::push('CheckWebsite', $check->id); } } }
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(); } } }
private function statusCount($success, $hours) { return CheckResult::where('check_id', '=', $this->id)->where('success', '=', $success)->where('created_at', '>=', DB::raw(sprintf('DATE_SUB(NOW(), INTERVAL %s HOUR)', $hours)))->count(); }