|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wrapper script to run a command not more than once at a time across multiple instances. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Matthias Gisder <[email protected]> |
|
6
|
|
|
* @copyright 2014 inGenerator Ltd |
|
7
|
|
|
* @licence BSD |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Ingenerator\RunSingle; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class RunSingle |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config_file = 'run_single_config.php'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Ingenerator\RunSingle\LockDriver |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $driver; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Ingenerator\RunSingle\CommandRunner $runner |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $runner; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LoggerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $logger; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var LockHolder |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $lock_holder; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param LockDriver $driver |
|
43
|
|
|
* @param CommandRunner $runner |
|
44
|
|
|
* @param LoggerInterface $logger |
|
45
|
|
|
* @param LockHolder $lock_holder |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
LockDriver $driver, |
|
49
|
|
|
CommandRunner $runner, |
|
50
|
|
|
LoggerInterface $logger, |
|
51
|
|
|
LockHolder $lock_holder |
|
52
|
|
|
) { |
|
53
|
|
|
$this->driver = $driver; |
|
54
|
|
|
$this->runner = $runner; |
|
55
|
|
|
$this->logger = $logger; |
|
56
|
|
|
$this->lock_holder = $lock_holder; |
|
57
|
|
|
// //print_r($lock_holder->get_lock_holder(), 1); |
|
58
|
|
|
// echo $lock_holder->get_lock_holder(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $task_name |
|
63
|
|
|
* @param string $command |
|
64
|
|
|
* @param string $timeout |
|
65
|
|
|
* @param string $garbage_collect |
|
66
|
|
|
* |
|
67
|
|
|
* @return integer |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public function execute($task_name, $command, $timeout, $garbage_collect) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($garbage_collect === TRUE) { |
|
72
|
|
|
$this->logger->info('garbage collecting for task '.$task_name); |
|
73
|
|
|
$this->driver->garbage_collect($task_name); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->logger->info('lock_holder id is '.$this->lock_holder->get_lock_holder()); |
|
77
|
|
|
$this->logger->info('trying to get lock for task '.$task_name); |
|
78
|
|
|
$lock_id = $this->driver->get_lock($task_name, $timeout, $this->lock_holder->get_lock_holder()); |
|
79
|
|
|
if ($lock_id !== FALSE) { |
|
80
|
|
|
$this->logger->info('executing task '.$task_name.' ...'); |
|
81
|
|
|
$this->logger->info('<command output>'); |
|
82
|
|
|
|
|
83
|
|
|
$start_time = \time(); |
|
84
|
|
|
$exit_code = $this->runner->execute($command); |
|
85
|
|
|
$end_time = \time(); |
|
86
|
|
|
|
|
87
|
|
|
$elapsed_time = $end_time - $start_time; |
|
88
|
|
|
$this->logger->info('</command output>'); |
|
89
|
|
|
$this->logger->info('finished '.$task_name.' after '.$elapsed_time.' seconds with exit code '.$exit_code); |
|
90
|
|
|
|
|
91
|
|
|
$this->driver->release_lock($task_name, $lock_id); |
|
|
|
|
|
|
92
|
|
|
return $exit_code; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->logger->info('no lock available for '.$task_name); |
|
96
|
|
|
return 0; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.