Exemplo n.º 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()
 {
     $exit = false;
     $starttime = time();
     while (!$exit) {
         $this->out('Looking for Job....');
         $data = $this->QueuedTask->requestJob($this->getTaskConf());
         if ($data != false) {
             $this->out('Running Job of type "' . $data['jobtype'] . '"');
             $taskname = 'queue_' . strtolower($data['jobtype']);
             $return = $this->{$taskname}->run(unserialize($data['data']));
             if ($return == true) {
                 $this->QueuedTask->markJobDone($data['id']);
                 $this->out('Job Finished.');
             } else {
                 $this->QueuedTask->markJobFailed($data['id']);
                 $this->out('Job did not finish, requeued.');
             }
         } else {
             $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')) {
             $exit = true;
             $this->out('Reached runtime of ' . (time() - $starttime) . ' Seconds (Max ' . Configure::read('queue.workermaxruntime') . '), terminating.');
         }
         if ($exit || rand(0, 100) > 100 - Configure::read('queue.gcprop')) {
             $this->out('Performing Old job cleanup.');
             $this->QueuedTask->cleanOldJobs();
         }
         $this->hr();
     }
 }
Exemplo n.º 2
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();
     }
     $exit = false;
     $starttime = time();
     $group = null;
     if (isset($this->params['group']) && !empty($this->params['group'])) {
         $group = $this->params['group'];
     }
     while (!$exit) {
         if ($this->_verbose) {
             $this->out('Looking for Job....');
         }
         $data = $this->QueuedTask->requestJob($this->getTaskConf(), $group);
         if ($this->QueuedTask->exit === true) {
             $exit = true;
         } else {
             if ($data !== false) {
                 $this->out('Running Job of type "' . $data['jobtype'] . '"');
                 $taskname = 'queue_' . strtolower($data['jobtype']);
                 $return = $this->{$taskname}->run(unserialize($data['data']));
                 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.');
                 $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')) {
                 $exit = true;
                 $this->out('Reached runtime of ' . (time() - $starttime) . ' Seconds (Max ' . Configure::read('queue.workermaxruntime') . '), terminating.');
             }
             if ($exit || rand(0, 100) > 100 - Configure::read('queue.gcprop')) {
                 $this->out('Performing Old job cleanup.');
                 $this->QueuedTask->cleanOldJobs();
             }
             if ($this->_verbose) {
                 $this->hr();
             }
         }
     }
 }
Exemplo n.º 3
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.
  *
  * @return void
  */
 public function runworker()
 {
     if ($pidFilePath = Configure::read('Queue.pidfilepath')) {
         if (!file_exists($pidFilePath)) {
             mkdir($pidFilePath, 0755, true);
         }
         if (function_exists('posix_getpid')) {
             $pid = posix_getpid();
         } else {
             $pid = $this->QueuedTask->key();
         }
         # global file
         $fp = fopen($pidFilePath . 'queue.pid', "w");
         fwrite($fp, $pid);
         fclose($fp);
         # specific pid file
         if (function_exists('posix_getpid')) {
             $pid = posix_getpid();
         } else {
             $pid = $this->QueuedTask->key();
         }
         $pidFileName = 'queue_' . $pid . '.pid';
         $fp = fopen($pidFilePath . $pidFileName, "w");
         fwrite($fp, $pid);
         fclose($fp);
     }
     // Enable Garbage Collector (PHP >= 5.3)
     if (function_exists('gc_enable')) {
         gc_enable();
     }
     if (function_exists('pcntl_signal')) {
         pcntl_signal(SIGTERM, [&$this, "_exit"]);
     }
     $this->_exit = false;
     $starttime = time();
     $group = null;
     if (!empty($this->params['group'])) {
         $group = $this->params['group'];
     }
     while (!$this->_exit) {
         // make sure accidental overriding isnt possible
         set_time_limit(0);
         if (!empty($pidFilePath)) {
             touch($pidFilePath . 'queue.pid');
         }
         if (!empty($pidFileName)) {
             touch($pidFilePath . $pidFileName);
         }
         $this->_log('runworker', isset($pid) ? $pid : null);
         $this->out('Looking for Job....');
         $data = $this->QueuedTask->requestJob($this->_getTaskConf(), $group);
         if ($this->QueuedTask->exit === true) {
             $this->_exit = true;
         } else {
             if ($data) {
                 $this->out('Running Job of type "' . $data['jobtype'] . '"');
                 $taskname = 'Queue' . $data['jobtype'];
                 if ($this->{$taskname}->autoUnserialize) {
                     $data['data'] = unserialize($data['data']);
                 }
                 $return = $this->{$taskname}->run($data['data'], $data['id']);
                 if ($return) {
                     $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 {
                 $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') && 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.gcprob')) {
                 $this->out('Performing Old job cleanup.');
                 $this->QueuedTask->cleanOldJobs();
             }
             $this->hr();
         }
     }
     if (!empty($pidFilePath)) {
         unlink($pidFilePath . 'queue_' . $pid . '.pid');
     }
 }