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
19 * This class contains scheduled jobs related functions.
21 class CRM_Core_BAO_Job
extends CRM_Core_DAO_Job
{
24 * Add the payment-processor type in the db
26 * @param array $params
27 * An assoc array of name/value pairs.
29 * @return CRM_Financial_DAO_PaymentProcessorType
31 public static function create($params) {
32 $job = new CRM_Core_DAO_Job();
33 $job->copyValues($params);
38 * Retrieve DB object and copy to defaults array.
40 * @param array $params
41 * Array of criteria values.
42 * @param array $defaults
43 * Array to be populated with found values.
46 * The DAO object, if found.
50 public static function retrieve($params, &$defaults) {
51 return self
::commonRetrieve(self
::class, $params, $defaults);
55 * Update the is_active flag in the db.
58 * Id of the database record.
59 * @param bool $is_active
60 * Value we want to set the is_active field.
63 * true if we found and updated the object, else false
65 public static function setIsActive($id, $is_active) {
66 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
70 * Function to delete scheduled job.
76 * @throws CRM_Core_Exception
78 public static function del($jobID) {
79 self
::deleteRecord(['id' => $jobID]);
84 * Trim job table on a regular basis to keep it at a good size.
86 * @see https://issues.civicrm.org/jira/browse/CRM-10513
88 * @param int $maxEntriesToKeep
89 * @param int $minDaysToKeep
91 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
92 // Prevent the job log from getting too big
93 // For now, keep last minDays days and at least maxEntries records
94 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
95 $count = (int) CRM_Core_DAO
::singleValueQuery($query);
97 if ($count <= $maxEntriesToKeep) {
101 $count = $count - (int) $maxEntriesToKeep;
103 $minDaysToKeep = (int) $minDaysToKeep;
104 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count";
105 CRM_Core_DAO
::executeQuery($query);
109 * Make a copy of a Job.
111 * @param int $id The job id to copy.
112 * @param array $params
113 * @return CRM_Core_DAO
115 public static function copy($id, $params = []) {
118 'name' => ' - ' . ts('Copy'),
120 'replace' => $params,
122 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
124 CRM_Utils_Hook
::copy('Job', $copy);