Skip to content

Commit fe12817

Browse files
committed
cli command for individual cron run
1 parent 19045c8 commit fe12817

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

tools/cli/commands/cron.php

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ob\tools\cli;
44

5+
global $argv;
6+
57
if (!defined('OB_CLI')) {
68
die('Command line access only.');
79
}
@@ -10,7 +12,72 @@
1012

1113
$db = \OBFDB::get_instance();
1214

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.
1481
$lock = new \OBFLock('core-cron');
1582

1683
// require cron files
@@ -51,7 +118,9 @@
51118
$moduleName = 'core';
52119
$namespaceName = $classReflection->getNamespaceName();
53120
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];
55124
}
56125

57126
// get our last run time

tools/cli/ob.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function help()
6565
echo Helpers::table(spacing: 5, rows: [
6666
['check', 'check installation for errors'],
6767
['cron run', 'run scheduled tasks once'],
68+
['cron run <module> <task> [now]', 'run scheduled task for module'],
6869
['cron monitor', 'monitor and run cron tasks as needed'],
6970
['updates list all', 'list all available updates'],
7071
['updates list core', 'list core ob updates'],

0 commit comments

Comments
 (0)