1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
class CountCommand extends ContainerAwareCommand |
10
|
|
|
{ |
11
|
4 |
|
protected function configure() |
12
|
|
|
{ |
13
|
|
|
$this |
14
|
4 |
|
->setName('dtc:queue:count') |
15
|
4 |
|
->setDescription('Display job queue status.'); |
16
|
4 |
|
} |
17
|
|
|
|
18
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
19
|
|
|
{ |
20
|
4 |
|
$container = $this->getContainer(); |
21
|
4 |
|
$jobManager = $container->get('dtc_queue.manager.job'); |
22
|
|
|
|
23
|
4 |
|
$waitingCount = $jobManager->getWaitingJobCount(); |
24
|
4 |
|
$status = $jobManager->getStatus(); |
25
|
|
|
|
26
|
4 |
|
$firstJob = key($status); |
27
|
3 |
|
if ($firstJob) { |
28
|
1 |
|
$jobKeys = array_keys($status); |
29
|
1 |
|
$maxLength = max(array_map(function ($item) { |
30
|
1 |
|
return strlen($item ?: ''); |
31
|
1 |
|
}, $jobKeys)); |
32
|
1 |
|
$formatLen = $this->determineFormatLength($maxLength); |
33
|
1 |
|
$format = '%-'.$formatLen.'s'; |
34
|
1 |
|
$headingArgs = ['Job name']; |
35
|
1 |
|
$initialKeys = array_keys($status[$firstJob]); |
36
|
1 |
|
$this->formatHeadings($initialKeys, $headingArgs, $format); |
37
|
1 |
|
array_unshift($headingArgs, $format); |
38
|
1 |
|
$msg = call_user_func_array('sprintf', $headingArgs); |
39
|
1 |
|
$output->writeln($msg); |
40
|
1 |
|
$this->outputStatus($output, $status, $initialKeys, $format); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$output->writeln("Total waiting jobs: {$waitingCount}"); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param OutputInterface $output |
48
|
|
|
* @param array $status |
49
|
|
|
* @param array $initialKeys |
50
|
|
|
* @param string $format |
51
|
|
|
*/ |
52
|
1 |
|
private function outputStatus(OutputInterface $output, array $status, array $initialKeys, $format) |
53
|
|
|
{ |
54
|
1 |
|
foreach ($status as $func => $info) { |
55
|
1 |
|
$lineArgs = [$format, $func]; |
56
|
1 |
|
foreach ($initialKeys as $statusKey) { |
57
|
1 |
|
$lineArgs[] = $info[$statusKey]; |
58
|
|
|
} |
59
|
1 |
|
$msg = call_user_func_array('sprintf', $lineArgs); |
60
|
1 |
|
$output->writeln($msg); |
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $initialKeys |
66
|
|
|
* @param string $format |
67
|
|
|
*/ |
68
|
1 |
|
private function formatHeadings(array $initialKeys, array &$headingArgs, &$format) |
69
|
|
|
{ |
70
|
1 |
|
foreach ($initialKeys as $statusName) { |
71
|
1 |
|
$headingStr = ucwords(str_replace('_', ' ', $statusName)); |
72
|
1 |
|
$format .= ' %'.(1 + strlen($headingStr)).'s'; |
73
|
1 |
|
$headingArgs[] = $headingStr; |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $maxLength |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
1 |
|
private function determineFormatLength($maxLength) |
83
|
|
|
{ |
84
|
1 |
|
$formatLen = $maxLength > 50 ? 50 : $maxLength; |
85
|
1 |
|
$formatMinLen = strlen('Job name') + 1; |
86
|
1 |
|
$formatLen = $formatLen < $formatMinLen ? $formatMinLen : $formatLen; |
87
|
|
|
|
88
|
1 |
|
return $formatLen; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|