/** * @return bool * @throws \Exception Exception. */ public function main() { $this->out('<info>Started up</info>', 1, Shell::VERBOSE); $job = $this->enqueue('DelayedJobs.Test', ['type' => 'success']); if (!$job) { throw new \Exception("Success Job could not be loaded"); } $this->out('<success>Loaded Successful Job: Waiting 15 seconds to check if it was successful</success>', 1, Shell::VERBOSE); sleep(10); $job = JobManager::instance()->fetchJob($job->getId()); if ($job->getStatus() !== Job::STATUS_SUCCESS) { throw new \Exception("Successful job was not successful"); } $this->out('<success>Test Success</success>', 1, Shell::VERBOSE); //** Loading Job that will fail */ $job = $this->enqueue('DelayedJobs.Test', ['type' => 'fail'], ['maxRetries' => 1]); if (!$job) { throw new \Exception("Failed Job could not be loaded"); } $this->out('<success>Loaded Failed Job: Waiting 15 seconds to check if it failed</success>', 1, Shell::VERBOSE); sleep(10); $job = JobManager::instance()->fetchJob($job->getId()); if ($job->getStatus() !== Job::STATUS_BURRIED) { throw new \Exception("Failed Job did not fail"); } $this->out('<success>Test Success</success>', 1, Shell::VERBOSE); $this->out('<info>All Done</info>', 1, Shell::VERBOSE); return true; }
public function executeJob(Job $job) { $this->out(sprintf(' - <info>%s</info>', $job->getWorker()), 1, Shell::VERBOSE); $this->out(' - Executing job', 1, Shell::VERBOSE); $this->djLog(__('Executing: {0}', $job->getId())); $start = microtime(true); $response = JobManager::instance()->execute($job, $this->param('force')); if ($response instanceof \Throwable) { $this->_failedJob($job, $response); } else { $this->djLog(__('Done with: {0}', $job->getId())); $this->out(sprintf('<success> - Execution successful</success> :: <info>%s</info>', $response), 1, Shell::VERBOSE); } $end = microtime(true); $this->out(sprintf(' - Took: %.2f seconds', $end - $start), 1, Shell::VERBOSE); }
public function main() { $this->heartbeat(); $this->_manager = JobManager::instance(); $this->_manager->eventManager()->on('DelayedJob.beforeJobExecute', [$this, 'beforeExecute']); $this->_manager->eventManager()->on('DelayedJob.afterJobExecute', [$this, 'afterExecute']); $this->_manager->eventManager()->on('DelayedJob.heartbeat', [$this, 'heartbeat']); $this->_manager->startConsuming(); $this->stopHammerTime(); }
protected function _rabbitStatsWithCharts() { static $ready_points = []; static $unacked_points = []; $max_length = 50; $rabbit_status = JobManager::instance()->getMessageBroker()->queueStatus(); if (empty($rabbit_status)) { return; } if (empty($ready_points) || $this->loop_counter % 4 === 0) { $ready_points[] = $rabbit_status['messages_ready']; $unacked_points[] = $rabbit_status['messages_unacknowledged']; } if (count($ready_points) > $max_length) { array_splice($ready_points, -$max_length); array_splice($unacked_points, -$max_length); } $this->helper('DelayedJobs.Sparkline')->output(['data' => $ready_points, 'title' => "MQ Ready", 'length' => $max_length, 'formatter' => '%7d']); $this->helper('DelayedJobs.Sparkline')->output(['data' => $unacked_points, 'title' => 'MQ Unacked', 'length' => $max_length, 'formatter' => '%7d']); }
public function revive() { $stats = JobManager::instance()->getMessageBroker()->queueStatus(); if ($stats['messages'] > 0) { $this->out(__('<error>There are {0} messages currently queued</error>', $stats['messages'])); $this->out('We cannot reliablily determine which messages to requeue unless the RabbitMQ queue is empty.'); $this->_stop(1); } $this->loadModel('DelayedJobs.DelayedJobs'); $sequences = $this->DelayedJobs->find()->select(['sequence'])->group('sequence')->where(['status in' => [Job::STATUS_NEW, Job::STATUS_FAILED], 'run_at <=' => Time::now(), 'sequence is not' => null])->hydrate(false)->map(function ($sequence) { return $this->DelayedJobs->find()->select(['id', 'priority'])->where(['status in' => [Job::STATUS_NEW, Job::STATUS_FAILED], 'run_at <=' => Time::now(), 'sequence' => $sequence['sequence']])->order(['id' => 'ASC'])->hydrate(false)->first(); }); $no_sequences = $this->DelayedJobs->find()->select(['id', 'priority'])->where(['status in' => [Job::STATUS_NEW, Job::STATUS_FAILED], 'run_at <=' => Time::now(), 'sequence is' => null])->hydrate(false)->all(); $all_jobs = $sequences->append($no_sequences); foreach ($all_jobs as $job) { /** * @var \DelayedJobs\Model\Entity\DelayedJob $job */ if ($this->_io->level() < Shell::VERBOSE) { $this->out('.', 0, Shell::QUIET); } $this->out(__(' - Queing job <info>{0}</info>', $job['id']), 0, Shell::VERBOSE); if (JobManager::instance()->enqueuePersisted($job['id'], $job['priority'])) { $this->out(' :: <success>√</success>', 1, Shell::VERBOSE); } else { $this->out(' :: <error>X</error>', 1, Shell::VERBOSE); } } }