-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathultimate_cron.module
executable file
·323 lines (295 loc) · 9.42 KB
/
ultimate_cron.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* @file
* Ultimate Cron. Extend cron functionality in Drupal.
*/
use Drupal\Core\Database\Database;
use Drupal\ultimate_cron\CronPlugin;
use Drupal\ultimate_cron\Logger\LoggerBase;
use Drupal\ultimate_cron\PluginCleanupInterface;
define('ULTIMATE_CRON_LOG_TYPE_NORMAL', 0);
define('ULTIMATE_CRON_LOG_TYPE_ADMIN', 1);
define('ULTIMATE_CRON_LOG_TYPE_ALL', -1);
/**
* Pseudo define.
*/
function _ultimate_cron_define_log_type_all() {
return array(ULTIMATE_CRON_LOG_TYPE_NORMAL, ULTIMATE_CRON_LOG_TYPE_ADMIN);
}
$GLOBALS['ultimate_cron_shutdown_functions'] = array();
/**
* The shutdown function itself is also overridable.
*
* In case it is necessary to add it earlier, say settings.php.
* Remeber to invoke the registered ultimate cron shutdown handlers.
* If the function exists, we assume that the register_shutdown_handler() has
* also been setup correctly.
*
* @todo: Move _ultimate_cron_out_of_memory_protection() to a service.
*/
if (!function_exists('_ultimate_cron_out_of_memory_protection')) {
/**
* Shutdown hander that unleash the memory reserved.
*/
function _ultimate_cron_out_of_memory_protection() {
// error_log("RELEASING MEMORY");
unset($GLOBALS['__RESERVED_MEMORY']);
// error_log(print_r($GLOBALS['ultimate_cron_shutdown_functions'], TRUE));
foreach ($GLOBALS['ultimate_cron_shutdown_functions'] as $function) {
call_user_func_array($function['callback'], $function['arguments']);
}
}
// The minor overhead in _drupal_shutdown_function() can mean the
// difference between life and death for our shutdown handlers in
// a memory exhaust situation. We want our shutdown handler to be
// called as early as possible. If no callbacks have been registrered
// yet, we use PHPs built-in register_shutdown_function() otherwise
// we ensure, that we are the first in the list of Drupals registered
// shutdown functions.
$callbacks = &drupal_register_shutdown_function();
if (empty($callbacks)) {
register_shutdown_function('_ultimate_cron_out_of_memory_protection');
}
else {
array_unshift($callbacks, array('callback' => '_ultimate_cron_out_of_memory_protection', 'arguments' => array()));
// Reset internal array pointer just in case ...
reset($callbacks);
}
}
/**
* Registers a function for execution on shutdown.
*
* Wrapper for register_shutdown_function() that catches thrown exceptions to
* avoid "Exception thrown without a stack frame in Unknown".
*
* This is a duplicate of the built-in functionality in Drupal, however we
* need to perform our tasks before that.
*
* @param callback $callback
* The shutdown function to register.
* @param ...
* Additional arguments to pass to the shutdown function.
*
* @see register_shutdown_function()
*
* @ingroup php_wrappers
*/
function ultimate_cron_register_shutdown_function($callback) {
$args = func_get_args();
array_shift($args);
$GLOBALS['ultimate_cron_shutdown_functions'][] = array(
'callback' => $callback,
'arguments' => $args,
);
}
/**
* Load callback for plugins.
*
* @param string $type
* Type of the plugin (settings, scheduler, launcher, logger).
* @param string $name
* Name of the plugin (general, queue, serial, database, etc.).
*
* @return object
* The instance of the plugin (singleton).
*/
function ultimate_cron_plugin_load($type, $name) {
$cache = &drupal_static('ultimate_cron_plugin_load_all', array());
if (!isset($cache[$type][$name])) {
ultimate_cron_plugin_load_all($type);
$cache[$type][$name] = isset($cache[$type][$name]) ? $cache[$type][$name] : FALSE;
}
return $cache[$type][$name];
}
function ultimate_cron_fake_cron() {
$counter = \Drupal::state()->get('ultimate_cron.cron_run_counter', 0);
$counter++;
\Drupal::state()->set('ultimate_cron.cron_run_counter', $counter);
}
/**
* Load all callback for plugins.
*
* @param string $type
* Type of the plugin (settings, scheduler, launcher, logger).
*
* @return array
* The instances of the plugin type (singletons).
*/
function ultimate_cron_plugin_load_all($type, $reset = FALSE) {
$cache = &drupal_static('ultimate_cron_plugin_load_all', array());
if (!$reset && isset($cache[$type])) {
return $cache[$type];
}
/* @var \Drupal\Core\Plugin\DefaultPluginManager $manager */
$manager = \Drupal::service('plugin.manager.ultimate_cron.' . $type);
$plugins = $manager->getDefinitions();
foreach ($plugins as $plugin_id => $definition) {
if ($object = $manager->createInstance($plugin_id)) {
$plugins[$plugin_id] = $object;
}
}
$cache[$type] = $plugins;
return $cache[$type];
}
// ---------- HOOKS ----------
/**
* Implements hook_hook_info().
*/
function ultimate_cron_hook_info() {
$hooks = array();
$hooks['background_process_shutdown'] = array(
'group' => 'background_process_legacy',
);
return $hooks;
}
/**
* Implements hook_init().
*
* Make sure we have the proper "last run" of cron in global $conf
* for maximum compatibility with core.
*
* @todo: Port this.
*/
function ultimate_cron_init() {
if (!variable_get('ultimate_cron_bypass_transactional_safe_connection')) {
$info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'ultimate_cron', $info['default']);
}
$GLOBALS['ultimate_cron_original_session_saving'] = drupal_save_session();
$GLOBALS['ultimate_cron_original_user'] = $GLOBALS['user'];
_ultimate_cron_variable_load('cron_last');
}
/**
* Implements hook_help().
*
*/
function ultimate_cron_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ultimate_cron':
// Return a line-break version of the module README.
return '<pre>' . file_get_contents(dirname(__FILE__) . '/README.txt') . '</pre>';
}
}
/**
* Implements hook_cronapi().
*
* Adds clean up jobs for plugins.
* */
function ultimate_cron_cron() {
$items = array();
$plugin_types = CronPlugin::getPluginTypes();
foreach ($plugin_types as $plugin_type => $info) {
foreach (ultimate_cron_plugin_load_all($plugin_type) as $name => $plugin) {
if ($plugin->isValid() && ($plugin instanceof PluginCleanupInterface)) {
$plugin->cleanup();
}
}
}
return $items;
}
/**
* Implements hook_watchdog().
*
* Capture watchdog messages and send them to the loggers.
*
* @todo: port this.
*/
function ultimate_cron_watchdog(array $log_entry) {
if (class_exists('Drupal\ultimate_cron\Logger\LoggerBase')) {
LoggerBase::hook_watchdog($log_entry);
}
}
/**
* * Implements hook_modules_installed().
*/
function ultimate_cron_modules_installed($modules) {
\Drupal::service('ultimate_cron.discovery')->discoverCronJobs();
}
// ---------- CRON RULE FUNCTIONS ----------
/**
* Return blank values for all keys in an array.
*
* @param array $array
* Array to generate blank values from.
*
* @return array
* Array with same keys as input, but with blank values (empty string).
*/
function ultimate_cron_blank_values($array) {
$result = array();
foreach ($array as $key => $value) {
switch (gettype($value)) {
case 'array':
$result[$key] = array();
break;
default:
$result[$key] = '';
}
}
return $result;
}
/**
* Log message either to watchdog or to screen.
*
* @param string $type
* The category to which this message belongs. Can be any string, but the
* general practice is to use the name of the module calling watchdog().
* @param string $message
* The message to store in the log. Keep $message translatable
* by not concatenating dynamic values into it! Variables in the
* message should be added by using placeholder strings alongside
* the variables argument to declare the value of the placeholders.
* See t() for documentation on how $message and $variables interact.
* @param array $variables
* Array of variables to replace in the message on display or
* NULL if message is already translated or not possible to
* translate.
* @param integer $severity
* The severity of the message; one of the following values as defined in
* \Drupal\Core\Logger\RfcLogLevel.
* @param string $status
* The message's type.
* supported:
* - 'status'
* - 'warning'
* - 'error'
* @param boolean $set_message
* Use drupal_set_message() instead of watchdog logging.
*/
function ultimate_cron_watchdog_message($type, $message, $variables, $severity, $status, $set_message) {
if ($set_message) {
drupal_set_message(t($message, $variables), $status);
}
else {
\Drupal::logger($type)->log($severity,$message, $variables);
}
}
/**
* Custom sort callback for sorting cron jobs by start time.
*/
function _ultimate_cron_sort_jobs_by_start_time($a, $b) {
return $a->log_entry->start_time == $b->log_entry->start_time ? 0 : ($a->log_entry->start_time > $b->log_entry->start_time ? 1 : -1);
}
/**
* Sort callback for multiple column sort.
*/
function _ultimate_cron_multi_column_sort($a, $b) {
$a = (array) $a;
$b = (array) $b;
foreach ($a['sort'] as $i => $sort) {
if ($a['sort'][$i] == $b['sort'][$i]) {
continue;
}
return $a['sort'][$i] < $b['sort'][$i] ? -1 : 1;
}
return 0;
}
/**
* Get transactional safe connection.
*
* @return string
* Connection target.
*/
function _ultimate_cron_get_transactional_safe_connection() {
return !\Drupal::config('ultimate_cron.settings')->get('bypass_transactional_safe_connection') && \Drupal\Core\Database\Database::getConnection()->inTransaction() ? 'ultimate_cron' : 'default';
}