Skip to content

Commit

Permalink
origin modules not active,thanks robfig#386
Browse files Browse the repository at this point in the history
but this pull not have any testcase,Later on need add testcase.
  • Loading branch information
penglj committed May 9, 2022
1 parent 81bea23 commit 8c8ef6f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Entry struct {
// Prev is the last time this job was run, or the zero time if never.
Prev time.Time

// Activate determines whether to skip Job execution.
Activate bool

// WrappedJob is the thing to run when the Schedule is activated.
WrappedJob Job

Expand Down Expand Up @@ -202,6 +205,7 @@ func (c *Cron) Schedule(schedule Schedule, cmd Job, entryOpts ...EntryOption) En
Schedule: schedule,
WrappedJob: c.chain.Then(cmd),
Job: cmd,
Activate: true,
Paused: false,
}
for _, fn := range entryOpts {
Expand Down Expand Up @@ -298,6 +302,18 @@ func (c *Cron) Remove(id EntryID) {
}
}

// Activate an entry to enable it's execution.
func (c *Cron) Activate(id EntryID) {
c.setEntryActivate(id, true)
c.logger.Info("activate", "entry", id)
}

// Deactivate an entry to prevent it's execution.
func (c *Cron) Deactivate(id EntryID) {
c.setEntryActivate(id, false)
c.logger.Info("deactivate", "entry", id)
}

// Start the cron scheduler in its own goroutine, or no-op if already started.
func (c *Cron) Start() {
c.runningMu.Lock()
Expand Down Expand Up @@ -361,7 +377,7 @@ func (c *Cron) run() {

// Run every entry whose next time was less than now
for k, e := range c.entries {
if e.Next.After(now) || e.Next.IsZero() {
if e.Next.After(now) || e.Next.IsZero() || !e.Activate {
break
}

Expand Down Expand Up @@ -521,3 +537,12 @@ func (c *Cron) removeEntry(id EntryID) {
}
c.entries = entries
}

func (c *Cron) setEntryActivate(id EntryID, activate bool) {
for _, e := range c.entries {
if e.ID == id {
e.Activate = activate
return
}
}
}

0 comments on commit 8c8ef6f

Please sign in to comment.