Skip to content

Commit

Permalink
add Process::$master_pid and shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 25, 2019
1 parent a9f8972 commit a1d6eaa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
21 changes: 21 additions & 0 deletions swoole_process_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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[] =
{
Expand All @@ -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
};

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/swoole_process_pool/getprocess.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions tests/swoole_process_pool/master_pid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
swoole_process_pool: master pid
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$pool = new Swoole\Process\Pool(1);
$pid = posix_getpid();
$pool->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--
20 changes: 20 additions & 0 deletions tests/swoole_process_pool/shutdown.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
swoole_process_pool: shutdown
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$pool = new Swoole\Process\Pool(1);
$pool->on('workerStart', function (Swoole\Process\Pool $pool, int $workerId)
{
$pool->shutdown();
sleep(20);
echo "ERROR\n";
});

$pool->start();
?>
--EXPECT--

0 comments on commit a1d6eaa

Please sign in to comment.