From e8f618133bcddc97787e6d8d5aa5c72f417c64a3 Mon Sep 17 00:00:00 2001 From: David Reedy Jr Date: Mon, 28 Dec 2015 12:20:09 -0600 Subject: [PATCH] CRM-17686 Add field to scheduled job form... ...to specify first/next run --- CRM/Admin/Form/Job.php | 41 +++++++++++++++++++ .../Incremental/sql/4.7.beta5.mysql.tpl | 4 ++ templates/CRM/Admin/Form/Job.tpl | 9 ++++ xml/schema/Core/Job.xml | 7 ++++ 4 files changed, 61 insertions(+) diff --git a/CRM/Admin/Form/Job.php b/CRM/Admin/Form/Job.php index 013a564ce6..549d07855c 100644 --- a/CRM/Admin/Form/Job.php +++ b/CRM/Admin/Form/Job.php @@ -96,6 +96,9 @@ class CRM_Admin_Form_Job extends CRM_Admin_Form { $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency()); + // CRM-17686 + $this->addDateTime('next_scheduled_run', ts(($this->_id ? 'Next' : 'First') . ' Run Date / Time'), FALSE, array('formatType' => 'activityDateTime')); + $this->add('textarea', 'parameters', ts('Command parameters'), "cols=50 rows=6" ); @@ -156,6 +159,13 @@ class CRM_Admin_Form_Job extends CRM_Admin_Form { CRM_Core_DAO::storeValues($dao, $defaults); + // CRM-17686 + if (!empty($dao->next_scheduled_run)) { + $ts = strtotime($dao->next_scheduled_run); + $defaults['next_scheduled_run'] = date('m/d/Y', $ts); + $defaults['next_scheduled_run_time'] = date('h:iA', $ts); + } + // CRM-10708 // job entity thats shipped with core is all lower case. // this makes sure camel casing is followed for proper working of default population. @@ -194,6 +204,37 @@ class CRM_Admin_Form_Job extends CRM_Admin_Form { $dao->description = $values['description']; $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0); + // CRM-17686 + $dt = ''; + if (!empty($values['next_scheduled_run'])) { + $dt = $values['next_scheduled_run'] . ' ';; + } + if (!empty($values['next_scheduled_run_time'])) { + $dt .= $values['next_scheduled_run_time']; + } + $ts = strtotime(trim($dt)); + // if a date/time is supplied and not in the past, then set the next scheduled run... + if ($ts > time()) { + $dao->next_scheduled_run = CRM_Utils_Date::currentDBDate($ts); + // warn about monthly/quarterly scheduling, if applicable + if (($dao->run_frequency == 'Monthly') || ($dao->run_frequency == 'Quarter')) { + $info = getdate($ts); + if ($info['mday'] > 28) { + CRM_Core_Session::setStatus( + ts('Relative month values are calculated based on the length of month(s) that they pass through. + The result will land on the same day of the month except for days 29-31 when the target month contains fewer days. + For example, if a job is scheduled to run on August 31st, the following invocation will occur on October 1st. + To avoid this issue, please schedule Monthly and Quarterly jobs to run within the first 28 days of the month.'), + ts('Warning'), 'info', array('expires' => 0)); + } + } + } + // ...otherwise, if this isn't a new scheduled job, clear the next scheduled run + elseif ($this->_id) { + $job = new CRM_Core_ScheduledJob(array('id' => $dao->id)); + $job->clearNextScheduledRun(); + } + $dao->save(); // CRM-11143 - Give warning message if update_greetings is Enabled (is_active) since it generally should not be run automatically via execute action or runjobs url. diff --git a/CRM/Upgrade/Incremental/sql/4.7.beta5.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.beta5.mysql.tpl index a97a616157..99af7aeb51 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.beta5.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.beta5.mysql.tpl @@ -1 +1,5 @@ {* file to handle db changes in 4.7.beta5 during upgrade *} + +-- CRM-17686 +ALTER TABLE `civicrm_job` +ADD COLUMN `next_scheduled_run` timestamp NULL DEFAULT NULL COMMENT 'When is this cron entry scheduled to run next' AFTER `last_run`; diff --git a/templates/CRM/Admin/Form/Job.tpl b/templates/CRM/Admin/Form/Job.tpl index 00092b408f..2521de7859 100644 --- a/templates/CRM/Admin/Form/Job.tpl +++ b/templates/CRM/Admin/Form/Job.tpl @@ -105,6 +105,15 @@ CRM.$(function($) { {$form.parameters.label}
{docURL page="Managing Scheduled Jobs" resource="wiki"} {$form.parameters.html} + + {$form.next_scheduled_run.label} + + {if $hideCalender neq true}{include file="CRM/common/jcalendar.tpl" elementName=next_scheduled_run}{else}{$next_scheduled_run|crmDate}{/if}
+
{ts}Do not run this job before this date / time. + Leave blank to run {if $action eq 1}as soon as possible{else}at next scheduled interval{/if}.{/ts} +
+ + {$form.is_active.html} {$form.is_active.label} diff --git a/xml/schema/Core/Job.xml b/xml/schema/Core/Job.xml index b897731cbf..1abeeb8e0f 100644 --- a/xml/schema/Core/Job.xml +++ b/xml/schema/Core/Job.xml @@ -60,6 +60,13 @@ When was this cron entry last run 4.1 + + next_scheduled_run + timestamp + NULL + When is this cron entry scheduled to run next + 4.7 + name Job Name -- 2.25.1