Пример #1
0
 /**
  * 当 worker 进程投递的任务在 task_worker 中完成时, task_worker 会通过 swoole_server_finish 函数将任务处理的结果发送给 worker 进程。
  * 
  * @param ISwoole $sw
  * @param int $task_id
  * @param string $data
  */
 function onFinish($sw, $task_id, $data)
 {
     Console::info('Swoole::onFinish() 事件被调用。');
 }
Пример #2
0
 /**
  * 执行任务。
  */
 function execute()
 {
     \LeePHP\Utility\Console::debug($this->data);
     $this->data['name'] = $this->data['name'] . ' / ' . date('Y-m-d H:i:s');
 }
Пример #3
0
 /**
  * 请求调度。
  * 
  * @param array $argv    指定 CLI 模式运行时的命令行参数集合。
  * @param array $def_cnf 指定系统全局配置参数。
  */
 function dispatch(&$argv, &$def_cnf)
 {
     if (0 != strcmp(PHP_SAPI, 'cli')) {
         echo '此脚本仅支持 cli 模式运行!', PHP_EOL;
         exit(0);
     }
     // 载入命令行参数
     $this->argv = $argv;
     include SYS_CONF . 'cmd.inc.php';
     $this->cmds = $g_cmd_hash;
     $this->timestamp = time();
     $this->_start_ms = microtime(true);
     $this->cfgs['g'] = $def_cnf;
     // 设置系统全局参数
     date_default_timezone_set($this->_timeZone);
     error_reporting($this->_errorLevel);
     set_error_handler(array($this, 'defErrorHandler'), $this->_errorLevel);
     set_exception_handler(array($this, 'defExceptionHandler'));
     // 初始化全局基础对象
     $this->app = new Application($this);
     Console::initialize($this, true);
     // 实例化基础对象 ...
     $this->logger = new Logger($this);
     $this->db = new DbPdo($this->_dbAutoCommit, $this->_dbPersistent);
     $this->db->addDb($this->cfgs['g']['db']);
     // 初始化 Swoole 实例 ...
     $this->_as = new AppServer($this);
     $this->_ci = new \swoole_server($this->_host, $this->_port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
     $this->_ci->set($this->cfgs['g']['swoole']);
     $this->_ci->on('start', array($this->_as, 'onStart'));
     $this->_ci->on('shutdown', array($this->_as, 'onShutdown'));
     $this->_ci->on('connect', array($this->_as, 'onConnect'));
     $this->_ci->on('close', array($this->_as, 'onClose'));
     $this->_ci->on('workerStart', array($this->_as, 'onWorkerStart'));
     $this->_ci->on('workerStop', array($this->_as, 'onWorkerStop'));
     $this->_ci->on('masterConnect', array($this->_as, 'onMasterConnect'));
     $this->_ci->on('masterClose', array($this->_as, 'onMasterClose'));
     $this->_ci->on('receive', array($this->_as, 'onReceive'));
     $this->_ci->on('timer', array($this->_as, 'onTimer'));
     $this->_ci->on('task', array($this->_as, 'onTask'));
     $this->_ci->on('finish', array($this->_as, 'onFinish'));
     if (!empty($this->_listeners)) {
         foreach ($this->_listeners as $v) {
             $this->_ci->addlistener($v[0], $v[1], SWOOLE_SOCK_TCP);
         }
     }
     $this->_ci->start();
 }
Пример #4
0
 /**
  * 启动多进程工作任务。
  */
 protected function launch(&$data)
 {
     if (!$this->handler) {
         throw new ArgumentException('尚未设置 IProcessHandler 对象引用!', -1);
     }
     $pid = pcntl_fork();
     if ($pid == -1) {
         Console::error('Could not launch new job, exiting');
         return false;
     } else {
         if ($pid) {
             $this->jobs[$pid] = $data;
             if (isset($this->signals[$pid])) {
                 $this->childSignalHandler(SIGCHLD, $pid, $this->signals[$pid]);
                 unset($this->signals[$pid]);
             }
         } else {
             $exit_code = 0;
             $this->ctx->pid = getmypid();
             // do something ...
             $this->handler->doWork($data);
             exit($exit_code);
         }
     }
     return true;
 }