Example #1
0
 public function run(\Morker\Worker $worker)
 {
     while (!$this->kill) {
         $worker->houseKeep();
         echo "Running " . $worker->nr . "\n";
         sleep(3);
         exit(254);
     }
 }
Example #2
0
 public function spawnMissingWorkers()
 {
     for ($workerNr = 0; $workerNr < $this->workerProcesses; $workerNr++) {
         foreach ($this->workers as $worker) {
             if ($worker->nr == $workerNr) {
                 continue 2;
             }
         }
         $this->dispatcher->dispatch(Events::SPAWNING_WORKERS);
         echo "DEBUG: spawning new worker: " . $workerNr . "\n";
         $this->dispatcher->dispatch(Events::PRE_FORK);
         $worker = new Worker(false, $workerNr, null, $this, null);
         $worker->task = $this->workerTask;
         if (-1 === ($pid = pcntl_fork())) {
             throw new \Exception('Unable to fork a new process', 0);
         }
         if (0 === $pid) {
             // setup the fifo (blocks until parent connects)
             $fifo = new Fifo($this, null);
             $worker->fifo = $fifo;
             $worker->pid = posix_getpid();
             $this->dispatcher->dispatch(Events::POST_FORK);
             $status = 0;
             try {
                 $worker->start();
             } catch (\Exception $e) {
                 $status = 1;
             }
             exit($status);
         }
         $fifo = new Fifo($this, $pid);
         $worker->fifo = $fifo;
         $worker->pid = $pid;
         $worker->isMaster = true;
         $this->workers[$pid] = $worker;
     }
 }