fork() public static method

Will close connection to Redis before forking.
public static fork ( ) : integer
return integer Return vars as per pcntl_fork(). False if pcntl_fork is unavailable
Ejemplo n.º 1
0
 /**
  * The primary loop for a worker which when called on an instance starts
  * the worker's life cycle.
  *
  * Queues are checked every $interval (seconds) for new jobs.
  *
  * @param int $interval How often to check for new jobs across the queues.
  */
 public function work($interval = 5)
 {
     $this->updateProcLine('Starting');
     $this->startup();
     while (true) {
         if ($this->shutdown) {
             break;
         }
         // Attempt to find and reserve a job
         $job = false;
         if (!$this->paused) {
             $job = $this->reserve();
         }
         if (!$job) {
             // For an interval of 0, break now - helps with unit testing etc
             if ($interval == 0) {
                 break;
             }
             // If no job was found, we sleep for $interval before continuing and checking again
             $this->log('Sleeping for ' . $interval, true);
             if ($this->paused) {
                 $this->updateProcLine('Paused');
             } else {
                 $this->updateProcLine('Waiting for ' . implode(',', $this->queues));
             }
             usleep($interval * 1000000);
             continue;
         }
         $this->log('got ' . $job);
         Resque_Event::trigger('beforeFork', $job);
         $this->workingOn($job);
         $this->child = Resque::fork();
         // Forked and we're the child. Run the job.
         if ($this->child === 0 || $this->child === false) {
             $status = 'Processing ' . $job->queue . ' since ' . strftime('%F %T');
             $this->updateProcLine($status);
             $this->log($status, self::LOG_VERBOSE);
             $this->perform($job);
             if ($this->child === 0) {
                 exit(0);
             }
         }
         if ($this->child > 0) {
             // Parent process, sit and wait
             $status = 'Forked ' . $this->child . ' at ' . strftime('%F %T');
             $this->updateProcLine($status);
             $this->log($status, self::LOG_VERBOSE);
             // Wait until the child process finishes before continuing
             pcntl_wait($status);
             $exitStatus = pcntl_wexitstatus($status);
             if ($exitStatus !== 0) {
                 $job->fail(new Resque_Job_DirtyExitException('Job exited with exit code ' . $exitStatus));
             }
         }
         $this->child = null;
         $this->doneWorking();
     }
     $this->unregisterWorker();
 }
Ejemplo n.º 2
0
 /**
  * The primary loop for a worker which when called on an instance starts
  * the worker's life cycle.
  *
  * Queues are checked every $interval (seconds) for new jobs.
  *
  * @param int $interval How often to check for new jobs across the queues.
  */
 public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
 {
     $this->updateProcLine('Starting');
     $this->startup();
     while (true) {
         if ($this->shutdown) {
             break;
         }
         // Attempt to find and reserve a job
         $job = false;
         if (!$this->paused) {
             if ($blocking === true) {
                 $this->logger->log(Psr\Log\LogLevel::INFO, 'Starting blocking with timeout of {interval}', array('interval' => $interval));
                 $this->updateProcLine('Waiting for ' . implode(',', $this->queues) . ' with blocking timeout ' . $interval);
             } else {
                 $this->updateProcLine('Waiting for ' . implode(',', $this->queues) . ' with interval ' . $interval);
             }
             $job = $this->reserve($blocking, $interval);
         }
         if (!$job) {
             // For an interval of 0, break now - helps with unit testing etc
             if ($interval == 0) {
                 break;
             }
             if ($blocking === false) {
                 // If no job was found, we sleep for $interval before continuing and checking again
                 $this->logger->log(Psr\Log\LogLevel::INFO, 'Sleeping for {interval}', array('interval' => $interval));
                 if ($this->paused) {
                     $this->updateProcLine('Paused');
                 } else {
                     $this->updateProcLine('Waiting for ' . implode(',', $this->queues));
                 }
                 usleep($interval * 1000000);
             }
             continue;
         }
         $this->logger->log(Psr\Log\LogLevel::NOTICE, 'Starting work on {job}', array('job' => $job));
         Resque_Event::trigger('beforeFork', $job);
         $this->workingOn($job);
         $this->child = Resque::fork();
         // Forked and we're the child. Run the job.
         if ($this->child === 0 || $this->child === false) {
             $status = 'Processing ' . $job->queue . ' since ' . strftime('%F %T');
             $this->updateProcLine($status);
             $this->logger->log(Psr\Log\LogLevel::INFO, $status);
             $this->perform($job);
             if ($this->child === 0) {
                 exit(0);
             }
         }
         if ($this->child > 0) {
             // Parent process, sit and wait
             $status = 'Forked ' . $this->child . ' at ' . strftime('%F %T');
             $this->updateProcLine($status);
             $this->logger->log(Psr\Log\LogLevel::INFO, $status);
             // Wait until the child process finishes before continuing
             pcntl_wait($status);
             $exitStatus = pcntl_wexitstatus($status);
             if ($exitStatus !== 0) {
                 $job->fail(new Resque_Job_DirtyExitException('Job exited with exit code ' . $exitStatus));
             }
         }
         $this->child = null;
         $this->doneWorking();
     }
     $this->unregisterWorker();
 }
Ejemplo n.º 3
0
 /**
  * This command echoes what you have entered as the message.
  * @param string $message the message to be echoed.
  */
 public function actionIndex()
 {
     $includeFiles = getenv('INCLUDE_FILES');
     if ($includeFiles) {
         $includeFiles = explode(',', $includeFiles);
         foreach ($includeFiles as $file) {
             require_once $file;
         }
     }
     if (file_exists(Yii::getAlias('@app') . '/config/console.php')) {
         // Yii2-Basic
         $config = (require Yii::getAlias('@app') . '/config/console.php');
     } else {
         // Yii2-Advance
         $config = (require Yii::getAlias('@app') . '/config/main.php');
     }
     $application = new \yii\console\Application($config);
     # Turn off our amazing library autoload
     spl_autoload_unregister(array('Yii', 'autoload'));
     if (file_exists(Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php')) {
         // Yii2-Basic
         require_once Yii::getAlias('@vendor') . '/resque/yii2-resque/ResqueAutoloader.php';
     } else {
         // Yii2-Advance
         require_once Yii::getAlias('@app') . '/../vendor/resque/yii2-resque/ResqueAutoloader.php';
     }
     ResqueAutoloader::register();
     # Give back the power to Yii
     spl_autoload_register(array('Yii', 'autoload'));
     $QUEUE = getenv('QUEUE');
     if (empty($QUEUE)) {
         die("Set QUEUE env var containing the list of queues to work.\n");
     }
     $REDIS_BACKEND = getenv('REDIS_BACKEND');
     $REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
     $REDIS_AUTH = getenv('REDIS_AUTH');
     if (!empty($REDIS_BACKEND)) {
         $REDIS_BACKEND_DB = !empty($REDIS_BACKEND_DB) ? $REDIS_BACKEND_DB : 0;
         Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB, $REDIS_AUTH);
     }
     $logLevel = 0;
     $LOGGING = getenv('LOGGING');
     $VERBOSE = getenv('VERBOSE');
     $VVERBOSE = getenv('VVERBOSE');
     if (!empty($LOGGING) || !empty($VERBOSE)) {
         $logLevel = Resque_Worker::LOG_NORMAL;
     } else {
         if (!empty($VVERBOSE)) {
             $logLevel = Resque_Worker::LOG_VERBOSE;
         }
     }
     $logger = null;
     $LOG_HANDLER = getenv('LOGHANDLER');
     $LOG_HANDLER_TARGET = getenv('LOGHANDLERTARGET');
     if (class_exists('MonologInit_MonologInit')) {
         if (!empty($LOG_HANDLER) && !empty($LOG_HANDLER_TARGET)) {
             $logger = new MonologInit_MonologInit($LOG_HANDLER, $LOG_HANDLER_TARGET);
         } else {
             fwrite(STDOUT, '*** loghandler or logtarget is not set.' . "\n");
         }
     } else {
         fwrite(STDOUT, '*** MonologInit_MonologInit logger cannot be found, continue without loghandler.' . "\n");
     }
     $interval = 5;
     $INTERVAL = getenv('INTERVAL');
     if (!empty($INTERVAL)) {
         $interval = $INTERVAL;
     }
     $count = 1;
     $COUNT = getenv('COUNT');
     if (!empty($COUNT) && $COUNT > 1) {
         $count = $COUNT;
     }
     $PREFIX = getenv('PREFIX');
     if (!empty($PREFIX)) {
         fwrite(STDOUT, '*** Prefix set to ' . $PREFIX . "\n");
         Resque::redis()->prefix($PREFIX);
     }
     if ($count > 1) {
         for ($i = 0; $i < $count; ++$i) {
             $pid = Resque::fork();
             if ($pid == -1) {
                 die("Could not fork worker " . $i . "\n");
             } else {
                 if (!$pid) {
                     startWorker($QUEUE, $logLevel, $logger, $interval);
                     break;
                 }
             }
         }
     } else {
         $PIDFILE = getenv('PIDFILE');
         if ($PIDFILE) {
             file_put_contents($PIDFILE, getmypid()) or die('Could not write PID information to ' . $PIDFILE);
         }
         $this->startWorker($QUEUE, $logLevel, $logger, $interval);
     }
 }