22# @author Iván Todorovich <ivan.todorovich@camptocamp.com>
33# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44
5- from datetime import datetime
5+ from datetime import datetime , timedelta
66from unittest import mock
77
88from freezegun import freeze_time
@@ -37,12 +37,58 @@ def test_queue_job_process(self):
3737 job3 = self .env ["res.partner" ].with_delay (eta = 3600 ).create ({"name" : "Test" })
3838 job3_record = job3 .db_record ()
3939 # Run the job processing cron
40+ self .cron .nextcall = datetime .now () + timedelta (seconds = 600 )
4041 self .env ["queue.job" ]._job_runner (commit = False )
4142 # Check that the jobs were processed
4243 self .assertEqual (job1_record .state , "done" , "Processed OK" )
4344 self .assertEqual (job2_record .state , "failed" , "Has errors" )
4445 self .assertEqual (job3_record .state , "pending" , "Still pending, because of eta" )
4546
47+ def test_stop_processing_job (self ):
48+ # nextcall is the theorical start time of the current cron
49+ self .cron .nextcall = datetime (2022 , 2 , 22 , 22 , 20 , 22 )
50+ self .cron .interval_number = 2
51+ self .cron .interval_type = "minutes"
52+ self .env ["ir.config_parameter" ].set_param (
53+ "queue_job_cron_jobrunner.stop_processing_threshold_seconds" , "60"
54+ )
55+ job1 = self .env ["res.partner" ].with_delay ().create ({"name" : "test" })
56+ job1_record = job1 .db_record ()
57+ job2 = self .env ["res.partner" ].with_delay ().create ({"name" : "Test" })
58+ job2_record = job2 .db_record ()
59+ with freeze_time ("2022-02-22 22:21:23" ):
60+ # theorical nextcall is 2022-02-22 22:20:22 + 2' => 22:22:22
61+ # no queue job should be started after 22:22:22 - 60" => 22:21:22
62+ self .env ["queue.job" ]._job_runner (commit = False )
63+ self .assertEqual (job1_record .state , "done" , "Processed OK" )
64+ self .assertEqual (job2_record .state , "pending" , "no time left to start it" )
65+
66+ def test_stop_processing_multiple_jobs (self ):
67+ cron2 = self .cron .copy ()
68+ # why not negative thersholds meaning we stop after time
69+ # is already over for the nextcall
70+ self .env ["ir.config_parameter" ].set_param (
71+ "queue_job_cron_jobrunner.stop_processing_threshold_seconds" , "-10"
72+ )
73+
74+ self .cron .nextcall = datetime (2022 , 2 , 22 , 22 , 22 , 22 )
75+ self .cron .interval_number = 2
76+ self .cron .interval_type = "minutes"
77+
78+ cron2 .nextcall = datetime (2022 , 2 , 22 , 22 , 21 , 22 )
79+ cron2 .interval_number = 2
80+ cron2 .interval_type = "minutes"
81+
82+ # cron1 not after 22:24:32
83+ # cron2 not after 22:23:32
84+
85+ with freeze_time ("2022-02-22 22:23:32" ):
86+ self .assertTrue (self .env ["queue.job" ]._stop_processing ())
87+
88+ def test_stop_processing_inactive_cron_stop_processing (self ):
89+ self .cron .active = False
90+ self .assertTrue (self .env ["queue.job" ]._stop_processing ())
91+
4692 @freeze_time ("2022-02-22 22:22:22" )
4793 def test_queue_job_cron_trigger_enqueue_dependencies (self ):
4894 """Test that ir.cron execution enqueue waiting dependencies"""
@@ -53,6 +99,7 @@ def test_queue_job_cron_trigger_enqueue_dependencies(self):
5399 job_record = delayable ._generated_job .db_record ()
54100 job_record_depends = delayable2 ._generated_job .db_record ()
55101
102+ self .cron .nextcall = datetime (2022 , 2 , 22 , 23 , 23 , 23 )
56103 self .env ["queue.job" ]._job_runner (commit = False )
57104
58105 self .assertEqual (job_record .state , "done" , "Processed OK" )
0 commit comments