AutoCron.php 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 8/18/17
 * Time: 8:49 AM
 */

namespace qfq;

use qfq;

require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/database/Database.php');
require_once(__DIR__ . '/../qfq/exceptions/ShellException.php');
17
require_once(__DIR__ . '/../qfq/Evaluate.php');
18
require_once(__DIR__ . '/../qfq/report/SendMail.php');
19
require_once(__DIR__ . '/../qfq/helper/Support.php');
20
require_once(__DIR__ . '/../qfq/helper/DownloadPage.php');
21

22
23
24
25
/**
 * Class AutoCron
 * @package qfq
 */
26
27
28
29
30
31
32
class AutoCron {

    /**
     * @var Store
     */
    protected $store = null;

33
34
35
36
37
    /**
     * @var Evaluate
     */
    protected $evaluate = null;

38
39
40
41
42
    /**
     * @var bool
     */
    private $phpUnit = false;

43
44
45
46
47
48
49
    /**
     * @var Database[]
     */
    private $dbArray = array();

    private $dbIndexQfq = '';

50
    private $verbose = '';
51

52

53
54
55
56
57
    /**
     * AutoCron constructor.
     * @param bool $verbose
     * @param bool $phpUnit
     * @throws CodeException
58
     * @throws DbException
59
     * @throws UserFormException
60
     * @throws UserReportException
61
     */
62
63
64
    public function __construct($verbose = false, $phpUnit = false) {

        $this->verbose = $verbose;
65
66
67
68
69
70
71
        $this->phpUnit = $phpUnit;

        mb_internal_encoding("UTF-8");

//        set_error_handler("\\qfq\\ErrorHandler::exception_error_handler");

        $this->store = Store::getInstance();
72
73
74
75
76
77

        $this->dbIndexQfq = $this->store->getVar(SYSTEM_DB_INDEX_QFQ, STORE_SYSTEM);

        $this->dbArray[$this->dbIndexQfq] = new Database($this->dbIndexQfq);

        $this->evaluate = new Evaluate($this->store, $this->dbArray[$this->dbIndexQfq]);
78
79
80
    }

    /**
81
     * Check if there are started cronJobs, older than $ageMaxMinutes
82
83
     *
     * @param int $ageMaxMinutes
Carsten  Rose's avatar
Carsten Rose committed
84
     *
85
86
     * @throws CodeException
     * @throws DbException
87
     * @throws UserFormException
88
89
     */
    private function checkForOldJobs($ageMaxMinutes) {
90

91
        $sql = "SELECT CONCAT('AutoCron: cron.id=', c.id, ' in progress since: ', c.inProgress, ' - older than 10 mins') FROM Cron AS c WHERE DATE_ADD(c.inProgress, INTERVAL $ageMaxMinutes MINUTE)<NOW() AND c.status='enable' ";
92
93
94
95

        // If there are too long running jobs: throw an exception
        $rows = $this->dbArray[$this->dbIndexQfq]->sql($sql, ROW_REGULAR);
        if (!empty($rows)) {
96
            echo 'Fix problem on the following AutoCron job and clear cron.inProgress of that job.';
97
            echo implode(PHP_EOL, $rows[0]);
98
        }
99
100
101
102
    }

    /**
     * @param array $job
Carsten  Rose's avatar
Carsten Rose committed
103
     *
104
105
106
     * @return array
     * @throws CodeException
     * @throws DbException
107
     * @throws UserFormException
108
109
110
111
112
113
114
115
116
117
118
     */
    private function calcNextRun(array $job) {

        // If nextRun is already in the future, do nothing.
        if (date('Y-m-d H:i:s') < $job[AUTOCRON_NEXT_RUN]) {
            return $job;
        }

        // With no frequency: stop future repeating by setting nextRun=0
        if ($job[AUTOCRON_FREQUENCY] == '') {
            $job[AUTOCRON_NEXT_RUN] = 0;
Carsten  Rose's avatar
Carsten Rose committed
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            return $job;
        }

        // '1 DAY', '15 MINUTE', '6 MONTH', or empty
        $job[AUTOCRON_FREQUENCY] = trim($job[AUTOCRON_FREQUENCY]);

        $arr = explode(' ', $job[AUTOCRON_FREQUENCY]);
        $count = empty($arr[0]) ? '1' : $arr[0];
        $unit = empty($arr[1]) ? 'DAY' : $arr[1];

        // Regular: nextRun is in the past and +frequency is in the future
        // Late:   nextRun is so much behind that +frequency is still in the past. Calculate nextRun for the future with respect to the given shedule/frequency (skip lost past actions)
        // Do the calculation with MySQL to stay in the MySQL date/time calculation world.
        $sql = "SELECT DATE_ADD('" . $job[AUTOCRON_NEXT_RUN] . "', INTERVAL CEILING( ( 1 + TIMESTAMPDIFF($unit, '" .
            $job[AUTOCRON_NEXT_RUN] . "', NOW() ) ) / $count ) * " . $job[AUTOCRON_FREQUENCY] . ") AS " . AUTOCRON_NEXT_RUN;
135
        $row = $this->dbArray[$this->dbIndexQfq]->sql($sql, ROW_EXPECT_1);  // frequency, nextRun, unit, count
136
137
138
139
140
141
142

        $job[AUTOCRON_NEXT_RUN] = $row[AUTOCRON_NEXT_RUN];

        return $job;
    }

    /**
143
     * Call the web page given in $job[AUTOCRON_CONTENT].
144
     *
145
     * @param array $job
Carsten  Rose's avatar
Carsten Rose committed
146
     *
147
     * @return array $job, updated with AUTOCRON_LAST_STATUS
148
149
     * @throws CodeException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
150
     * @throws UserReportException
151
152
153
154
155
     */
    private function doJobWebsite(array $job) {

        $job[AUTOCRON_LAST_STATUS] = 'nothing done';
        $job[AUTOCRON_CONTENT] = trim($job[AUTOCRON_CONTENT]);
156

157
        if (!empty($job[AUTOCRON_CONTENT])) {
158

159
            $baseUrl = $this->store->getVar(SYSTEM_BASE_URL, STORE_SYSTEM);
160

161
            $page = DownloadPage::getContent($job[AUTOCRON_CONTENT], $baseUrl);
162
            if ($page === false) {
163
                $job[AUTOCRON_LAST_STATUS] = htmlspecialchars(AUTOCRON_STATUS_ERROR . 'failed to fetch "' . $job[AUTOCRON_CONTENT] . '"');
164
165
166
167
168
169
170

                return $job;
            }

            // If configured, log the download content
            if (!empty($job[AUTOCRON_OUTPUT_FILE])) {

171
                $job[AUTOCRON_OUTPUT_FILE] = Support::joinPath($this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM), $job[AUTOCRON_OUTPUT_FILE], FILE_PRIORITY);
172
173
174
175

                Logger::logMessage($page, $job[AUTOCRON_OUTPUT_FILE], $job[AUTOCRON_OUTPUT_MODE] == 'append' ? FILE_MODE_APPEND : FILE_MODE_WRITE);
            }

176
            $job[AUTOCRON_LAST_STATUS] = AUTOCRON_STATUS_OK . $page;
177
178
179
180
181
182
183

            // Check for pattern?
            if (!empty($job[AUTOCRON_OUTPUT_PATTERN])) {

                $rc = preg_match($job[AUTOCRON_OUTPUT_PATTERN], $page);

                if ($rc === false) {
184
                    $job[AUTOCRON_LAST_STATUS] = AUTOCRON_STATUS_ERROR . 'preg_match() failed - "' . $job[AUTOCRON_OUTPUT_PATTERN] . '"';
185
186
187
                }

                if ($rc === 0) {
188
                    $job[AUTOCRON_LAST_STATUS] = AUTOCRON_STATUS_ERROR . 'pattern not found - "' . $job[AUTOCRON_OUTPUT_PATTERN] . '"';
189
                }
190
191
192
            }
        }

193
194
        $job[AUTOCRON_LAST_STATUS] = htmlspecialchars($job[AUTOCRON_LAST_STATUS]);

195
196
197
198
        return $job;
    }

    /**
199
     * @param array $mailEntry
Carsten  Rose's avatar
Carsten Rose committed
200
     *
201
202
203
204
     * @return array
     */
    private function mailEntryFill(array $mailEntry) {
        foreach ([FE_SENDMAIL_TO, FE_SENDMAIL_CC, FE_SENDMAIL_BCC, FE_SENDMAIL_FROM, FE_SENDMAIL_SUBJECT,
205
                     FE_SENDMAIL_REPLY_TO, FE_SENDMAIL_FLAG_AUTO_SUBMIT, FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID,
206
207
                     FE_SENDMAIL_X_ID2, FE_SENDMAIL_X_ID3, FE_SENDMAIL_BODY_MODE,
                     FE_SENDMAIL_BODY_HTML_ENTITY, FE_SENDMAIL_SUBJECT_HTML_ENTITY] as $key) {
208
209
210
211
            if (!isset($mailEntry[$key])) {
                $mailEntry[$key] = '';
            }
        }
Carsten  Rose's avatar
Carsten Rose committed
212

213
214
215
216
217
218
219
220
        return $mailEntry;
    }

    /**
     * Send as many emails as $job[AUTOCRON_SQL1] has records.
     * Do not send mails if: no record OR no receiver OR empty body
     *
     * @param array $job
Carsten  Rose's avatar
Carsten Rose committed
221
     *
222
     * @return array $job, updated with AUTOCRON_LAST_STATUS
223
224
     * @throws CodeException
     * @throws DbException
225
     * @throws DownloadException
226
227
     * @throws UserFormException
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
228
229
230
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
231
232
233
234
235
     */
    private function doJobMail(array $job) {

        $mailArr = $this->evaluate->parse($job[AUTOCRON_SQL1]);

236
237
238
        $sendMail = new SendMail();

        $mailCount = 0;
239
240
        foreach ($mailArr as $mailEntry) {

241
242
            $this->store->setStore($mailEntry, STORE_PARENT_RECORD, true);

243
244
245
246
247
248
            $mailEntry = $this->mailEntryFill($mailEntry);

            $content = $this->evaluate->parse($job[AUTOCRON_CONTENT]);
            if ($content == '' OR ($mailEntry[FE_SENDMAIL_TO] == '' AND $mailEntry[FE_SENDMAIL_CC] == '' AND $mailEntry[FE_SENDMAIL_BCC] == '')) {
                continue; // no receiver: skip
            }
249
250
251
252
253
254
255
256
257
258
259
260
            $mail[SENDMAIL_TOKEN_RECEIVER] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_TO]);
            $mail[SENDMAIL_TOKEN_SENDER] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_FROM]);
            $mail[SENDMAIL_TOKEN_SUBJECT] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_SUBJECT]);
            $mail[SENDMAIL_TOKEN_BODY] = $content;
            $mail[SENDMAIL_TOKEN_REPLY_TO] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_REPLY_TO]);
            $mail[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_FLAG_AUTO_SUBMIT]) === 'off' ? 'off' : 'on';
            $mail[SENDMAIL_TOKEN_GR_ID] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_GR_ID]);
            $mail[SENDMAIL_TOKEN_X_ID] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_X_ID]);
            $mail[SENDMAIL_TOKEN_RECEIVER_CC] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_CC]);
            $mail[SENDMAIL_TOKEN_RECEIVER_BCC] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_BCC]);
            $mail[SENDMAIL_TOKEN_X_ID2] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_X_ID2]);
            $mail[SENDMAIL_TOKEN_X_ID3] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_X_ID3]);
261
262
263
            $mail[SENDMAIL_TOKEN_BODY_MODE] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_BODY_MODE]);
            $mail[SENDMAIL_TOKEN_BODY_HTML_ENTITY] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_BODY_HTML_ENTITY]);
            $mail[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] = $this->evaluate->parse($mailEntry[FE_SENDMAIL_SUBJECT_HTML_ENTITY]);
264
265

            $mail[SENDMAIL_TOKEN_SRC] = "AutoCron: Cron.id=" . $job[COLUMN_ID];
266
267

            // Mail: send
268
269
            $sendMail->process($mail);
            $mailCount++;
270
271
        }

272
        $job[AUTOCRON_LAST_STATUS] = "OK: $mailCount mails sent";
273
274
275
276
277
278
279

        return $job;
    }

    /**
     * Iterates over all AutoCron jobs and fire pending.
     *
280
281
     * @throws CodeException
     * @throws DbException
282
     * @throws DownloadException
283
284
     * @throws ShellException
     * @throws UserFormException
285
     * @throws UserReportException
286
287
288
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
289
290
     */
    public function process() {
291
292

        // Select pending jobs
293
        $sql = "SELECT * FROM Cron AS c WHERE c.status='enable' AND c.nextRun < NOW() AND c.nextRun!=0 AND c.inProgress=0";
294
        $jobs = $this->dbArray[$this->dbIndexQfq]->sql($sql);
295
296
297
298

        // Iterate over all AutoCron Jobs
        foreach ($jobs as $job) {

299
            $this->store->setStore($job, STORE_PARENT_RECORD, true);
300
            $nextRun = $job[AUTOCRON_NEXT_RUN];
301

302
            // Start progress counter
303
            $this->dbArray[$this->dbIndexQfq]->sql("UPDATE Cron SET inProgress=NOW() WHERE id=? LIMIT 1", ROW_REGULAR, [$job[COLUMN_ID]]);
304
305
306
307
308
309
310
311
312
313
314
315
316

            switch ($job[AUTOCRON_TYPE]) {
                case AUTOCRON_TYPE_WEBSITE:
                    $job = $this->doJobWebsite($job);
                    break;
                case AUTOCRON_TYPE_MAIL:
                    $job = $this->doJobMail($job);
                    break;
                default:
                    throw new ShellException('Unknown cron.type value: ' . $job[AUTOCRON_TYPE], ERROR_UNKNOWN_MODE);
            }

            $job = $this->calcNextRun($job);
317
318
319
320
321
322
323
324
325
326
327
328
329

            // Check 'asynchronous' job if they have been triggered during processing: prepare to be fired during the next cron turn again.
            if ($job[AUTOCRON_FREQUENCY] == '') {
                // Get latest c.nextRun to compare and check for an update in between
                $rowCheckAsynchronous = $this->dbArray[$this->dbIndexQfq]->sql("SELECT c.nextRun FROM Cron AS c WHERE id=? LIMIT 1", ROW_EXPECT_1, [$job[COLUMN_ID]]);
                if (strcmp($nextRun, $rowCheckAsynchronous[AUTOCRON_NEXT_RUN]) < 0) {
                    // There was an update on the current job in between: preserve 'nextRun' to fire the job on the next system cron turn.
                    $job[AUTOCRON_NEXT_RUN] = $rowCheckAsynchronous[AUTOCRON_NEXT_RUN];
                }
            }

            // Finish Job
            $sql = "UPDATE Cron SET lastRun=inProgress, lastStatus=?, nextRun=?, inProgress=0 WHERE id=? LIMIT 1";
330
            $this->dbArray[$this->dbIndexQfq]->sql($sql, ROW_REGULAR, [$job[AUTOCRON_LAST_STATUS], $job[AUTOCRON_NEXT_RUN], $job[COLUMN_ID]]);
331
332
333
334
335
        }

        $this->checkForOldJobs(AUTOCRON_MAX_AGE_MINUTES);
    }
}