|
1
|
|
|
<?php namespace Comodojo\Extender\Schedule; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
|
4
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
|
5
|
|
|
use \Comodojo\Foundation\Logging\LoggerTrait; |
|
6
|
|
|
use \Comodojo\Foundation\Events\EventsTrait; |
|
7
|
|
|
use \Comodojo\Extender\Components\Database; |
|
8
|
|
|
use \Comodojo\Foundation\Base\ConfigurationTrait; |
|
9
|
|
|
use \Doctrine\ORM\EntityManager; |
|
10
|
|
|
use \Psr\Log\LoggerInterface; |
|
11
|
|
|
use \DateTime; |
|
12
|
|
|
use \Exception; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @package Comodojo Extender |
|
16
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
* |
|
19
|
|
|
* LICENSE: |
|
20
|
|
|
* |
|
21
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27
|
|
|
* THE SOFTWARE. |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
class Updater { |
|
31
|
|
|
|
|
32
|
|
|
use ConfigurationTrait; |
|
33
|
|
|
use LoggerTrait; |
|
34
|
|
|
use EventsTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class constructor |
|
38
|
|
|
* |
|
39
|
|
|
* @param Configuration $configuration |
|
40
|
|
|
* @param LoggerInterface $logger |
|
41
|
|
|
* @param EventsManager $events |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
Configuration $configuration, |
|
45
|
|
|
LoggerInterface $logger, |
|
46
|
|
|
EventsManager $events |
|
47
|
|
|
) { |
|
48
|
|
|
|
|
49
|
|
|
$this->setConfiguration($configuration); |
|
50
|
|
|
$this->setLogger($logger); |
|
51
|
|
|
$this->setEvents($events); |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function updateFromResults(array $results = []) { |
|
56
|
|
|
|
|
57
|
|
|
$em = Database::init($this->getConfiguration())->getEntityManager(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($results as $result) { |
|
60
|
|
|
|
|
61
|
|
|
$id = $result->jid; |
|
62
|
|
|
|
|
63
|
|
|
if ( $id === null ) continue; |
|
64
|
|
|
|
|
65
|
|
|
$job = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id); |
|
66
|
|
|
|
|
67
|
|
|
if ( $job === null ) continue; |
|
68
|
|
|
|
|
69
|
|
|
if ( $job->getFirstrun() === null ) $job->setFirstrun($result->start); |
|
70
|
|
|
|
|
71
|
|
|
$job->setLastrun($result->start); |
|
72
|
|
|
|
|
73
|
|
|
$em->persist($job); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$em->flush(); |
|
78
|
|
|
|
|
79
|
|
|
$ncts = $this->getNextCycleTimestamp($em); |
|
80
|
|
|
|
|
81
|
|
|
$em->getConnection()->close(); |
|
82
|
|
|
$em->close(); |
|
83
|
|
|
|
|
84
|
|
|
return $ncts; |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getNextCycleTimestamp(EntityManager $em) { |
|
89
|
|
|
|
|
90
|
|
|
$items = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll(); |
|
91
|
|
|
|
|
92
|
|
|
$date = new DateTime(); |
|
93
|
|
|
$timestamps = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($items as $schedule) { |
|
96
|
|
|
|
|
97
|
|
|
$timestamps[] = $schedule->getNextPlannedRun($date)->getTimestamp(); |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return empty($timestamps) ? 0 : min($timestamps); |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|