-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterationCommand.php
101 lines (81 loc) · 1.93 KB
/
IterationCommand.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
abstract class IterationCommand extends ContainerAwareCommand
{
protected $iterationsCount = 1000;
protected $interval = 5;
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->isLock()) {
$output->writeln('The command is already locked in another process.');
sleep(60);
return 0;
}
$iterations = $this->getIterationsCount();
while ($iterations-- > 0) {
$this->executionBlock($input, $output);
$this->prolongLock();
sleep($this->getInterval());
}
return 0;
}
protected function isLock()
{
return $this->getContainer()->get('snc_redis.default')
->set("COMMAND_LOCK:{$this->getName()}", '1', 'EX', 60, 'NX') === null;
}
protected function removeLock()
{
$this->getContainer()->get('snc_redis.default')->del(["COMMAND_LOCK:{$this->getName()}"]);
}
protected function prolongLock()
{
$this->getContainer()->get('snc_redis.default')->expire('COMMAND_LOCK:' . $this->getName(), 60);
}
public function __destruct()
{
$this->removeLock();
}
protected function executionBlock(InputInterface $input, OutputInterface $output)
{
return 1;
}
/**
* @param int $iterationsCount
* @return IterationCommand
*/
public function setIterationsCount(int $iterationsCount): IterationCommand
{
$this->iterationsCount = $iterationsCount;
return $this;
}
/**
* @param int $interval
* @return IterationCommand
*/
public function setInterval(int $interval): IterationCommand
{
$this->interval = $interval;
return $this;
}
/**
* @return int
*/
public function getIterationsCount(): int
{
return $this->iterationsCount;
}
/**
* @return int
*/
public function getInterval(): int
{
return $this->interval;
}
}