From a1d6eaa1e2d6cb2d6b10e5ead347a9812835092d Mon Sep 17 00:00:00 2001 From: matyhtf Date: Mon, 25 Mar 2019 10:45:39 +0800 Subject: [PATCH] add Process::$master_pid and shutdown --- swoole_process_pool.cc | 21 +++++++++++++++++++++ tests/swoole_process_pool/getprocess.phpt | 3 ++- tests/swoole_process_pool/master_pid.phpt | 23 +++++++++++++++++++++++ tests/swoole_process_pool/shutdown.phpt | 20 ++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/swoole_process_pool/master_pid.phpt create mode 100644 tests/swoole_process_pool/shutdown.phpt diff --git a/swoole_process_pool.cc b/swoole_process_pool.cc index da0d925279d..bb12f107dba 100644 --- a/swoole_process_pool.cc +++ b/swoole_process_pool.cc @@ -47,6 +47,7 @@ static PHP_METHOD(swoole_process_pool, listen); static PHP_METHOD(swoole_process_pool, write); static PHP_METHOD(swoole_process_pool, getProcess); static PHP_METHOD(swoole_process_pool, start); +static PHP_METHOD(swoole_process_pool, shutdown); static const zend_function_entry swoole_process_pool_methods[] = { @@ -57,6 +58,7 @@ static const zend_function_entry swoole_process_pool_methods[] = PHP_ME(swoole_process_pool, listen, arginfo_swoole_process_pool_listen, ZEND_ACC_PUBLIC) PHP_ME(swoole_process_pool, write, arginfo_swoole_process_pool_write, ZEND_ACC_PUBLIC) PHP_ME(swoole_process_pool, start, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC) + PHP_ME(swoole_process_pool, shutdown, arginfo_swoole_process_pool_void, ZEND_ACC_PUBLIC) PHP_FE_END }; @@ -82,6 +84,8 @@ void swoole_process_pool_init(int module_number) SWOOLE_SET_CLASS_SERIALIZABLE(swoole_process_pool, zend_class_serialize_deny, zend_class_unserialize_deny); SWOOLE_SET_CLASS_CLONEABLE(swoole_process_pool, zend_class_clone_deny); SWOOLE_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_process_pool, zend_class_unset_property_deny); + + zend_declare_property_long(swoole_process_pool_ce_ptr, ZEND_STRL("master_pid"), -1, ZEND_ACC_PUBLIC); } static void php_swoole_process_pool_onWorkerStart(swProcessPool *pool, int worker_id) @@ -419,6 +423,8 @@ static PHP_METHOD(swoole_process_pool, start) pool->onWorkerStart = php_swoole_process_pool_onWorkerStart; pool->onWorkerStop = php_swoole_process_pool_onWorkerStop; + zend_update_property_long(swoole_process_pool_ce_ptr, getThis(), ZEND_STRL("master_pid"), getpid()); + if (swProcessPool_start(pool) < 0) { RETURN_FALSE; @@ -456,6 +462,21 @@ static PHP_METHOD(swoole_process_pool, getProcess) RETURN_ZVAL(current_process, 1, 0); } +static PHP_METHOD(swoole_process_pool, shutdown) +{ + zval rv; + zval *retval = zend_read_property(swoole_process_pool_ce_ptr, getThis(), ZEND_STRL("master_pid"), 1, &rv); + long pid = zval_get_long(retval); + if (pid > 0) + { + RETURN_BOOL(kill(pid, SIGTERM) == 0); + } + else + { + RETURN_FALSE; + } +} + static PHP_METHOD(swoole_process_pool, __destruct) { SW_PREVENT_USER_DESTRUCT; diff --git a/tests/swoole_process_pool/getprocess.phpt b/tests/swoole_process_pool/getprocess.phpt index 47d01130194..6b3b122bf83 100644 --- a/tests/swoole_process_pool/getprocess.phpt +++ b/tests/swoole_process_pool/getprocess.phpt @@ -17,7 +17,8 @@ $pool->on('workerStart', function (Swoole\Process\Pool $pool, int $workerId) use $process = $pool->getProcess(); assert($process->pid == posix_getpid()); posix_kill($pid, SIGTERM); - sleep(100); + sleep(20); + echo "ERROR\n"; }); $pool->start(); diff --git a/tests/swoole_process_pool/master_pid.phpt b/tests/swoole_process_pool/master_pid.phpt new file mode 100644 index 00000000000..897ab9394d8 --- /dev/null +++ b/tests/swoole_process_pool/master_pid.phpt @@ -0,0 +1,23 @@ +--TEST-- +swoole_process_pool: master pid +--SKIPIF-- + +--FILE-- +on('workerStart', function (Swoole\Process\Pool $pool, int $workerId) use ($pid) +{ + assert($pool->master_pid == $pid); + posix_kill($pid, SIGTERM); + sleep(20); + echo "ERROR\n"; +}); + +$pool->start(); +?> +--EXPECT-- diff --git a/tests/swoole_process_pool/shutdown.phpt b/tests/swoole_process_pool/shutdown.phpt new file mode 100644 index 00000000000..f0f503614c7 --- /dev/null +++ b/tests/swoole_process_pool/shutdown.phpt @@ -0,0 +1,20 @@ +--TEST-- +swoole_process_pool: shutdown +--SKIPIF-- + +--FILE-- +on('workerStart', function (Swoole\Process\Pool $pool, int $workerId) +{ + $pool->shutdown(); + sleep(20); + echo "ERROR\n"; +}); + +$pool->start(); +?> +--EXPECT--