forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunCommand.php
More file actions
130 lines (105 loc) · 3.71 KB
/
Copy pathRunCommand.php
File metadata and controls
130 lines (105 loc) · 3.71 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace PHPCensor\Command;
use Monolog\Logger;
use PHPCensor\Logging\BuildDBLogHandler;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\BuildStore;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPCensor\Store\Factory;
use PHPCensor\Builder;
use PHPCensor\BuildFactory;
use PHPCensor\Model\Build;
/**
* Run console command - Runs any pending builds.
*
* @deprecated The command is deprecated and will be deleted in version 2.0. Use 'the php-censor:worker' command instead.
* @author Dan Cryer <dan@block8.co.uk>
*/
class RunCommand extends LoggingCommand
{
/**
* @var BuildService
*/
protected $buildService;
/**
* @var int
*/
protected $maxBuilds = 10;
/**
* @param Logger $logger
* @param BuildService $buildService
* @param string $name
*/
public function __construct(
Logger $logger,
BuildService $buildService,
$name = null
)
{
parent::__construct($logger, $name);
$this->buildService = $buildService;
}
protected function configure()
{
$this
->setName('php-censor:run-builds')
->setDescription('<fg=red;options=bold>[DEPRECATED]</> Run all pending PHP Censor builds. <fg=red;options=bold>This command is deprecated and will be deleted in version 2.0. Use the \'php-censor:worker\' command instead.</>');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
/** @var BuildStore $buildStore */
$buildStore = Factory::getStore('Build');
$this->buildService->createPeriodicalBuilds($this->logger);
$result = $buildStore->getByStatus(Build::STATUS_PENDING, $this->maxBuilds);
$this->logger->warning(
'[DEPRECATED] This command is deprecated and will be deleted in version 2.0. Use the \'php-censor:worker\' command instead.'
);
$this->logger->notice(
sprintf('Found %d pending builds', count($result['items']))
);
$builds = 0;
while (count($result['items'])) {
$builds++;
$build = array_shift($result['items']);
$build = BuildFactory::getBuild($build);
// Logging relevant to this build should be stored
// against the build itself.
$buildDbLog = new BuildDBLogHandler($build, Logger::DEBUG);
$this->logger->pushHandler($buildDbLog);
$builder = new Builder($build, $this->logger);
try {
$builder->execute();
} catch (\Exception $ex) {
$builder->getBuildLogger()->log('');
$builder->getBuildLogger()->logFailure(
sprintf(
'BUILD FAILED! Exception: %s',
$build->getId(),
$ex->getMessage()
),
$ex
);
$build->setStatusFailed();
$build->setFinishDate(new \DateTime());
$buildStore->save($build);
$build->sendStatusPostback();
}
// After execution we no longer want to record the information
// back to this specific build so the handler should be removed.
$this->logger->popHandler();
// destructor implicitly call flush
unset($buildDbLog);
}
$this->logger->notice('Finished processing builds.');
return $builds;
}
/**
* @param int $numBuilds
*/
public function setMaxBuilds($numBuilds)
{
$this->maxBuilds = (int)$numBuilds;
}
}