redis() public static method

Return an instance of the Resque_Redis class instantiated for Resque.
public static redis ( ) : Resque_Redis
return Resque_Redis Instance of Resque_Redis.
示例#1
0
	public static function setBackend($server)
	{
		list($host, $port) = explode(':', $server);

		require_once dirname(__FILE__).'/Resque/Redis.php';
		self::$redis = new Resque_Redis($host, $port);
	}
示例#2
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;
 }
示例#3
0
 private function getRedis()
 {
     $redis = \Resque::$redis;
     if ($redis === null) {
         $redis = \Resque::redis();
     }
     return $redis;
 }
示例#4
0
 /**
  * @expectedException Resque_RedisException
  */
 public function testRedisExceptionsAreSurfaced()
 {
     $mockCredis = $this->getMockBuilder('Credis_Client')->setMethods(['connect', '__call'])->getMock();
     $mockCredis->expects($this->any())->method('__call')->will($this->throwException(new CredisException('failure')));
     Resque::setBackend(function ($database) use($mockCredis) {
         return new Resque_Redis('localhost:6379', $database, $mockCredis);
     });
     Resque::redis()->ping();
 }
示例#5
0
 /**
  * @return Job[]
  */
 public function jobs()
 {
     $result = \Resque::redis()->lrange('queue:' . $this->name, 0, -1);
     $jobs = [];
     foreach ($result as $job) {
         $jobs[] = (new \Resque_Job($this->name, \json_decode($job, true)))->getInstance();
     }
     return $jobs;
 }
示例#6
0
 public function getJobs($start = 0, $stop = -1)
 {
     $jobs = \Resque::redis()->lrange('queue:' . $this->name, $start, $stop);
     $result = array();
     foreach ($jobs as $job) {
         $job = new \Resque_Job($this->name, \json_decode($job, true));
         $result[] = $job->getInstance();
     }
     return $result;
 }
示例#7
0
 /**
  * Peek
  *
  * @param string $queue The name of the queue
  * @param integer $start
  * @param integer $count
  *
  * @return array List of jobs
  *
  */
 public static function peek($queue, $start = 0, $count = 1000)
 {
     $jobs = \Resque::redis()->lrange('queue:' . $queue, $start, $count);
     $curr_jobs = array();
     if (is_array($jobs)) {
         foreach ($jobs as $job) {
             $curr_jobs[] = json_decode($job, TRUE);
         }
     }
     return $curr_jobs;
 }
示例#8
0
 public static function requeue($id)
 {
     //        item = all(id)
     //        item['retried_at'] = Time.now.rfc2822
     //        Resque.redis.lset(:failed, id, Resque.encode(item))
     //        Job.create(item['queue'], item['payload']['class'], *item['payload']['args'])
     $item = current(self::all($id, 1));
     $item->retried_at = strftime('%a %b %d %H:%M:%S %Z %Y');
     $data = json_encode($item);
     \Resque::redis()->lset('failed', $id, $data);
     \Resque::enqueue($item->queue, $item->payload->class, (array) $item->payload->args[0]);
 }
示例#9
0
文件: Resque.php 项目: Rming/novophp
 /**
  * Given a host/port combination separated by a colon, set it as
  * the redis server that Resque will talk to.
  *
  * @param mixed $server Host/port combination separated by a colon, or
  * a nested array of servers with host/port pairs.
  */
 public static function setBackend($server, $database = 0)
 {
     if (is_array($server)) {
         require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
         self::$redis = new Resque_RedisCluster($server);
     } else {
         list($host, $port) = explode(':', $server);
         require_once dirname(__FILE__) . '/Resque/Redis.php';
         self::$redis = new Resque_Redis($host, $port);
     }
     self::redis()->select($database);
 }
示例#10
0
 /**
  * Initialize a failed job class and save it (where appropriate).
  *
  * @param object $payload Object containing details of the failed job.
  * @param object $exception Instance of the exception that was thrown by the failed job.
  * @param object $worker Instance of Resque_Worker that received the job.
  * @param string $queue The name of the queue the job was fetched from.
  */
 public function __construct($payload, $exception, $worker, $queue)
 {
     $data = new stdClass();
     $data->failed_at = strftime('%a %b %d %H:%M:%S %Z %Y');
     $data->payload = $payload;
     $data->exception = get_class($exception);
     $data->error = $exception->getMessage();
     $data->backtrace = explode("\n", $exception->getTraceAsString());
     $data->worker = (string) $worker;
     $data->queue = $queue;
     $data = json_encode($data);
     Resque::redis()->rpush('failed', $data);
 }
示例#11
0
 /**
  * fork() helper method for php-resque that handles issues PHP socket
  * and phpredis have with passing around sockets between child/parent
  * processes.
  *
  * Will close connection to Redis before forking.
  *
  * @return int Return vars as per pcntl_fork()
  */
 public static function fork()
 {
     if (!function_exists('pcntl_fork')) {
         return -1;
     }
     // Close the connection to Redis before forking.
     // This is a workaround for issues phpredis has.
     self::$redis = null;
     $pid = pcntl_fork();
     if ($pid === -1) {
         throw new RuntimeException('Unable to fork child worker.');
     }
     return $pid;
 }
示例#12
0
 /**
  * Startup callback.
  *
  * Initializes defaults.
  */
 public function startup()
 {
     $resqueLib = Configure::read('CakeResque.Resque.lib');
     $schedulerLib = Configure::read('CakeResque.Scheduler.lib');
     $pluginVendorPath = CakePlugin::path('CakeResque') . 'vendor' . DS;
     if (substr($resqueLib, 0, 1) !== '/') {
         $resqueLib = $pluginVendorPath . $resqueLib;
     }
     $this->_resqueLibrary = realpath($resqueLib) . DS;
     if (substr($schedulerLib, 0, 1) !== '/') {
         $schedulerLib = $pluginVendorPath . $schedulerLib;
     }
     $this->_resqueSchedulerLibrary = realpath($schedulerLib) . DS;
     $this->ResqueStatus = new ResqueStatus\ResqueStatus(Resque::redis());
     $this->stdout->styles('success', array('text' => 'green'));
     $this->stdout->styles('bold', array('bold' => true));
 }
示例#13
0
 protected function getJobs()
 {
     $jobs = new ArrayList();
     $resqueJobs = Resque::redis()->lrange('queue:' . $this->Name, 0, -1);
     if (!$resqueJobs) {
         $jobs->push(new ResqueJob(array('Name' => 'null', 'Value' => 'null')));
         return $jobs;
     }
     foreach ($resqueJobs as $idx => $job) {
         $stdClass = json_decode($job);
         if ($stdClass->class === 'CapistranoDeploy' || $stdClass->class === 'DeployJob') {
             $jobs->push(new ResqueJob(array('Name' => $stdClass->class, 'Value' => $stdClass->args[0]->projectName . ':' . $stdClass->args[0]->environment . ' - ' . $stdClass->args[0]->sha)));
         } else {
             $jobs->push(new ResqueJob(array('Name' => $stdClass->class, 'Value' => var_export($stdClass->args[0], true))));
         }
     }
     return $jobs;
 }
示例#14
0
 public static function get_all_workers($queue = NULL)
 {
     $ret = array();
     $workers = Resque::redis()->smembers('workers');
     if (!is_array($workers)) {
         $workers = array();
     }
     foreach ($workers as $workerId) {
         $worker_data = explode(':', $workerId, 4);
         $worker = array();
         $worker['hostname'] = $worker_data[0];
         $worker['queues'] = explode(',', $worker_data[2]);
         $worker['pid'] = $worker_data[1];
         $worker['programname'] = $worker_data[3];
         $worker['workerId'] = $workerId;
         if ($queue && (in_array($queue, $worker['queues']) || in_array("*", $worker['queues'])) || !$queue) {
             $ret[] = $worker;
         }
     }
     return $ret;
 }
示例#15
0
 public function workerRemoveDead($user)
 {
     $this->ResqueStatus = new ResqueStatus\ResqueStatus(Resque::redis());
     $workers = $this->ResqueStatus->getWorkers();
     $this->Log = ClassRegistry::init('Log');
     if (function_exists('posix_getpwuid')) {
         $currentUser = posix_getpwuid(posix_geteuid());
         $currentUser = $currentUser['name'];
     } else {
         $currentUser = trim(shell_exec('whoami'));
     }
     foreach ($workers as $pid => $worker) {
         if (!is_numeric($pid)) {
             throw new MethodNotAllowedException('Non numeric PID found!');
         }
         $pidTest = substr_count(trim(shell_exec('ps -p ' . $pid)), PHP_EOL) > 0 ? true : false;
         if ($worker['user'] == $currentUser && !$pidTest) {
             $this->ResqueStatus->removeWorker($pid);
             $this->Log->create();
             $this->Log->save(array('org' => $user['org'], 'model' => 'User', 'model_id' => $user['id'], 'email' => $user['email'], 'action' => 'remove_dead_workers', 'user_id' => $user['id'], 'title' => 'Removing a dead worker.', 'change' => 'Removind dead worker data. Worker was of type ' . $worker['queue'] . ' with pid ' . $pid));
         }
     }
 }
示例#16
0
 /**
  * This command echoes what you have entered as the message.
  * @param string $message the message to be echoed.
  */
 public function actionIndex()
 {
     $includeFiles = getenv('INCLUDE_FILES');
     if ($includeFiles) {
         $includeFiles = explode(',', $includeFiles);
         foreach ($includeFiles as $file) {
             require_once $file;
         }
     }
     if (file_exists(Yii::getAlias('@app') . '/config/console.php')) {
         // Yii2-Basic
         $config = (require Yii::getAlias('@app') . '/config/console.php');
     } else {
         // Yii2-Advance
         $config = (require Yii::getAlias('@app') . '/config/main.php');
     }
     $application = new \yii\console\Application($config);
     # Turn off our amazing library autoload
     spl_autoload_unregister(array('Yii', 'autoload'));
     if (file_exists(Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php')) {
         // Yii2-Basic
         require_once Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php';
     } else {
         // Yii2-Advance
         require_once Yii::getAlias('@app') . '/../vendor/resque/yii2-resque/ResqueAutoloader.php';
     }
     ResqueAutoloader::register();
     # Give back the power to Yii
     spl_autoload_register(array('Yii', 'autoload'));
     $QUEUE = getenv('QUEUE');
     if (empty($QUEUE)) {
         die("Set QUEUE env var containing the list of queues to work.\n");
     }
     $REDIS_BACKEND = getenv('REDIS_BACKEND');
     $REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
     $REDIS_AUTH = getenv('REDIS_AUTH');
     if (!empty($REDIS_BACKEND)) {
         $REDIS_BACKEND_DB = !empty($REDIS_BACKEND_DB) ? $REDIS_BACKEND_DB : 0;
         Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB, $REDIS_AUTH);
     }
     $logLevel = 0;
     $LOGGING = getenv('LOGGING');
     $VERBOSE = getenv('VERBOSE');
     $VVERBOSE = getenv('VVERBOSE');
     if (!empty($LOGGING) || !empty($VERBOSE)) {
         $logLevel = Resque_Worker::LOG_NORMAL;
     } else {
         if (!empty($VVERBOSE)) {
             $logLevel = Resque_Worker::LOG_VERBOSE;
         }
     }
     $logger = null;
     $LOG_HANDLER = getenv('LOGHANDLER');
     $LOG_HANDLER_TARGET = getenv('LOGHANDLERTARGET');
     if (class_exists('MonologInit_MonologInit')) {
         if (!empty($LOG_HANDLER) && !empty($LOG_HANDLER_TARGET)) {
             $logger = new MonologInit_MonologInit($LOG_HANDLER, $LOG_HANDLER_TARGET);
         } else {
             fwrite(STDOUT, '*** loghandler or logtarget is not set.' . "\n");
         }
     } else {
         fwrite(STDOUT, '*** MonologInit_MonologInit logger cannot be found, continue without loghandler.' . "\n");
     }
     $interval = 5;
     $INTERVAL = getenv('INTERVAL');
     if (!empty($INTERVAL)) {
         $interval = $INTERVAL;
     }
     $count = 1;
     $COUNT = getenv('COUNT');
     if (!empty($COUNT) && $COUNT > 1) {
         $count = $COUNT;
     }
     $PREFIX = getenv('PREFIX');
     if (!empty($PREFIX)) {
         fwrite(STDOUT, '*** Prefix set to ' . $PREFIX . "\n");
         Resque::redis()->prefix($PREFIX);
     }
     if ($count > 1) {
         for ($i = 0; $i < $count; ++$i) {
             $pid = Resque::fork();
             if ($pid == -1) {
                 die("Could not fork worker " . $i . "\n");
             } else {
                 if (!$pid) {
                     startWorker($QUEUE, $logLevel, $logger, $interval);
                     break;
                 }
             }
         }
     } else {
         $PIDFILE = getenv('PIDFILE');
         if ($PIDFILE) {
             file_put_contents($PIDFILE, getmypid()) or die('Could not write PID information to ' . $PIDFILE);
         }
         $this->startWorker($QUEUE, $logLevel, $logger, $interval);
     }
 }
示例#17
0
 public function loadSchedule()
 {
     $this->updateProcLine('Loading Schedule');
     /* if ( $this->dynamic )  */
     ResqueScheduler::reloadSchedules();
     $schedules = ResqueScheduler::schedules();
     if (empty($schedules)) {
         $this->logger->log(Psr\Log\LogLevel::NOTICE, 'Schedule empty! Set Resque.schedule');
     }
     $this->scheduledJobs = array();
     foreach ($schedules as $name => $config) {
         $this->loadScheduleJob($name, $config);
     }
     Resque::redis()->del('schedules_changed');
     $this->updateProcLine('Schedules Loaded');
 }
示例#18
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);
     }
 }
示例#19
0
文件: Status.php 项目: Rming/novophp
 /**
  * Stop tracking the status of a job.
  */
 public function stop()
 {
     Resque::redis()->del((string) $this);
 }
示例#20
0
文件: Stat.php 项目: shonm/php-resque
 /**
  * Delete a statistic with the given name.
  *
  * @param  string  $stat The name of the statistic to delete.
  * @return boolean True if successful, false if not.
  */
 public static function clear($stat)
 {
     return (bool) Resque::redis()->del('stat:' . $stat);
 }
 /**
  * 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 function nextItemForTimestamp($timestamp)
 {
     $timestamp = self::getTimestamp($timestamp);
     $key = 'delayed:' . $timestamp;
     $item = json_decode(Resque::redis()->lpop($key), true);
     self::cleanupTimestamp($key, $timestamp);
     return $item;
 }
示例#22
0
 /**
  * Tell Redis which job we're currently working on.
  *
  * @param object $job Resque_Job instance containing the job we're working on.
  */
 public function workingOn(Resque_Job $job)
 {
     $job->worker = $this;
     $this->currentJob = $job;
     $job->updateStatus(\Resque_Job_Status::STATUS_RUNNING);
     $data = json_encode(array('queue' => $job->queue, 'run_at' => strftime('%a %b %d %H:%M:%S %Z %Y'), 'payload' => $job->payload));
     \Resque::redis()->set('worker:' . $job->worker, $data);
 }
示例#23
0
 /**
  * Clear the unique queue after the job has failed.
  *
  * @param object $exception
  *   Exception that occurred.
  * @param Resque_Job $job
  *   The job that failed.
  */
 public static function onFailure($exception, Resque_Job $job)
 {
     if (Php_Resque::redis()->exists($job->payload['args'][0]['drupal_unique_key'])) {
         Php_Resque::redis()->del($job->payload['args'][0]['drupal_unique_key']);
     }
 }
 /**
  * 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;
 }
示例#25
0
 /**
  * Clear the given queue.
  * 
  * @param string $queue The name of the queue
  *
  * @return int The number of removed items
  */
 public function clearQueue($queue)
 {
     $length = \Resque::redis()->llen('queue:' . $queue);
     \Resque::redis()->del('queue:' . $queue);
     return $length;
 }
示例#26
0
 /**
  * Return Redis
  *
  * @return object Redis instance
  */
 public function redis()
 {
     return Resque::redis();
 }
 /**
  * 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 = self::QUEUE_NAME . ':' . $timestamp;
     $item = json_decode(\Resque::redis()->lpop($key), true);
     self::cleanupTimestamp($key, $timestamp);
     return $item;
 }
示例#28
0
 /**
  * Get current logger instance
  * @param  string 	$workerId 	Worker id
  * @return object
  */
 public function getLogger($workerId)
 {
     $settings = json_decode(Resque::redis()->hget('workerLogger', (string) $workerId));
     $logger = new MonologInit_MonologInit($settings[0], $settings[1]);
     return $logger->getInstance();
 }
 /**
  * Return an instance of the Resque_Redis class instantiated for Resque.
  * 获取一个redis的实例
  * @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.
     // getmypid — 获取 PHP 进程的 ID
     $pid = getmypid();
     // 如果当前PHP进程的ID与类中定义的不同,清空redis实例,将类中定义的ID改为当前PHP进程的ID
     if (self::$pid !== $pid) {
         self::$redis = null;
         self::$pid = $pid;
     }
     // redis实例存在,直接返回
     if (!is_null(self::$redis)) {
         return self::$redis;
     }
     // redis实例不存在,取出redis配置,默认为'localhost:6379'
     $server = self::$redisServer;
     if (empty($server)) {
         $server = 'localhost:6379';
     }
     // 如果配置是个array,就会生成多个redis实例
     // 配置项$server是[['host' => hostname0, 'port' => port0], ['host' => hostname1, 'port' => port1]]
     // 也可以是['redis0' => ['host' => hostname0, 'port' => port0], 'redis1' => ['host' => hostname1, 'port' => port1]]
     if (is_array($server)) {
         require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
         self::$redis = new Resque_RedisCluster($server);
     } else {
         if (strpos($server, 'unix:') === false) {
             // $server is '127.0.0.1:6379'
             list($host, $port) = explode(':', $server);
         } else {
             // $server is 'unix:/tmp/redis.sock'
             // 注:redis 默认没有开启unix socket,需要在/etc/redis/redis.conf中修改
             $host = $server;
             $port = null;
         }
         require_once dirname(__FILE__) . '/Resque/Redis.php';
         self::$redis = new Resque_Redis($host, $port);
     }
     // 选择redis的数据库
     self::$redis->select(self::$redisDatabase);
     return self::$redis;
 }
示例#30
0
 /**
  * Determines the position of a job within a specified queue
  *
  * Note: https://github.com/chrisboulton/php-resque/issues/45
  *
  * @return int Returns queue position or else -1 if job not found
  */
 private function _getQueueNum($queue, $id)
 {
     $i = 0;
     foreach (Resque::redis()->lrange('queue:' . $queue, 0, -1) as $job) {
         if (strpos($job, $id) !== false) {
             return $i;
         }
         $i += 1;
     }
     return -1;
 }