/** * Process a job. * * Note: A handler implementation does not need to worry about exception * handling and retries. All this is automatically managed by the task * runner. * * @param Job $job * @param JobSchedulerInterface $scheduler */ public function fire(Job $job, JobSchedulerInterface $scheduler) { $jobs = Job::query()->whereIn('state', [JobState::FAILED, JobState::COMPLETE, JobState::CANCELLED]); $repeatIn = $job->get('repeatIn', -1); // How far back to look $days = $job->get('days', 30); $jobs->where('created_at', '<', Carbon::now()->subDays($days)); $job->append('Removing jobs from ' . $days . ' days ago'); $total = $jobs->count(); $processed = 0; $jobs->chunk(25, function ($jobs) use(&$processed, $total, $job) { $processed += count($jobs); foreach ($jobs as $staleJob) { $staleJob->delete(); } $job->append('Progress: ' . $processed . '/' . $total); }); if ($repeatIn > -1) { $scheduler->pushCopy($job, Carbon::now()->addMinutes(max($repeatIn, 1)), Carbon::now()->addMinutes($job->get('expiresAfter', 1440))); } }
/** * Get jobs that have been scheduled an ready to run. * * @param int $take * * @return Collection */ public function findReady($take = 20) { $now = Carbon::now(); return Job::query()->where('state', JobState::SCHEDULED)->where('run_at', '<', $now)->where(function (Builder $query) use($now) { $query->where('expires_at', '>', $now)->orWhereNull('expires_at'); })->orderBy('run_at', 'asc')->take($take)->get(); }
/** * Delete a specific job. * * @param mixed $jobId * * @return mixed */ public function delete($jobId) { return Job::query()->where('id', $jobId)->delete(); }