Пример #1
0
function master_process($workers)
{
    //监听子进程,如果停止,会再拉起来
    swoole_process::signal(SIGCHLD, function ($signo) use(&$workers) {
        while (1) {
            $ret = swoole_process::wait(false);
            if ($ret) {
                $pid = $ret['pid'];
                //这里实现一个自动拉起的能力
                $child_process = $workers[$pid];
                logprint('info', "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid);
                $new_pid = $child_process->start();
                $workers[$new_pid] = $child_process;
                unset($workers[$pid]);
            } else {
                break;
            }
        }
    });
    //kill -10 结束全部程序
    swoole_process::signal(SIGUSR1, function ($signo) use(&$workers) {
        swoole_process::signal(SIGCHLD, null);
        foreach ($workers as $pid => $worker) {
            swoole_process::kill($pid);
        }
        //处理子进程,然后自己退出
        exit;
    });
}
Пример #2
0
 public static function reload()
 {
     $pid = file_get_contents(Main::$pidFile);
     if ($pid) {
         $res = swoole_process::kill($pid, SIGUSR1);
         if ($res) {
             Main::log("reload success");
         }
     } else {
         Main::log("进程不存在,reload失败");
     }
 }
Пример #3
0
 /**
  * 停止服务
  */
 public static function stop()
 {
     $pid = file_get_contents(self::$pid_file);
     if ($pid) {
         if (\swoole_process::kill($pid, 0)) {
             \swoole_process::kill($pid, SIGTERM);
         } else {
             @unlink(self::$pid_file);
         }
         defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, self::$process_name . ' exit success');
     }
 }
Пример #4
0
 /**
  * 停止进程
  * @param bool $output
  */
 public static function stop($output = true)
 {
     $pid = @file_get_contents(self::$pid_file);
     if ($pid) {
         if (swoole_process::kill($pid, 0)) {
             swoole_process::kill($pid, SIGTERM);
             Main::log_write("进程" . $pid . "已结束");
         } else {
             @unlink(self::$pid_file);
             Main::log_write("进程" . $pid . "不存在,删除pid文件");
         }
     } else {
         $output && Main::log_write("需要停止的进程未启动");
     }
 }
Пример #5
0
 public function checkStatus()
 {
     $args = getopt('s:');
     if (isset($args['s'])) {
         switch ($args['s']) {
             case 'reload':
                 $pid = file_get_contents($this->cacheDir . "/pid");
                 echo "当前进程" . $pid . "\n";
                 echo "热重启中\n";
                 if ($pid) {
                     if (swoole_process::kill($pid, 0)) {
                         swoole_process::kill($pid, SIGUSR1);
                     }
                 }
                 echo "重启完成\n";
                 swoole_process::daemon();
                 break;
             default:
                 break;
         }
         exit;
     }
 }
Пример #6
0
 /**
  * 监控进程是否在运行
  * @param $opt
  * @return array
  */
 public static function params_m($opt)
 {
     if (isset($opt["m"]) || isset($opt["monitor"])) {
         $pid = @file_get_contents(Crontab::$pid_file);
         if ($pid && swoole_process::kill($pid, 0)) {
             exit;
         } else {
             $opt = array("s" => "restart");
         }
     }
     return $opt;
 }
Пример #7
0
//启动4个进程进行处理
$worker_num = 4;
for ($i = 0; $i < $worker_num; $i++) {
    $process = new swoole_process('callback_function', false, true);
    $pid = $process->start();
    $process->write(dotest());
    $process->pid = $pid;
    // $workers[$pid] = $process;
    swoole_event_add($process->pipe, function ($pipe) use($process) {
        $recv = $process->read();
        if ($recv != '') {
            $data = dotest();
            if ($data != false) {
                $process->write($data);
            } else {
                swoole_process::kill($process->pid);
            }
        }
    });
}
//开启子进程进行异步处理
function callback_function(swoole_process $worker)
{
    $GLOBALS['worker'] = $worker;
    swoole_event_add($worker->pipe, function ($pipe) {
        $worker = $GLOBALS['worker'];
        $recv = $worker->read();
        if ($recv) {
            $data = call_user_func_array('dosomething', [json_decode($recv, true)]);
            $worker->write($data);
        } else {
Пример #8
0
 public function onWorkerStop(\swoole_http_server $server, $worker_id)
 {
     if ($worker_id) {
         return true;
     }
     $worker_pids = $this->shm->get('worker_pid');
     foreach ($worker_pids as $worker_pid) {
         \swoole_process::kill($worker_pid, 9);
     }
     $this->shm->del("worker_pid");
     return true;
 }
Пример #9
0
 /**
  * 注册信号
  */
 private static function register_signal()
 {
     swoole_process::signal(SIGTERM, function ($signo) {
         if (!empty(Main::$http_server)) {
             swoole_process::kill(Main::$http_server->pid, SIGKILL);
         }
         self::exit2p("收到退出信号,退出主进程");
     });
     swoole_process::signal(SIGCHLD, function ($signo) {
         while (($pid = pcntl_wait($status, WNOHANG)) > 0) {
             $task = self::$task_list[$pid];
             $end = microtime(true);
             $start = $task["start"];
             $id = $task["id"];
             Main::log_write("{$id} [Runtime:" . sprintf("%0.6f", $end - $start) . "]");
             unset(self::$task_list[$pid]);
             if (isset(self::$unique_list[$id]) && self::$unique_list[$id] > 0) {
                 self::$unique_list[$id]--;
             }
         }
     });
     swoole_process::signal(SIGUSR1, function ($signo) {
         LoadConfig::reload_config();
     });
 }
Пример #10
0
 /**
  * 结束已开进程
  * @param $workers
  */
 public static function exitprocess($workers)
 {
     foreach ($workers as $task => $data) {
         foreach (self::$workers as $pid => $process) {
             if ($process["task"] == $task) {
                 Squire_Master::$workers[$pid]["logout"] = true;
                 swoole_process::kill($pid, SIGUSR2);
                 unset(self::$process_list[$task]);
             }
         }
     }
 }
Пример #11
0
 protected function _killAllProcess()
 {
     $this->_sigterm = true;
     $workers = \Daemon\Library\Ipc\Manager::getInstance()->getWorkersPid();
     foreach ($workers as $pid) {
         \swoole_process::kill($pid, SIGUSR1);
     }
 }
Пример #12
0
Console::put("swoole_get_local_ip()", !is_array(swoole_get_local_ip()));
Console::put("swoole_version()", !is_string(swoole_version()));
if (!class_exists('swoole_process')) {
    Console::put("no have swoole_process", true, true);
}
$server = new swoole_process(function ($process) {
    $php = $_SERVER['_'];
    return $process->exec($php, array(__DIR__ . "/../examples/server.php", 'daemon'));
}, false, false);
if (!$server->start()) {
    Console::put("Server start failed.", true, true);
}
usleep(200000);
register_shutdown_function(function () {
    global $server;
    if (swoole_process::kill($server->pid, SIGTERM)) {
        Console::put("swoole_process::kill()");
        $status = swoole_process::wait();
        Console::put("swoole_process::wait()", !($status['pid'] == $server->pid));
    } else {
        Console::put("swoole_process::kill()", true);
    }
    echo "------------------------------------------------------------------\n";
    echo "Swoole UnitTest Finish.\n";
});
/**
 * UnitTests for swoole server.
 * This is just a client. Server see examples/server.php
 */
$client = new swoole_client(SWOOLE_TCP);
if (!$client->connect('127.0.0.1', 9501)) {
Пример #13
0
    Console::put("swoole_server->taskwait()", $data and $data == 'taskwaitok');
    Console::put("swoole_server->taskWaitMulti()", test_taskWaitMulti($client));
}
$client->send("close");
$data = $client->recv();
Console::put("swoole_server->close()", $data == '');
echo "------------------------------------------------------------------\n";
Console::put("swoole_client[TCP]->recv()", true);
Console::put("swoole_client[TCP]->recv(256K, MSG_WAITALL)", $sendBigPkgTest);
$sockname = $client->getsockname();
Console::put("swoole_client[TCP]->getsockname()", is_array($sockname));
echo "------------------------------------------------------------------\n";
Console::put("swoole_client[TCP]->close()", $client->close());
echo "------------------------------------------------------------------\n";
$client = new swoole_client(SWOOLE_UDP);
Console::put("swoole_client[UDP]->connect()", $client->connect('127.0.0.1', 9502));
Console::put("swoole_client[UDP]->send()", $client->send("hello"));
$data = $client->recv();
Console::put("swoole_client[UDP]->recv()", $data and $data == 'Swoole: hello');
$peername = $client->getpeername();
Console::put("swoole_client[UDP]->getpeername()", is_array($peername) and $peername['port'] == 9502);
echo "------------------------------------------------------------------\n";
/**
 * Server测试结束,关闭服务器
 */
swoole_process::kill($server->pid + 1, SIGTERM);
Console::put("swoole_process::kill()");
$status = swoole_process::wait();
Console::put("swoole_process::wait()", $status['pid'] == $server->pid);
echo "------------------------------------------------------------------\n";
echo "Swoole UnitTest Finish.\n";
Пример #14
0
 public static function stop()
 {
     $pid = @file_get_contents(self::$pid_file);
     if ($pid) {
         if (swoole_process::kill($pid, 0)) {
             swoole_process::kill($pid, SIGTERM);
             Main::log_write("进程" . $pid . "已结束");
         }
     } else {
         Main::log_write("未找到启动的进程号");
     }
     @unlink(self::$pid_file);
 }
Пример #15
0
/**
 * Kill somebody
 *
 * @param $pid
 * @param int $signo
 * @return int
 */
function process_kill($pid, $signo = SIGTERM)
{
    return swoole_process::kill($pid, $signo);
}