Skip to content

Commit

Permalink
[REF] Minor cleanup around actionn schedule code.
Browse files Browse the repository at this point in the history
CRM_Core_BAO_ActionSchedule::processQueue is only called from one place. It either succeeds or throws
an exception so there is no value in returning anything & the parsing of what it does return assumes that it
could return false

Some slight tightenig around typing as buildRecipientContact is also only called from one place
  • Loading branch information
eileenmcnaughton committed Apr 23, 2020
1 parent 6bcf9fe commit 84f6779
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
31 changes: 19 additions & 12 deletions CRM/Core/BAO/ActionSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule {
/**
* @param array $filters
* Filter by property (e.g. 'id').
*
* @return array
* Array(scalar $id => Mapping $mapping).
*
* @throws \CRM_Core_Exception
*/
public static function getMappings($filters = NULL) {
static $_action_mapping;
Expand Down Expand Up @@ -320,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 @@ -346,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

0 comments on commit 84f6779

Please sign in to comment.