Пример #1
0
 protected function setupWorker(Worker $worker)
 {
     $log = $this->log;
     $worker->on('init', function (Job $job) use($log) {
         $log->addInfo(sprintf('Accepted job %s', get_class($job)), array('job' => $job));
     });
     $worker->on('error', function (Job $job, $code, $message, $file, $line) use($log) {
         $log->addError(sprintf('Job %s failed: "%s" in file %s:%d', get_class($job), $message, $file, $line), array('job' => $job));
     });
     $worker->on('exception', function (Job $job, \Exception $exception) use($log) {
         $log->addError(sprintf('Job "%s" failed: %s', get_class($job), $exception), array('job' => $job));
     });
     $worker->on('success', function (Job $job) use($log) {
         $log->addInfo(sprintf('Job "%s" finished successfully', get_class($job)), array('job' => $job));
     });
     foreach ($this->events as $event => $handler) {
         # A single event handler
         if (is_callable($handler)) {
             $worker->on($event, $handler);
             # Multiple handlers for the event
         } elseif (is_array($handler)) {
             foreach ($handler as $h) {
                 $worker->on($event, $h);
             }
         }
     }
 }
Пример #2
0
 /**
  * Process with worker
  *
  * @param string   $type
  * @param \Closure $fn
  */
 public function process($type = null, $fn = null)
 {
     if ($type instanceof \Closure) {
         $fn = $type;
         $type = null;
     }
     if ($fn) {
         $this->on('process:' . ($type ? $type : '*'), $fn);
     }
     $this->emit('process', $type, $fn);
     $worker = new Worker($this, $type);
     $worker->start();
 }