|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCI - Continuous Integration for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2014, Block 8 Limited. |
|
6
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://www.phptesting.org/ |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PHPCI\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Monolog\Logger; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Daemon that loops and call the run-command. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Gabriel Baker <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class DaemoniseCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Logger |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $logger; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var OutputInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $output; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $run; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $sleep; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \Monolog\Logger $logger |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Logger $logger, $name = null) |
|
51
|
|
|
{ |
|
52
|
|
|
parent::__construct($name); |
|
53
|
|
|
$this->logger = $logger; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function configure() |
|
57
|
|
|
{ |
|
58
|
|
|
$this |
|
59
|
|
|
->setName('phpci:daemonise') |
|
60
|
|
|
->setDescription('Starts the daemon to run commands.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Loops through running. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
67
|
|
|
{ |
|
68
|
|
|
$cmd = "echo %s > '%s/daemon/daemon.pid'"; |
|
69
|
|
|
$command = sprintf($cmd, getmypid(), PHPCI_DIR); |
|
70
|
|
|
exec($command); |
|
71
|
|
|
|
|
72
|
|
|
$this->output = $output; |
|
73
|
|
|
$this->run = true; |
|
74
|
|
|
$this->sleep = 0; |
|
75
|
|
|
$runner = new RunCommand($this->logger); |
|
76
|
|
|
$runner->setMaxBuilds(1); |
|
77
|
|
|
$runner->setDaemon(true); |
|
78
|
|
|
|
|
79
|
|
|
$emptyInput = new ArgvInput(array()); |
|
80
|
|
|
|
|
81
|
|
|
while ($this->run) { |
|
82
|
|
|
$buildCount = 0; |
|
83
|
|
|
|
|
84
|
|
|
try { |
|
85
|
|
|
$buildCount = $runner->run($emptyInput, $output); |
|
86
|
|
|
} catch (\Exception $e) { |
|
87
|
|
|
$output->writeln('<error>Exception: '.$e->getMessage().'</error>'); |
|
88
|
|
|
$output->writeln('<error>Line: '.$e->getLine().' - File: '.$e->getFile().'</error>'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (0 == $buildCount && $this->sleep < 15) { |
|
92
|
|
|
++$this->sleep; |
|
93
|
|
|
} elseif (1 < $this->sleep) { |
|
94
|
|
|
--$this->sleep; |
|
95
|
|
|
} |
|
96
|
|
|
echo '.'.(0 === $buildCount ? '' : 'build'); |
|
97
|
|
|
sleep($this->sleep); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Called when log entries are made in Builder / the plugins. |
|
103
|
|
|
* |
|
104
|
|
|
* @see \PHPCI\Builder::log() |
|
105
|
|
|
*/ |
|
106
|
|
|
public function logCallback($log) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->output->writeln($log); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|