Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: add support for optionJob #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 53 additions & 1 deletion cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ type Job interface {
Run()
}

type JobOption struct {
id EntryID
}

func (jo JobOption) GetID() EntryID {
return jo.id
}

type OptionJob interface {
Job
RunWithOption(*JobOption)
}

// Schedule describes a job's duty cycle.
type Schedule interface {
// Next returns the next activation time, later than the given time.
Expand Down Expand Up @@ -132,16 +145,24 @@ func New(opts ...Option) *Cron {

// FuncJob is a wrapper that turns a func() into a cron.Job
type FuncJob func()
type FuncOptionJob func(*JobOption)

func (f FuncJob) Run() { f() }

func (f FuncOptionJob) Run() { f(&JobOption{}) }
func (f FuncOptionJob) RunWithOption(o *JobOption) { f(o) }

// AddFunc adds a func to the Cron to be run on the given schedule.
// The spec is parsed using the time zone of this Cron instance as the default.
// An opaque ID is returned that can be used to later remove it.
func (c *Cron) AddFunc(spec string, cmd func()) (EntryID, error) {
return c.AddJob(spec, FuncJob(cmd))
}

func (c *Cron) AddOptionFunc(spec string, cmd func(*JobOption)) (EntryID, error) {
return c.AddOptionJob(spec, FuncOptionJob(cmd))
}

// AddJob adds a Job to the Cron to be run on the given schedule.
// The spec is parsed using the time zone of this Cron instance as the default.
// An opaque ID is returned that can be used to later remove it.
Expand All @@ -153,6 +174,19 @@ func (c *Cron) AddJob(spec string, cmd Job) (EntryID, error) {
return c.Schedule(schedule, cmd), nil
}

func (c *Cron) AddOptionJob(spec string, cmd OptionJob) (EntryID, error) {

schedule, err := c.parser.Parse(spec)
if err != nil {
return 0, err
}
return c.ScheduleOptionJob(schedule, cmd), nil
}

func (c *Cron) ScheduleOptionJob(schedule Schedule, cmd OptionJob) EntryID {
return c.Schedule(schedule, cmd)
}

// Schedule adds a Job to the Cron to be run on the given schedule.
// The job is wrapped with the configured Chain.
func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID {
Expand Down Expand Up @@ -309,7 +343,14 @@ func (c *Cron) startJob(j Job) {
c.jobWaiter.Add(1)
go func() {
defer c.jobWaiter.Done()
j.Run()
switch j := j.(type) {
case OptionJob:
j.RunWithOption(&JobOption{
c.nextID,
})
case Job:
j.Run()
}
}()
}

Expand Down Expand Up @@ -353,3 +394,14 @@ func (c *Cron) removeEntry(id EntryID) {
}
c.entries = entries
}

func (c *Cron) hasEntry(id EntryID) bool {

for _, e := range c.entries {
if e.ID == id {
return true
}
}

return false
}
21 changes: 21 additions & 0 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ func TestAddWhileRunningWithDelay(t *testing.T) {
}
}

func TestRemoveOptionJob(t *testing.T) {
cron := newWithSeconds()
cron.Start()
defer cron.Stop()

id, err := cron.AddOptionFunc("* * * * * ?", func(o *JobOption) {
cron.Remove(o.GetID())
})
if err != nil {
t.Fatal(err)
}

for range time.After(OneSecond) {
if ok := cron.hasEntry(id); ok {
t.Errorf("test remove option job: want removed(true), got not removed(false)")
} else {
return
}
}
}

// Add a job, remove a job, start cron, expect nothing runs.
func TestRemoveBeforeRunning(t *testing.T) {
wg := &sync.WaitGroup{}
Expand Down