mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-06 15:46:37 +08:00
fix(drivers): propagate app-lifetime context through inproc cron/worker drivers
cron 触发与 worker 执行的任务现在继承应用生命周期 context(关闭时级联取消,带超时子上下文), 替代裸 context.Background()。contextcheck 清零。 lint_issues 26→24
This commit is contained in:
@@ -56,8 +56,9 @@ func (p *Plugin) Type() core.DriverType {
|
||||
return core.DriverTypeScheduler
|
||||
}
|
||||
|
||||
// Start boots the in-process cron scheduler.
|
||||
func (p *Plugin) Start(_ context.Context) error {
|
||||
// Start boots the in-process cron scheduler. ctx is the app-lifetime context;
|
||||
// cron-dispatched tasks carry it so cancellation propagates on shutdown.
|
||||
func (p *Plugin) Start(ctx context.Context) error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
@@ -66,7 +67,7 @@ func (p *Plugin) Start(_ context.Context) error {
|
||||
p.scheduler = newInprocScheduler(p.coreCtx.Schedules(), p.coreCtx.Tasks(), taskSvc)
|
||||
}
|
||||
|
||||
return p.scheduler.Start()
|
||||
return p.scheduler.Start(ctx)
|
||||
}
|
||||
|
||||
// Stop terminates the in-process cron scheduler.
|
||||
|
||||
@@ -36,7 +36,11 @@ func newInprocScheduler(scheduleReg extpoints.ScheduleExtension, taskReg extpoin
|
||||
}
|
||||
}
|
||||
|
||||
func (s *inprocScheduler) Start() error {
|
||||
func (s *inprocScheduler) Start(ctx context.Context) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
@@ -46,7 +50,7 @@ func (s *inprocScheduler) Start() error {
|
||||
|
||||
if s.scheduleReg != nil {
|
||||
for _, def := range s.scheduleReg.Schedules() {
|
||||
s.registerJob(def)
|
||||
s.registerJob(ctx, def)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +74,7 @@ func (s *inprocScheduler) Stop() {
|
||||
|
||||
const standardCronFields = 5
|
||||
|
||||
func (s *inprocScheduler) registerJob(def extpoints.ScheduleDefinition) {
|
||||
func (s *inprocScheduler) registerJob(ctx context.Context, def extpoints.ScheduleDefinition) {
|
||||
spec := def.Spec
|
||||
taskType := def.TaskType
|
||||
|
||||
@@ -94,8 +98,8 @@ func (s *inprocScheduler) registerJob(def extpoints.ScheduleDefinition) {
|
||||
|
||||
_, err := s.cronRunner.AddFunc(cronSpec, func() {
|
||||
if s.taskSvc != nil {
|
||||
if _, dispatchErr := s.taskSvc.Dispatch(context.Background(), taskType, payloadBytes, "inproc_cron"); dispatchErr != nil {
|
||||
logger.ErrorF(context.Background(), "driver_inproc_cron: dispatch task %q failed: %v", taskType, dispatchErr)
|
||||
if _, dispatchErr := s.taskSvc.Dispatch(ctx, taskType, payloadBytes, "inproc_cron"); dispatchErr != nil {
|
||||
logger.ErrorF(ctx, "driver_inproc_cron: dispatch task %q failed: %v", taskType, dispatchErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -107,15 +111,15 @@ func (s *inprocScheduler) registerJob(def extpoints.ScheduleDefinition) {
|
||||
if timeout <= 0 {
|
||||
timeout = 5 * time.Minute
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
runCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
_ = invokeHandler(ctx, td.Handler, payloadBytes)
|
||||
_ = invokeHandler(runCtx, td.Handler, payloadBytes)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
logger.ErrorF(context.Background(), "driver_inproc_cron: invalid cron spec %q for task %q: %v", spec, taskType, err)
|
||||
logger.ErrorF(ctx, "driver_inproc_cron: invalid cron spec %q for task %q: %v", spec, taskType, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user