forked from ezsystems/ezpublish-legacy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
runcronjobs.php
executable file
·401 lines (365 loc) · 12.6 KB
/
runcronjobs.php
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env php
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package kernel
*/
/* No more than one instance of a cronjob script can be run at any given time.
If a script uses more time than the configured MaxScriptExecutionTime (see
cronjob.ini), the next instance of it will try to gracefully steal the
cronjob script mutex. If the process has been running for more than two
times MaxScriptExecutionTime, the original process will be killed.
*/
/* Set a default time zone if none is given. The time zone can be overridden
in config.php or php.ini.
*/
if ( !ini_get( "date.timezone" ) )
{
date_default_timezone_set( "UTC" );
}
require_once 'autoload.php';
require_once( 'kernel/common/i18n.php' );
eZContentLanguage::setCronjobMode();
$cli = eZCLI::instance();
$script = eZScript::instance( array( 'debug-message' => '',
'use-session' => true,
'use-modules' => true,
'use-extensions' => true ) );
$script->startup();
$webOutput = $cli->isWebOutput();
function help()
{
$argv = $_SERVER['argv'];
$cli = eZCLI::instance();
$cli->output( "Usage: " . $argv[0] . " [OPTION]... [PART]\n" .
"Executes eZ Publish cronjobs.\n" .
"\n" .
"General options:\n" .
" -h,--help display this help and exit \n" .
" -q,--quiet do not give any output except when errors occur\n" .
" -s,--siteaccess selected siteaccess for operations, if not specified default siteaccess is used\n" .
" -d,--debug display debug output at end of execution\n" .
" -c,--colors display output using ANSI colors\n" .
" --sql display sql queries (must be used in conjunction with debug option)\n" .
" --logfiles create log files\n" .
" --no-logfiles do not create log files (default)\n" .
" --list list all cronjobs parts and the scripts contained by each one\n" .
" --no-colors do not use ANSI coloring (default)\n" );
}
function changeSiteAccessSetting( &$siteaccess, $optionData )
{
$cli = eZCLI::instance();
if ( file_exists( 'settings/siteaccess/' . $optionData ) )
{
$siteaccess = $optionData;
return "Using siteaccess $siteaccess for cronjob";
}
elseif ( isExtensionSiteaccess( $optionData ) )
{
$siteaccess = $optionData;
eZExtension::prependExtensionSiteAccesses( $siteaccess );
return "Using extension siteaccess $siteaccess for cronjob";
}
else
{
return "Siteaccess $optionData does not exist, using default siteaccess";
}
}
/*
Look in the ActiveExtensions for $siteaccessName
We only need to look in ActiveExtensions and not ActiveAccessExtensions
Return true if siteaccessName exists in an extension, false if not.
*/
function isExtensionSiteaccess( $siteaccessName )
{
$ini = eZINI::instance();
$extensionDirectory = $ini->variable( 'ExtensionSettings', 'ExtensionDirectory' );
$activeExtensions = $ini->variable( 'ExtensionSettings', 'ActiveExtensions' );
foreach ( $activeExtensions as $extensionName )
{
$possibleExtensionPath = $extensionDirectory . '/' . $extensionName . '/settings/siteaccess/' . $siteaccessName;
if ( file_exists( $possibleExtensionPath ) )
return true;
}
return false;
}
$siteaccess = false;
$debugOutput = false;
$allowedDebugLevels = false;
$useDebugAccumulators = false;
$useDebugTimingpoints = false;
$useIncludeFiles = false;
$useColors = false;
$isQuiet = false;
$useLogFiles = false;
$showSQL = false;
$cronPart = false;
$listCronjobs = false;
$optionsWithData = array( 's' );
$longOptionsWithData = array( 'siteaccess' );
$readOptions = true;
$siteAccessSet = false;
for ( $i = 1, $count = count( $argv ); $i < $count; ++$i )
{
$arg = $argv[$i];
if ( $readOptions and
strlen( $arg ) > 0 and
$arg[0] == '-' )
{
if ( strlen( $arg ) > 1 and
$arg[1] == '-' )
{
$flag = substr( $arg, 2 );
if ( in_array( $flag, $longOptionsWithData ) )
{
$optionData = $argv[$i+1];
++$i;
}
if ( $flag == 'help' )
{
help();
exit();
}
else if ( $flag == 'siteaccess' )
{
$siteAccessSet = $optionData;
}
else if ( $flag == 'debug' )
{
$debugOutput = true;
}
else if ( $flag == 'quiet' )
{
$isQuiet = true;
}
else if ( $flag == 'colors' )
{
$useColors = true;
}
else if ( $flag == 'no-colors' )
{
$useColors = false;
}
else if ( $flag == 'no-logfiles' )
{
$useLogFiles = false;
}
else if ( $flag == 'logfiles' )
{
$useLogFiles = true;
}
else if ( $flag == 'list' )
{
$listCronjobs = true;
}
else if ( $flag == 'sql' )
{
$showSQL = true;
}
}
else
{
$flag = substr( $arg, 1, 1 );
$optionData = false;
if ( in_array( $flag, $optionsWithData ) )
{
if ( strlen( $arg ) > 2 )
{
$optionData = substr( $arg, 2 );
}
else
{
$optionData = $argv[$i+1];
++$i;
}
}
if ( $flag == 'h' )
{
help();
exit();
}
else if ( $flag == 'q' )
{
$isQuiet = true;
}
else if ( $flag == 'c' )
{
$useColors = true;
}
else if ( $flag == 'd' )
{
$debugOutput = true;
if ( strlen( $arg ) > 2 )
{
$levels = explode( ',', substr( $arg, 2 ) );
$allowedDebugLevels = array();
foreach ( $levels as $level )
{
if ( $level == 'all' )
{
$useDebugAccumulators = true;
$allowedDebugLevels = false;
$useDebugTimingpoints = true;
break;
}
if ( $level == 'accumulator' )
{
$useDebugAccumulators = true;
continue;
}
if ( $level == 'timing' )
{
$useDebugTimingpoints = true;
continue;
}
if ( $level == 'include' )
{
$useIncludeFiles = true;
}
if ( $level == 'error' )
$level = eZDebug::LEVEL_ERROR;
else if ( $level == 'warning' )
$level = eZDebug::LEVEL_WARNING;
else if ( $level == 'debug' )
$level = eZDebug::LEVEL_DEBUG;
else if ( $level == 'notice' )
$level = eZDebug::LEVEL_NOTICE;
else if ( $level == 'timing' )
$level = eZDebug::LEVEL_TIMING_POINT;
$allowedDebugLevels[] = $level;
}
}
}
else if ( $flag == 's' )
{
$siteAccessSet = $optionData;
}
}
}
else
{
if ( $cronPart === false )
{
$readOptions = false;
$cronPart = $arg;
}
}
}
$script->setUseDebugOutput( $debugOutput );
$script->setAllowedDebugLevels( $allowedDebugLevels );
$script->setUseDebugAccumulators( $useDebugAccumulators );
$script->setUseDebugTimingPoints( $useDebugTimingpoints );
$script->setUseIncludeFiles( $useIncludeFiles );
$script->setIsQuiet( $isQuiet );
$siteAccessChangeMessage = false;
if ( $siteAccessSet )
{
$siteAccessChangeMessage = changeSiteAccessSetting( $siteaccess, $siteAccessSet );
}
if ( $webOutput )
$useColors = true;
$cli->setUseStyles( $useColors );
$script->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' ) . str_repeat( '#', 36 ) . "\n" );
$script->setUseSiteAccess( $siteaccess );
$script->initialize();
if ( !$script->isInitialized() )
{
$cli->error( 'Error initializing script: ' . $script->initializationError() . '.' );
$script->shutdown( 0 );
}
if ( $siteAccessChangeMessage )
{
$cli->output( $siteAccessChangeMessage );
}
else
{
$cli->output( "Using siteaccess $siteaccess for cronjob" );
}
if ( $cronPart )
{
$cli->output( "Running cronjob part '$cronPart'" );
}
$db = eZDB::instance();
$db->setIsSQLOutputEnabled( $showSQL );
$ini = eZINI::instance( 'cronjob.ini' );
$scriptDirectories = $ini->variable( 'CronjobSettings', 'ScriptDirectories' );
/* Include extension directories */
$extensionDirectories = $ini->variable( 'CronjobSettings', 'ExtensionDirectories' );
$scriptDirectories = array_merge( $scriptDirectories, eZExtension::expandedPathList( $extensionDirectories, 'cronjobs' ) );
if ( $listCronjobs )
{
foreach ( $ini->groups() as $block => $blockValues )
{
if ( strpos( $block, 'Cronjob' ) !== false )
{
$cli->output( $cli->endLineString() );
$cli->output( "{$block}:" );
foreach ( $blockValues['Scripts'] as $fileName )
{
$fileExists = false;
foreach ( $scriptDirectories as $scriptDirectory )
{
$filePath = $scriptDirectory . "/" . $fileName;
if ( file_exists( $filePath ) )
{
$fileExists = true;
$cli->output( "{$cli->goToColumn( 4 )} {$filePath}" );
}
}
if ( !$fileExists )
$cli->output( "{$cli->goToColumn( 4 )} Error: No {$fileName} file in any of configured directories!" );
}
}
}
$script->shutdown( 0 );
}
$scriptGroup = 'CronjobSettings';
if ( $cronPart !== false )
$scriptGroup = "CronjobPart-$cronPart";
$scripts = $ini->variable( $scriptGroup, 'Scripts' );
if ( !is_array( $scripts ) or empty( $scripts ) )
{
$cli->notice( 'Notice: No scripts found for execution.' );
$script->shutdown( 0 );
}
$index = 0;
foreach ( $scripts as $cronScript )
{
foreach ( $scriptDirectories as $scriptDirectory )
{
$scriptFile = $scriptDirectory . '/' . $cronScript;
if ( file_exists( $scriptFile ) )
break;
}
if ( file_exists( $scriptFile ) )
{
if ( $index > 0 )
{
$cli->output();
}
if ( !$isQuiet )
{
$startTime = new eZDateTime();
$cli->output( 'Running ' . $cli->stylize( 'emphasize', $scriptFile ) . ' at: ' . $startTime->toString( true ) );
}
eZDebug::addTimingPoint( "Script $scriptFile starting" );
eZRunCronjobs::runScript( $cli, $scriptFile );
eZDebug::addTimingPoint( "Script $scriptFile done" );
++$index;
// The transaction check
$transactionCounterCheck = eZDB::checkTransactionCounter();
if ( isset( $transactionCounterCheck['error'] ) )
$cli->error( $transactionCounterCheck['error'] );
if ( !$isQuiet )
{
$endTime = new eZDateTime();
$cli->output( 'Completing ' . $cli->stylize( 'emphasize', $scriptFile ) . ' at: ' . $endTime->toString( true ) );
$elapsedTime = new eZTime( $endTime->timeStamp() - $startTime->timeStamp() );
$cli->output( 'Elapsed time: ' . sprintf( '%02d:%02d:%02d', $elapsedTime->hour(), $elapsedTime->minute(), $elapsedTime->second() ) );
}
}
}
$script->shutdown();
?>