Skip to content

Commit ca775ae

Browse files
authored
* optimize naming * Support use process::signal in manager process * Support use process::signal in task process
1 parent dd3fd53 commit ca775ae

File tree

5 files changed

+122
-7
lines changed

5 files changed

+122
-7
lines changed

ext-src/swoole_process.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,7 @@ static PHP_METHOD(swoole_process, signal) {
534534
RETURN_FALSE;
535535
}
536536

537-
php_swoole_check_reactor();
538-
539537
swSignalHandler handler = swSignal_get_handler(signo);
540-
541538
if (handler && handler != php_swoole_onSignal) {
542539
php_swoole_fatal_error(
543540
E_WARNING, "signal [" ZEND_LONG_FMT "] processor has been registered by the system", signo);
@@ -572,6 +569,18 @@ static PHP_METHOD(swoole_process, signal) {
572569
handler = php_swoole_onSignal;
573570
}
574571

572+
if (sw_server() && sw_server()->is_sync_process()) {
573+
if (signal_fci_caches[signo]) {
574+
sw_zend_fci_cache_free(signal_fci_caches[signo]);
575+
} else {
576+
SwooleTG.signal_listener_num++;
577+
}
578+
signal_fci_caches[signo] = fci_cache;
579+
swSignal_set(signo, handler);
580+
RETURN_TRUE;
581+
}
582+
583+
php_swoole_check_reactor();
575584
// for swSignalfd_setup
576585
SwooleTG.reactor->check_signalfd = true;
577586
if (!SwooleTG.reactor->isset_exit_condition(Reactor::EXIT_CONDITION_SIGNAL_LISTENER)) {
@@ -649,7 +658,7 @@ static void php_swoole_onSignal(int signo) {
649658

650659
if (fci_cache) {
651660
zval argv[1];
652-
ZVAL_LONG(& argv[0], signo);
661+
ZVAL_LONG(&argv[0], signo);
653662

654663
if (UNEXPECTED(!zend::function::call(fci_cache, 1, argv, nullptr, php_swoole_is_enable_coroutine()))) {
655664
php_swoole_fatal_error(
@@ -784,7 +793,7 @@ static PHP_METHOD(swoole_process, read) {
784793

785794
zend_string *buf = zend_string_alloc(buf_size, 0);
786795
ssize_t ret = process->pipe_current->read(buf->val, buf_size);
787-
796+
788797
if (ret < 0) {
789798
efree(buf);
790799
if (errno != EINTR) {

ext-src/swoole_server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ static PHP_METHOD(swoole_server, protect) {
37773777

37783778
static PHP_METHOD(swoole_server, getWorkerId) {
37793779
Server *serv = php_swoole_server_get_and_check_server(ZEND_THIS);
3780-
if (!serv->is_worker()) {
3780+
if (!serv->is_worker() && !serv->is_task_worker()) {
37813781
RETURN_FALSE;
37823782
} else {
37833783
RETURN_LONG(SwooleG.process_id);
@@ -3812,7 +3812,7 @@ static PHP_METHOD(swoole_server, getWorkerStatus) {
38123812

38133813
static PHP_METHOD(swoole_server, getWorkerPid) {
38143814
Server *serv = php_swoole_server_get_and_check_server(ZEND_THIS);
3815-
if (!serv->is_worker()) {
3815+
if (!serv->is_worker() && !serv->is_task_worker()) {
38163816
zend_long worker_id = -1;
38173817
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &worker_id) == FAILURE) {
38183818
RETURN_FALSE;

include/swoole_server.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,15 @@ class Server {
10801080
return SwooleG.process_type == SW_PROCESS_USERWORKER;
10811081
}
10821082

1083+
bool is_sync_process() {
1084+
if (is_manager()) {
1085+
return true;
1086+
}
1087+
if (is_task_worker() && !task_enable_coroutine) {
1088+
return true;
1089+
}
1090+
return false;
1091+
}
10831092
inline bool is_shutdown() {
10841093
return gs->shutdown;
10851094
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
swoole_process: signal in manager
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
use Swoole\Process;
9+
use Swoole\Server;
10+
use function Swoole\Coroutine\run;
11+
12+
define('PID_FILE', __DIR__.'/manager.pid');
13+
14+
$pm = new SwooleTest\ProcessManager;
15+
16+
$pm->parentFunc = function ($pid) use ($pm) {
17+
$manager_pid = file_get_contents(PID_FILE);
18+
Process::kill($manager_pid, SIGINT);
19+
$pm->wait();
20+
$pm->kill();
21+
};
22+
23+
$pm->childFunc = function () use ($pm) {
24+
$serv = new Server('127.0.0.1', $pm->getFreePort());
25+
$serv->set(["worker_num" => 1, 'log_file' => '/dev/null']);
26+
$serv->on("ManagerStart", function (Server $serv) use ($pm) {
27+
file_put_contents(PID_FILE, $serv->getManagerPid());
28+
$pm->wakeup();
29+
Process::signal(SIGINT, function () use($pm) {
30+
echo "SIGINT triggered\n";
31+
$pm->wakeup();
32+
});
33+
});
34+
$serv->on("Receive", function (Server $serv, $fd, $reactorId, $data) {
35+
});
36+
$serv->start();
37+
};
38+
$pm->childFirst();
39+
$pm->run();
40+
unlink(PID_FILE);
41+
?>
42+
--EXPECT--
43+
SIGINT triggered
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
swoole_process: signal in task worker
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
use Swoole\Process;
9+
use Swoole\Server;
10+
use Swoole\Coroutine\Client;
11+
use function Swoole\Coroutine\run;
12+
13+
define('PID_FILE', __DIR__.'/task_worker.pid');
14+
15+
$pm = new SwooleTest\ProcessManager;
16+
17+
$pm->parentFunc = function ($pid) use ($pm) {
18+
$_pid = file_get_contents(PID_FILE);
19+
Process::kill($_pid, SIGINT);
20+
$pm->wait();
21+
$pm->kill();
22+
};
23+
24+
$pm->childFunc = function () use ($pm) {
25+
$serv = new Server('127.0.0.1', $pm->getFreePort());
26+
$serv->set([
27+
'worker_num' => 1,
28+
'task_worker_num' => 1,
29+
'log_file' => '/dev/null'
30+
]);
31+
32+
$serv->on("WorkerStart", function (Server $serv) use ($pm) {
33+
if ($serv->taskworker) {
34+
file_put_contents(PID_FILE, $serv->getWorkerPid());
35+
$pm->wakeup();
36+
Process::signal(SIGINT, function () use($pm) {
37+
echo "SIGINT triggered\n";
38+
$pm->wakeup();
39+
});
40+
}
41+
});
42+
$serv->on("Task", function (Server $serv) use ($pm) {
43+
44+
});
45+
$serv->on("Receive", function (Server $serv, $fd, $reactorId, $data) {
46+
});
47+
$serv->start();
48+
};
49+
$pm->childFirst();
50+
$pm->run();
51+
unlink(PID_FILE);
52+
?>
53+
--EXPECT--
54+
SIGINT triggered

0 commit comments

Comments
 (0)