Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Minor cleanup around action schedule code. #17151

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
*
* @return array
* Array(scalar $id => Mapping $mapping).
*
* @throws \CRM_Core_Exception
*/
public static function getMappings($filters = NULL) {
Expand Down Expand Up @@ -322,14 +323,21 @@ public static function sendMailings($mappingID, $now) {
}

/**
* @param int $mappingID
* @param $now
* Build a list of the contacts to send to.
*
* @param string $mappingID
* Value from the mapping_id field in the civicrm_action_schedule able. It might be a string like
* 'contribpage' for an older class like CRM_Contribute_ActionMapping_ByPage of for ones following
* more recent patterns, an integer.
* @param string $now
* @param array $params
*
* @throws API_Exception
* @throws \CRM_Core_Exception
*/
public static function buildRecipientContacts($mappingID, $now, $params = []) {
public static function buildRecipientContacts(string $mappingID, $now, $params = []) {
$actionSchedule = new CRM_Core_DAO_ActionSchedule();

$actionSchedule->mapping_id = $mappingID;
$actionSchedule->is_active = 1;
if (!empty($params)) {
Expand All @@ -348,25 +356,22 @@ public static function buildRecipientContacts($mappingID, $now, $params = []) {
}

/**
* @param null $now
* Main processing callback for sending out scheduled reminders.
*
* @param string $now
* @param array $params
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public static function processQueue($now = NULL, $params = []) {
$now = $now ? CRM_Utils_Time::setTime($now) : CRM_Utils_Time::getTime();

$mappings = CRM_Core_BAO_ActionSchedule::getMappings();
foreach ($mappings as $mappingID => $mapping) {
CRM_Core_BAO_ActionSchedule::buildRecipientContacts($mappingID, $now, $params);
CRM_Core_BAO_ActionSchedule::buildRecipientContacts((string) $mappingID, $now, $params);
CRM_Core_BAO_ActionSchedule::sendMailings($mappingID, $now);
}

$result = [
'is_error' => 0,
'messages' => ts('Sent all scheduled reminders successfully'),
];
return $result;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CRM_Utils_Time {
* @param string $returnFormat
* Format in which date is to be retrieved.
*
* @return date
* @return string
*/
public static function getTime($returnFormat = 'YmdHis') {
return date($returnFormat, self::getTimeRaw());
Expand All @@ -56,7 +56,7 @@ public static function getTimeRaw() {
* @param string $returnFormat
* Format in which date is to be retrieved.
*
* @return date
* @return string
*/
public static function setTime($newDateTime, $returnFormat = 'YmdHis') {
self::$_delta = strtotime($newDateTime) - time();
Expand Down
2 changes: 0 additions & 2 deletions Civi/Api4/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
* $Id$
*
*/


Expand Down
22 changes: 9 additions & 13 deletions api/v3/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ function _civicrm_api3_job_geocode_spec(&$params) {
}

/**
* Send the scheduled reminders for all contacts (either for activities or events).
* Send the scheduled reminders as configured.
*
* @param array $params
* (reference ) input parameters.
* now - the time to use, in YmdHis format
* - makes testing a bit simpler since we can simulate past/future time
* - now - the time to use, in YmdHis format
* - makes testing a bit simpler since we can simulate past/future time
*
* @return array
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_job_send_reminder($params) {
//note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
Expand All @@ -199,18 +201,12 @@ function civicrm_api3_job_send_reminder($params) {
$params['rowCount'] = 0;
$lock = Civi::lockManager()->acquire('worker.core.ActionSchedule');
if (!$lock->isAcquired()) {
return civicrm_api3_create_error('Could not acquire lock, another ActionSchedule process is running');
throw new API_Exception('Could not acquire lock, another ActionSchedule process is running');
}

$result = CRM_Core_BAO_ActionSchedule::processQueue(CRM_Utils_Array::value('now', $params), $params);
CRM_Core_BAO_ActionSchedule::processQueue($params['now'] ?? NULL, $params);
$lock->release();

if ($result['is_error'] == 0) {
return civicrm_api3_create_success();
}
else {
return civicrm_api3_create_error($result['messages']);
}
return civicrm_api3_create_success(1, $params, 'ActionSchedule', 'send_reminder');
}

/**
Expand Down