1 | <?php
|
---|
2 |
|
---|
3 | class ModuleScheduler extends AppModule
|
---|
4 | {
|
---|
5 | function __construct($System)
|
---|
6 | {
|
---|
7 | parent::__construct($System);
|
---|
8 | $this->Name = 'Scheduler';
|
---|
9 | $this->Version = '1.0';
|
---|
10 | $this->Creator = 'Chronos';
|
---|
11 | $this->License = 'GNU/GPL';
|
---|
12 | $this->Description = 'Allow to setup and execute planned and recurring tasks';
|
---|
13 | $this->Dependencies = array();
|
---|
14 | }
|
---|
15 |
|
---|
16 | function DoStart()
|
---|
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 | 'Log' => array('Type' => 'Text', 'Caption' => 'Poslední záznam', 'Default' => '', 'ReadOnly' => true),
|
---|
29 | 'LastExecutedTime' => array('Type' => 'DateTime', 'Caption' => 'Čas posledního spuštění', 'Default' => '', 'ReadOnly' => true),
|
---|
30 | ),
|
---|
31 | ));
|
---|
32 | $this->System->FormManager->RegisterClass('SchedulerAction', array(
|
---|
33 | 'Title' => 'Akce plánovače',
|
---|
34 | 'Table' => 'SchedulerAction',
|
---|
35 | 'DefaultSortColumn' => 'Name',
|
---|
36 | 'ReadOnly' => true,
|
---|
37 | 'Items' => array(
|
---|
38 | 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => '', 'ReadOnly' => true),
|
---|
39 | 'Class' => array('Type' => 'String', 'Caption' => 'Vykonat třídu', 'Default' => '', 'ReadOnly' => true),
|
---|
40 | ),
|
---|
41 | ));
|
---|
42 | $this->System->FormManager->RegisterFormType('TSchedulerAction', array(
|
---|
43 | 'Type' => 'Reference',
|
---|
44 | 'Table' => 'SchedulerAction',
|
---|
45 | 'Id' => 'Id',
|
---|
46 | 'Name' => 'Name',
|
---|
47 | 'Filter' => '1',
|
---|
48 | ));
|
---|
49 | $this->System->RegisterCommandLine('run-scheduler', array('ModuleScheduler', 'Run'));
|
---|
50 | }
|
---|
51 |
|
---|
52 | function DoInstall()
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | function DoUnInstall()
|
---|
57 | {
|
---|
58 | }
|
---|
59 |
|
---|
60 | function Run()
|
---|
61 | {
|
---|
62 | while(true)
|
---|
63 | {
|
---|
64 | $DbResult = $this->Database->query('SELECT `Scheduler`.*, `SchedulerAction`.`Class` AS `Class` FROM `Scheduler` '.
|
---|
65 | 'LEFT JOIN `SchedulerAction` ON `SchedulerAction`.`Id` = `Scheduler`.`Action` '.
|
---|
66 | 'WHERE (`Scheduler`.`Enabled`=1) AND (((`Scheduler`.`ScheduledTime` < "'.TimeToMysqlDateTime(time()).'") AND '.
|
---|
67 | ' (`Scheduler`.`LastExecutedTime` < `Scheduler`.`ScheduledTime`)) OR '.
|
---|
68 | '(`Scheduler`.`ScheduledTime` IS NULL))');
|
---|
69 | while($DbRow = $DbResult->fetch_assoc())
|
---|
70 | {
|
---|
71 | echo('Executing '.$DbRow['Name']."\n");
|
---|
72 | $Output = '';
|
---|
73 | if(class_exists($DbRow['Class']))
|
---|
74 | {
|
---|
75 | $Class = new $DbRow['Class']($this->System);
|
---|
76 | $Output = $Class->Execute();
|
---|
77 | echo($Output);
|
---|
78 | } else echo('Class '.$DbRow['Class'].' not found'."\n");
|
---|
79 | $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
80 | array('Log' => $Output, 'LastExecutedTime' => 'NOW()'));
|
---|
81 | if($DbRow['ScheduledTime'] == '') $NewScheduledTime = time() + $DbRow['Period'];
|
---|
82 | else $NewScheduledTime = MysqlDateTimeToTime($DbRow['ScheduledTime']) + $DbRow['Period'];
|
---|
83 | if($DbRow['Period'] != '') $this->Database->update('Scheduler', 'Id='.$DbRow['Id'],
|
---|
84 | array('ScheduledTime' => TimeToMysqlDateTime($NewScheduledTime)));
|
---|
85 | }
|
---|
86 | echo('.');
|
---|
87 | sleep(1);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | class SchedulerTask extends Model
|
---|
93 | {
|
---|
94 | function Execute()
|
---|
95 | {
|
---|
96 | }
|
---|
97 | }
|
---|