Newer
Older
<?php
/**
* This File contains QUI\Cron\Manager
*/
namespace QUI\Cron;
/**
* Cron Manager
*
* @author www.pcsg.de (Henning Leutz)
*/
class Manager
{
* @param unknown $cron - Name of the Cron
* @param unknown $min - On which minute should it start
* @param unknown $hour - On which hour should it start
* @param unknown $day - On which day should it start
* @param unknown $month - On which month should it start
*/
public function add($cron, $min, $hour, $day, $month)
{
if ( !$this->_cronExists( $cron ) ) {
throw new \QUI\Exception( 'Cannot add Cron. Cron not exists', 404 );
}
$cronData = $this->getCronData( $cron );
\QUI::getDataBase()->insert($this->Table(), array(
'exec' => $cronData['exec'],
'title' => $cronData['title'],
'min' => $min,
'hour' => $hour,
'day' => $day,
'month' => $month
\QUI::getMessagesHandler()->addSuccess(
/**
* activate a cron in the cron list
* @param Integer $cronId - ID of the cron
*/
public function activateCron($cronId)
{
\QUI::getDataBase()->update(
$this->Table(),
array('active' => 1),
array('id' => (int)$cronId)
);
}
/**
* deactivate a cron in the cron list
* @param Integer $cronId - ID of the cron
*/
public function deactivateCron($cronId)
{
\QUI::getDataBase()->update(
$this->Table(),
array('active' => 0),
array('id' => (int)$cronId)
);
}
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
/**
* Delete the crons
* @param Array $ids - Array of the Cron-Ids
*/
public function deleteCronIds($ids)
{
$DataBase = \QUI::getDataBase();
foreach ( $ids as $id )
{
$id = (int)$id;
if ( $this->getCronById( $id ) === false ) {
return;
}
$DataBase->delete($this->Table(), array(
'id' => $id
));
}
}
/**
* Execute all upcoming crons
*/
public function execute()
{
$list = $this->getList();
$time = time();
foreach ( $list as $entry )
{
if ( $entry['active'] != 1 ) {
continue;
}
$lastexec = $entry['lastexec'];
$min = $entry['min'];
$hour = $entry['hour'];
$day = $entry['day'];
$month = $entry['month'];
$year = '*';
$Cron = \Cron\CronExpression::factory(
"$min $hour $day $month $year"
);
$next = $Cron->getNextRunDate( $lastexec )->getTimestamp();
// no execute
if ( $next > $time ) {
continue;
}
// execute cron
$this->executeCron( $entry['id'] );
}
}
/**
* Execute a cron
*
* @param Integer $cronId - ID of the cron
* @return \QUI\Cron\Manager
*/
public function executeCron($cronId)
{
$cronData = $this->getCronById( $cronId );
if ( !$cronData ) {
throw new \QUI\Exception( 'Cron ID not exist' );
}
call_user_func_array( $cronData['exec'], array($this) );
\QUI::getMessagesHandler()->addSuccess(
\QUI::getLocale()->get(
'quiqqer/cron',
'message.cron.succesful.executed'
)
);
\QUI::getDataBase()->insert(self::TableHistory(), array(
'cronid' => $cronId,
'lastexec' => date( 'Y-m-d H:i:s' ),
'uid' => \QUI::getUserBySession()->getId()
));
return $this;
}
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
/**
* Return the Crons which are available and from other Plugins provided
*
* @return Array
*/
public function getAvailableCrons()
{
$PackageManager = \QUI::getPackageManager();
$packageList = $PackageManager->getInstalled();
$result = array();
foreach ( $packageList as $entry )
{
$dir = OPT_DIR . $entry['name'] .'/';
$cronFile = $dir . 'cron.xml';
if ( !file_exists( $cronFile ) ) {
continue;
}
$result = array_merge(
$result,
$this->getCronsFromFile( $cronFile )
);
}
return $result;
}
/**
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
* Return the data of a inserted cron
*
* @param Integer $cronId - ID of the Cron
* @return Array|false - Cron Data
*/
public function getCronById($cronId)
{
$result = \QUI::getDataBase()->fetch(array(
'from' => $this->Table(),
'where' => array(
'id' => (int)$cronId
),
'limit' => 1
));
if ( !isset( $result[ 0 ] ) ) {
return false;
}
return $result[ 0 ];
}
/**
* Return the data of a specific cron from the available cron list
* This cron is not in the cron list
*
* @param String $cron - Name of the Cron
* @return Array|false - Cron Data
*/
public function getCronData($cron)
{
$availableCrons = $this->getAvailableCrons();
// check if cron is available
foreach ( $availableCrons as $entry )
{
return $entry;
}
}
return false;
}
/**
* Return the history list
*/
public function getHistoryList()
{
return \QUI::getDataBase()->fetch(array(
'from' => self::TableHistory()
));
}
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
/**
* Return the cron list
*
* @return Array
*/
public function getList()
{
return \QUI::getDataBase()->fetch(array(
'from' => self::Table()
));
}
/**
* Exist the cron?
*
* @return Bool
*/
protected function _cronExists($cron)
{
return $this->getCronData( $cron ) === false ? false : true;
}
/**
* static
*/
/**
* Return the cron tabe
*
* @return String
*/
static function Table()
{
return QUI_DB_PRFX .'cron';
}
/**
* Return the cron tabe
*
* @return String
*/
static function TableHistory()
{
return QUI_DB_PRFX .'cron_history';
}
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
/**
* Return the Crons from a XML File
*
* @param String $file
* @return Array
*/
static function getCronsFromFile($file)
{
if ( !file_exists( $file ) ) {
return array();
}
$Dom = \QUI\Utils\XML::getDomFromXml( $file );
$crons = $Dom->getElementsByTagName( 'crons' );
if ( !$crons || !$crons->length ) {
return array();
}
$Crons = $crons->item( 0 );
$list = $Crons->getElementsByTagName( 'cron' );
if ( !$list || !$list->length ) {
return array();
}
$result = array();
for ( $i = 0; $i < $list->length; $i++ )
{
$Cron = $list->item( $i );
$title = '';
$desc = '';
$Title = $Cron->getElementsByTagName( 'title' );
$Desc = $Cron->getElementsByTagName( 'description' );
if ( $Title->length ) {
$title = trim( $Title->item( 0 )->nodeValue );
}
if ( $Desc->length ) {
$desc = trim( $Desc->item( 0 )->nodeValue );
}
$result[] = array(
'title' => $title,
'description' => $desc,
'exec' => $Cron->getAttribute( 'exec' )
);
}
return $result;
}
/**
* Print a message to the log cron.log
*
* @param String $message - Message
*/
static function log($message)
{
$User = \QUI::getUsers()->getUserBySession();
$dir = VAR_DIR . 'log/';
$file = $dir . 'cron_'. date('Y-m-d') .'.log';
$str = '['. date('Y-m-d H:i:s') .' :: '. $User->getName() .'] '. $message;
QUI\System\Log::write( $str, 'cron' );
}