/** * Enqueue a job for execution at a given timestamp. * * Identical to Resque::enqueue, however the first argument is a timestamp * (either UNIX timestamp in integer format or an instance of the DateTime * class in PHP). * * @param DateTime|int $at Instance of PHP DateTime object or int of UNIX timestamp. * @param string $queue The name of the queue to place the job in. * @param string $class The name of the class that contains the code to execute the job. * @param array $args Any optional arguments that should be passed when the job is executed. */ public static function enqueueAt($at, $queue, $class, $args = array()) { self::validateJob($class, $queue); $job = self::jobToHash($queue, $class, $args); self::delayedPush($at, $job); Event::trigger('afterSchedule', array('at' => $at, 'queue' => $queue, 'class' => $class, 'args' => $args)); }
/** * Schedule all of the delayed jobs for a given timestamp. * * Searches for all items for a given timestamp, pulls them off the list of * delayed jobs and pushes them across to Resque. * * @param DateTime|int $timestamp Search for any items up to this timestamp to schedule. */ public function enqueueDelayedItemsForTimestamp($timestamp) { $item = null; while ($item = ResqueScheduler::nextItemForTimestamp($timestamp)) { $this->log('queueing ' . $item['class'] . ' in ' . $item['queue'] . ' [delayed]'); Event::trigger('beforeDelayedEnqueue', array('queue' => $item['queue'], 'class' => $item['class'], 'args' => $item['args'])); $payload = array_merge(array($item['queue'], $item['class']), $item['args']); call_user_func_array('resque\\Resque::enqueue', $payload); } }
public function testStopListeningRemovesListener() { $callback = 'beforePerformEventCallback'; $event = 'beforePerform'; Event::listen($event, array($this, $callback)); Event::stopListening($event, array($this, $callback)); $job = $this->getEventTestJob(); $this->worker->perform($job); $this->worker->work(0); $this->assertNotContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was called though Event::stopListening was called'); }
/** * Perform necessary actions to start a worker. */ private function startup() { $this->registerSigHandlers(); $this->pruneDeadWorkers(); Event::trigger('beforeFirstFork', $this); $this->registerWorker(); }
/** * Create a new job and save it to the specified queue. * * @param string $queue The name of the queue to place the job in. * @param string $class The name of the class that contains the code to execute the job. * @param array $args Any optional arguments that should be passed when the job is executed. * @param boolean $trackStatus Set to true to be able to monitor the status of a job. * * @return string */ public static function enqueue($queue, $class, $args = null, $trackStatus = false) { $result = Job::create($queue, $class, $args, $trackStatus); if ($result) { Event::trigger('afterEnqueue', array('class' => $class, 'args' => $args, 'queue' => $queue)); } return $result; }
<?php require_once __DIR__ . '/../autoload.php'; use resque\core\Event; // Somewhere in our application, we need to register: Event::listen('afterEnqueue', array('ResquePlugin', 'afterEnqueue')); Event::listen('beforeFirstFork', array('ResquePlugin', 'beforeFirstFork')); Event::listen('beforeFork', array('ResquePlugin', 'beforeFork')); Event::listen('afterFork', array('ResquePlugin', 'afterFork')); Event::listen('beforePerform', array('ResquePlugin', 'beforePerform')); Event::listen('afterPerform', array('ResquePlugin', 'afterPerform')); Event::listen('onFailure', array('ResquePlugin', 'onFailure')); class ResquePlugin { public static function afterEnqueue($class, $arguments) { echo "Job was queued for " . $class . ". Arguments:"; print_r($arguments); } public static function beforeFirstFork($worker) { echo "Worker started. Listening on queues: " . implode(', ', $worker->queues(false)) . "\n"; } public static function beforeFork($job) { echo "Just about to fork to run " . $job; } public static function afterFork($job) { echo "Forked to run " . $job . ". This is the child process.\n"; }
/** * Mark the current job as having failed. * * @param $exception */ public function fail($exception) { Event::trigger('onFailure', array('exception' => $exception, 'job' => $this)); $this->updateStatus(Status::STATUS_FAILED); Failure::create($this->payload, $exception, $this->worker, $this->queue); Stat::incr('failed'); Stat::incr('failed:' . $this->worker); }