Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
5de2af 1 <?php
M 2
3 /*
4 Copyright (c) 2013, Marius Cramer, pixcept KG
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class cronjob {
32
b1a6a5 33     // default is every 5 minutes
MC 34     protected $_schedule = '*/5 * * * *';
5de2af 35
b1a6a5 36     // may a run be skipped?
MC 37     protected $_no_skip = false;
38
39     // if true, this job is run when it is first recognized. If false, the next run is calculated from schedule on first run.
40     protected $_run_at_new = false;
41
42     protected $_last_run = null;
43     protected $_next_run = null;
44     private $_running = false;
45
46     /** return schedule */
47
48
49     public function getSchedule() {
d22277 50         global $app, $conf;
MB 51         
52         $class = get_class($this);
53         
54         switch ($class) {
55             case 'cronjob_backup':
56                 $app->uses('ini_parser,getconf');
57                 $server_id = $conf['server_id'];
58                 $server_conf = $app->getconf->get_server_config($server_id, 'server');
59                 if(isset($server_conf['backup_time']) && $server_conf['backup_time'] != ''){
60                     list($hour, $minute) = explode(':', $server_conf['backup_time']);
61                     $schedule = $minute.' '.$hour.' * * *';
62                 } else {
63                     $schedule = '0 0 * * *';
64                 }
65                 break;
66             /*case 'cronjob_backup_mail':
67                 $schedule = '1 0 * * *';
68                 break;*/
69             default:
70                 $schedule = $this->_schedule;
71         }
72         
73         return $schedule;
b1a6a5 74     }
MC 75
76
77
78     /** run through cronjob sequence **/
79     public function run() {
80
81         print "Called run() for class " . get_class($this) . "\n";
d22277 82         print "Job has schedule: " . $this->getSchedule() . "\n";
b1a6a5 83         $this->onPrepare();
MC 84         $run_it = $this->onBeforeRun();
85         if($run_it == true) {
86             $this->onRunJob();
87             $this->onAfterRun();
88         }
89         $this->onCompleted();
90
91         return;
92     }
93
94     /* this function prepares some data for the job and sets next run time if first executed */
95     protected function onPrepare() {
96         global $app;
97
98         print "Called onPrepare() for class " . get_class($this) . "\n";
99         // check the run time and values for this job
100
101         // get previous run data
cc7a82 102         $data = $app->db->queryOneRecord("SELECT `last_run`, `next_run`, `running` FROM `sys_cron` WHERE `name` = ?", get_class($this));
b1a6a5 103         if($data) {
MC 104             if($data['last_run']) $this->_last_run = $data['last_run'];
105             if($data['next_run']) $this->_next_run = $data['next_run'];
106             if($data['running'] == 1) $this->_running = true;
107         }
108         if(!$this->_next_run) {
109             if($this->_run_at_new == true) {
110                 $this->_next_run = ISPConfigDateTime::dbtime(); // run now.
111             } else {
d22277 112                 $app->cron->parseCronLine($this->getSchedule());
b1a6a5 113                 $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
MC 114                 $this->_next_run = $next_run;
115
cc7a82 116                 $app->db->query("REPLACE INTO `sys_cron` (`name`, `last_run`, `next_run`, `running`) VALUES (?, ?, ?, ?)", get_class($this), ($this->_last_run ? $this->_last_run : "#NULL#"), ($next_run === false ? "#NULL#" : $next_run . "'"), ($this->_running == true ? "1" : "0"));
b1a6a5 117             }
MC 118         }
119     }
120
121     /* this function checks if a cron job's next runtime is reached and returns true or false */
122     protected function onBeforeRun() {
123         global $app;
124
125         print "Called onBeforeRun() for class " . get_class($this) . "\n";
126
127         if($this->_running == true) return false; // job is still marked as running!
128
129         print "Jobs next run is " . $this->_next_run . "\n";
130         $reached = ISPConfigDateTime::compare($this->_next_run, ISPConfigDateTime::dbtime());
131         print "Date compare of " . ISPConfigDateTime::to_timestamp($this->_next_run) . " and " . ISPConfigDateTime::dbtime() . " is " . $reached . "\n";
132         if($reached === false) return false; // error!
133
134         if($reached === -1) {
135             // next_run time not reached
136             return false;
137         }
138
139         // next_run time reached (reached === 0 or -1)
140
141         // calculare next run time based on last_run or current time
d22277 142         $app->cron->parseCronLine($this->getSchedule());
b1a6a5 143         if($this->_no_skip == true) {
MC 144             // we need to calculare the next run based on the previous next_run, as we may not skip one.
145             $next_run = $app->cron->getNextRun($this->_next_run);
146             if($next_run === false) {
147                 // we could not calculate next run, try it with current time
148                 $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
149             }
150         } else {
151             // calculate next run based on current time
152             $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
153         }
154
155         print "Jobs next run is now " . $next_run . "\n";
156
cc7a82 157         $app->db->query("REPLACE INTO `sys_cron` (`name`, `last_run`, `next_run`, `running`) VALUES (?, NOW(), ?, 1)", get_class($this), ($next_run === false ? "#NULL#" : $next_run));
b1a6a5 158         return true;
MC 159     }
160
161     // child classes should override this!
162     protected function onRunJob() {
163         global $app;
164
165         print "Called onRun() for class " . get_class($this) . "\n";
166     }
167
168     // child classes may override this!
169     protected function onAfterRun() {
170         global $app;
171
172         print "Called onAfterRun() for class " . get_class($this) . "\n";
173     }
174
175     // child classes may NOT override this!
176     private function onCompleted() {
177         global $app;
178
179         print "Called onCompleted() for class " . get_class($this) . "\n";
cc7a82 180         $app->db->query("UPDATE `sys_cron` SET `running` = 0 WHERE `name` = ?", get_class($this));
b1a6a5 181     }
5de2af 182
M 183 }
184
185 ?>