Пример #1
0
 /**
  * Run a QueueWorker loop.
  * Runs a Queue Worker process which will try to find unassigned jobs in the queue
  * which it may run and try to fetch and execute them.
  */
 public function runworker()
 {
     // Enable Garbage Collector (PHP >= 5.3)
     if (function_exists('gc_enable')) {
         gc_enable();
     }
     // Prevent mid-job termination (if platform supported)
     if (function_exists('pcntl_signal')) {
         pcntl_signal(SIGTERM, array(&$this, "_exit"));
         $this->exit = false;
     }
     $starttime = time();
     $group = null;
     if (isset($this->params['group']) && !empty($this->params['group'])) {
         $group = $this->params['group'];
     }
     while (!$this->exit) {
         if ($this->_verbose) {
             $this->out('Looking for Job....');
         }
         $data = $this->QueuedTask->requestJob($this->getTaskConf(), $group);
         if ($this->QueuedTask->exit === true) {
             $this->exit = true;
         } else {
             if ($data !== false) {
                 $this->out('Running Job of type "' . $data['jobtype'] . '"');
                 $taskname = 'queue' . $data['jobtype'];
                 $jobData = unserialize($data['data']);
                 if (!$this->{$taskname}->canRun($jobData)) {
                     $this->QueuedTask->requeueJob($data['id'], $this->getTaskConf($taskname, 'timeout'));
                     $this->out('Job could not be run, requeued.');
                 } else {
                     $return = $this->{$taskname}->run($jobData);
                     if ($return == true) {
                         $this->QueuedTask->markJobDone($data['id']);
                         $this->out('Job Finished.');
                     } else {
                         $failureMessage = null;
                         if (isset($this->{$taskname}->failureMessage) && !empty($this->{$taskname}->failureMessage)) {
                             $failureMessage = $this->{$taskname}->failureMessage;
                         }
                         $this->QueuedTask->markJobFailed($data['id'], $failureMessage);
                         $this->out('Job did not finish, requeued.');
                     }
                 }
             } elseif (Configure::read('queue.exitwhennothingtodo')) {
                 $this->out('nothing to do, exiting.');
                 $this->exit = true;
             } else {
                 if ($this->_verbose) {
                     $this->out('nothing to do, sleeping.');
                 }
                 sleep(Configure::read('queue.sleeptime'));
             }
             // check if we are over the maximum runtime and end processing if so.
             if (Configure::read('queue.workermaxruntime') != 0 && time() - $starttime >= Configure::read('queue.workermaxruntime')) {
                 $this->exit = true;
                 $this->out('Reached runtime of ' . (time() - $starttime) . ' Seconds (Max ' . Configure::read('queue.workermaxruntime') . '), terminating.');
             }
             if ($this->exit || rand(0, 100) > 100 - Configure::read('queue.gcprop')) {
                 $this->out('Performing Old job cleanup.');
                 $this->QueuedTask->cleanOldJobs();
             }
             if ($this->_verbose) {
                 $this->hr();
             }
         }
     }
 }