/** * Do a job * @param int $jobId * @throws \InvalidArgumentException */ public function work($jobId) { $job = new Job(); if (!$job->load($jobId)) { throw new \InvalidArgumentException(sprintf("No job found for Job ID: %s", $jobId)); } $job->setStartedOn(new \DateTime()); $job->setStartedBy(get_current_user()); $job->save(); $this->path = Config::get('base_path'); $arguments = $job->getArguments(); $argumentString = implode(" ", array_map(function ($argument) { return "\"{$argument}\""; }, $arguments)); $command = new Process("php -f {$this->path}vadmin.php {$job->getCallable()} {$argumentString}"); while (!$command->isFinished()) { sleep(1); } $job->setFinishedOn(new \DateTime()); $job->setSummary($command->getOutput()); $job->save(); }
/** * Schedule a single job * @param Job $job * @param \DateTime $scheduleTime * @return boolean * @throws \Exception */ protected function _scheduleJob(Job $job, \DateTime $scheduleTime) { $cronExpr = $this->getExpressionService()->formatCronExpression($job->getCronExpr()); if (!$this->getExpressionService()->isValidCronExpression($cronExpr)) { return FALSE; } if ($this->canSchedule($cronExpr, $scheduleTime)) { $job->setScheduledFor($scheduleTime); if (!$job->save()) { throw new \Exception(sprintf("Failed to schedule job: %s, reason: %s", $job->getName(), $job->getLastError())); } $job->setId(NULL); //allow it to be saved again return true; } return false; }
/** * * @param string $jobName * @param mixed $jobCallable * @param string $cronExpr */ public static function add($jobName, $jobCallable, $jobArguments = array(), $cronExpr = '0 0 * * *') { $job = new Job(array('name' => $jobName, 'callable' => $jobCallable, 'cron_expr' => $cronExpr)); $job->setArguments($jobArguments); return self::$_jobs[] = $job; }