Пример #1
0
 protected function launchJob($jobID)
 {
     if (FALSE === $this->fire('onLauncher', array(&$this))) {
         usleep(20000);
         return false;
     }
     $pid = pcntl_fork();
     if ($pid == -1) {
         $this->fire('onLaunchJobError', array(&$this));
         return false;
     } else {
         if ($pid) {
             $this->currentObjects[$pid] = $this->childObject;
             $this->currentJobs[$pid] = $jobID;
             if (isset($this->signalQueue[$pid])) {
                 $this->childSignalHandler(SIGCHLD, $pid, $this->signalQueue[$pid]);
                 unset($this->signalQueue[$pid]);
             }
         } else {
             unset($this->currentObjects);
             $exitStatus = 0;
             setproctitle($this->childsProcName);
             $this->fire('onLaunchJob', array(&$this));
             exit($exitStatus);
         }
     }
     return true;
 }
Пример #2
0
 *   $boris->onStart(function($worker, $vars){
 *     $worker->setLocal('date', date('Y-m-d'));
 *   });
 *
 *   $boris->onStart('echo "The date is $date\n";');
 */
  public function onStart($hook)
  {
      $this->_startHooks[] = $hook;
  }
  /**
 * Add a new hook to run in the context of the REPL when a fatal error occurs.
 *
 * @param mixed $hook
 *
 * The hook is either a string of PHP code to eval(), or a Closure accepting
 * the EvalWorker object as its first argument and the array of defined
 * local variables in the second argument.
 *
 * If the hook is a callback and needs to set any local variables in the
 * REPL's scope, it should invoke $worker->setLocal($var_name, $value) to
 * do so.
 *
 * Hooks are guaranteed to run in the order they were added and the state
 * set by each hook is available to the next hook (either through global
 * resources, such as classes and interfaces, or through the 2nd parameter
 * of the callback, if any local variables were set.
 *
 * @example An example if your project requires some database connection cleanup:
 *
 *   $boris->onFailure(function($worker, $vars){
Пример #3
0
 private static function changeProcessTitleTo($processTitle)
 {
     if (function_exists('cli_set_process_title')) {
         cli_set_process_title($processTitle);
     } elseif (function_exists('setproctitle')) {
         setproctitle($processTitle);
     }
 }
Пример #4
0
 /**
  * 设置进程名称
  *
  * @param $title
  */
 protected static function setProcessName($title)
 {
     $title = "php_daemon_{$title}";
     if (function_exists('cli_set_process_title')) {
         cli_set_process_title($title);
     } elseif (extension_loaded('proctitle') && function_exists('setproctitle')) {
         setproctitle($title);
     }
 }
Пример #5
0
 protected function _init()
 {
     setproctitle('Daemon: QueueManager');
     $this->_initSockets();
     $this->_initFactories();
     $this->_processingLoop();
     $this->_shutdownProcessingLoop();
     exit;
 }
Пример #6
0
 protected function _init()
 {
     setproctitle('Daemon: downloader');
     $this->_initSockets();
     $this->_getDownloadListFromWorker();
     $this->_checkAndFilterDownloads();
     $this->_startDownloads();
     exit;
 }
Пример #7
0
 protected function _init()
 {
     $this->_identity = 'worker/' . uniqid();
     setproctitle(sprintf('Daemon: %s', $this->_identity));
     $this->_initSocket();
     $this->_registerAtQueueManager();
     register_shutdown_function(array($this, 'shutdown'));
     $this->_processingLoop();
 }
Пример #8
0
 function wfProfileOut($fn = '')
 {
     global $hackwhere, $wgDBname;
     if (count($hackwhere)) {
         array_pop($hackwhere);
     }
     if (function_exists("setproctitle") && count($hackwhere)) {
         setproctitle($hackwhere[count($hackwhere) - 1] . " [{$wgDBname}]");
     }
 }
Пример #9
0
 /**
  * 设置进程名
  * @param [type] $title [description]
  */
 public static function setTitle($title)
 {
     if (function_exists('cli_set_process_title')) {
         // PHP >= 5.5
         cli_set_process_title($title);
     } else {
         if (function_exists('setproctitle')) {
             // PECL proctitle >= 0.1.0
             setproctitle($title);
         }
     }
 }
Пример #10
0
 /**
  * Sets the process title.
  *
  * @param string $title
  */
 public function setProcessTitle($title)
 {
     if (function_exists('cli_set_process_title')) {
         cli_set_process_title($title);
         //PHP >= 5.5.
     } else {
         if (function_exists('setproctitle')) {
             setproctitle($title);
             //PECL proctitle
         }
     }
 }
Пример #11
0
 public static function setTitle($title)
 {
     $ret = true;
     if (function_exists('cli_set_process_title') && PHP_OS != 'Darwin') {
         cli_set_process_title($title);
     } elseif (function_exists('setproctitle')) {
         setproctitle($title);
     } else {
         $ret = false;
     }
     return $ret;
 }
Пример #12
0
/**
 * Stop profiling of a function
 * @param $fn string
 */
function wfProfileOut($fn = '')
{
    global $hackwhere, $wgDBname, $haveProctitle;
    if (!$haveProctitle) {
        return;
    }
    if (count($hackwhere)) {
        array_pop($hackwhere);
    }
    if (count($hackwhere)) {
        setproctitle($hackwhere[count($hackwhere) - 1] . " [{$wgDBname}]");
    }
}
Пример #13
0
 protected function _init()
 {
     $pid = pcntl_fork();
     if ($pid !== 0) {
         return;
     }
     $pidFile = $this->_config->get('pid_file');
     if (file_exists($pidFile) && '' != exec('ps -p `cat ' . $pidFile . '` --no-heading')) {
         trigger_error('Process running with PID ' . file_get_contents($pidFile), E_USER_NOTICE);
         exit(0);
     }
     file_put_contents($pidFile, getmypid());
     setproctitle('Daemon: ForkMaster');
     $this->_forkChilds();
     $this->_monitorChildProcesses();
 }
Пример #14
0
 /**
  * Run the execution loop.
  *
  * Forks into a master and a loop process. The loop process will handle the
  * evaluation of all instructions, then return its state via a socket upon
  * completion.
  *
  * @param Shell $shell
  */
 public function run(Shell $shell)
 {
     list($up, $down) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
     if (!$up) {
         throw new \RuntimeException('Unable to create socket pair.');
     }
     $pid = pcntl_fork();
     if ($pid < 0) {
         throw new \RuntimeException('Unable to start execution loop.');
     } elseif ($pid > 0) {
         // This is the main thread. We'll just wait for a while.
         // We won't be needing this one.
         fclose($up);
         // Wait for a return value from the loop process.
         $read = array($down);
         $write = null;
         $except = null;
         if (stream_select($read, $write, $except, null) === false) {
             throw new \RuntimeException('Error waiting for execution loop.');
         }
         $content = stream_get_contents($down);
         fclose($down);
         if ($content) {
             $shell->setScopeVariables(@unserialize($content));
         }
         return;
     }
     // This is the child process. It's going to do all the work.
     if (function_exists('setproctitle')) {
         setproctitle('psysh (loop)');
     }
     // We won't be needing this one.
     fclose($down);
     // Let's do some processing.
     parent::run($shell);
     // Send the scope variables back up to the main thread
     fwrite($up, $this->serializeReturn($shell->getScopeVariables()));
     fclose($up);
     exit;
 }
Пример #15
0
 /**
  * Sets the proccess title
  *
  * This function call requires php5.5+ or the proctitle extension!
  * Empty title strings won't be set.
  * @param string $title the new process title
  * @param array $replacements an associative array of replacment values
  * @return void
  */
 public static function setProcessTitle($title, array $replacements = array())
 {
     // skip when empty title names or running on MacOS
     if (trim($title) == '' || PHP_OS == 'Darwin') {
         return;
     }
     // 1. replace the values
     $title = preg_replace_callback('/\\%([a-z0-9]+)\\%/i', function ($match) use($replacements) {
         if (isset($replacements[$match[1]])) {
             return $replacements[$match[1]];
         }
         return $match[0];
     }, $title);
     // 2. remove forbidden chars
     $title = preg_replace('/[^a-z0-9-_.: \\\\\\]\\[]/i', '', $title);
     // 3. set the title
     if (function_exists('cli_set_process_title')) {
         cli_set_process_title($title);
         // PHP 5.5+ has a builtin function
     } elseif (function_exists('setproctitle')) {
         setproctitle($title);
         // pecl proctitle extension
     }
 }
 /**
  * Rename process name
  *
  * @param string $prefix Prefix for the process name
  * @throws NotSupportedException
  */
 protected function renameProcess($prefix = '')
 {
     $name = $this->getProcessName();
     if (false === empty($prefix)) {
         $name = $prefix . '-' . $name;
     }
     //rename process
     if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
         cli_set_process_title($name);
     } else {
         if (function_exists('setproctitle')) {
             setproctitle($name);
         } else {
             throw new NotSupportedException("Can't find cli_set_process_title or setproctitle function");
         }
     }
 }
 /**
  * On supported systems (with the PECL proctitle module installed), update
  * the name of the currently running process to indicate the current state
  * of a worker.
  *
  * @param string $status The updated process title.
  */
 private function updateProcLine($status)
 {
     if (function_exists('setproctitle')) {
         // setproctitle — 设置PHP进程的标题
         setproctitle('resque-' . Resque::VERSION . ': ' . $status);
     }
 }
Пример #18
0
 public function procline($string)
 {
     if (function_exists('setproctitle')) {
         setproctitle("resque-pool-manager{$this->appName}: {$string}");
     }
 }
Пример #19
0
 /**
  * Update the status of the current worker process.
  *
  * On supported systems (with the PECL proctitle module installed), update
  * the name of the currently running process to indicate the current state
  * of a worker.
  *
  * @param string $status The updated process title.
  */
 private function updateProcLine($status)
 {
     if (function_exists('setproctitle')) {
         setproctitle('resque-scheduler-' . ResqueRepeater::VERSION . ': ' . $status);
     }
 }
Пример #20
0
 /**
  * process start
  */
 protected function doStart()
 {
     if ($this->demonize) {
         $pid = pcntl_fork();
         if ($pid == -1) {
             $this->halt(self::EXIT_CODE_ERROR, 'pcntl_fork() rise error');
         } elseif ($pid) {
             $this->halt(self::EXIT_CODE_NORMAL);
         } else {
             posix_setsid();
             //close std streams (unlink console)
             if (is_resource(STDIN)) {
                 fclose(STDIN);
                 $stdIn = fopen('/dev/null', 'r');
             }
             if (is_resource(STDOUT)) {
                 fclose(STDOUT);
                 $stdOut = fopen('/dev/null', 'ab');
             }
             if (is_resource(STDERR)) {
                 fclose(STDERR);
                 $stdErr = fopen('/dev/null', 'ab');
             }
         }
     }
     //rename process
     if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
         cli_set_process_title($this->getProcessName());
     } else {
         if (function_exists('setproctitle')) {
             setproctitle($this->getProcessName());
         } else {
             throw new NotSupportedException("Can't find cli_set_process_title or setproctitle function");
         }
     }
     //run iterator
     return $this->loop();
 }
Пример #21
0
function cl_processtitle($title)
{
    $title = preg_replace('/[^\\w-]/', '', $title);
    if (function_exists('cli_set_process_title')) {
        // PHP5.5 and up
        cli_set_process_title('phpList:' . $GLOBALS['installation_name'] . ':' . $title);
    } elseif (function_exists('setproctitle')) {
        // pecl extension
        setproctitle('phpList:' . $GLOBALS['installation_name'] . ':' . $title);
    }
}
Пример #22
0
 /**
  * Set new process name
  */
 protected function changeProcessName()
 {
     //rename process
     if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
         cli_set_process_title($this->getProcessName());
     } else {
         if (function_exists('setproctitle')) {
             setproctitle($this->getProcessName());
         } else {
             \Yii::error('Can\'t find cli_set_process_title or setproctitle function');
         }
     }
 }
Пример #23
0
 /**
  * Update the status of the current worker process.
  *
  * On supported systems (with the PECL proctitle module installed), update
  * the name of the currently running process to indicate the current state
  * of a worker.
  *
  * @param string $status The updated process title.
  */
 private function updateProcLine($status)
 {
     $processTitle = 'resque-scheduler-' . ResqueScheduler::VERSION . ': ' . $status;
     if (function_exists('cli_set_process_title')) {
         cli_set_process_title($processTitle);
     } else {
         if (function_exists('setproctitle')) {
             setproctitle($processTitle);
         }
     }
 }
Пример #24
0
 /**
  * Runs the command.
  *
  * The code to execute is either defined directly with the
  * setCode() method or by overriding the execute() method
  * in a sub-class.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return int The command exit code
  *
  * @throws \Exception
  *
  * @see setCode()
  * @see execute()
  */
 public function run(InputInterface $input, OutputInterface $output)
 {
     // force the creation of the synopsis before the merge with the app definition
     $this->getSynopsis(true);
     $this->getSynopsis(false);
     // add the application arguments and options
     $this->mergeApplicationDefinition();
     // bind the input against the command specific arguments/options
     try {
         $input->bind($this->definition);
     } catch (ExceptionInterface $e) {
         if (!$this->ignoreValidationErrors) {
             throw $e;
         }
     }
     $this->initialize($input, $output);
     if (null !== $this->processTitle) {
         if (function_exists('cli_set_process_title')) {
             cli_set_process_title($this->processTitle);
         } elseif (function_exists('setproctitle')) {
             setproctitle($this->processTitle);
         } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
             $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
         }
     }
     if ($input->isInteractive()) {
         $this->interact($input, $output);
     }
     // The command name argument is often omitted when a command is executed directly with its run() method.
     // It would fail the validation if we didn't make sure the command argument is present,
     // since it's required by the application.
     if ($input->hasArgument('command') && null === $input->getArgument('command')) {
         $input->setArgument('command', $this->getName());
     }
     $input->validate();
     if ($this->code) {
         $statusCode = call_user_func($this->code, $input, $output);
     } else {
         $statusCode = $this->execute($input, $output);
     }
     return is_numeric($statusCode) ? (int) $statusCode : 0;
 }
Пример #25
0
 /**
  * Sets a title of the current process
  * @param string Title
  * @return void
  */
 protected function setproctitle($title)
 {
     if (function_exists('setproctitle')) {
         return setproctitle($title);
     }
     return FALSE;
 }
Пример #26
0
 /**
  * On supported systems (with the PECL proctitle module installed), update
  * the name of the currently running process to indicate the current state
  * of a worker.
  *
  * @param string $status The updated process title.
  */
 protected function updateProcLine($status)
 {
     if (function_exists('setproctitle')) {
         setproctitle('resque-' . Resque::VERSION . ': ' . $status);
     }
 }
Пример #27
0
<?php

include __DIR__ . '/webdata/init.inc.php';
if (function_exists('setproctitle')) {
    setproctitle('WMS: ' . $_SERVER['REQUEST_URI']);
}
Pix_Controller::addCommonHelpers();
Pix_Controller::dispatch(__DIR__ . '/webdata/');
if (function_exists('setproctitle')) {
    setproctitle('null');
}
Пример #28
0
 /**
  * 设置进程名称,需要proctitle支持 或者php>=5.5
  * @param string $title
  * @return void
  */
 protected static function setProcTitle($title)
 {
     // >=php 5.5
     if (function_exists('cli_set_process_title')) {
         @cli_set_process_title($title);
     } elseif (extension_loaded('proctitle') && function_exists('setproctitle')) {
         @setproctitle($title);
     }
 }
Пример #29
0
 /**
  * Runs the command.
  *
  * The code to execute is either defined directly with the
  * setCode() method or by overriding the execute() method
  * in a sub-class.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return int The command exit code
  *
  * @throws \Exception
  *
  * @see setCode()
  * @see execute()
  *
  * @api
  */
 public function run(InputInterface $input, OutputInterface $output)
 {
     // force the creation of the synopsis before the merge with the app definition
     $this->getSynopsis(true);
     $this->getSynopsis(false);
     // add the application arguments and options
     $this->mergeApplicationDefinition();
     // bind the input against the command specific arguments/options
     try {
         $input->bind($this->definition);
     } catch (\Exception $e) {
         if (!$this->ignoreValidationErrors) {
             throw $e;
         }
     }
     $this->initialize($input, $output);
     if (null !== $this->processTitle) {
         if (function_exists('cli_set_process_title')) {
             cli_set_process_title($this->processTitle);
         } elseif (function_exists('setproctitle')) {
             setproctitle($this->processTitle);
         } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
             $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
         }
     }
     if ($input->isInteractive()) {
         $this->interact($input, $output);
     }
     $input->validate();
     if ($this->code) {
         $statusCode = call_user_func($this->code, $input, $output);
     } else {
         $statusCode = $this->execute($input, $output);
     }
     return is_numeric($statusCode) ? (int) $statusCode : 0;
 }
Пример #30
0
 public function setTitle($title)
 {
     if (function_exists('\\setproctitle')) {
         \setproctitle($title);
     }
 }