mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 16:26:38 +00:00
simplify isRunning
This commit is contained in:
@@ -46,17 +46,15 @@ func (mock *MockScheduler) IsJobRunning(_ string) bool {
|
|||||||
// DefaultScheduler is a generic structure that allows to schedule jobs (functions) to run in the future and cancel them.
|
// DefaultScheduler is a generic structure that allows to schedule jobs (functions) to run in the future and cancel them.
|
||||||
type DefaultScheduler struct {
|
type DefaultScheduler struct {
|
||||||
// jobs map holds cancellation channels indexed by the job ID
|
// jobs map holds cancellation channels indexed by the job ID
|
||||||
jobs map[string]chan struct{}
|
jobs map[string]chan struct{}
|
||||||
mu *sync.Mutex
|
mu *sync.Mutex
|
||||||
isJobRunning map[string]bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDefaultScheduler creates an instance of a DefaultScheduler
|
// NewDefaultScheduler creates an instance of a DefaultScheduler
|
||||||
func NewDefaultScheduler() *DefaultScheduler {
|
func NewDefaultScheduler() *DefaultScheduler {
|
||||||
return &DefaultScheduler{
|
return &DefaultScheduler{
|
||||||
jobs: make(map[string]chan struct{}),
|
jobs: make(map[string]chan struct{}),
|
||||||
isJobRunning: make(map[string]bool),
|
mu: &sync.Mutex{},
|
||||||
mu: &sync.Mutex{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +92,6 @@ func (wm *DefaultScheduler) Schedule(ctx context.Context, in time.Duration, ID s
|
|||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(in)
|
ticker := time.NewTicker(in)
|
||||||
wm.isJobRunning[ID] = true
|
|
||||||
|
|
||||||
wm.jobs[ID] = cancel
|
wm.jobs[ID] = cancel
|
||||||
log.WithContext(ctx).Debugf("scheduled a job %s to run in %s. There are %d total jobs scheduled.", ID, in.String(), len(wm.jobs))
|
log.WithContext(ctx).Debugf("scheduled a job %s to run in %s. There are %d total jobs scheduled.", ID, in.String(), len(wm.jobs))
|
||||||
@@ -106,7 +103,6 @@ func (wm *DefaultScheduler) Schedule(ctx context.Context, in time.Duration, ID s
|
|||||||
case <-cancel:
|
case <-cancel:
|
||||||
log.WithContext(ctx).Debugf("scheduled job %s was canceled, stop timer", ID)
|
log.WithContext(ctx).Debugf("scheduled job %s was canceled, stop timer", ID)
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
wm.isJobRunning[ID] = false
|
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
log.WithContext(ctx).Debugf("time to do a scheduled job %s", ID)
|
log.WithContext(ctx).Debugf("time to do a scheduled job %s", ID)
|
||||||
@@ -118,7 +114,6 @@ func (wm *DefaultScheduler) Schedule(ctx context.Context, in time.Duration, ID s
|
|||||||
delete(wm.jobs, ID)
|
delete(wm.jobs, ID)
|
||||||
log.WithContext(ctx).Debugf("job %s is not scheduled to run again", ID)
|
log.WithContext(ctx).Debugf("job %s is not scheduled to run again", ID)
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
wm.isJobRunning[ID] = false
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// we need this comparison to avoid resetting the ticker with the same duration and missing the current elapsesed time
|
// we need this comparison to avoid resetting the ticker with the same duration and missing the current elapsesed time
|
||||||
@@ -136,5 +131,5 @@ func (wm *DefaultScheduler) Schedule(ctx context.Context, in time.Duration, ID s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wm *DefaultScheduler) IsJobRunning(ID string) bool {
|
func (wm *DefaultScheduler) IsJobRunning(ID string) bool {
|
||||||
return wm.isJobRunning[ID]
|
return wm.jobs[ID] != nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user