log = $log; $this->entityManager = $entityManager; $this->queueUtil = $queueUtil; $this->scheduleUtil = $scheduleUtil; $this->jobFactory = $jobFactory; } public function process(): void { $activeScheduledJobList = $this->scheduleUtil->getActiveScheduledJobList(); $runningScheduledJobIdList = $this->queueUtil->getRunningScheduledJobIdList(); foreach ($activeScheduledJobList as $scheduledJob) { try { $this->createJobsFromScheduledJob($scheduledJob, $runningScheduledJobIdList); } catch (Throwable $e) { $id = $scheduledJob->getId(); $this->log->error("Scheduled Job '{$id}': " . $e->getMessage()); } } } private function createJobsFromScheduledJob( ScheduledJobEntity $scheduledJob, array $runningScheduledJobIdList ): void { $scheduling = $scheduledJob->getScheduling(); $id = $scheduledJob->getId(); $asSoonAsPossible = in_array($scheduling, $this->asSoonAsPossibleSchedulingList); if ($asSoonAsPossible) { $executeTime = date('Y-m-d H:i:s'); } else { try { $cronExpression = CronExpression::factory($scheduling); } catch (Exception $e) { $this->log->error( "Scheduled Job '{$id}': Scheduling expression error: " . $e->getMessage() . '.' ); return; } try { $executeTime = $cronExpression->getNextRunDate()->format('Y-m-d H:i:s'); } catch (Exception $e) { $this->log->error( "Scheduled Job '{$id}': Unsupported scheduling expression '{$scheduling}'." ); return; } $jobAlreadyExists = $this->queueUtil->hasScheduledJobOnMinute($id, $executeTime); if ($jobAlreadyExists) { return; } } $jobName = $scheduledJob->getJob(); if ($this->jobFactory->isPreparable($jobName)) { $jobObj = $this->jobFactory->create($jobName); $jobObj->prepare($scheduledJob, $executeTime); return; } if (in_array($id, $runningScheduledJobIdList)) { return; } $pendingCount = $this->queueUtil->getPendingCountByScheduledJobId($id); if ($asSoonAsPossible) { if ($pendingCount > 0) { return; } } else { if ($pendingCount > 1) { return; } } $this->entityManager->createEntity('Job', [ 'name' => $scheduledJob->getName(), 'status' => JobManager::PENDING, 'scheduledJobId' => $id, 'executeTime' => $executeTime, ]); } }