1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TMSolution\CronBundle\Command; |
10
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\StringInput; |
15
|
|
|
|
16
|
|
|
class CronTaskRunCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
const EXEC_CLI = 1; |
20
|
|
|
const EXEC_SYMFONYCLI = 2; |
21
|
|
|
const EXEC_REST = 3; |
22
|
|
|
const EXEC_SYMFONYSERVICE = 4; |
23
|
|
|
|
24
|
|
|
private $output; |
25
|
|
|
private $em; |
26
|
|
|
|
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
|
|
->setName('crontasks:run') |
31
|
|
|
->setDescription('Runs Cron Tasks if needed') |
32
|
|
|
; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function getEntityManager() |
36
|
|
|
{ |
37
|
|
|
return $this->getContainer()->get('doctrine.orm.entity_manager'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
//$output->writeln('<comment>Running Cron Tasks...</comment>'); |
43
|
|
|
|
44
|
|
|
$this->output = $output; |
45
|
|
|
$this->em = $this->getEntityManager(); |
46
|
|
|
// $i = 0; |
47
|
|
|
while (true) { |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$crontasks = $this->em->getRepository('TMSolutionCronBundle:CronTask')->getNewTasks(20); |
55
|
|
|
|
56
|
|
|
foreach($crontasks as $crontask) { |
57
|
|
|
if ($crontask) { |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->runCronTask($crontask, $output); |
61
|
|
|
$this->em->flush(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
//$i++; |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
echo "Błąd crona."; |
67
|
|
|
} |
68
|
|
|
sleep(10); |
69
|
|
|
} |
70
|
|
|
$output->writeln('<comment>Done!</comment>'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function runCronTask($crontask, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$output->writeln(sprintf('Running Cron Task <info>%s</info>', $crontask->getName())); |
76
|
|
|
$crontask->setRunDate(new \DateTime()); |
77
|
|
|
$commands = $crontask->getCommands(); |
78
|
|
|
$crontask->setUsed(1); |
79
|
|
|
$this->em->persist($crontask); |
80
|
|
|
try { |
81
|
|
|
$this->executeCommands($commands, $crontask->getExecType(), $output); |
82
|
|
|
$output->writeln('<info>SUCCESS</info>'); |
83
|
|
|
$crontask->setSuccess(1); |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
$output->writeln(sprintf('<error>ERROR</error><comment>%s</comment>', $e->getMessage())); |
86
|
|
|
$crontask->setSuccess(0); |
87
|
|
|
} |
88
|
|
|
$this->em->persist($crontask); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function executeCommands($commands, $execType, OutputInterface $output) |
92
|
|
|
{ |
93
|
|
|
foreach ($commands as $command) { |
94
|
|
|
$output->writeln(sprintf('Executing command <comment>%s</comment>...', $command)); |
95
|
|
|
if ($execType == self::EXEC_SYMFONYCLI) { |
96
|
|
|
return $this->runSymfonyCliCommand($command); |
97
|
|
|
} elseif ($execType == self::EXEC_CLI) { |
98
|
|
|
return $this->runCliCommand($command); |
99
|
|
|
} elseif ($execType == self::EXEC_REST) { |
100
|
|
|
return $this->runRestCommand($command); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function runSymfonyCliCommand($string) |
106
|
|
|
{ |
107
|
|
|
$namespace = explode(' ', $string)[0]; |
108
|
|
|
$command = $this->getApplication()->find($namespace); |
109
|
|
|
$input = new StringInput($string); |
110
|
|
|
$returnCode = $command->run($input, $this->output); |
111
|
|
|
|
112
|
|
|
return $returnCode != 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function runCliCommand($command) |
116
|
|
|
{ |
117
|
|
|
$command = 'nohup ' . $command . ' > /dev/null 2>&1 & echo $!'; |
118
|
|
|
$op = []; |
119
|
|
|
exec($command, $op); |
120
|
|
|
|
121
|
|
|
if (!isset($op[0])) { |
122
|
|
|
throw new \Exception; |
123
|
|
|
} else { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function runRestCommand($command) |
129
|
|
|
{ |
130
|
|
|
//next example will recieve all messages for specific conversation |
131
|
|
|
$service_url = 'http://localhost/owca/web/app_dev.php/dummy'; |
132
|
|
|
$curl = curl_init($service_url); |
133
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
134
|
|
|
$curl_response = curl_exec($curl); |
135
|
|
|
if ($curl_response === false) { |
136
|
|
|
$info = curl_getinfo($curl); |
137
|
|
|
curl_close($curl); |
138
|
|
|
throw new \Exception('error occured during curl exec. Additinal info: ' . var_export($info)); |
139
|
|
|
} |
140
|
|
|
curl_close($curl); |
141
|
|
|
$decoded = json_decode($curl_response); |
142
|
|
|
|
143
|
|
|
var_dump($decoded); |
144
|
|
|
exit; |
145
|
|
|
|
146
|
|
|
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { |
|
|
|
|
147
|
|
|
throw new \Exception('error occured: ' . $decoded->response->errormessage); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
echo 'response ok!'; |
153
|
|
|
var_export($decoded->response); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.