Test Failed
Push — master ( fd0a7a...05b8bb )
by Hirofumi
15:37
created

JobEnqueue::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 23
cts 23
cp 1
rs 8.7857
c 0
b 0
f 0
cc 6
nc 10
nop 0
crap 6
1
<?php
2
3
namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Illuminate\Console\Command;
7
use LogicException;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use Shippinno\Job\Application\Messaging\EnqueueStoredJobsService;
12
use Shippinno\Job\Domain\Model\FailedToEnqueueStoredJobException;
13
use Shippinno\Job\Infrastructure\Persistence\Doctrine\ManagerRegistryAwareTrait;
14
15
class JobEnqueue extends Command
16
{
17
    use ManagerRegistryAwareTrait;
18
    use LoggerAwareTrait;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $signature = 'job:enqueue';
24
25
    /**
26
     * @var EnqueueStoredJobsService
27
     */
28
    private $service;
29
30
    /**
31
     * @var bool
32
     */
33
    private $terminating = false;
34
35
    /**
36
     * @param EnqueueStoredJobsService $service
37
     * @param ManagerRegistry|null $managerRegistry
38
     * @param LoggerInterface|null $logger
39
     */
40 4 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
        EnqueueStoredJobsService $service,
42
        ManagerRegistry $managerRegistry = null,
43
        LoggerInterface $logger = null
44
    ) {
45 4
        parent::__construct();
46 4
        $this->service = $service;
47 4
        $this->setManagerRegistry($managerRegistry);
48 4
        $this->setLogger(null !== $logger ? $logger : new NullLogger);
49 4
    }
50
51 4
    public function handle()
52
    {
53 4
        if (extension_loaded('pcntl')) {
54 4
            pcntl_signal(SIGTERM, [$this, 'terminate']);
55
        }
56
57 4
        $topicName = $this->topicName();
58 3
        $testing = env('JOB_TESTING', false);
59
        do {
60 3
            $this->clear();
61
            try {
62 3
                $enqueuedMessagesCount = $this->service->execute($topicName);
63 2
                $this->flushAndLog(
64 2
                    $enqueuedMessagesCount > 0,
65 2
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
66
                );
67 1
            } catch (FailedToEnqueueStoredJobException $e) {
68 1
                $enqueuedMessagesCount = $e->enqueuedMessagesCount();
69 1
                $this->flushAndLog(
70 1
                    $enqueuedMessagesCount > 0,
71 1
                    sprintf('%d job(s) enqueued.', $enqueuedMessagesCount)
72
                );
73 1
                $interval = !$testing ? 60 : 0;
74 1
                $this->logger->alert(
75 1
                    sprintf('Failed to enqueue stored job, retrying in %d second(s).', $interval),
76 1
                    ['exception' => $e]
77
                );
78 1
                sleep($interval);
79 1
                continue;
80
            }
81 3
        } while (!$this->terminating && !$testing);
82 3
    }
83
84
    /**
85
     * @return string
86
     */
87 4
    private function topicName(): string
88
    {
89 4
        $topicName = env('JOB_ENQUEUE_TOPIC');
90 4
        if (!$topicName) {
91 1
            throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined');
92
        }
93
94 3
        return $topicName;
95
    }
96
97
    /**
98
     * @param bool $condition
99
     * @param string $logMessage
100
     */
101 3
    private function flushAndLog(bool $condition, string $logMessage): void
102
    {
103 3
        if (!$condition) {
104
            return;
105
        }
106 3
        $this->flush();
107 3
        $this->logger->debug($logMessage);
108 3
    }
109
110
    private function terminate()
111
    {
112
        $this->logger->info('Terminating job:enqueue.');
113
        $this->terminating = true;
114
    }
115
116
}
117