|
1
|
|
|
<?php namespace Comodojo\Extender; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Extender\Base\Daemon; |
|
4
|
|
|
use \Comodojo\Extender\Components\DefaultConfiguration; |
|
5
|
|
|
use \Comodojo\Extender\Components\Version; |
|
6
|
|
|
use \Comodojo\Extender\Console\Arguments as ConsoleArgumentsTrait; |
|
7
|
|
|
use \Comodojo\Extender\Console\LogHandler; |
|
8
|
|
|
use \Comodojo\Extender\Jobs\Results; |
|
9
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
|
10
|
|
|
use \Comodojo\Dispatcher\Components\LogManager; |
|
11
|
|
|
use \Comodojo\Dispatcher\Components\EventsManager; |
|
12
|
|
|
use \Comodojo\Dispatcher\Components\CacheManager; |
|
13
|
|
|
use \Comodojo\Cache\Cache; |
|
14
|
|
|
use \League\CLImate\CLImate; |
|
15
|
|
|
use \Psr\Log\LoggerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Extender extends Daemon { |
|
18
|
|
|
|
|
19
|
|
|
use ConsoleArgumentsTrait; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
$configuration = array(), |
|
23
|
|
|
LoggerInterface $logger = null, |
|
24
|
|
|
EventsManager $events = null, |
|
25
|
|
|
Cache $cache = null |
|
26
|
|
|
) { |
|
27
|
|
|
|
|
28
|
|
|
// parsing configuration |
|
29
|
|
|
$this->configuration = new Configuration( DefaultConfiguration::get() ); |
|
|
|
|
|
|
30
|
|
|
$this->configuration->merge($configuration); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
// fix the daemon start time |
|
33
|
|
|
$this->starttime = microtime(true); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
// init counters |
|
36
|
|
|
$this->completedjobs = 0; |
|
|
|
|
|
|
37
|
|
|
$this->failedjobs = 0; |
|
|
|
|
|
|
38
|
|
|
$this->currentjobs = new Results(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// init the console |
|
41
|
|
|
$this->console = new CLImate(); |
|
|
|
|
|
|
42
|
|
|
$this->console->description(Version::getFullDescription($this->configuration)); |
|
|
|
|
|
|
43
|
|
|
$this->console->arguments->add($this->console_arguments); |
|
|
|
|
|
|
44
|
|
|
$this->console->arguments->parse(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
// init the logger |
|
47
|
|
|
$this->logger = is_null($logger) ? LogManager::create($this->configuration, 'extender') : $logger; |
|
|
|
|
|
|
48
|
|
|
if ( $this->console->arguments->get('verbose') === true ) { |
|
|
|
|
|
|
49
|
|
|
$this->logger->pushHandler(new LogHandler()); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// init event manager |
|
53
|
|
|
$this->events = is_null($events) ? new EventsManager($this->logger) : $events; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
// init the cache abstraction layer |
|
56
|
|
|
$this->cache = is_null($cache) ? CacheManager::create($this->configuration, $this->logger) : $cache; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
// install the loop limiter |
|
59
|
|
|
$this->looplimit = $this->console->arguments->get('iterations'); |
|
|
|
|
|
|
60
|
|
|
$this->events->subscribe("extender.daemon.loopstop", "\Comodojo\Extender\Listeners\LoopLimit"); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
// install the summary report listener if requested |
|
63
|
|
|
if ( $this->console->arguments->get('summary') === true ) { |
|
|
|
|
|
|
64
|
|
|
$this->events->subscribe("extender.daemon.loopstop", "\Comodojo\Extender\Listeners\LoopSummary"); |
|
|
|
|
|
|
65
|
|
|
$this->events->subscribe("extender.daemon.stop", "\Comodojo\Extender\Listeners\StopSummary"); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// get daemon parameters |
|
69
|
|
|
$looptime = $this->configuration->get('looptime'); |
|
|
|
|
|
|
70
|
|
|
$niceness = $this->configuration->get('niceness'); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
// build daemon |
|
73
|
|
|
parent::__construct($this->configuration, $this->logger, $this->events, $looptime, $niceness); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function loop() { |
|
78
|
|
|
|
|
79
|
|
|
// fix the relative timestamp |
|
80
|
|
|
$this->setTimestamp(); |
|
81
|
|
|
|
|
82
|
|
|
// check plans (if any) and return if no plans for this loop |
|
83
|
|
|
|
|
84
|
|
|
// if no plans, retrieve schedule and build plan cache |
|
85
|
|
|
|
|
86
|
|
|
// load jobs in runner (if any) |
|
87
|
|
|
|
|
88
|
|
|
// go runner, go! |
|
89
|
|
|
|
|
90
|
|
|
// collect job result |
|
91
|
|
|
|
|
92
|
|
|
// update counters |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function extend() { |
|
97
|
|
|
|
|
98
|
|
|
if ( $this->console->arguments->get('help') === true ) { |
|
|
|
|
|
|
99
|
|
|
// show help and exit |
|
100
|
|
|
$this->console->usage(); |
|
|
|
|
|
|
101
|
|
|
$this->end(0); |
|
102
|
|
|
} else if ( $this->console->arguments->get('daemon') === true ) { |
|
|
|
|
|
|
103
|
|
|
// run extender as a deamon |
|
104
|
|
|
$this->daemonize(); |
|
105
|
|
|
} else { |
|
106
|
|
|
// run extender as a normal foreground process |
|
107
|
|
|
$this->start(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.