3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class contains scheduled jobs related functions.
23 class CRM_Core_BAO_Job
extends CRM_Core_DAO_Job
{
28 public function __construct() {
29 parent
::__construct();
33 * Add the payment-processor type in the db
35 * @param array $params
36 * An assoc array of name/value pairs.
38 * @return CRM_Financial_DAO_PaymentProcessorType
40 public static function create($params) {
41 $job = new CRM_Core_DAO_Job();
42 $job->copyValues($params);
47 * Retrieve DB object based on input parameters.
49 * It also stores all the retrieved values in the default array.
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
53 * @param array $defaults
54 * (reference ) an assoc array to hold the flattened values.
56 * @return CRM_Core_DAO_Job|null
57 * object on success, null otherwise
59 public static function retrieve(&$params, &$defaults) {
60 $job = new CRM_Core_DAO_Job();
61 $job->copyValues($params);
62 if ($job->find(TRUE)) {
63 CRM_Core_DAO
::storeValues($job, $defaults);
70 * Update the is_active flag in the db.
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
78 * true if we found and updated the object, else false
80 public static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
85 * Function to delete scheduled job.
88 * ID of the job to be deleted.
91 * @throws CRM_Core_Exception
93 public static function del($jobID) {
95 throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));
98 $dao = new CRM_Core_DAO_Job();
100 if (!$dao->find(TRUE)) {
104 if ($dao->delete()) {
110 * Trim job table on a regular basis to keep it at a good size.
112 * @see https://issues.civicrm.org/jira/browse/CRM-10513
114 * @param int $maxEntriesToKeep
115 * @param int $minDaysToKeep
117 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
118 // Prevent the job log from getting too big
119 // For now, keep last minDays days and at least maxEntries records
120 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
121 $count = (int) CRM_Core_DAO
::singleValueQuery($query);
123 if ($count <= $maxEntriesToKeep) {
127 $count = $count - (int) $maxEntriesToKeep;
129 $minDaysToKeep = (int) $minDaysToKeep;
130 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count";
131 CRM_Core_DAO
::executeQuery($query);
135 * Make a copy of a Job.
137 * @param int $id The job id to copy.
138 * @param array $params
139 * @return CRM_Core_DAO
141 public static function copy($id, $params = []) {
144 'name' => ' - ' . ts('Copy'),
146 'replace' => $params,
148 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
150 CRM_Utils_Hook
::copy('Job', $copy);