Esempio n. 1
0
 /**
  * Display usefull stats about the workers.
  *
  * Note: The workers status is conveniently stored by ResqueStatus.
  *
  * @return void
  * @see ResqueStatus\ResqueStatus::isSchedulerWorker()
  * @see ResqueStatus\ResqueStatus::getPausedWorker()
  */
 public function stats()
 {
     $ResqueStatus = $this->ResqueStatus;
     $workers = CakeResque::getWorkers();
     // List of all queues
     $queues = array_unique(CakeResque::getQueues());
     // List of queues monitored by a worker
     $activeQueues = [];
     foreach ($workers as $worker) {
         $workerParams = explode(':', $worker);
         $activeQueues = array_merge($activeQueues, explode(',', array_pop($workerParams)));
     }
     $this->out("\n");
     $this->out('<info>' . __d('cake_resque', 'Resque Statistics') . '</info>');
     $this->hr();
     $this->out("\n");
     $this->out('<info>' . __d('cake_resque', 'Jobs Stats') . '</info>');
     $this->out('   ' . __d('cake_resque', 'Processed Jobs : {0}', number_format(Resque_Stat::get('processed'))));
     $this->out('   <warning>' . __d('cake_resque', 'Failed Jobs    : {0}', number_format(Resque_Stat::get('failed'))) . '</warning>');
     if (Configure::read('CakeResque.Scheduler.enabled') === true) {
         $this->out('   ' . __d('cake_resque', 'Scheduled Jobs : {0}', number_format(Stat::get())));
     }
     $this->out("\n");
     $count = [];
     $this->out('<info>' . __d('cake_resque', 'Queues Stats') . '</info>');
     for ($i = count($queues) - 1; $i >= 0; --$i) {
         $count[$queues[$i]] = CakeResque::getQueueSize($queues[$i]);
         if (!in_array($queues[$i], $activeQueues) && $count[$queues[$i]] == 0) {
             unset($queues[$i]);
         }
     }
     $this->out('   ' . __d('cake_resque', 'Queues count : {0}', count($queues)));
     foreach ($queues as $queue) {
         $this->out(sprintf("\t- %-15s : %12s %s", $queue, number_format($count[$queue]), __dn('cake_resque', 'pending job', 'pending jobs', $count[$queue]) . (!in_array($queue, $activeQueues) ? " <error>(unmonitored queue)</error>" : '')));
     }
     $this->out("\n");
     $this->out('<info>' . __d('cake_resque', 'Workers Stats') . '</info>');
     $this->out('   ' . __d('cake_resque', 'Workers count : {0}', count($workers)));
     $pausedWorkers = $ResqueStatus->getPausedWorker();
     $schedulerWorkers = [];
     if (!empty($workers)) {
         $this->out("\t<info>" . strtoupper(__d('cake_resque', 'regular workers')) . "</info>");
         foreach ($workers as $worker) {
             if (Configure::read('CakeResque.Scheduler.enabled') === true && $ResqueStatus->isSchedulerWorker($worker)) {
                 $schedulerWorkers[] = $worker;
                 continue;
             }
             $this->out("\t* <bold>" . (string) $worker . '</bold>' . (in_array((string) $worker, $pausedWorkers) ? ' <warning>(' . __d('cake_resque', 'paused') . ')</warning>' : ''));
             $this->out("\t   - " . __d('cake_resque', 'Started on') . "     : " . CakeResque::getWorkerStartDate($worker));
             $this->out("\t   - " . __d('cake_resque', 'Processed Jobs') . " : " . $worker->getStat('processed'));
             $worker->getStat('failed') == 0 ? $this->out("\t   - " . __d('cake_resque', 'Failed Jobs') . "    : " . $worker->getStat('failed')) : $this->out("\t   - <warning>" . __d('cake_resque', 'Failed Jobs') . "    : " . $worker->getStat('failed') . "</warning>");
         }
     }
     $this->out("\n");
     if (!empty($schedulerWorkers)) {
         $this->out("\t<info>" . strtoupper(__d('cake_resque', 'scheduler worker')) . "</info>" . (in_array((string) $schedulerWorkers[0], $pausedWorkers) ? ' <warning>(' . __d('cake_resque', 'paused') . ')</warning>' : ''));
         foreach ($schedulerWorkers as $worker) {
             $schedulerWorker = new ResqueScheduler();
             $delayedJobCount = $schedulerWorker->getDelayedQueueScheduleSize();
             $this->out("\t   - " . __d('cake_resque', 'Started on') . "     : " . CakeResque::getWorkerStartDate($worker));
             $this->out("\t   - " . __d('cake_resque', 'Delayed Jobs') . "   : " . $delayedJobCount);
             if ($delayedJobCount > 0) {
                 $this->out("\t   - " . __d('cake_resque', 'Next Job on') . "    : " . strftime('%a %b %d %H:%M:%S %Z %Y', $schedulerWorker->nextDelayedTimestamp()));
             }
         }
         $this->out("\n");
     } elseif (Configure::read('CakeResque.Scheduler.enabled') === true) {
         $jobsCount = ResqueScheduler::getDelayedQueueScheduleSize();
         if ($jobsCount > 0) {
             $this->out("\t<error>************ " . __d('cake_resque', 'Alert') . " ************</error>");
             $this->out("\t<bold>" . __d('cake_resque', 'The Scheduler Worker is not running') . "</bold>");
             $this->out("\t" . __d('cake_resque', 'But there is still <bold>{0}</bold> scheduled jobs left in its queue', $jobsCount));
             $this->out("\t<error>********************************</error>");
             $this->out("\n");
         }
     }
 }
Esempio n. 2
0
 /**
  * Print some stats about the workers
  *
  * @return  void
  */
 public function stats()
 {
     $workers = call_user_func(array($this->ResqueStats, 'getWorkers'));
     // List of all queues
     $queues = array_unique(call_user_func(array($this->ResqueStats, 'getQueues')));
     // List of queues monitored by a worker
     $activeQueues = array();
     foreach ($workers as $worker) {
         $tokens = explode(':', $worker);
         $activeQueues = array_merge($activeQueues, explode(',', array_pop($tokens)));
     }
     $this->outputTitle('Resque statistics');
     $this->output->outputLine();
     $this->output->outputLine('Jobs Stats', 'subtitle');
     $this->output->outputLine('   ' . sprintf('Processed Jobs : %10s', number_format(\Resque_Stat::get('processed'))));
     $this->output->outputLine('   ' . sprintf('Failed Jobs    : %10s', number_format(\Resque_Stat::get('failed'))), 'failure');
     if ($this->runtime['Scheduler']['enabled'] === true) {
         $this->output->outputLine('   ' . sprintf('Scheduled Jobs : %10s', number_format(\ResqueScheduler\Stat::get())));
     }
     $this->output->outputLine();
     $count = array();
     $this->output->outputLine('Queues Stats', 'subtitle');
     for ($i = count($queues) - 1; $i >= 0; --$i) {
         $count[$queues[$i]] = call_user_func_array(array($this->ResqueStats, 'getQueueLength'), array($queues[$i]));
         if (!in_array($queues[$i], $activeQueues) && $count[$queues[$i]] == 0) {
             unset($queues[$i]);
         }
     }
     $this->output->outputLine('   ' . sprintf('Queues count : %d', count($queues)));
     foreach ($queues as $queue) {
         $this->output->outputText(sprintf("\t- %-20s : %10s pending jobs", $queue, number_format($count[$queue])));
         if (!in_array($queue, $activeQueues)) {
             $this->output->outputText(' (unmonitored queue)', 'failure');
         }
         $this->output->outputText("\n");
     }
     $this->output->outputLine();
     $this->output->outputLine('Workers Stats', 'subtitle');
     $this->output->outputLine('  Active Workers : ' . count($workers));
     $schedulerWorkers = array();
     if (!empty($workers)) {
         $pausedWorkers = call_user_func(array($this->ResqueStatus, 'getPausedWorker'));
         foreach ($workers as $worker) {
             if ($this->runtime['Scheduler']['enabled'] === true && $this->ResqueStatus->isSchedulerWorker($worker)) {
                 $schedulerWorkers[] = $worker;
                 continue;
             }
             $this->output->outputText('    Worker : ' . $worker, 'bold');
             if (in_array((string) $worker, $pausedWorkers)) {
                 $this->output->outputText(' (Paused)', 'success');
             }
             $this->output->outputText("\n");
             $startDate = call_user_func_array(array($this->ResqueStats, 'getWorkerStartDate'), array($worker));
             $this->output->outputLine('     - Started on     : ' . $startDate);
             $this->output->outputLine('     - Uptime         : ' . $this->formatDateDiff(new \DateTime($startDate)));
             $this->output->outputLine('     - Processed Jobs : ' . $worker->getStat('processed'));
             $worker->getStat('failed') == 0 ? $this->output->outputLine('     - Failed Jobs    : ' . $worker->getStat('failed')) : $this->output->outputLine('     - Failed Jobs    : ' . $worker->getStat('failed'), 'failure');
         }
     }
     $this->output->outputLine("\n");
     if (!empty($schedulerWorkers)) {
         $this->output->outputText(ucwords('    scheduler worker'), 'bold');
         if (in_array((string) $schedulerWorkers[0], $pausedWorkers)) {
             $this->output->outputText(' (Paused)', 'success');
         }
         $this->output->outputText("\n");
         foreach ($schedulerWorkers as $worker) {
             $schedulerWorker = new \ResqueScheduler\ResqueScheduler();
             $delayedJobCount = $schedulerWorker->getDelayedQueueScheduleSize();
             $this->output->outputLine('    - Started on      : ' . call_user_func_array(array($this->ResqueStats, 'getWorkerStartDate'), array($worker)));
             $this->output->outputLine('    - Delayed Jobs    : ' . number_format($delayedJobCount));
             if ($delayedJobCount > 0) {
                 $this->output->outputLine('    - Next Job on     : ' . strftime('%a %b %d %H:%M:%S %Z %Y', $schedulerWorker->nextDelayedTimestamp()));
             }
         }
         $this->output->outputLine("\n");
     } elseif ($this->runtime['Scheduler']['enabled'] === true) {
         $jobsCount = \ResqueScheduler\ResqueScheduler::getDelayedQueueScheduleSize();
         if ($jobsCount > 0) {
             $this->output->outputLine("    ************ " . 'Alert' . " ************", 'failure');
             $this->output->outputLine("    " . 'The Scheduler Worker is not running', 'bold');
             $this->output->outputLine("    " . 'But there is still ' . number_format($jobsCount) . ' scheduled jobs left in its queue');
             $this->output->outputLine("    ********************************", 'failure');
             $this->output->outputLine("\n");
         }
     }
 }
 /**
  * Push a raw payload onto the queue after a delay
  *
  * @param  string $payload
  * @param  string $queue
  * @param  array $options
  * @return mixed
  */
 public function laterRaw($delay, $payload, $queue = null, array $options = array())
 {
     if (!class_exists('ResqueScheduler\\ResqueScheduler')) {
         throw new Exception("Please add \"chrisboulton/php-resque-scheduler\": \"dev-master\" to your composer.json and run composer update");
     }
     $queue = $this->getQueue($queue);
     $payload = json_decode($payload, true);
     if (is_int($delay)) {
         ResqueScheduler::enqueueIn($delay, $queue, 'Resque\\ResqueJob', $payload);
     } else {
         ResqueScheduler::enqueueAt($delay, $queue, 'Resque\\ResqueJob', $payload);
     }
 }