[729] | 1 | <?php
|
---|
| 2 |
|
---|
[899] | 3 | class ModuleScheduler extends Module
|
---|
[729] | 4 | {
|
---|
[887] | 5 | function __construct(System $System)
|
---|
[729] | 6 | {
|
---|
| 7 | parent::__construct($System);
|
---|
| 8 | $this->Name = 'Scheduler';
|
---|
| 9 | $this->Version = '1.0';
|
---|
| 10 | $this->Creator = 'Chronos';
|
---|
[899] | 11 | $this->License = 'GNU/GPLv3';
|
---|
[729] | 12 | $this->Description = 'Allow to setup and execute planned and recurring tasks';
|
---|
[899] | 13 | $this->Models = array(SchedulerAction::GetClassName(), Scheduler::GetClassName());
|
---|
[729] | 14 | }
|
---|
[738] | 15 |
|
---|
[887] | 16 | function DoStart(): void
|
---|
[729] | 17 | {
|
---|
| 18 | $this->System->FormManager->RegisterClass('Scheduler', array(
|
---|
| 19 | 'Title' => 'Plánovač',
|
---|
| 20 | 'Table' => 'Scheduler',
|
---|
| 21 | 'DefaultSortColumn' => 'ScheduledTime',
|
---|
| 22 | 'Items' => array(
|
---|
| 23 | 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
|
---|
| 24 | 'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '0'),
|
---|
| 25 | 'ScheduledTime' => array('Type' => 'DateTime', 'Caption' => 'Plánovaný čas', 'Default' => ''),
|
---|
[730] | 26 | 'Action' => array('Type' => 'TSchedulerAction', 'Caption' => 'Akce', 'Default' => ''),
|
---|
[729] | 27 | 'Period' => array('Type' => 'Integer', 'Caption' => 'Opakovat po', 'Default' => '', 'Null' => true, 'Suffix' => 'sekund'),
|
---|
| 28 | 'LastExecutedTime' => array('Type' => 'DateTime', 'Caption' => 'Čas posledního spuštění', 'Default' => '', 'ReadOnly' => true),
|
---|
[759] | 29 | 'Duration' => array('Type' => 'TimeDiff', 'Caption' => 'Trvání', 'Default' => '', 'ReadOnly' => true),
|
---|
[760] | 30 | 'Log' => array('Type' => 'Text', 'Caption' => 'Poslední záznam', 'Default' => '', 'ReadOnly' => true, 'NotInList' => true),
|
---|
[729] | 31 | ),
|
---|
| 32 | ));
|
---|
[730] | 33 | $this->System->FormManager->RegisterClass('SchedulerAction', array(
|
---|
| 34 | 'Title' => 'Akce plánovače',
|
---|
| 35 | 'Table' => 'SchedulerAction',
|
---|
| 36 | 'DefaultSortColumn' => 'Name',
|
---|
[738] | 37 | 'ReadOnly' => true,
|
---|
[730] | 38 | 'Items' => array(
|
---|
| 39 | 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => '', 'ReadOnly' => true),
|
---|
| 40 | 'Class' => array('Type' => 'String', 'Caption' => 'Vykonat třídu', 'Default' => '', 'ReadOnly' => true),
|
---|
| 41 | ),
|
---|
| 42 | ));
|
---|
| 43 | $this->System->FormManager->RegisterFormType('TSchedulerAction', array(
|
---|
[738] | 44 | 'Type' => 'Reference',
|
---|
| 45 | 'Table' => 'SchedulerAction',
|
---|
| 46 | 'Id' => 'Id',
|
---|
| 47 | 'Name' => 'Name',
|
---|
| 48 | 'Filter' => '1',
|
---|
[730] | 49 | ));
|
---|
[887] | 50 | $this->System->RegisterCommandLine('run-scheduler', 'Runs scheduled tasks',
|
---|
| 51 | array(ModuleScheduler::Cast($this->System->GetModule('Scheduler')), 'Run'));
|
---|
[729] | 52 | }
|
---|
[738] | 53 |
|
---|
[887] | 54 | function Run(array $Parameters): void
|
---|
[738] | 55 | {
|
---|
[873] | 56 | while (true)
|
---|
[738] | 57 | {
|
---|
| 58 | $DbResult = $this->Database->query('SELECT `Scheduler`.*, `SchedulerAction`.`Class` AS `Class` FROM `Scheduler` '.
|
---|
| 59 | 'LEFT JOIN `SchedulerAction` ON `SchedulerAction`.`Id` = `Scheduler`.`Action` '.
|
---|
[759] | 60 | 'WHERE (`Scheduler`.`Enabled`=1) AND ( '.
|
---|
| 61 | '(`Scheduler`.`ScheduledTime` < "'.TimeToMysqlDateTime(time()).'") OR '.
|
---|
[738] | 62 | '(`Scheduler`.`ScheduledTime` IS NULL))');
|
---|
[873] | 63 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
[738] | 64 | {
|
---|
| 65 | echo('Executing '.$DbRow['Name']."\n");
|
---|
| 66 | $Output = '';
|
---|
[759] | 67 | $StartTime = time();
|
---|
[873] | 68 | if (class_exists($DbRow['Class']))
|
---|
[738] | 69 | {
|
---|
| 70 | $Class = new $DbRow['Class']($this->System);
|
---|
[830] | 71 | try {
|
---|
| 72 | $Output = $Class->Execute();
|
---|
| 73 | } catch (Exception $E) {
|
---|
| 74 | echo($E->getMessage()."\n");
|
---|
| 75 | }
|
---|
[754] | 76 | echo($Output);
|
---|
[738] | 77 | } else echo('Class '.$DbRow['Class'].' not found'."\n");
|
---|
[759] | 78 | $StopTime = time();
|
---|
| 79 | $Duration = $StopTime - $StartTime;
|
---|
[738] | 80 | $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
[761] | 81 | array('Log' => $Output, 'LastExecutedTime' => TimeToMysqlDateTime($StartTime),
|
---|
| 82 | 'Duration' => $Duration));
|
---|
[873] | 83 | if ($DbRow['Period'] != '') {
|
---|
| 84 | if ($DbRow['ScheduledTime'] == '') $NewScheduledTime = $StartTime + $DbRow['Period'];
|
---|
[759] | 85 | else $NewScheduledTime = MysqlDateTimeToTime($DbRow['ScheduledTime']) + $DbRow['Period'];
|
---|
[873] | 86 | if ($NewScheduledTime < $StopTime) $NewScheduledTime = $StopTime + $DbRow['Period'];
|
---|
[759] | 87 | $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
| 88 | array('ScheduledTime' => TimeToMysqlDateTime($NewScheduledTime)));
|
---|
| 89 | }
|
---|
[738] | 90 | }
|
---|
| 91 | echo('.');
|
---|
| 92 | sleep(1);
|
---|
| 93 | }
|
---|
[729] | 94 | }
|
---|
[887] | 95 |
|
---|
[899] | 96 | static function Cast(Module $Module): ModuleScheduler
|
---|
[887] | 97 | {
|
---|
[899] | 98 | if ($Module instanceof ModuleScheduler)
|
---|
[887] | 99 | {
|
---|
[899] | 100 | return $Module;
|
---|
[887] | 101 | }
|
---|
[899] | 102 | throw new Exception('Expected ModuleScheduler type but got '.gettype($Module));
|
---|
[887] | 103 | }
|
---|
[729] | 104 | }
|
---|
| 105 |
|
---|
| 106 | class SchedulerTask extends Model
|
---|
| 107 | {
|
---|
[887] | 108 | function Execute(): string
|
---|
[738] | 109 | {
|
---|
[887] | 110 | return '';
|
---|
[738] | 111 | }
|
---|
[729] | 112 | }
|
---|
[759] | 113 |
|
---|
| 114 | class ScheduleTaskTest extends SchedulerTask
|
---|
| 115 | {
|
---|
[887] | 116 | function Execute(): string
|
---|
[759] | 117 | {
|
---|
| 118 | $Output = '#';
|
---|
[874] | 119 | return $Output;
|
---|
[759] | 120 | }
|
---|
[873] | 121 | }
|
---|
[894] | 122 |
|
---|
| 123 | class Scheduler extends Model
|
---|
| 124 | {
|
---|
[899] | 125 | static function GetModelDesc(): ModelDesc
|
---|
[894] | 126 | {
|
---|
| 127 | $Desc = new ModelDesc(self::GetClassName());
|
---|
| 128 | $Desc->AddString('Name');
|
---|
| 129 | $Desc->AddBoolean('Enabled');
|
---|
| 130 | $Desc->AddDateTime('ScheduledTime');
|
---|
| 131 | $Desc->AddReference('Action', SchedulerAction::GetClassName());
|
---|
| 132 | $Desc->AddInteger('Period');
|
---|
| 133 | $Desc->AddDateTime('LastExecutedTime');
|
---|
| 134 | $Desc->AddInteger('Duration');
|
---|
| 135 | $Desc->AddText('Log');
|
---|
| 136 | return $Desc;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | class SchedulerAction extends Model
|
---|
| 141 | {
|
---|
[899] | 142 | static function GetModelDesc(): ModelDesc
|
---|
[894] | 143 | {
|
---|
| 144 | $Desc = new ModelDesc(self::GetClassName());
|
---|
| 145 | $Desc->AddString('Name');
|
---|
| 146 | $Desc->AddString('Class');
|
---|
| 147 | return $Desc;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|