diff --git a/mistral/services/periodic.py b/mistral/services/periodic.py index d5e2b660b..b10ea50a0 100644 --- a/mistral/services/periodic.py +++ b/mistral/services/periodic.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import json from oslo_config import cfg @@ -121,9 +122,11 @@ def advance_cron_trigger(t): delete_trust=False ) else: # if remaining execution = None or > 0. + # In case the we are lagging or if the api stopped for some time + # we use the max of the current time or the next scheduled time. next_time = triggers.get_next_execution_time( t.pattern, - t.next_execution_time + max(datetime.datetime.utcnow(), t.next_execution_time) ) # Update the cron trigger with next execution details diff --git a/mistral/tests/unit/engine/test_cron_trigger.py b/mistral/tests/unit/engine/test_cron_trigger.py index 9089ac65b..5791e0818 100644 --- a/mistral/tests/unit/engine/test_cron_trigger.py +++ b/mistral/tests/unit/engine/test_cron_trigger.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import datetime +import time import mock from oslo_config import cfg @@ -120,7 +121,9 @@ class ProcessCronTriggerTest(base.EngineTestCase): next_trigger = next_triggers[0] next_execution_time_before = next_trigger.next_execution_time + ts_before = datetime.datetime.utcnow() + time.sleep(1) # this is to simulate lagging periodic.process_cron_triggers_v2(None, None) next_triggers = triggers.get_next_cron_triggers() @@ -130,6 +133,11 @@ class ProcessCronTriggerTest(base.EngineTestCase): next_trigger = next_triggers[0] next_execution_time_after = next_trigger.next_execution_time + self.assertGreater( + next_execution_time_after, + ts_before + ) + self.assertNotEqual( next_execution_time_before, next_execution_time_after