Example #1
0
 /**
  * The primary loop for a worker.
  *
  * Every $interval (seconds), the scheduled queue will be checked for jobs
  * that should be pushed to Resque.
  *
  * @param int $interval
  *            How often to check schedules.
  */
 public function work($interval = null)
 {
     if ($interval !== null) {
         $this->interval = $interval;
     }
     Resque::redis()->set('worker:delayed:started', time());
     $this->updateProcLine('Starting');
     while (true) {
         $this->handleDelayedItems();
         $this->sleep();
     }
 }
Example #2
0
 /**
  * Stop tracking the status of a job.
  */
 public function stop()
 {
     Resque::redis()->del((string) $this);
 }
Example #3
0
 /**
  * Return an object describing the job this worker is currently working on.
  *
  * @return object Object with details of current job.
  */
 public function job()
 {
     $job = Resque::redis()->get('worker:' . $this);
     if (!$job) {
         return array();
     } else {
         return json_decode($job, true);
     }
 }
Example #4
0
 /**
  * Re-queue the current job.
  *
  * @param int $enqueuedAt
  *            Enqueue timestamp
  * @return string the job id
  */
 public function recreate($enqueuedAt = 0, $isLast = false)
 {
     if ($isLast) {
         $queue = Constant::$LAST_RETRY_QUEUE;
     } else {
         $queue = $this->queue;
     }
     $status = new Resque_Job_Status($this->payload['id']);
     $this->payload['args'][0]['id'] = $this->payload['id'];
     if (Resque::redis()->exists('worker:delayed:started')) {
         $enqueuedAt += (int) time();
         $jobItem = array_merge($this->payload, array('queue' => $queue));
         unset($jobItem['id']);
         call_user_func_array('ResqueScheduler::delayedPush', array($enqueuedAt, $jobItem));
     } else {
         self::create($queue, $this->payload['class'], $this->payload['args'], $status->isTracking());
     }
     Resque_Event::trigger('afterRetry', array('payload' => $this->payload, 'enqueuedAt' => $enqueuedAt));
     return $this->payload['id'];
 }
Example #5
0
 /**
  * Expire a statistic with the given time.
  *
  * @param string $stat
  *            The name of the statistic to expire.
  * @param integer $timeout
  *            After the timeout has expired, the key will automatically be deleted.
  * @return boolean True if successful, false if not.
  */
 public static function expire($stat, $timeout)
 {
     return (bool) Resque::redis()->expire('stat:' . $stat, $timeout);
 }
 /**
  * Pop a job off the delayed queue for a given timestamp.
  *
  * @param DateTime|int $timestamp
  *            Instance of DateTime or UNIX timestamp.
  * @return array Matching job at timestamp.
  */
 public static function nextItemForTimestamp($timestamp)
 {
     $timestamp = self::getTimestamp($timestamp);
     $key = 'delayed:' . $timestamp;
     $item = json_decode(Resque::redis()->lpop($key), true);
     self::cleanupTimestamp($key, $timestamp);
     return $item;
 }
Example #7
0
 /**
  * List the pending jobs in queue
  *
  * @return array the pending jobs information in JSON format
  */
 public function getPendingJobsInQueue($queue)
 {
     return Resque::redis()->lrange('queue:' . $queue, 0, -1);
 }