Skip to content

Commit

Permalink
origin modules not active,thanks robfig#393
Browse files Browse the repository at this point in the history
  • Loading branch information
penglj committed May 9, 2022
1 parent 8c8ef6f commit d3b5075
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
54 changes: 53 additions & 1 deletion cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -171,17 +184,25 @@ func New(opts ...Option) *Cron {

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

// Run will satisfies interface declaration
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(), entryOpts ...EntryOption) (EntryID, error) {
return c.AddJob(spec, FuncJob(cmd), entryOpts...)
}

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 @@ -194,6 +215,19 @@ func (c *Cron) AddJob(spec string, cmd Job, entryOpts ...EntryOption) (EntryID,
return c.Schedule(schedule, cmd, entryOpts...), 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, entryOpts ...EntryOption) EntryID {
Expand Down Expand Up @@ -454,7 +488,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 @@ -538,6 +579,17 @@ 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
}

func (c *Cron) setEntryActivate(id EntryID, activate bool) {
for _, e := range c.entries {
if e.ID == id {
Expand Down
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

0 comments on commit d3b5075

Please sign in to comment.