|
2 | 2 |
|
3 | 3 | namespace ob\tools\cli; |
4 | 4 |
|
| 5 | +global $argv; |
| 6 | + |
5 | 7 | if (!defined('OB_CLI')) { |
6 | 8 | die('Command line access only.'); |
7 | 9 | } |
|
10 | 12 |
|
11 | 13 | $db = \OBFDB::get_instance(); |
12 | 14 |
|
13 | | -// lock is aquired right before running task, and released right after. |
| 15 | +// Check if a specific cron job was specified, and if the 'now' flag is set. |
| 16 | +if (isset($argv[3]) && isset($argv[4])) { |
| 17 | + $module = $argv[3]; |
| 18 | + $task = $argv[4]; |
| 19 | + $forceRun = ($argv[5] ?? '') === 'now'; |
| 20 | + |
| 21 | + // Get cron job class instance. |
| 22 | + require_once('classes/base/cron.php'); |
| 23 | + if ($module === 'core') { |
| 24 | + if (! file_exists('classes/cron/' . $task . '.php')) { |
| 25 | + echo 'Task not found.' . PHP_EOL; |
| 26 | + exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + require_once('classes/cron/' . $task . '.php'); |
| 30 | + $class = '\\OB\\Classes\\Cron\\' . $task; |
| 31 | + } else { |
| 32 | + if (! file_exists('modules/' . $module . '/cron/' . $task . '.php')) { |
| 33 | + echo 'Task not found.' . PHP_EOL; |
| 34 | + exit(1); |
| 35 | + } |
| 36 | + |
| 37 | + require_once('modules/' . $module . '/cron/' . $task . '.php'); |
| 38 | + $moduleNamespace = str_replace(' ', '', ucwords(str_replace('_', ' ', $module))); |
| 39 | + $class = '\\OB\\Modules\\' . $moduleNamespace . '\\Cron\\' . $task; |
| 40 | + } |
| 41 | + |
| 42 | + $job = new $class(); |
| 43 | + |
| 44 | + // Check when last run if "now" isn't specified, and display an error if it's too |
| 45 | + // soon. |
| 46 | + $db->where('name', 'cron-' . $module . '-' . $task); |
| 47 | + $lastRun = $db->get_one('settings'); |
| 48 | + |
| 49 | + if (! $forceRun && $lastRun && $lastRun['value'] + $job->interval() > time()) { |
| 50 | + echo 'Task was run too recently. Use "now" to force run.' . PHP_EOL; |
| 51 | + exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + // Run job. |
| 55 | + echo "Running job..." . PHP_EOL; |
| 56 | + $status = $job->run(); |
| 57 | + |
| 58 | + if ($status) { |
| 59 | + if ($lastRun) { |
| 60 | + $db->where('name', 'cron-' . $module . '-' . $task); |
| 61 | + $db->update('settings', [ |
| 62 | + 'value' => time() |
| 63 | + ]); |
| 64 | + } else { |
| 65 | + $db->insert('settings', [ |
| 66 | + 'name' => 'cron-' . $module . '-' . $task, 'value' => time() |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + echo "Job ran successfully." . PHP_EOL; |
| 71 | + } else { |
| 72 | + echo "Failed to run job. Check individual cron job messages for more detailed information." . PHP_EOL; |
| 73 | + } |
| 74 | + |
| 75 | + exit(); |
| 76 | +} |
| 77 | + |
| 78 | +// NOTE: Code below is for running cron jobs in monitor mode or running all jobs once. |
| 79 | + |
| 80 | +// lock is acquired right before running task, and released right after. |
14 | 81 | $lock = new \OBFLock('core-cron'); |
15 | 82 |
|
16 | 83 | // require cron files |
|
51 | 118 | $moduleName = 'core'; |
52 | 119 | $namespaceName = $classReflection->getNamespaceName(); |
53 | 120 | if (strpos($namespaceName, 'OB\\Modules\\') === 0) { |
54 | | - $moduleName = strtolower(str_replace('\\Cron', '', str_replace('OB\\Modules\\', '', $namespaceName))); |
| 121 | + $matches = []; |
| 122 | + preg_match("/\/modules\/(.*)\/cron\//", $classReflection->getFileName(), $matches); |
| 123 | + $moduleName = $matches[1]; |
55 | 124 | } |
56 | 125 |
|
57 | 126 | // get our last run time |
|
0 commit comments