-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathezscheduledscript.php
More file actions
265 lines (225 loc) · 9.37 KB
/
ezscheduledscript.php
File metadata and controls
265 lines (225 loc) · 9.37 KB
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
<?php
/**
* File containing the eZScheduledScript class
*
* @copyright Copyright (C) 1999-2010 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/gnu_gpl GNU GPLv2
*
*/
class eZScheduledScript extends eZPersistentObject
{
const SITE_ACCESS_STRING = '__SITE_ACCESS__'; // Magic string to be replaced with site access
const SCRIPT_NAME_STRING = '__SCRIPT_NAME__'; // Magic string to be replaced with script name
const STATUS_NOT_STARTED = 'not_started';
const STATUS_ACTIVE = 'active';
const STATUS_DELAYED = 'delayed';
const STATUS_DEAD = 'dead';
const STATUS_COMPLETE = 'complete';
const STATUS_ACTIVE_TIMEOUT = 300; // 5 minutes
const STATUS_DELAYED_TIMEOUT = 900; // 15 minutes
const STATUS_VISIBLE_TIMEOUT = 18000; // 5 hours
const STATUS_PURGE_TIMEOUT = 86400; // 24 hours
const PROGRESS_UNKNOWN = 999; // For scripts who don't know their own progress
/*!
Constructor
*/
function __construct( $row )
{
$this->eZPersistentObject( $row );
}
/*!
\return a new object.
*/
static function create( $name, $command, $userID = false )
{
if ( trim( $name ) == '' )
{
eZDebug::writeError( 'Empty name. You must supply a valid script name string.', 'ezscriptmonitor' );
return false;
}
if ( trim( $command ) == '' )
{
eZDebug::writeError( 'Empty command. You must supply a valid command string.', 'ezscriptmonitor' );
return false;
}
$name = trim( $name );
$command = trim( $command );
if ( !$userID )
{
$userID = eZUser::currentUserID();
}
$scriptMonitorIni = eZINI::instance( 'ezscriptmonitor.ini' );
$scriptSiteAccess = $scriptMonitorIni->variable( 'GeneralSettings', 'ScriptSiteAccess' );
$command = str_replace( self::SCRIPT_NAME_STRING, $name, $command );
$command = str_replace( self::SITE_ACCESS_STRING, $scriptSiteAccess, $command );
if ( strlen( $command ) > 2000 )
{
eZDebug::writeError( 'Your command string is too long, it must be less than 2000 characters.', 'ezscriptmonitor' );
return false;
}
// Negative progress means not started yet
return new self( array( 'name' => $name,
'command' => $command,
'last_report_timestamp' => time(),
'progress' => -1,
'user_id' => $userID ) );
}
/*!
\return the persistent object definition.
*/
static function definition()
{
return array( 'fields' => array( 'id' => array( 'name' => 'ID',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'process_id' => array( 'name' => 'ProcessID',
'datatype' => 'integer',
'default' => 0,
'required' => false ),
'name' => array( 'name' => 'Name',
'datatype' => 'string',
'default' => '',
'required' => true ),
'command' => array( 'name' => 'Command',
'datatype' => 'string',
'default' => '',
'required' => true ),
'last_report_timestamp' => array( 'name' => 'LastReportTimestamp',
'datatype' => 'integer',
'default' => 0,
'required' => false ),
'progress' => array( 'name' => 'Progress',
'datatype' => 'integer',
'default' => 0,
'required' => false ),
'user_id' => array( 'name' => 'UserID',
'datatype' => 'integer',
'default' => 0,
'required' => false ) ),
'function_attributes' => array( 'process_id_text' => 'processIDText',
'status_text' => 'statusText',
'progress_text' => 'progressText' ),
'keys' => array( 'id' ),
'increment_key' => 'id',
'class_name' => 'eZScheduledScript',
'name' => 'ezscheduled_script' );
}
/*!
\return a translated string of the process ID
*/
function processIDText()
{
if ( is_numeric( $this->ProcessID ) and $this->ProcessID > 0 )
{
return $this->ProcessID;
}
return ezpI18n::tr( 'ezscriptmonitor', 'n/a' );
}
/*!
\return the string constant describing the status
*/
function statusText()
{
if ( $this->Progress == 100 )
{
return self::STATUS_COMPLETE;
}
if ( $this->Progress < 0 )
{
return self::STATUS_NOT_STARTED;
}
$timeSinceLastReport = time() - $this->LastReportTimestamp;
if ( $timeSinceLastReport <= self::STATUS_ACTIVE_TIMEOUT )
{
return self::STATUS_ACTIVE;
}
if ( $timeSinceLastReport <= self::STATUS_DELAYED_TIMEOUT )
{
return self::STATUS_DELAYED;
}
return self::STATUS_DEAD;
}
/*!
\return a translated string describing the progress
*/
function progressText()
{
if ( $this->Progress == self::PROGRESS_UNKNOWN )
{
return ezpI18n::tr( 'ezscriptmonitor', 'Unknown' );
}
if ( $this->Progress < 0 )
{
return '0%';
}
if ( $this->Progress > 100 )
{
return '100%';
}
return $this->Progress . '%';
}
/*!
\return the given script
*/
static function fetch( $scriptID, $asObject = true )
{
$conditions = array( 'id' => $scriptID );
return eZPersistentObject::fetchObject( eZScheduledScript::definition(),
null, $conditions, $asObject );
}
/*!
\return all current (recently active) scripts
*/
static function fetchCurrentScripts( $asObject = true, $offset = false, $limit = false )
{
$conditions = array( 'last_report_timestamp' => array( '>', time() - self::STATUS_VISIBLE_TIMEOUT ) );
$sorting = array( 'last_report_timestamp' => 'desc' );
$limitation = null;
if ( $offset !== false or $limit !== false )
{
$limitation = array( 'offset' => $offset, 'length' => $limit );
}
return eZPersistentObject::fetchObjectList( eZScheduledScript::definition(),
null, $conditions, $sorting, $limitation, $asObject );
}
/*!
\return all old and completed scripts
*/
static function fetchScriptsToPurge( $asObject = true, $offset = false, $limit = false )
{
$conditions = array( 'last_report_timestamp' => array( '<', time() - self::STATUS_PURGE_TIMEOUT ),
'progress' => 100 );
$limitation = null;
if ( $offset !== false or $limit !== false )
{
$limitation = array( 'offset' => $offset, 'length' => $limit );
}
return eZPersistentObject::fetchObjectList( eZScheduledScript::definition(),
null, $conditions, null, $limitation, $asObject );
}
/*!
\return all scripts that have not started yet
*/
static function fetchNotStartedScripts( $asObject = true, $offset = false, $limit = false )
{
$conditions = array( 'progress' => array( '<', 0 ) );
$limitation = null;
if ( $offset !== false or $limit !== false )
{
$limitation = array( 'offset' => $offset, 'length' => $limit );
}
return eZPersistentObject::fetchObjectList( eZScheduledScript::definition(),
null, $conditions, null, $limitation, $asObject );
}
/*!
Stores the new percentage and updates the timestamp
*/
function updateProgress( $progressPercentage )
{
$this->setAttribute( 'progress', (int)$progressPercentage );
$this->setAttribute( 'last_report_timestamp', time() );
$this->store();
}
}
?>