|
1
|
|
|
<?php namespace Comodojo\Extender\Task; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
|
4
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
|
5
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
|
6
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
|
7
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
|
8
|
|
|
use \Comodojo\Extender\Traits\TasksTableTrait; |
|
9
|
|
|
use \Comodojo\Extender\Utils\Validator as ExtenderCommonValidations; |
|
10
|
|
|
use \Psr\Log\LoggerInterface; |
|
11
|
|
|
use \Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @package Comodojo Extender |
|
15
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* LICENSE: |
|
19
|
|
|
* |
|
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
26
|
|
|
* THE SOFTWARE. |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class Manager { |
|
31
|
|
|
|
|
32
|
|
|
use ConfigurationTrait; |
|
33
|
|
|
use LoggerTrait; |
|
34
|
|
|
use EventsTrait; |
|
35
|
|
|
use TasksTableTrait; |
|
36
|
|
|
|
|
37
|
|
|
protected $lagger_timeout; |
|
38
|
|
|
|
|
39
|
|
|
protected $multithread; |
|
40
|
|
|
|
|
41
|
|
|
protected $max_runtime; |
|
42
|
|
|
|
|
43
|
|
|
protected $max_childs; |
|
44
|
|
|
|
|
45
|
|
|
protected $ipc; |
|
46
|
|
|
|
|
47
|
|
|
protected $status_table; |
|
48
|
|
|
|
|
49
|
|
|
protected $runner; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct( |
|
52
|
|
|
Configuration $configuration, |
|
53
|
|
|
LoggerInterface $logger, |
|
54
|
|
|
TasksTable $tasks, |
|
55
|
|
|
EventsManager $events, |
|
56
|
|
|
EntityManager $em = null |
|
57
|
|
|
) { |
|
58
|
|
|
|
|
59
|
|
|
$this->setConfiguration($configuration); |
|
60
|
|
|
$this->setLogger($logger); |
|
61
|
|
|
$this->setTasksTable($tasks); |
|
62
|
|
|
$this->setEvents($events); |
|
63
|
|
|
|
|
64
|
|
|
$em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em; |
|
65
|
|
|
|
|
66
|
|
|
$this->ipc = new Ipc($configuration); |
|
67
|
|
|
$this->locker = Locker::create($configuration, $logger); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// init the runner |
|
70
|
|
|
$this->runner = new Runner( |
|
71
|
|
|
$configuration, |
|
72
|
|
|
$logger, |
|
73
|
|
|
$tasks, |
|
74
|
|
|
$events, |
|
75
|
|
|
$em |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
// retrieve parameters |
|
79
|
|
|
$this->lagger_timeout = ExtenderCommonValidations::laggerTimeout($this->configuration->get('child-lagger-timeout')); |
|
80
|
|
|
$this->multithread = ExtenderCommonValidations::multithread($this->configuration->get('multithread')); |
|
81
|
|
|
$this->max_runtime = ExtenderCommonValidations::maxChildRuntime($this->configuration->get('child-max-runtime')); |
|
82
|
|
|
$this->max_childs = ExtenderCommonValidations::forkLimit($this->configuration->get('fork-limit')); |
|
83
|
|
|
|
|
84
|
|
|
$logger->debug("Tasks Manager online", array( |
|
85
|
|
|
'lagger_timeout' => $this->lagger_timeout, |
|
86
|
|
|
'multithread' => $this->multithread, |
|
87
|
|
|
'max_runtime' => $this->max_runtime, |
|
88
|
|
|
'max_childs' => $this->max_childs |
|
89
|
|
|
)); |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function __destruct() { |
|
94
|
|
|
|
|
95
|
|
|
$this->locker->release(); |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: