-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathultimate_cron.poorman.inc
211 lines (180 loc) · 5.13 KB
/
ultimate_cron.poorman.inc
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
<?php
/**
* @file
* Poormans cron functions.
*/
use Drupal\Core\Logger\RfcLogLevel;
/**
* Output buffer callback for poormans cron early page flush.
*
* @param string $content
* The content of the output buffer.
*
* @return string
* The content of the output buffer.
*/
function _ultimate_cron_poorman_page_flush($content) {
if (empty($GLOBALS['ultimate_cron_page_flush'])) {
return $content;
}
// Check output buffer handlers to determine if we can safely
// set the content size in the http header.
$handlers = ob_list_handlers();
// Pop ourselves off...
array_pop($handlers);
// We can safe ignore default output handlers.
$default = array_keys($handlers, 'default output handler');
foreach ($default as $key) {
unset($handlers[$key]);
}
// We can also safely ignore zlib output handlers, if we remember to
// disable compression.
if ($zlib = array_keys($handlers, 'zlib output compression')) {
foreach ($zlib as $key) {
unset($handlers[$key]);
}
ini_set('zlib.output_compression', 'Off');
}
// If there are any unknown handlers left, we cannot safely set the
// Content-Length.
if (empty($handlers)) {
$size = strlen($content);
header("Content-Length: $size");
}
// In any case, we can always tell the client to close the connection.
header("Connection: close");
return $content;
}
// Output buffering messes up Drush ...
if (ultimate_cron_poorman_capable()) {
ob_start('_ultimate_cron_poorman_page_flush');
}
///**
// * Determines if poormans cron can run on this page.
// */
//function ultimate_cron_poorman_capable() {
// static $capable;
// if (isset($capable)) {
// return $capable;
// }
//
// if (drupal_is_cli()) {
// $capable = FALSE;
// }
// elseif ($_SERVER['REQUEST_METHOD'] !== 'GET') {
// $capable = FALSE;
// }
// elseif (basename($_SERVER['PHP_SELF']) == 'cron.php') {
// $capable = FALSE;
// }
// else {
// $capable = TRUE;
// }
//
// return $capable;
//}
/**
* Implements hook_exit().
*/
function ultimate_cron_exit($dest = NULL) {
// No need for poorman until site is installed.
if (variable_get('install_task') != 'done') {
return;
}
// Don't attempt poormans stuff until fully bootstrapped.
if (drupal_bootstrap() < DRUPAL_BOOTSTRAP_FULL) {
return;
}
// We're picky about when we run poormans cron, because
// we may want to flush the output buffer.
if (!ultimate_cron_poorman_capable()) {
return;
}
$poorman = ultimate_cron_plugin_load('settings', 'poorman');
if (!$poorman) {
return;
}
$settings = $poorman->getDefaultConfiguration();
if (!$settings['launcher']) {
return;
}
$launcher = ultimate_cron_plugin_load('launcher', $settings['launcher']);
if (!$launcher) {
\Drupal::logger('ultimate_cron')->error('Invalid poormans cron launcher: @name', array(
'@name' => $settings['launcher'],
));
return;
}
// Poormans cron needs to be the last that runs.
// Run remaining exit hooks, and shutdown like core does.
$modules = module_implements('exit');
do {
$module = array_shift($modules);
} while ($modules && ($module !== 'ultimate_cron'));
foreach ($modules as $module) {
module_invoke($module, 'exit', $dest);
}
// Commit the user session, if needed.
drupal_session_commit();
if (variable_get('cache', 0) && ($cache = drupal_page_set_cache())) {
drupal_serve_page_from_cache($cache);
}
// ultimate_cron_poorman_page_flush();
_registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
drupal_cache_system_paths();
module_implements_write_cache();
// Poormans cron should run as anonymous, just like regular cron does.
global $user;
$original_user = $user;
$user = drupal_anonymous_user();
$launcher->launchPoorman();
$user = $original_user;
exit;
}
/**
* Flush the page and end the client request.
*/
function ultimate_cron_poorman_page_flush() {
@ignore_user_abort();
$GLOBALS['ultimate_cron_page_flush'] = TRUE;
while (ob_get_level() > 0) {
ob_end_flush();
}
flush();
// FastCGI may need another way of letting the client know, that
// we're done with it.
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
}
/**
* Page callback for triggering poormans cron.
*/
function ultimate_cron_poorman_page() {
drupal_page_is_cacheable(FALSE);
drupal_exit();
}
/**
* Send the actual HTTP request for launching threads.
*/
function ultimate_cron_poorman_trigger() {
$url_options = variable_get('ultimate_cron_poorman_url_options', array());
$http_options = variable_get('ultimate_cron_poorman_http_options', array());
$url_options = array(
'query' => array('timestamp' => REQUEST_TIME),
'absolute' => TRUE,
) + $url_options;
$http_options = array(
'headers' => array(
'User-Agent' => 'Ultimate Cron',
),
) + $http_options;
$url = url('admin/config/system/cron/poorman', $url_options);
$response = drupal_http_request($url, $http_options);
if (!empty($response->error)) {
watchdog('ultimate_cron', 'Could not trigger poorman at @url. Error: @error', array(
'@url' => $url,
'@error' => $response->error,
), RfcLogLevel::ERROR);
}
}