WorkCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 18
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A listenTerminateSignals() 0 14 1
A handle() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Integration\Laravel\Console\Commands;
6
7
use Umbrellio\TableSync\Integration\Laravel\Console\ProcessManagerCommand;
8
use Umbrellio\TableSync\Rabbit\Consumer;
9
10
class WorkCommand extends ProcessManagerCommand
11
{
12
    protected $signature = 'table_sync:work';
13
    protected $description = 'Run table_sync worker';
14
15
    public function __destruct()
16
    {
17
        $this->pidManager->removePid();
18
    }
19
20
    public function handle(Consumer $consumer): void
21
    {
22
        if ($this->pidManager->pidExists()) {
23
            $this->warn("Table sync worker already running (PID: {$this->pidManager->readPid()})");
24
            return;
25
        }
26
27
        $this->pidManager->writePid();
28
        $this->listenTerminateSignals($consumer);
29
30
        $consumer->consume();
31
    }
32
33
    private function listenTerminateSignals(Consumer $consumer): void
34
    {
35
        pcntl_async_signals(true);
36
37
        pcntl_signal(SIGTERM, function () use ($consumer) {
38
            $this->line('Shutting down by SIGTERM.');
39
40
            $consumer->terminate();
41
        });
42
43
        pcntl_signal(SIGINT, function () use ($consumer) {
44
            $this->line('Shutting down by SIGINT');
45
46
            $consumer->terminate();
47
        });
48
    }
49
}
50