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