|
108 | 108 | $jobs = []; |
109 | 109 |
|
110 | 110 | foreach (glob('classes/cron/*.php') as $file) { |
| 111 | + require_once($file); |
| 112 | + $class = '\\OB\\Classes\Cron\\' . basename($file, '.php'); |
| 113 | + $instance = new $class(); |
| 114 | + |
111 | 115 | $jobs[] = [ |
112 | | - 'module' => 'core', |
113 | | - 'name' => basename($file, '.php'), |
| 116 | + 'module' => 'core', |
| 117 | + 'name' => basename($file, '.php'), |
| 118 | + 'interval' => $instance->interval(), |
114 | 119 | ]; |
115 | 120 | } |
116 | 121 |
|
117 | 122 | foreach (glob('modules/*', GLOB_ONLYDIR) as $module) { |
118 | 123 | foreach (glob($module . '/cron/*.php') as $file) { |
| 124 | + require_once($file); |
| 125 | + $moduleNamespace = str_replace(' ', '', ucwords(str_replace('_', ' ', basename($module)))); |
| 126 | + $class = '\\OB\\Modules\\' . $moduleNamespace . '\\Cron\\' . basename($file, '.php'); |
| 127 | + $instance = new $class(); |
| 128 | + |
119 | 129 | $jobs[] = [ |
120 | | - 'module' => basename($module), |
121 | | - 'name' => basename($file, '.php'), |
| 130 | + 'module' => basename($module), |
| 131 | + 'name' => basename($file, '.php'), |
| 132 | + 'interval' => $instance->interval(), |
122 | 133 | ]; |
123 | 134 | } |
124 | 135 | } |
|
129 | 140 | exec($argv[0] . ' cron run ' . $job['module'] . ' ' . $job['name'] . ' >> ' . OB_CRON_LOG . ' &'); |
130 | 141 | } |
131 | 142 | } elseif ($subcommand === 'monitor') { |
132 | | - // TODO |
| 143 | + while (true) { |
| 144 | + foreach ($jobs as $job) { |
| 145 | + $db->where('name', 'cron-' . $job['module'] . '-' . $job['name']); |
| 146 | + $lastRun = $db->get_one('settings'); |
| 147 | + |
| 148 | + if ($lastRun && $lastRun['value'] + $job['interval'] > time()) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + echo "Running job '{$job['module']}/{$job['name']}'..." . PHP_EOL; |
| 153 | + exec($argv[0] . ' cron run ' . $job['module'] . ' ' . $job['name'] . ' >> ' . OB_CRON_LOG . ' &'); |
| 154 | + } |
| 155 | + |
| 156 | + sleep(1); |
| 157 | + } |
133 | 158 | } |
134 | 159 |
|
135 | 160 | // lock is acquired right before running task, and released right after. |
|
0 commit comments