/**
  * 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.
  * @return array the token of delayed job and timestamp
  */
 public static function enqueueAt($at, $queue, $class, $args = array())
 {
     $id = md5(uniqid('', true));
     // merge delayed job token to the args
     $args = array_merge($args, array('djID' => $id));
     self::validateJob($class, $queue);
     $job = self::jobToHash($queue, $class, $args);
     self::delayedPush($at, $job);
     Resque_Event::trigger('afterSchedule', array('at' => $at, 'queue' => $queue, 'class' => $class, 'args' => $args));
     return array('token' => $id, 'at' => self::getTimestamp($at));
 }
Example #2
0
 /**
  * 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]');
         Resque_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::enqueue', $payload);
         call_user_func_array(array(new Resque(), 'enqueue'), $payload);
     }
 }
Example #3
0
 /**
  * Perform necessary actions to start a worker.
  */
 protected function startup()
 {
     $this->log(array('message' => 'Starting worker ' . $this, 'data' => array('type' => 'start', 'worker' => (string) $this)), self::LOG_TYPE_INFO);
     $this->registerSigHandlers();
     $this->pruneDeadWorkers();
     Resque_Event::trigger('beforeFirstFork', $this);
     $this->registerWorker();
 }
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'];
 }
/**
 * Resque_EventHandler class perferm the default behavior for events.
 *
 * @package system\resque\components
 * @author Vincent.Hou <*****@*****.**>
 *
 */
Resque_Event::listen('afterEnqueue', array('Resque_EmailEventHandler', 'afterEnqueue'));
Resque_Event::listen('beforePerform', array('Resque_EmailEventHandler', 'beforePerform'));
Resque_Event::listen('afterPerform', array('Resque_EmailEventHandler', 'afterPerform'));
Resque_Event::listen('beforeFirstFork', array('Resque_EmailEventHandler', 'beforeFirstFork'));
Resque_Event::listen('beforeFork', array('Resque_EmailEventHandler', 'beforeFork'));
Resque_Event::listen('afterFork', array('Resque_EmailEventHandler', 'afterFork'));
Resque_Event::listen('onFailure', array('Resque_EmailEventHandler', 'onFailure'));
Resque_Event::listen('afterSchedule', array('Resque_EmailEventHandler', 'afterSchedule'));
Resque_Event::listen('afterRetry', array('Resque_EmailEventHandler', 'afterRetry'));
class Resque_EmailEventHandler
{
    public static function afterEnqueue($class, $args, $queue)
    {
        ResqueUtil::log('[afterEnqueue]Job class ' . $class . ' enqueued ' . $queue . ' queue with arguments:' . CJSON::encode($args));
    }
    public static function beforePerform($job)
    {
        ResqueUtil::log('[beforePerform]Begin to perform job ' . $job);
    }
    public static function afterPerform($job)
    {
        ResqueUtil::log('[afterPerform]Just performed job ' . $job);
    }
    public static function beforeFirstFork($worker)
Example #6
0
 * @author Vincent.Hou <*****@*****.**>
 */
/**
 * Resque_EventHandler class perferm the default behavior for events.
 *
 * @package system\resque\components
 * @author Vincent.Hou <*****@*****.**>
 *
 */
// Resque_Event::listen('afterEnqueue', array('Resque_EventHandler', 'afterEnqueue'));
// Resque_Event::listen('beforePerform', array('Resque_EventHandler', 'beforePerform'));
// Resque_Event::listen('afterPerform', array('Resque_EventHandler', 'afterPerform'));
// Resque_Event::listen('beforeFirstFork', array('Resque_EventHandler', 'beforeFirstFork'));
// Resque_Event::listen('beforeFork', array('Resque_EventHandler', 'beforeFork'));
// Resque_Event::listen('afterFork', array('Resque_EventHandler', 'afterFork'));
Resque_Event::listen('onFailure', array('Resque_EventHandler', 'onFailure'));
// Resque_Event::listen('afterSchedule', array('Resque_EventHandler', 'afterSchedule'));
// Resque_Event::listen('afterRetry', array('Resque_EventHandler', 'afterRetry'));
class Resque_EventHandler
{
    public static function afterEnqueue($class, $args, $queue)
    {
        ResqueUtil::log('[afterEnqueue]Job class ' . $class . ' enqueued ' . $queue . ' queue with arguments:' . CJSON::encode($args));
    }
    public static function beforePerform($job)
    {
        ResqueUtil::log('[beforePerform]Begin to perform job ' . $job);
    }
    public static function afterPerform($job)
    {
        ResqueUtil::log('[afterPerform]Just performed job ' . $job);
Example #7
0
<?php

namespace backend\modules\resque\components\lib\Resque;

use backend\modules\resque\components\ResqueUtil;
require_once dirname(__FILE__) . '/../Resque.php';
require_once dirname(__FILE__) . '/Worker.php';
Resque_Event::listen('afterEnqueue', array('Resque_Scaler', 'afterEnqueue'));
Resque_Event::listen('beforeFork', array('Resque_Scaler', 'beforeFork'));
class Resque_Scaler
{
    // define how many jobs require how many workers.
    public static $SCALE_SETTING = array(15 => 2, 25 => 3, 40 => 4, 60 => 5);
    public static function afterEnqueue($class, $arguments, $queue)
    {
        ResqueUtil::log("Job was queued for " . $class . ".\n");
        if (self::check_need_worker($queue)) {
            ResqueUtil::log("we need more workers\n");
            self::add_worker($queue);
        } else {
            ResqueUtil::log("workers is enough.\n");
        }
    }
    public static function beforeFork($job)
    {
        ResqueUtil::log("Just about to perform " . $job . "\n");
        if (self::check_kill_worker($job->queue)) {
            ResqueUtil::log("too many workers...kill this one.\n");
            // NOTE: tried to kill with $worker->shutdown but it's not working. use kill to send SIGQUIT instead.
            $server_workers = self::server_workers(self::get_all_workers($job->queue));
            $current_workers = $server_workers[self::get_hostname()];
Example #8
0
 /**
  * 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 = true)
 {
     $result = Resque_Job::create($queue, $class, $args, $trackStatus);
     if ($result) {
         Resque_Event::trigger('afterEnqueue', array('class' => $class, 'args' => $args, 'queue' => $queue));
     }
     return $result;
 }
Example #9
0
 /**
  * Stop listen on the pre-defined event
  *
  * This makes it possible to stop the registered callback function for a specified event.
  * The supported events can be found in the project wiki
  *
  * @param string $event
  *            Name of event to listen on.
  * @param mixed $callback
  *            Any callback callable by call_user_func_array.
  */
 public function stopListening($event, $callback)
 {
     Resque_Event::stopListening($event, $callback);
 }