/** * Execute the command. */ public function fire() { $take = $this->option('take'); $ready = $this->scheduler->findReady($take); if (count($ready) < 1) { $this->line('No jobs ready to run.'); return; } $defaultConnection = $this->config->get('jobs.queue.connection'); $defaultQueue = $this->config->get('jobs.queue.id'); /** @var Job $job */ foreach ($ready as $job) { $this->pusher->push(RunTaskCommand::class, ['job_id' => $job->id], Std::coalesce($job->queue_connection, $defaultConnection), Std::coalesce($job->queue_name, $defaultQueue)); $job = $job->fresh(); // Sometimes, a developer might be running a sync queue. This means // we have to check if the jobs is still in a scheduled state. // Only then, we will update the status to queued. Otherwise, the // job gets stuck in a queued state. if ($job->state === JobState::SCHEDULED) { $job->state = JobState::QUEUED; $job->save(); } $this->line('Queued Job ID: ' . $job->id . ' ' . $job->task); } $this->line('Finished.'); }
/** * 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))); } }