1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\CronBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
|
7
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
8
|
|
|
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
use Oro\Bundle\CronBundle\Job\Daemon; |
15
|
|
|
use Oro\Component\Log\OutputLogger; |
16
|
|
|
|
17
|
|
|
class DaemonMonitorCommand extends ContainerAwareCommand |
18
|
|
|
{ |
19
|
|
|
const COMMAND_NAME = 'oro:daemon'; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $repeatTime = [5, 10, 15, 25, 25]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function configure() |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
|
|
->setName(static::COMMAND_NAME) |
31
|
|
|
->addOption( |
32
|
|
|
'start', |
33
|
|
|
'start', |
34
|
|
|
InputOption::VALUE_NONE, |
35
|
|
|
'If option exists Daemon will start' |
36
|
|
|
) |
37
|
|
|
->addOption( |
38
|
|
|
'stop', |
39
|
|
|
'stop', |
40
|
|
|
InputOption::VALUE_NONE, |
41
|
|
|
'If option exists Daemon will stop' |
42
|
|
|
) |
43
|
|
|
->addOption( |
44
|
|
|
'restart', |
45
|
|
|
'restart', |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
'If option exists Daemon will restart' |
48
|
|
|
) |
49
|
|
|
->setDescription('Monitor Daemon and restart it once a day if it has pid'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$logger = new OutputLogger($output); |
58
|
|
|
$daemon = $this->getContainer()->get('oro_cron.job_daemon'); |
59
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
60
|
|
|
$stop = (int)$input->getOption('stop'); |
61
|
|
|
$start = (int)$input->getOption('start'); |
62
|
|
|
$restart = (int)$input->getOption('restart'); |
63
|
|
|
$isClearQueue = false; |
64
|
|
|
|
65
|
|
|
if (($stop + $start + $restart) > 1) { |
66
|
|
|
throw new \RuntimeException('Please use only one option at a time'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($restart || $this->needToRestart($daemon)) { |
70
|
|
|
$stop = true; |
71
|
|
|
$start = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($this->repeatTime as $time) { |
75
|
|
|
$jobQueue = (int)$this->getJobInQueue($em); |
76
|
|
|
|
77
|
|
|
if ($jobQueue > 0) { |
78
|
|
|
$logger->info(sprintf('There is a %d job(s), script will wait %d seconds', $jobQueue, $time)); |
79
|
|
|
sleep($time); |
80
|
|
|
} else { |
81
|
|
|
$isClearQueue = true; |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($stop && $isClearQueue) { |
87
|
|
|
$this->doStop($daemon, $logger); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($start && $isClearQueue) { |
91
|
|
|
$this->doStart($daemon, $logger); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Daemon $daemon |
97
|
|
|
* @param OutputLogger $logger |
98
|
|
|
*/ |
99
|
|
|
protected function doStop(Daemon $daemon, OutputLogger $logger) |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
if ($daemon->stop()) { |
103
|
|
|
$logger->info('Daemon was stopped'); |
104
|
|
|
} |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
$logger->info('Daemon already stopped'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Daemon $daemon |
112
|
|
|
* @param OutputLogger $logger |
113
|
|
|
*/ |
114
|
|
|
protected function doStart(Daemon $daemon, OutputLogger $logger) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
if ($daemon->run()) { |
|
|
|
|
118
|
|
|
$logger->info('Daemon was started'); |
119
|
|
|
} |
120
|
|
|
} catch (\Exception $e) { |
121
|
|
|
$logger->info('Daemon can`t be started'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Daemon $daemon |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function needToRestart(Daemon $daemon) |
131
|
|
|
{ |
132
|
|
|
if ($daemon->getPid()) { |
133
|
|
|
$interval = date_diff($daemon->getDateStart(), new \DateTime('now')); |
134
|
|
|
return ((int)$interval->format('%d')) >= 1; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param EntityManager $em |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function getJobInQueue(EntityManager $em) |
146
|
|
|
{ |
147
|
|
|
$qb = $em->getRepository('JMSJobQueueBundle:Job')->createQueryBuilder('j'); |
148
|
|
|
$qb->select($qb->expr()->count('j')); |
149
|
|
|
$qb->andWhere($qb->expr()->in('j.state', [Job::STATE_RUNNING, Job::STATE_NEW, Job::STATE_PENDING])); |
150
|
|
|
return $qb->getQuery()->getSingleScalarResult(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: