1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMS\JobQueueBundle\Console; |
4
|
|
|
|
5
|
|
|
declare(ticks=10000000); |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Statement; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
15
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Records debugging information for executed commands. |
19
|
|
|
* |
20
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Application extends BaseApplication |
23
|
|
|
{ |
24
|
|
|
private $insertStatStmt; |
25
|
|
|
private $input; |
26
|
|
|
|
27
|
|
|
public function __construct(KernelInterface $kernel) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($kernel); |
30
|
|
|
|
31
|
|
|
$this->getDefinition()->addOption(new InputOption('--jms-job-id', null, InputOption::VALUE_REQUIRED, 'The ID of the Job.')); |
32
|
|
|
|
33
|
|
|
$kernel->boot(); |
34
|
|
|
if ($kernel->getContainer()->getParameter('jms_job_queue.statistics')) { |
35
|
|
|
$this->insertStatStmt = "INSERT INTO jms_job_statistics (job_id, characteristic, createdAt, charValue) VALUES (:jobId, :name, :createdAt, :value)"; |
36
|
|
|
register_tick_function(array($this, 'onTick')); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$this->input = $input; |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$rs = parent::doRun($input, $output); |
46
|
|
|
$this->saveDebugInformation(); |
47
|
|
|
|
48
|
|
|
return $rs; |
49
|
|
|
} catch (\Exception $ex) { |
50
|
|
|
$this->saveDebugInformation($ex); |
51
|
|
|
|
52
|
|
|
throw $ex; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function onTick() |
57
|
|
|
{ |
58
|
|
View Code Duplication |
if (!$this->input->hasOption('jms-job-id') || null === $jobId = $this->input->getOption('jms-job-id')) { |
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$characteristics = array( |
63
|
|
|
'memory' => memory_get_usage(), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if (!$this->insertStatStmt instanceof Statement) { |
67
|
|
|
$this->insertStatStmt = $this->getConnection()->prepare($this->insertStatStmt); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->insertStatStmt->bindValue('jobId', $jobId, \PDO::PARAM_INT); |
71
|
|
|
$this->insertStatStmt->bindValue('createdAt', new \DateTime(), Type::getType('datetime')); |
72
|
|
|
|
73
|
|
|
foreach ($characteristics as $name => $value) { |
74
|
|
|
$this->insertStatStmt->bindValue('name', $name); |
75
|
|
|
$this->insertStatStmt->bindValue('value', $value); |
76
|
|
|
$this->insertStatStmt->execute(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function saveDebugInformation(\Throwable $ex = null) |
81
|
|
|
{ |
82
|
|
View Code Duplication |
if (!$this->input->hasOption('jms-job-id') || null === $jobId = $this->input->getOption('jms-job-id')) { |
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->getConnection()->executeUpdate( |
87
|
|
|
"UPDATE jms_jobs SET stackTrace = :trace, memoryUsage = :memoryUsage, memoryUsageReal = :memoryUsageReal WHERE id = :id", |
88
|
|
|
array( |
89
|
|
|
'id' => $jobId, |
90
|
|
|
'memoryUsage' => memory_get_peak_usage(), |
91
|
|
|
'memoryUsageReal' => memory_get_peak_usage(true), |
92
|
|
|
'trace' => serialize($ex ? FlattenException::create($ex) : null), |
93
|
|
|
), |
94
|
|
|
array( |
95
|
|
|
'id' => \PDO::PARAM_INT, |
96
|
|
|
'memoryUsage' => \PDO::PARAM_INT, |
97
|
|
|
'memoryUsageReal' => \PDO::PARAM_INT, |
98
|
|
|
'trace' => \PDO::PARAM_LOB, |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function getConnection() |
104
|
|
|
{ |
105
|
|
|
return $this->getKernel()->getContainer()->get('doctrine')->getManagerForClass('JMSJobQueueBundle:Job')->getConnection(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.