|
156 | 156 | sleep(1); |
157 | 157 | } |
158 | 158 | } |
159 | | - |
160 | | -// lock is acquired right before running task, and released right after. |
161 | | -/*$lock = new \OBFLock('core-cron'); |
162 | | -
|
163 | | -// require cron files |
164 | | -$jobs = []; |
165 | | -require_once('classes/base/cron.php'); |
166 | | -foreach (glob('classes/cron/*.php') as $file) { |
167 | | - require_once($file); |
168 | | - $class = '\\OB\\Classes\Cron\\' . basename($file, '.php'); |
169 | | - $jobs[] = new $class(); |
170 | | -} |
171 | | -
|
172 | | -foreach (glob('modules/*', GLOB_ONLYDIR) as $module) { |
173 | | - // Only run cron jobs for installed modules. |
174 | | - $moduleDir = basename($module); |
175 | | - $db->where('directory', $moduleDir); |
176 | | - $moduleInstalled = $db->get_one('modules'); |
177 | | - if (!$moduleInstalled) { |
178 | | - continue; |
179 | | - } |
180 | | -
|
181 | | - // Require module cron files and add to jobs array. |
182 | | - foreach (glob($module . '/cron/*.php') as $file) { |
183 | | - $moduleNamespace = str_replace(' ', '', ucwords(str_replace('_', ' ', basename($module)))); |
184 | | - require_once($file); |
185 | | - $class = '\\OB\\Modules\\' . $moduleNamespace . '\\Cron\\' . basename($file, '.php'); |
186 | | - $jobs[] = new $class(); |
187 | | - } |
188 | | -} |
189 | | -
|
190 | | -// check our jobs to see what needs to be run |
191 | | -$jobs_to_run = []; |
192 | | -foreach ($jobs as $index => $job) { |
193 | | - // reliable way to get the class name without namespace |
194 | | - $classReflection = new \ReflectionClass($job); |
195 | | - $className = strtolower($classReflection->getShortName()); |
196 | | -
|
197 | | - // check if module or core, if module, save module name |
198 | | - $moduleName = 'core'; |
199 | | - $namespaceName = $classReflection->getNamespaceName(); |
200 | | - if (strpos($namespaceName, 'OB\\Modules\\') === 0) { |
201 | | - $matches = []; |
202 | | - preg_match("/\/modules\/(.*)\/cron\//", $classReflection->getFileName(), $matches); |
203 | | - $moduleName = $matches[1]; |
204 | | - } |
205 | | -
|
206 | | - // get our last run time |
207 | | - $db->where('name', 'cron-' . $moduleName . '-' . $className); |
208 | | - $lastRun = $db->get_one('settings'); |
209 | | -
|
210 | | - // if no last run time, set nextRun to zero |
211 | | - if (!$lastRun) { |
212 | | - $nextRun = 0; |
213 | | - } else { |
214 | | - $nextRun = $lastRun['value'] + $job->interval(); |
215 | | - } |
216 | | - $jobs_to_run[$index] = [ |
217 | | - 'nextRun' => $nextRun, |
218 | | - 'className' => $className, |
219 | | - 'moduleName' => $moduleName |
220 | | - ]; |
221 | | -} |
222 | | -
|
223 | | -$run_jobs = function () use ($jobs, &$jobs_to_run, $db, $lock) { |
224 | | - // loop through jobs and check interval against last run. run if needed. |
225 | | - foreach ($jobs_to_run as $index => $job_to_run) { |
226 | | - // if not time to run, skip |
227 | | - if ($job_to_run['nextRun'] > time()) { |
228 | | - continue; |
229 | | - } |
230 | | -
|
231 | | - // aquire lock (or exit) |
232 | | - $lock_aquired = $lock->acquire(); |
233 | | - if (!$lock_aquired) { |
234 | | - echo 'Unable to get cron lock. Is another monitor or job already running?' . PHP_EOL; |
235 | | - exit(1); |
236 | | - } |
237 | | -
|
238 | | - // get the job |
239 | | - $job = $jobs[$index]; |
240 | | -
|
241 | | - // run job |
242 | | - $status = $job->run(); |
243 | | -
|
244 | | - // if successful, update last run time for this job and main, as well as update our next run time locally |
245 | | - if ($status) { |
246 | | - // update last run time for this job |
247 | | - if ($job_to_run['nextRun'] > 0) { |
248 | | - // has previous run, update time. |
249 | | - $db->where('name', 'cron-' . $job_to_run['moduleName'] . '-' . $job_to_run['className']); |
250 | | - $db->update('settings', ['value' => time()]); |
251 | | - } else { |
252 | | - // first time running, insert time. |
253 | | - $db->insert('settings', ['name' => 'cron-' . $job_to_run['moduleName'] . '-' . $job_to_run['className'], 'value' => time()]); |
254 | | - } |
255 | | -
|
256 | | - // update next run time in our local variable |
257 | | - $jobs_to_run[$index]['nextRun'] = time() + $job->interval(); |
258 | | -
|
259 | | - // update our main cron_last_run time |
260 | | - $db->where('name', 'cron_last_run'); |
261 | | - $cron_last_run = $db->get_one('settings'); |
262 | | - if (!$cron_last_run) { |
263 | | - $db->insert('settings', ['name' => 'cron_last_run', 'value' => time()]); |
264 | | - } else { |
265 | | - $db->where('name', 'cron_last_run'); |
266 | | - $db->update('settings', ['value' => time()]); |
267 | | - } |
268 | | -
|
269 | | - // update $jobs_to_run with next time |
270 | | - $jobs_to_run[$index]['nextRun'] = time() + $job->interval(); |
271 | | - } |
272 | | -
|
273 | | - // release our lock |
274 | | - $lock->release(); |
275 | | - } |
276 | | -}; |
277 | | -
|
278 | | -if ($subcommand === 'monitor') { |
279 | | - // monitor mode |
280 | | - echo 'cron monitor started' . PHP_EOL; |
281 | | - while (true) { |
282 | | - $run_jobs(); |
283 | | - sleep(1); |
284 | | - } |
285 | | -} else { |
286 | | - $run_jobs(); |
287 | | -} |
288 | | -*/ |
0 commit comments