Author: Chris Boulton (chris@bigcommerce.com)
Esempio n. 1
0
 /**
  * Return an instance of the Resque_Redis class instantiated for Resque.
  *
  * @return Resque_Redis Instance of Resque_Redis.
  */
 public static function redis()
 {
     // Detect when the PID of the current process has changed (from a fork, etc)
     // and force a reconnect to redis.
     $pid = getmypid();
     if (self::$pid !== $pid) {
         self::$redis = null;
         self::$pid = $pid;
     }
     if (!is_null(self::$redis)) {
         return self::$redis;
     }
     $server = self::$redisServer;
     if (empty($server)) {
         $server = 'localhost:6379';
     }
     if (is_array($server)) {
         require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
         self::$redis = new Resque_RedisCluster($server);
     } else {
         if (strpos($server, 'unix:') === false) {
             list($host, $port) = explode(':', $server);
         } else {
             $host = $server;
             $port = null;
         }
         require_once dirname(__FILE__) . '/Resque/Redis.php';
         $redisInstance = new Resque_Redis($host, $port);
         $redisInstance->prefix(self::$namespace);
         self::$redis = $redisInstance;
     }
     self::$redis->select(self::$redisDatabase);
     return self::$redis;
 }
Esempio n. 2
0
 /**
  * Set Redis namespace (prefix) default: resque
  * @param string $namespace
  */
 public static function prefix($namespace)
 {
     if (strpos($namespace, ':') === false) {
         $namespace .= ':';
     }
     self::$defaultNamespace = $namespace;
 }
Esempio n. 3
0
 /**
  * Set Redis namespace (prefix) default: resque
  * @param string $namespace
  */
 public static function prefix($namespace)
 {
     if (substr($namespace, -1) !== ':') {
         $namespace .= ':';
     }
     self::$defaultNamespace = $namespace;
 }
 public function __construct(array $options)
 {
     $this->defaultQueue = $options['default_queue'];
     $this->setKernelOptions($options['kernel_options']);
     $this->debug = $options['debug'];
     if (!$this->debug) {
         \Resque::setBackend("{$options['host']}:{$options['port']}");
         if (isset($options['prefix'])) {
             \Resque_Redis::prefix($options['prefix']);
         }
     }
     $this->trackStatus = $options['track_status'];
 }
Esempio n. 5
0
 /**
  * @param string $host
  * @param int $port
  * @param array $kernelOptions
  * @param string $defaultQueue
  * @param bool $debug if debug is true then no calls to Resque will be made
  * @param boolean $trackStatus Set to true to be able to monitor the status of jobs
  */
 public function __construct($host, $port, $kernelOptions, $defaultQueue, $prefix, $debug, $trackStatus)
 {
     $this->defaultQueue = $defaultQueue;
     $this->setKernelOptions($kernelOptions);
     $this->debug = $debug;
     if (!$debug) {
         \Resque::setBackend("{$host}:{$port}");
         if ($prefix) {
             \Resque_Redis::prefix($prefix);
         }
     }
     $this->trackStatus = $trackStatus;
 }
Esempio n. 6
0
 public static function add($job_name, $queue_name, $args = array())
 {
     \Resque::setBackend('127.0.0.1:6379');
     if (strpos($queue_name, ':') !== false) {
         list($namespace, $queue_name) = explode(':', $queue_name);
         \Resque_Redis::prefix($namespace);
     }
     try {
         $klass = new \ReflectionClass($job_name);
         $jobId = \Resque::enqueue($queue_name, $klass->getName(), $args, true);
         return $jobId;
     } catch (\ReflectionException $rfe) {
         throw new \RuntimeException($rfe->getMessage());
     }
 }
Esempio n. 7
0
 public static function update($status, $to_job_id, $namespace)
 {
     \Resque::setBackend('127.0.0.1:6379');
     if (!empty($namespace)) {
         \Resque_Redis::prefix($namespace);
     }
     $job = new \Resque_Job_Status($to_job_id);
     if (!$job->get()) {
         throw new \RuntimeException("Job {$to_job_id} was not found");
     }
     $class = new \ReflectionObject($job);
     foreach ($class->getConstants() as $constant_value) {
         if ($constant_value == $status) {
             $job->update($status);
             return true;
         }
     }
     return false;
 }
 public function daemon()
 {
     \Resque::setBackend('127.0.0.1:6379');
     if (strpos($this->queue, ':') !== false) {
         list($namespace, $queue) = explode(':', $this->queue);
         \Resque_Redis::prefix($namespace);
         $this->queue = $queue;
     }
     if ($this->getForkInstances() > 1) {
         for ($i = 0; $i < $this->getForkInstances(); ++$i) {
             $pid = pcntl_fork();
             if ($pid == -1) {
                 throw new \RuntimeException("Could not fork worker {$i}");
             }
             $this->work();
             break;
         }
     } else {
         $this->work();
     }
 }
Esempio n. 9
0
 /**
  * @param $prefix
  */
 public function setPrefix($prefix)
 {
     \Resque_Redis::prefix($prefix);
 }
 /**
  * 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);
     $redis = Resque::redis();
     $key = Resque_Redis::getPrefix() . self::SET_KEY;
     $item = $redis->eval(self::ZPOP_SCRIPT, array($key), array($timestamp));
     if (!empty($item)) {
         return json_decode($item, true);
     }
     return false;
 }
Esempio n. 11
0
 public function testJobWithNamespace()
 {
     Resque_Redis::prefix('php');
     $queue = 'jobs';
     $payload = array('another_value');
     Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);
     $this->assertEquals(Resque::queues(), array('jobs'));
     $this->assertEquals(Resque::size($queue), 1);
     Resque_Redis::prefix('resque');
     $this->assertEquals(Resque::size($queue), 0);
 }
Esempio n. 12
0
 /**
  * @param string $prefix
  *
  * @return Resque
  */
 public function setPrefix($prefix)
 {
     \Resque_Redis::prefix($prefix);
     return $this;
 }
 /**
  * Check if a queue has anything in it which is the same as an existence check
  *
  * @param $queue
  * @return bool
  */
 public function queueIsEmpty($queue)
 {
     return $this->redis->llen($queue) === 0;
 }
Esempio n. 14
0
 /**
  * @dataProvider bogusDsnStringProvider
  * @expectedException InvalidArgumentException
  */
 public function testParsingBogusDsnStringThrowsException($dsn)
 {
     // The next line should throw an InvalidArgumentException
     $result = Resque_Redis::parseDsn($dsn);
 }
    if (!empty($VVERBOSE)) {
        $logLevel = ResqueRepeater_Worker::LOG_VERBOSE;
    }
}
// Check for jobs every $interval seconds
$interval = 5;
$INTERVAL = getenv('INTERVAL');
if (!empty($INTERVAL)) {
    $interval = $INTERVAL;
}
// Load the user's application if one exists
$APP_INCLUDE = getenv('APP_INCLUDE');
if ($APP_INCLUDE) {
    if (!file_exists($APP_INCLUDE)) {
        die('APP_INCLUDE (' . $APP_INCLUDE . ") does not exist.\n");
    }
    require_once $APP_INCLUDE;
}
$PREFIX = getenv('PREFIX');
if (!empty($PREFIX)) {
    fwrite(STDOUT, '*** Prefix set to ' . $PREFIX . "\n");
    Resque_Redis::prefix($PREFIX);
}
$worker = new ResqueRepeater_Worker();
$worker->logLevel = $logLevel;
$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
    file_put_contents($PIDFILE, getmypid()) or die('Could not write PID information to ' . $PIDFILE);
}
fwrite(STDOUT, "*** Starting scheduler worker\n");
$worker->work($interval);