Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 0 additions & 99 deletions .vscode/launch.json

This file was deleted.

13 changes: 7 additions & 6 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"runtime"
"sort"
"sync/atomic"
"time"
)

Expand All @@ -19,7 +20,7 @@ type Cron struct {
snapshot chan []*Entry
running bool
location *time.Location
nextID EntryID
nextID uint32
ErrorLog *log.Logger
}

Expand All @@ -36,7 +37,7 @@ type Schedule interface {
}

// EntryID identifies an entry within a Cron instance
type EntryID int
type EntryID uint32

// Entry consists of a schedule and the func to execute on that schedule.
type Entry struct {
Expand Down Expand Up @@ -144,10 +145,10 @@ func (c *Cron) AddJobN(name, spec string, cmd Job) (EntryID, error) {

// Schedule adds a Job to the Cron to be run on the given schedule.
func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID {
c.nextID++
nextID := atomic.AddUint32(&c.nextID, 1)

entry := &Entry{
ID: c.nextID,
ID: EntryID(nextID),
Schedule: schedule,
Job: cmd,
}
Expand All @@ -163,10 +164,10 @@ func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID {

// ScheduleN adds a Job to the Cron to be run on the given schedule.
func (c *Cron) ScheduleN(name, spec string, schedule Schedule, cmd Job) EntryID {
c.nextID++
nextID := atomic.AddUint32(&c.nextID, 1)

entry := &Entry{
ID: c.nextID,
ID: EntryID(nextID),
Name: name,
Spec: spec,
Schedule: schedule,
Expand Down
46 changes: 46 additions & 0 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,52 @@ func TestRemoveWhileRunning(t *testing.T) {
}
}

// Schedule within multiple go routines
func TestParallelSchedule(t *testing.T) {
var wg sync.WaitGroup
const numRoutine = 4

errSlice := make([]error, numRoutine)
idSlice := make([][]EntryID, numRoutine)
cron := New()
cron.Start()
for i := 0; i < numRoutine; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
const numTry = 100
for iTry := 0; iTry < numTry; iTry++ {
sched, err := Parse("* * * * * ?")
if err != nil {
errSlice[i] = err
return
}
var job testJob
entryID := cron.Schedule(sched, job)
idSlice[i] = append(idSlice[i], entryID)
}
}(i)
}
wg.Wait()

for i, err := range errSlice {
if err != nil {
t.Fatal("Error in #", i, ":", err)
}
}

idMap := make(map[EntryID]bool)
for i, ids := range idSlice {
for j, id := range ids {
if idMap[id] {
t.Fatal("id duplicate in #", i, "No:", j, "id:", id)
}
idMap[id] = true
}
}
cron.Stop()
}

// Test timing with Entries.
func TestSnapshotEntries(t *testing.T) {
wg := &sync.WaitGroup{}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tangyanhan/cron

go 1.13