seeddms-code/utils/schedulercli.php

151 lines
4.6 KiB
PHP
Raw Normal View History

2018-04-04 13:31:43 +00:00
<?php
if(isset($_SERVER['SEEDDMS_HOME'])) {
ini_set('include_path', $_SERVER['SEEDDMS_HOME'].'/utils'. PATH_SEPARATOR .ini_get('include_path'));
2021-08-06 07:19:54 +00:00
$myincpath = $_SERVER['SEEDDMS_HOME'];
2018-04-04 13:31:43 +00:00
} else {
ini_set('include_path', dirname($argv[0]). PATH_SEPARATOR .ini_get('include_path'));
$myincpath = dirname($argv[0]);
}
function usage() { /* {{{ */
echo "Usage:\n";
echo " seeddms-schedulercli [-h] [-v] [--config <file>]\n";
echo "\n";
echo "Description:\n";
echo " Check for scheduled tasks.\n";
echo "\n";
echo "Options:\n";
echo " -h, --help: print usage information and exit.\n";
echo " -v, --version: print version and exit.\n";
echo " --config: set alternative config file.\n";
echo " --mode: set mode of operation (run, dryrun, check, list).\n";
2018-04-04 13:31:43 +00:00
} /* }}} */
$version = "0.0.1";
$shortoptions = "hvc";
$longoptions = array('help', 'version', 'config:', 'mode:');
if(false === ($options = getopt($shortoptions, $longoptions))) {
usage();
exit(0);
}
/* Print help and exit */
if(isset($options['h']) || isset($options['help'])) {
usage();
exit(0);
}
/* Print version and exit */
if(isset($options['v']) || isset($options['verѕion'])) {
echo $version."\n";
exit(0);
}
/* Set alternative config file */
if(isset($options['config'])) {
define('SEEDDMS_CONFIG_FILE', $options['config']);
2020-09-03 14:04:57 +00:00
} elseif(isset($_SERVER['SEEDDMS_CONFIG_FILE'])) {
define('SEEDDMS_CONFIG_FILE', $_SERVER['SEEDDMS_CONFIG_FILE']);
2018-04-04 13:31:43 +00:00
}
$mode = 'list';
if(isset($options['mode'])) {
if(!in_array($options['mode'], array('run', 'dryrun', 'check', 'list'))) {
2018-04-04 13:31:43 +00:00
usage();
exit(1);
}
$mode = $options['mode'];
}
include($myincpath."/inc/inc.Settings.php");
2021-02-23 19:16:26 +00:00
include($myincpath."/inc/inc.Utils.php");
2022-12-11 15:56:05 +00:00
include($myincpath."/inc/inc.LogInit.php");
2018-04-04 13:31:43 +00:00
include($myincpath."/inc/inc.Init.php");
2019-02-14 11:35:57 +00:00
include($myincpath."/inc/inc.Language.php");
2018-04-04 13:31:43 +00:00
include($myincpath."/inc/inc.Extension.php");
include($myincpath."/inc/inc.DBInit.php");
include($myincpath."/inc/inc.Scheduler.php");
include($myincpath."/inc/inc.ClassController.php");
if(!($user = $dms->getUserByLogin('cli_scheduler'))) {
add_log_line("Execution of tasks failed because of missing user 'cli_scheduler'. Will exit now.", PEAR_LOG_ERR);
exit;
}
2018-04-04 13:31:43 +00:00
$scheduler = new SeedDMS_Scheduler($db);
$tasks = $scheduler->getTasks();
foreach($tasks as $task) {
2020-06-28 11:59:54 +00:00
if(isset($GLOBALS['SEEDDMS_SCHEDULER']['tasks'][$task->getExtension()]) && is_object($taskobj = resolveTask($GLOBALS['SEEDDMS_SCHEDULER']['tasks'][$task->getExtension()][$task->getTask()]))) {
2018-04-04 13:31:43 +00:00
switch($mode) {
case "run":
case "dryrun":
2018-04-04 13:31:43 +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();
2023-01-26 17:42:28 +00:00
add_log_line("Running '".$task->getExtension()."::".$task->getTask()."::".$task->getName()."'");
echo "Running '".$task->getExtension()."::".$task->getTask()."::".$task->getName()."'\n";
if($taskobj->execute($task)) {
2023-01-26 12:16:47 +00:00
add_log_line("Execution of task '".$task->getExtension()."::".$task->getTask()."::".$task->getName()."' successful.");
} else {
2023-01-26 12:16:47 +00:00
add_log_line("Execution of task ".$task->getExtension()."::".$task->getTask()."::".$task->getName()." failed, task has been disabled.", PEAR_LOG_ERR);
$task->setDisabled(1);
2020-05-18 14:24:19 +00:00
}
} elseif($mode == 'dryrun') {
2023-01-26 12:16:47 +00:00
echo "Running '".$task->getExtension()."::".$task->getTask()."::".$task->getName()."' in dry mode\n";
2018-04-04 13:31:43 +00:00
}
}
}
break;
case "check":
echo "Checking ".$task->getExtension()."::".$task->getTask().":\n";
if(!method_exists($taskobj, 'execute')) {
echo " Missing method execute()\n";
}
if(get_parent_class($taskobj) != 'SeedDMS_SchedulerTaskBase') {
echo " wrong parent class\n";
}
break;
case "list":
if(!$task->getDisabled()) {
if($task->isDue())
echo "*";
else
echo " ";
} else {
echo "-";
}
2023-01-26 12:16:47 +00:00
echo " '".$task->getExtension()."::".$task->getTask()."::".$task->getName()."'";
2018-04-04 13:31:43 +00:00
echo " ".$task->getNextRun();
echo " ".$task->getFrequency();
2018-04-04 13:31:43 +00:00
echo "\n";
if($params = $task->getParameter()) {
foreach($params as $key=>$value) {
2020-06-25 06:54:10 +00:00
$p = $taskobj->getAdditionalParamByName($key);
echo " ".$key.": ";
switch($p['type']) {
case 'password':
echo '********';
break;
default:
if(is_array($value))
echo implode(', ', $value);
else
echo $value;
}
echo PHP_EOL;
}
}
2018-04-04 13:31:43 +00:00
break;
}
}
}