Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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() {
50         return $this->_schedule;
51     }
52
53
54
55     /** run through cronjob sequence **/
56     public function run() {
57
58         print "Called run() for class " . get_class($this) . "\n";
59         print "Job has schedule: " . $this->_schedule . "\n";
60         $this->onPrepare();
61         $run_it = $this->onBeforeRun();
62         if($run_it == true) {
63             $this->onRunJob();
64             $this->onAfterRun();
65         }
66         $this->onCompleted();
67
68         return;
69     }
70
71     /* this function prepares some data for the job and sets next run time if first executed */
72     protected function onPrepare() {
73         global $app;
74
75         print "Called onPrepare() for class " . get_class($this) . "\n";
76         // check the run time and values for this job
77
78         // get previous run data
cc7a82 79         $data = $app->db->queryOneRecord("SELECT `last_run`, `next_run`, `running` FROM `sys_cron` WHERE `name` = ?", get_class($this));
b1a6a5 80         if($data) {
MC 81             if($data['last_run']) $this->_last_run = $data['last_run'];
82             if($data['next_run']) $this->_next_run = $data['next_run'];
83             if($data['running'] == 1) $this->_running = true;
84         }
85         if(!$this->_next_run) {
86             if($this->_run_at_new == true) {
87                 $this->_next_run = ISPConfigDateTime::dbtime(); // run now.
88             } else {
89                 $app->cron->parseCronLine($this->_schedule);
90                 $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
91                 $this->_next_run = $next_run;
92
cc7a82 93                 $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 94             }
MC 95         }
96     }
97
98     /* this function checks if a cron job's next runtime is reached and returns true or false */
99     protected function onBeforeRun() {
100         global $app;
101
102         print "Called onBeforeRun() for class " . get_class($this) . "\n";
103
104         if($this->_running == true) return false; // job is still marked as running!
105
106         print "Jobs next run is " . $this->_next_run . "\n";
107         $reached = ISPConfigDateTime::compare($this->_next_run, ISPConfigDateTime::dbtime());
108         print "Date compare of " . ISPConfigDateTime::to_timestamp($this->_next_run) . " and " . ISPConfigDateTime::dbtime() . " is " . $reached . "\n";
109         if($reached === false) return false; // error!
110
111         if($reached === -1) {
112             // next_run time not reached
113             return false;
114         }
115
116         // next_run time reached (reached === 0 or -1)
117
118         // calculare next run time based on last_run or current time
119         $app->cron->parseCronLine($this->_schedule);
120         if($this->_no_skip == true) {
121             // we need to calculare the next run based on the previous next_run, as we may not skip one.
122             $next_run = $app->cron->getNextRun($this->_next_run);
123             if($next_run === false) {
124                 // we could not calculate next run, try it with current time
125                 $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
126             }
127         } else {
128             // calculate next run based on current time
129             $next_run = $app->cron->getNextRun(ISPConfigDateTime::dbtime());
130         }
131
132         print "Jobs next run is now " . $next_run . "\n";
133
cc7a82 134         $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 135         return true;
MC 136     }
137
138     // child classes should override this!
139     protected function onRunJob() {
140         global $app;
141
142         print "Called onRun() for class " . get_class($this) . "\n";
143     }
144
145     // child classes may override this!
146     protected function onAfterRun() {
147         global $app;
148
149         print "Called onAfterRun() for class " . get_class($this) . "\n";
150     }
151
152     // child classes may NOT override this!
153     private function onCompleted() {
154         global $app;
155
156         print "Called onCompleted() for class " . get_class($this) . "\n";
cc7a82 157         $app->db->query("UPDATE `sys_cron` SET `running` = 0 WHERE `name` = ?", get_class($this));
b1a6a5 158     }
5de2af 159
M 160 }
161
162 ?>