mirror of https://github.com/portainer/portainer
fix(scheduler): remove unnecessary goroutines BE-11044 (#12059)
parent
03e8d05f18
commit
3b5ce1b053
|
@ -94,7 +94,12 @@ func (s *Scheduler) StopJob(jobID string) error {
|
||||||
// Returns job id that could be used to stop the given job.
|
// Returns job id that could be used to stop the given job.
|
||||||
// When job run returns an error, that job won't be run again.
|
// When job run returns an error, that job won't be run again.
|
||||||
func (s *Scheduler) StartJobEvery(duration time.Duration, job func() error) string {
|
func (s *Scheduler) StartJobEvery(duration time.Duration, job func() error) string {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
entryID := new(cron.EntryID)
|
||||||
|
|
||||||
|
cancelFn := func() {
|
||||||
|
log.Debug().Msg("job cancelled, stopping")
|
||||||
|
s.crontab.Remove(*entryID)
|
||||||
|
}
|
||||||
|
|
||||||
jobFn := cron.FuncJob(func() {
|
jobFn := cron.FuncJob(func() {
|
||||||
err := job()
|
err := job()
|
||||||
|
@ -105,7 +110,7 @@ func (s *Scheduler) StartJobEvery(duration time.Duration, job func() error) stri
|
||||||
var permErr *PermanentError
|
var permErr *PermanentError
|
||||||
if errors.As(err, &permErr) {
|
if errors.As(err, &permErr) {
|
||||||
log.Error().Err(permErr).Msg("job returned a permanent error, it will be stopped")
|
log.Error().Err(permErr).Msg("job returned a permanent error, it will be stopped")
|
||||||
cancel()
|
cancelFn()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -113,17 +118,11 @@ func (s *Scheduler) StartJobEvery(duration time.Duration, job func() error) stri
|
||||||
log.Error().Err(err).Msg("job returned an error, it will be rescheduled")
|
log.Error().Err(err).Msg("job returned an error, it will be rescheduled")
|
||||||
})
|
})
|
||||||
|
|
||||||
entryID := s.crontab.Schedule(cron.Every(duration), jobFn)
|
*entryID = s.crontab.Schedule(cron.Every(duration), jobFn)
|
||||||
|
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.activeJobs[entryID] = cancel
|
s.activeJobs[*entryID] = cancelFn
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
go func(entryID cron.EntryID) {
|
return strconv.Itoa(int(*entryID))
|
||||||
<-ctx.Done()
|
|
||||||
log.Debug().Msg("job cancelled, stopping")
|
|
||||||
s.crontab.Remove(entryID)
|
|
||||||
}(entryID)
|
|
||||||
|
|
||||||
return strconv.Itoa(int(entryID))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue