PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / crontab / simulator.php

simulator.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/crontab/simulator.php

161 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Cron scheduler and execution simulator.
16 *
17 * @since 1.7
18 */
19 final class VBOCrontabSimulator implements IteratorAggregate, Countable
20 {
21 /** @var VBOCrontabRunner[] */
22 private $schedules = [];
23
24 /** @var VBOCrontabSemaphore */
25 private $semaphore;
26
27 /** @var VBOCrontabLogger */
28 private $logger;
29
30 /**
31 * Whether crontab should prevent the execution of the runners.
32 *
33 * @var bool
34 */
35 private $stopped = false;
36
37 /**
38 * Class contructor.
39 *
40 * @param VBOCrontabSemaphore $semaphore The instance that will be used to control the execution of the crons.
41 * @param VBOCrontabLogger $logger The instance that will be used to log details about the execution of the crons.
42 */
43 public function __construct(VBOCrontabSemaphore $semaphore, VBOCrontabLogger $logger)
44 {
45 $this->semaphore = $semaphore;
46 $this->logger = $logger;
47 }
48
49 /**
50 * Schedules a cron for running at a precise interval.
51 *
52 * @param VBOCrontabRunner $cron The cron to schedule.
53 *
54 * @return self
55 */
56 public function schedule(VBOCrontabRunner $cron)
57 {
58 // queue execution
59 $this->schedules[] = $cron;
60
61 return $this;
62 }
63
64 /**
65 * Prevent the execution of the registered runners.
66 *
67 * @return self
68 */
69 public function stop()
70 {
71 $this->stopped = true;
72
73 return $this;
74 }
75
76 /**
77 * Executes the scheduled crons if they are due to execution.
78 *
79 * @return void
80 */
81 public function run()
82 {
83 if ($this->stopped) {
84 // execution prevented
85 return;
86 }
87
88 // prevent duplicate runs under the same session
89 $this->stop();
90
91 // iterate all the registered schedules
92 foreach ($this->schedules as $cron) {
93 // check whether the cron can actually run
94 if (!$cron->canRun()) {
95 // nope, go ahead
96 continue;
97 }
98
99 // check whether this cron should pass
100 if (!$this->semaphore->shouldPass($cron)) {
101 // cron still locked, go ahead
102 continue;
103 }
104
105 try {
106 // track the execution of the cron
107 $this->logger->log(sprintf('Executing the cron [%s]...', $cron->getID()));
108 // execute the cron job
109 $cron->execute($this->logger);
110 // execution terminated successfully
111 $this->logger->log('Cron terminated successfully.');
112 } catch (Throwable $error) {
113 // log the error faced
114 $this->logger->log($error->getMessage(), 'error');
115 }
116 }
117 }
118
119 /**
120 * Returns the registered semaphore instance.
121 *
122 * @return VBOCrontabSemaphore
123 */
124 public function getSemaphore()
125 {
126 return $this->semaphore;
127 }
128
129 /**
130 * Returns the registered logger instance.
131 *
132 * @return VBOCrontabLogger
133 */
134 public function getLogger()
135 {
136 return $this->logger;
137 }
138
139 /**
140 * Exposes the registered schedules to an iterator.
141 *
142 * @see IteratorAggregate
143 */
144 #[ReturnTypeWillChange]
145 public function getIterator()
146 {
147 return new ArrayIterator($this->schedules);
148 }
149
150 /**
151 * Counts the number of registered schedules.
152 *
153 * @see Countable
154 */
155 #[ReturnTypeWillChange]
156 public function count()
157 {
158 return count($this->schedules);
159 }
160 }
161