forked from ezsystems/ezpublish-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runcronjobs.php
executable file
·376 lines (342 loc) · 12.2 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
#!/usr/bin/env php
<?php
//
// Created on: <18-Mar-2003 17:06:45 amos>
//
// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
// SOFTWARE NAME: eZ Publish
// SOFTWARE RELEASE: 4.1.x
// COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
// SOFTWARE LICENSE: GNU General Public License v2.0
// NOTICE: >
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2.0 of the GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of version 2.0 of the GNU General
// Public License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
//
// No more than one instance of a cronjob script can be run at any given time. If a script uses more
// time than eZRunCronjobs_MaxScriptExecutionTime, 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 the eZRunCronjobs_MaxScriptExecutionTime, the original process will be killed.
// Define maximum script execution time to 12 hours
define( 'eZRunCronjobs_MaxScriptExecutionTime', 12*3600 );
// Set a default time zone if none is given. The time zone can be overriden
// in config.php or php.ini.
if ( !ini_get( "date.timezone" ) )
{
date_default_timezone_set( "UTC" );
}
require '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();
$endl = $cli->endlineString();
$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\n" .
" --logfiles create log files\n" .
" --no-logfiles do not create log files (default)\n" .
" --no-colors do not use ANSI coloring (default)\n" );
}
function changeSiteAccessSetting( &$siteaccess, $optionData )
{
global $isQuiet;
global $cronPart;
$cli = eZCLI::instance();
if ( file_exists( 'settings/siteaccess/' . $optionData ) )
{
$siteaccess = $optionData;
if ( !$isQuiet )
$cli->notice( "Using siteaccess $siteaccess for cronjob" );
}
elseif ( isExtensionSiteaccess( $optionData ) )
{
$siteaccess = $optionData;
if ( !$isQuiet )
$cli->notice( "Using extension siteaccess $siteaccess for cronjob" );
eZExtension::prependExtensionSiteAccesses( $siteaccess );
}
else
{
if ( !$isQuiet )
$cli->notice( "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;
$optionsWithData = array( 's' );
$longOptionsWithData = array( 'siteaccess' );
$readOptions = true;
for ( $i = 1; $i < count( $argv ); ++$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' )
{
changeSiteAccessSetting( $siteaccess, $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 == '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::EZ_LEVEL_TIMING;
$allowedDebugLevels[] = $level;
}
}
}
else if ( $flag == 's' )
{
changeSiteAccessSetting( $siteaccess, $optionData );
}
}
}
else
{
if ( $cronPart === false )
{
$readOptions = false;
$cronPart = $arg;
}
}
}
$script->setUseDebugOutput( $debugOutput );
$script->setAllowedDebugLevels( $allowedDebugLevels );
$script->setUseDebugAccumulators( $useDebugAccumulators );
$script->setUseDebugTimingPoints( $useDebugTimingpoints );
$script->setUseIncludeFiles( $useIncludeFiles );
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 ( $cronPart )
{
if ( !$isQuiet )
print( "Running cronjob part '$cronPart'$endl" );
}
$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' ) );
$scriptGroup = 'CronjobSettings';
if ( $cronPart !== false )
$scriptGroup = "CronjobPart-$cronPart";
$scripts = $ini->variable( $scriptGroup, 'Scripts' );
if ( !is_array( $scripts ) or count( $scripts ) == 0 and !$isQuiet )
{
$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 ( !$isQuiet &&
$index > 0 )
{
print( $endl );
}
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();
?>