Merge pull request #16067 from eileenmcnaughton/fatal
[civicrm-core.git] / CRM / Core / ScheduledJob.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This interface defines methods that need to be implemented
14 * by every scheduled job (cron task) in CiviCRM.
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
18 * $Id$
19 *
20 */
21class CRM_Core_ScheduledJob {
22
518fa0ee 23 public $version = 3;
6a488035 24
518fa0ee 25 public $name = NULL;
6a488035 26
518fa0ee 27 public $apiParams = [];
6a488035 28
518fa0ee 29 public $remarks = [];
6a488035 30
a0ee3941 31 /**
c490a46a 32 * @param array $params
a0ee3941 33 */
6a488035
TO
34 public function __construct($params) {
35 foreach ($params as $name => $param) {
36 $this->$name = $param;
37 }
38
39 // version is set to 3 by default - if different number
40 // defined in params, it's replaced later on, however,
41 // it's practically useles, since it seems none of api v2
42 // will work properly in cron job setup. It might become
43 // useful when/if api v4 starts to emerge and will need
44 // testing in the cron job setup. To permanenty require
45 // hardcoded api version, it's enough to move below line
46 // under following if block.
be2fb01f 47 $this->apiParams = ['version' => $this->version];
6a488035
TO
48
49 if (!empty($this->parameters)) {
50 $lines = explode("\n", $this->parameters);
51
52 foreach ($lines as $line) {
53 $pair = explode("=", $line);
94ca7fdb 54 if ($pair === FALSE || count($pair) != 2 || trim($pair[0]) == '' || trim($pair[1]) == '') {
6a488035
TO
55 $this->remarks[] .= 'Malformed parameters!';
56 break;
57 }
58 $this->apiParams[trim($pair[0])] = trim($pair[1]);
59 }
60 }
61 }
62
a0ee3941
EM
63 /**
64 * @param null $date
65 */
6a488035 66 public function saveLastRun($date = NULL) {
353ffa53
TO
67 $dao = new CRM_Core_DAO_Job();
68 $dao->id = $this->id;
6a488035
TO
69 $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date);
70 $dao->save();
71 }
72
bda41fcb
DRJ
73 /**
74 * @return void
75 */
d7bace22
DRJ
76 public function clearScheduledRunDate() {
77 CRM_Core_DAO::executeQuery('UPDATE civicrm_job SET scheduled_run_date = NULL WHERE id = %1',
be2fb01f
CW
78 [
79 '1' => [$this->id, 'Integer'],
80 ]);
bda41fcb
DRJ
81 }
82
a0ee3941
EM
83 /**
84 * @return bool
85 */
6a488035 86 public function needsRunning() {
bda41fcb
DRJ
87
88 // CRM-17686
89 // check if the job has a specific scheduled date/time
d7bace22
DRJ
90 if (!empty($this->scheduled_run_date)) {
91 if (strtotime($this->scheduled_run_date) <= time()) {
92 $this->clearScheduledRunDate();
bda41fcb
DRJ
93 return TRUE;
94 }
95 else {
96 return FALSE;
97 }
98 }
99
6a488035
TO
100 // run if it was never run
101 if (empty($this->last_run)) {
102 return TRUE;
103 }
104
105 // run_frequency check
106 switch ($this->run_frequency) {
107 case 'Always':
108 return TRUE;
109
bda41fcb
DRJ
110 // CRM-17669
111 case 'Yearly':
112 $offset = '+1 year';
113 break;
114
115 case 'Quarter':
116 $offset = '+3 months';
117 break;
118
119 case 'Monthly':
120 $offset = '+1 month';
121 break;
122
123 case 'Weekly':
124 $offset = '+1 week';
d0be1535 125 break;
6a488035
TO
126
127 case 'Daily':
bda41fcb 128 $offset = '+1 day';
d0be1535 129 break;
7fce7b7f 130
bda41fcb
DRJ
131 case 'Hourly':
132 $offset = '+1 hour';
133 break;
6a488035
TO
134 }
135
bda41fcb
DRJ
136 $now = strtotime(CRM_Utils_Date::currentDBDate());
137 $lastTime = strtotime($this->last_run);
138 $nextTime = strtotime($offset, $lastTime);
d0be1535 139
bda41fcb 140 return ($now >= $nextTime);
6a488035
TO
141 }
142
2aa397bc
TO
143 public function __destruct() {
144 }
96025800 145
6a488035 146}