Skip to content

feat: support for running immediately #436

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
64 changes: 40 additions & 24 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import (
// specified by the schedule. It may be started, stopped, and the entries may
// be inspected while running.
type Cron struct {
entries []*Entry
chain Chain
stop chan struct{}
add chan *Entry
remove chan EntryID
snapshot chan chan []Entry
running bool
logger Logger
runningMu sync.Mutex
location *time.Location
parser ScheduleParser
nextID EntryID
jobWaiter sync.WaitGroup
entries []*Entry
chain Chain
stop chan struct{}
add chan *Entry
remove chan EntryID
snapshot chan chan []Entry
immediatelyRun chan *Entry
running bool
logger Logger
runningMu sync.Mutex
location *time.Location
parser ScheduleParser
nextID EntryID
jobWaiter sync.WaitGroup
}

// ScheduleParser is an interface for schedule spec parsers that return a Schedule
Expand Down Expand Up @@ -112,17 +113,18 @@ func (s byTime) Less(i, j int) bool {
// See "cron.With*" to modify the default behavior.
func New(opts ...Option) *Cron {
c := &Cron{
entries: nil,
chain: NewChain(),
add: make(chan *Entry),
stop: make(chan struct{}),
snapshot: make(chan chan []Entry),
remove: make(chan EntryID),
running: false,
runningMu: sync.Mutex{},
logger: DefaultLogger,
location: time.Local,
parser: standardParser,
entries: nil,
chain: NewChain(),
add: make(chan *Entry),
stop: make(chan struct{}),
snapshot: make(chan chan []Entry),
remove: make(chan EntryID),
immediatelyRun: make(chan *Entry),
running: false,
runningMu: sync.Mutex{},
logger: DefaultLogger,
location: time.Local,
parser: standardParser,
}
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -234,6 +236,12 @@ func (c *Cron) Run() {
c.run()
}

// ImmediatelyRun schedule the job immediately
func (c *Cron) ImmediatelyRun(id EntryID) {
entry := c.Entry(id)
c.immediatelyRun <- &entry
}

// run the scheduler.. this is private just due to the need to synchronize
// access to the 'running' state variable.
func (c *Cron) run() {
Expand Down Expand Up @@ -297,6 +305,14 @@ func (c *Cron) run() {
now = c.now()
c.removeEntry(id)
c.logger.Info("removed", "entry", id)

case entry := <-c.immediatelyRun:
timer.Stop()
now = c.now()
c.startJob(entry.WrappedJob)
entry.Prev = now
entry.Next = now
c.logger.Info("run immediately", "now", now, "entry", entry.ID, "next", entry.Next)
}

break
Expand Down