seeddms-code/controllers/class.Cron.php

106 lines
3.2 KiB
PHP
Raw Normal View History

2020-05-27 20:07:13 +00:00
<?php
/**
* Implementation of Cron controller
*
* @category DMS
* @package SeedDMS
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010-2020 Uwe Steinmann
* @version Release: @package_version@
*/
/**
* Class which does the busines logic for the regular cron job
*
* @category DMS
* @package SeedDMS
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010-2020 Uwe Steinmann
* @version Release: @package_version@
*/
class SeedDMS_Controller_Cron extends SeedDMS_Controller_Common {
public function run() { /* {{{ */
$dms = $this->params['dms'];
$user = $this->params['user'];
2020-05-27 20:07:13 +00:00
$settings = $this->params['settings'];
2023-01-10 14:42:50 +00:00
$logger = $this->params['logger'];
$mode = $this->params['mode'];
2020-05-27 20:07:13 +00:00
$db = $dms->getDb();
2020-06-28 11:59:54 +00:00
$scheduler = new SeedDMS_Scheduler($db);
$tasks = $scheduler->getTasks();
2020-05-27 20:07:13 +00:00
$jsonarr = [];
2020-06-28 11:59:54 +00:00
foreach($tasks as $task) {
if(isset($GLOBALS['SEEDDMS_SCHEDULER']['tasks'][$task->getExtension()]) && is_object($taskobj = resolveTask($GLOBALS['SEEDDMS_SCHEDULER']['tasks'][$task->getExtension()][$task->getTask()]))) {
$arr = array(
'extension'=>$task->getExtension(),
'name'=>$task->getTask(),
'mode'=>$mode,
'disabled' => (bool) $task->getDisabled(),
'isdue' => $task->isDue(),
);
2020-06-28 11:59:54 +00:00
switch($mode) {
case "run":
case "dryrun":
2020-06-28 11:59:54 +00:00
if(method_exists($taskobj, 'execute')) {
if(!$task->getDisabled() && $task->isDue()) {
if($mode == 'run') {
/* Schedule the next run right away to prevent a second execution
* of the task when the cron job of the scheduler is called before
* the last run was finished. The task itself can still be scheduled
* to fast, but this is up to the admin of seeddms.
*/
$task->updateLastNextRun();
2020-06-28 11:59:54 +00:00
if($taskobj->execute($task)) {
add_log_line("Execution of task ".$task->getExtension()."::".$task->getTask()." successful.");
$arr['success'] = true;
2020-06-28 11:59:54 +00:00
} else {
add_log_line("Execution of task ".$task->getExtension()."::".$task->getTask()." failed, task has been disabled.", PEAR_LOG_ERR);
$arr['success'] = false;
2020-06-28 11:59:54 +00:00
$task->setDisabled(1);
}
} elseif($mode == 'dryrun') {
$arr['success'] = true;
2020-06-28 11:59:54 +00:00
}
}
2020-05-27 20:07:13 +00:00
}
2020-06-28 11:59:54 +00:00
break;
case "check":
$arr['error'] = false;
if(!method_exists($taskobj, 'execute')) {
$arr['error'] = true;
$arr['messages'][] = 'Missing method execute()';
}
if(get_parent_class($taskobj) != 'SeedDMS_SchedulerTaskBase') {
$arr['error'] = true;
$arr['error'][] = "Wrong parent class";
}
break;
case "list":
default:
header("Content-Type: application/json");
$arr['nextrun']=$task->getNextRun();
$arr['frequency']=$task->getFrequency();
$arr['params']=array();
if($params = $task->getParameter()) {
foreach($params as $key=>$value) {
$p = $taskobj->getAdditionalParamByName($key);
$arr['params'][$key] = ($p['type'] == 'password') ? '*******' : $value;
}
}
break;
2020-05-27 20:07:13 +00:00
}
$jsonarr[] = $arr;
2020-05-27 20:07:13 +00:00
}
}
echo json_encode($jsonarr);
2020-05-27 20:07:13 +00:00
return true;
2020-05-27 20:07:13 +00:00
} /* }}} */
}