-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clear entries on start * Do not init the Cron more than once * Wait for jobs to finish on graceful stop * Fix test * Reuse inner cron.Stop() method comment * ClearCron needs to empty the map and set proper metrics * Add test for ClearCron * Remove the usage of EntryJobMap Avoid to maintain a parallel map for job-ids. Refactor it to EntryJob struct containing the ID and Job for the entry. * Remove what's in the Cron anyway * Remove unnecessary continues * Add comments on scheduler stop to clarify design decision
- Loading branch information
Victor Castell
authored
Jun 2, 2022
1 parent
6d6c2d7
commit 5f4a31e
Showing
6 changed files
with
119 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package dkron | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/distribworks/dkron/v3/extcron" | ||
"github.com/robfig/cron/v3" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
@@ -28,8 +31,8 @@ func TestSchedule(t *testing.T) { | |
assert.True(t, sched.Started()) | ||
now := time.Now().Truncate(time.Second) | ||
|
||
entry, _ := sched.GetEntry(testJob1.Name) | ||
assert.Equal(t, now.Add(time.Second*2), entry.Next) | ||
ej, _ := sched.GetEntryJob(testJob1.Name) | ||
assert.Equal(t, now.Add(time.Second*2), ej.entry.Next) | ||
|
||
testJob2 := &Job{ | ||
Name: "cron_job", | ||
|
@@ -43,12 +46,27 @@ func TestSchedule(t *testing.T) { | |
|
||
assert.True(t, sched.started) | ||
assert.True(t, sched.Started()) | ||
|
||
sched.Stop() | ||
} | ||
|
||
func TestClearCron(t *testing.T) { | ||
log := getTestLogger() | ||
sched := NewScheduler(log) | ||
|
||
testJob := &Job{ | ||
Name: "cron_job", | ||
Schedule: "@every 2s", | ||
Executor: "shell", | ||
ExecutorConfig: map[string]string{"command": "echo 'test1'", "shell": "true"}, | ||
Owner: "John Dough", | ||
OwnerEmail: "[email protected]", | ||
} | ||
sched.AddJob(testJob) | ||
assert.Len(t, sched.Cron.Entries(), 1) | ||
|
||
sched.Cron.Remove(1) | ||
sched.ClearCron() | ||
assert.Len(t, sched.Cron.Entries(), 0) | ||
|
||
sched.Stop() | ||
} | ||
|
||
func TestTimezoneAwareJob(t *testing.T) { | ||
|
@@ -69,3 +87,36 @@ func TestTimezoneAwareJob(t *testing.T) { | |
assert.Len(t, sched.Cron.Entries(), 1) | ||
sched.Stop() | ||
} | ||
|
||
func TestScheduleStop(t *testing.T) { | ||
log := getTestLogger() | ||
sched := NewScheduler(log) | ||
|
||
sched.Cron = cron.New(cron.WithParser(extcron.NewParser())) | ||
sched.Cron.AddFunc("@every 2s", func() { | ||
time.Sleep(time.Second * 5) | ||
fmt.Println("function done") | ||
}) | ||
sched.Cron.Start() | ||
sched.started = true | ||
|
||
testJob1 := &Job{ | ||
Name: "cron_job", | ||
Schedule: "@every 2s", | ||
Executor: "shell", | ||
ExecutorConfig: map[string]string{"command": "echo 'test1'", "shell": "true"}, | ||
Owner: "John Dough", | ||
OwnerEmail: "[email protected]", | ||
} | ||
err := sched.Start([]*Job{testJob1}, &Agent{}) | ||
assert.Error(t, err) | ||
|
||
// Wait for the job to start | ||
time.Sleep(time.Second * 2) | ||
<-sched.Stop().Done() | ||
err = sched.Start([]*Job{testJob1}, &Agent{}) | ||
assert.NoError(t, err) | ||
|
||
sched.Stop() | ||
assert.False(t, sched.Started()) | ||
} |