There are two common ways to stop a running command so:
- CTRL+C on the terminal — This will send an interrupt signal (SIGINT) to the process
- Run kill [process id] - This will send a termination signal (SIGTERM) to the process
Then we need to tell PHP to handle ticks using declare(ticks = 1)
or cntl_async_signals(true)
, and register two signal handlers using pcntl_signal()
.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyWorker extends Command
{
protected $signature = 'my-worker';
protected $description = 'Demonstration worker that gracefully stops on exit';
private $run = true;
public function fire()
{
// PHP 7.0 and before can handle asynchronous signals with ticks
declare(ticks=1);
// PHP 7.1 and later can handle asynchronous signals natively
pcntl_async_signals(true);
pcntl_signal(SIGINT, [$this, 'shutdown']); // Call $this->shutdown() on SIGINT
pcntl_signal(SIGTERM, [$this, 'shutdown']); // Call $this->shutdown() on SIGTERM
$this->info('Worker started');
$worker = new Worker();
while ($this->run) {
$worker->work();
}
$this->info('Worker stopped');
}
public function shutdown()
{
$this->info('Gracefully stopping worker...');
// When set to false, worker will finish current item and stop.
$this->run = false;
// send SIGUSR1 to current process id
// posix_* functions require the posix extension
posix_kill(posix_getpid(), SIGUSR1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Refs:
- https://www.egeniq.com/blog/how-gracefully-stop-laravel-cli-command
Comment