mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 16:26:38 +00:00
[management] scheduler cancel all jobs (#4158)
This commit is contained in:
@@ -11,14 +11,17 @@ import (
|
||||
// Scheduler is an interface which implementations can schedule and cancel jobs
|
||||
type Scheduler interface {
|
||||
Cancel(ctx context.Context, IDs []string)
|
||||
CancelAll(ctx context.Context)
|
||||
Schedule(ctx context.Context, in time.Duration, ID string, job func() (nextRunIn time.Duration, reschedule bool))
|
||||
IsSchedulerRunning(ID string) bool
|
||||
}
|
||||
|
||||
// MockScheduler is a mock implementation of Scheduler
|
||||
type MockScheduler struct {
|
||||
CancelFunc func(ctx context.Context, IDs []string)
|
||||
ScheduleFunc func(ctx context.Context, in time.Duration, ID string, job func() (nextRunIn time.Duration, reschedule bool))
|
||||
CancelFunc func(ctx context.Context, IDs []string)
|
||||
CancelAllFunc func(ctx context.Context)
|
||||
ScheduleFunc func(ctx context.Context, in time.Duration, ID string, job func() (nextRunIn time.Duration, reschedule bool))
|
||||
IsSchedulerRunningFunc func(ID string) bool
|
||||
}
|
||||
|
||||
// Cancel mocks the Cancel function of the Scheduler interface
|
||||
@@ -30,6 +33,15 @@ func (mock *MockScheduler) Cancel(ctx context.Context, IDs []string) {
|
||||
log.WithContext(ctx).Warnf("MockScheduler doesn't have Cancel function defined ")
|
||||
}
|
||||
|
||||
// CancelAll mocks the CancelAll function of the Scheduler interface
|
||||
func (mock *MockScheduler) CancelAll(ctx context.Context) {
|
||||
if mock.CancelAllFunc != nil {
|
||||
mock.CancelAllFunc(ctx)
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).Warnf("MockScheduler doesn't have CancelAll function defined ")
|
||||
}
|
||||
|
||||
// Schedule mocks the Schedule function of the Scheduler interface
|
||||
func (mock *MockScheduler) Schedule(ctx context.Context, in time.Duration, ID string, job func() (nextRunIn time.Duration, reschedule bool)) {
|
||||
if mock.ScheduleFunc != nil {
|
||||
@@ -40,7 +52,9 @@ func (mock *MockScheduler) Schedule(ctx context.Context, in time.Duration, ID st
|
||||
}
|
||||
|
||||
func (mock *MockScheduler) IsSchedulerRunning(ID string) bool {
|
||||
// MockScheduler does not implement IsSchedulerRunning, so we return false
|
||||
if mock.IsSchedulerRunningFunc != nil {
|
||||
return mock.IsSchedulerRunningFunc(ID)
|
||||
}
|
||||
log.Warnf("MockScheduler doesn't have IsSchedulerRunning function defined")
|
||||
return false
|
||||
}
|
||||
@@ -52,6 +66,15 @@ type DefaultScheduler struct {
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func (wm *DefaultScheduler) CancelAll(ctx context.Context) {
|
||||
wm.mu.Lock()
|
||||
defer wm.mu.Unlock()
|
||||
|
||||
for id := range wm.jobs {
|
||||
wm.cancel(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
// NewDefaultScheduler creates an instance of a DefaultScheduler
|
||||
func NewDefaultScheduler() *DefaultScheduler {
|
||||
return &DefaultScheduler{
|
||||
|
||||
Reference in New Issue
Block a user