1 | <?php
|
---|
2 |
|
---|
3 | class ModuleScheduler extends Module
|
---|
4 | {
|
---|
5 | function __construct(System $System)
|
---|
6 | {
|
---|
7 | parent::__construct($System);
|
---|
8 | $this->Name = 'Scheduler';
|
---|
9 | $this->Version = '1.0';
|
---|
10 | $this->Creator = 'Chronos';
|
---|
11 | $this->License = 'GNU/GPLv3';
|
---|
12 | $this->Description = 'Allow to setup and execute planned and recurring tasks';
|
---|
13 | $this->Models = array(SchedulerAction::GetClassName(), Scheduler::GetClassName());
|
---|
14 | }
|
---|
15 |
|
---|
16 | function DoStart(): void
|
---|
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' => ''),
|
---|
26 | 'Action' => array('Type' => 'TSchedulerAction', 'Caption' => 'Akce', 'Default' => ''),
|
---|
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),
|
---|
29 | 'Duration' => array('Type' => 'TimeDiff', 'Caption' => 'Trvání', 'Default' => '', 'ReadOnly' => true),
|
---|
30 | 'Log' => array('Type' => 'Text', 'Caption' => 'Poslední záznam', 'Default' => '', 'ReadOnly' => true, 'NotInList' => true),
|
---|
31 | ),
|
---|
32 | ));
|
---|
33 | $this->System->FormManager->RegisterClass('SchedulerAction', array(
|
---|
34 | 'Title' => 'Akce plánovače',
|
---|
35 | 'Table' => 'SchedulerAction',
|
---|
36 | 'DefaultSortColumn' => 'Name',
|
---|
37 | 'ReadOnly' => true,
|
---|
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(
|
---|
44 | 'Type' => 'Reference',
|
---|
45 | 'Table' => 'SchedulerAction',
|
---|
46 | 'Id' => 'Id',
|
---|
47 | 'Name' => 'Name',
|
---|
48 | 'Filter' => '1',
|
---|
49 | ));
|
---|
50 | $this->System->RegisterCommandLine('run-scheduler', 'Runs scheduled tasks',
|
---|
51 | array(ModuleScheduler::Cast($this->System->GetModule('Scheduler')), 'Run'));
|
---|
52 | }
|
---|
53 |
|
---|
54 | function Run(array $Parameters): void
|
---|
55 | {
|
---|
56 | while (true)
|
---|
57 | {
|
---|
58 | $DbResult = $this->Database->query('SELECT `Scheduler`.*, `SchedulerAction`.`Class` AS `Class` FROM `Scheduler` '.
|
---|
59 | 'LEFT JOIN `SchedulerAction` ON `SchedulerAction`.`Id` = `Scheduler`.`Action` '.
|
---|
60 | 'WHERE (`Scheduler`.`Enabled`=1) AND ( '.
|
---|
61 | '(`Scheduler`.`ScheduledTime` < "'.TimeToMysqlDateTime(time()).'") OR '.
|
---|
62 | '(`Scheduler`.`ScheduledTime` IS NULL))');
|
---|
63 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
64 | {
|
---|
65 | echo('Executing '.$DbRow['Name']."\n");
|
---|
66 | $Output = '';
|
---|
67 | $StartTime = time();
|
---|
68 | if (class_exists($DbRow['Class']))
|
---|
69 | {
|
---|
70 | $Class = new $DbRow['Class']($this->System);
|
---|
71 | try {
|
---|
72 | $Output = $Class->Execute();
|
---|
73 | } catch (Exception $E) {
|
---|
74 | echo($E->getMessage()."\n");
|
---|
75 | }
|
---|
76 | echo($Output);
|
---|
77 | } else echo('Class '.$DbRow['Class'].' not found'."\n");
|
---|
78 | $StopTime = time();
|
---|
79 | $Duration = $StopTime - $StartTime;
|
---|
80 | $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
81 | array('Log' => $Output, 'LastExecutedTime' => TimeToMysqlDateTime($StartTime),
|
---|
82 | 'Duration' => $Duration));
|
---|
83 | if ($DbRow['Period'] != '') {
|
---|
84 | if ($DbRow['ScheduledTime'] == '') $NewScheduledTime = $StartTime + $DbRow['Period'];
|
---|
85 | else $NewScheduledTime = MysqlDateTimeToTime($DbRow['ScheduledTime']) + $DbRow['Period'];
|
---|
86 | if ($NewScheduledTime < $StopTime) $NewScheduledTime = $StopTime + $DbRow['Period'];
|
---|
87 | $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
88 | array('ScheduledTime' => TimeToMysqlDateTime($NewScheduledTime)));
|
---|
89 | }
|
---|
90 | }
|
---|
91 | echo('.');
|
---|
92 | sleep(1);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | static function Cast(Module $Module): ModuleScheduler
|
---|
97 | {
|
---|
98 | if ($Module instanceof ModuleScheduler)
|
---|
99 | {
|
---|
100 | return $Module;
|
---|
101 | }
|
---|
102 | throw new Exception('Expected ModuleScheduler type but got '.gettype($Module));
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | class SchedulerTask extends Model
|
---|
107 | {
|
---|
108 | function Execute(): string
|
---|
109 | {
|
---|
110 | return '';
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | class ScheduleTaskTest extends SchedulerTask
|
---|
115 | {
|
---|
116 | function Execute(): string
|
---|
117 | {
|
---|
118 | $Output = '#';
|
---|
119 | return $Output;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | class Scheduler extends Model
|
---|
124 | {
|
---|
125 | static function GetModelDesc(): ModelDesc
|
---|
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 | {
|
---|
142 | static function GetModelDesc(): ModelDesc
|
---|
143 | {
|
---|
144 | $Desc = new ModelDesc(self::GetClassName());
|
---|
145 | $Desc->AddString('Name');
|
---|
146 | $Desc->AddString('Class');
|
---|
147 | return $Desc;
|
---|
148 | }
|
---|
149 | }
|
---|