File tree Expand file tree Collapse file tree 3 files changed +34
-15
lines changed
Expand file tree Collapse file tree 3 files changed +34
-15
lines changed Original file line number Diff line number Diff line change 1717
1818
1919def init_pipeline_scheduled ():
20- """Initialize schedule jobs for active PipelineSchedule."""
21- active_pipeline_qs = models .PipelineSchedule .objects .filter (is_active = True ).order_by (
22- "created_date"
23- )
24- for pipeline_schedule in active_pipeline_qs :
25- if scheduled_job_exists (pipeline_schedule .schedule_work_id ):
26- continue
27- new_id = pipeline_schedule .create_new_job ()
28- pipeline_schedule .schedule_work_id = new_id
29- pipeline_schedule .save (update_fields = ["schedule_work_id" ])
20+ """
21+ Initialize schedule jobs for active PipelineSchedule.
22+ - Create new schedule if there is no schedule for active pipeline
23+ - Create new schedule if schedule is corrupted for an active pipeline
24+ - Delete schedule for inactive pipeline
25+ """
26+ pipeline_qs = models .PipelineSchedule .objects .order_by ("created_date" )
27+ for pipeline in pipeline_qs :
28+ reset_schedule = pipeline .is_active != bool (pipeline .schedule_work_id )
29+ if not scheduled_job_exists (pipeline .schedule_work_id ):
30+ reset_schedule = True
31+
32+ if reset_schedule :
33+ pipeline .schedule_work_id = pipeline .create_new_job ()
34+ pipeline .save (update_fields = ["schedule_work_id" ])
3035
3136
3237class Command (rqscheduler .Command ):
Original file line number Diff line number Diff line change @@ -89,7 +89,15 @@ def update_pipeline_schedule():
8989 from vulnerabilities .improvers import IMPROVERS_REGISTRY
9090 from vulnerabilities .models import PipelineSchedule
9191
92- pipeline_ids = [* IMPORTERS_REGISTRY .keys (), * IMPROVERS_REGISTRY .keys ()]
93-
94- PipelineSchedule .objects .exclude (pipeline_id__in = pipeline_ids ).delete ()
95- [PipelineSchedule .objects .get_or_create (pipeline_id = id ) for id in pipeline_ids ]
92+ pipelines = IMPORTERS_REGISTRY | IMPROVERS_REGISTRY
93+
94+ PipelineSchedule .objects .exclude (pipeline_id__in = pipelines .keys ()).delete ()
95+ for id , pipeline_class in pipelines .items ():
96+ run_once = getattr (pipeline_class , "run_once" , False )
97+
98+ PipelineSchedule .objects .get_or_create (
99+ pipeline_id = id ,
100+ defaults = {
101+ "is_run_once" : run_once ,
102+ },
103+ )
Original file line number Diff line number Diff line change 1717from vulnerabilities import models
1818from vulnerabilities .importer import Importer
1919from vulnerabilities .improver import Improver
20- from vulnerablecode .settings import VULNERABLECODE_PIPELINE_TIMEOUT
2120
2221logger = logging .getLogger (__name__ )
2322
@@ -48,6 +47,13 @@ def execute_pipeline(pipeline_id, run_id):
4847 exitcode = 1
4948
5049 run .set_run_ended (exitcode = exitcode , output = output )
50+
51+ # Onetime pipeline are inactive after first execution.
52+ pipeline = run .pipeline
53+ if pipeline .is_run_once :
54+ pipeline .is_active = False
55+ pipeline .save ()
56+
5157 logger .info ("Update Run instance with exitcode, output, and end_date" )
5258
5359
You can’t perform that action at this time.
0 commit comments