@@ -28,28 +28,36 @@ type Manager struct {
2828 cron * cron.Cron
2929 cronEntries map [string ]map [string ]cron.EntryID
3030 executor Executor
31+ execCtx context.Context
32+ execCancel context.CancelFunc
3133}
3234
33- func New (logger * zap.SugaredLogger , executor Executor , proModeEnabled bool ) Manager {
34- return Manager {
35+ func New (logger * zap.SugaredLogger , executor Executor , proModeEnabled bool ) * Manager {
36+ ctx , cancel := context .WithCancel (context .Background ())
37+ return & Manager {
3538 proModeEnabled : proModeEnabled ,
3639 logger : logger ,
3740 cron : cron .New (),
3841 cronEntries : make (map [string ]map [string ]cron.EntryID ),
3942 executor : executor ,
43+ execCtx : ctx ,
44+ execCancel : cancel ,
4045 }
4146}
4247
4348// Start the cron manager in its own goroutine, or no-op if already started.
44- func (m Manager ) Start () {
49+ func (m * Manager ) Start () {
4550 m .logger .Infow ("cron manager starting" )
51+ m .execCancel ()
52+ m .execCtx , m .execCancel = context .WithCancel (context .Background ())
4653 m .cron .Start ()
4754 m .logger .Infow ("cron manager started" )
4855}
4956
5057// Stop stops the cron manager if it is running; otherwise it does nothing.
51- func (m Manager ) Stop () {
58+ func (m * Manager ) Stop () {
5259 m .logger .Infow ("cron manager stopping" )
60+ m .execCancel ()
5361 m .cron .Stop ()
5462 m .logger .Infow ("cron manager stopped" )
5563}
@@ -62,7 +70,7 @@ func cronSpec(config testkube.TestWorkflowCronJobConfig) string {
6270 return spec
6371}
6472
65- func (m Manager ) ReplaceWorkflowSchedules (ctx context.Context , workflow cronjob.Workflow , configs []testkube.TestWorkflowCronJobConfig ) error {
73+ func (m * Manager ) ReplaceWorkflowSchedules (ctx context.Context , workflow cronjob.Workflow , configs []testkube.TestWorkflowCronJobConfig ) error {
6674 log := m .logger .With ("workflow" , workflow .Name )
6775 // Delete all existing schedules for this workflow.
6876 // This is because we may not know when a schedule is removed from
@@ -93,7 +101,7 @@ func (m Manager) ReplaceWorkflowSchedules(ctx context.Context, workflow cronjob.
93101 "cron" , config .Cron ,
94102 )
95103 }
96- entryId , err := m .cron .AddJob (spec , m .testWorkflowExecuteJob (ctx , workflow .Name , spec , config ))
104+ entryId , err := m .cron .AddJob (spec , m .testWorkflowExecuteJob (workflow .Name , spec , config ))
97105 if err != nil {
98106 m .logger .Errorw ("Error adding cron for workflow, continuing processing" ,
99107 "cron" , spec ,
@@ -115,8 +123,9 @@ func (m Manager) ReplaceWorkflowSchedules(ctx context.Context, workflow cronjob.
115123 return nil
116124}
117125
118- func (m Manager ) testWorkflowExecuteJob (ctx context. Context , workflow , cronSpec string , config testkube.TestWorkflowCronJobConfig ) cron.FuncJob {
126+ func (m * Manager ) testWorkflowExecuteJob (workflow , cronSpec string , config testkube.TestWorkflowCronJobConfig ) cron.FuncJob {
119127 return cron .FuncJob (func () {
128+ execCtx := m .execCtx
120129 var targets []* cloud.ExecutionTarget
121130 if config .Target != nil {
122131 targets = commonmapper .MapAllTargetsApiToGrpc ([]testkube.ExecutionTarget {* config .Target })
@@ -141,7 +150,7 @@ func (m Manager) testWorkflowExecuteJob(ctx context.Context, workflow, cronSpec
141150 )
142151 log .Info ("executing scheduled workflow" )
143152
144- results , err := m .executor .Execute (ctx , request )
153+ results , err := m .executor .Execute (execCtx , request )
145154 if err != nil {
146155 log .Errorw ("unable to execute scheduled workflow" ,
147156 "error" , err )
0 commit comments