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

feat: add AddFuncWithID and AddJobWithID to schedule jobs with predef… #530

Open
wants to merge 5 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ _cgo_export.*
_testmain.go

*.exe
/main
59 changes: 50 additions & 9 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"context"
"fmt"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -97,17 +98,17 @@ func (s byTime) Less(i, j int) bool {
//
// Available Settings
//
// Time Zone
// Description: The time zone in which schedules are interpreted
// Default: time.Local
// Time Zone
// Description: The time zone in which schedules are interpreted
// Default: time.Local
//
// Parser
// Description: Parser converts cron spec strings into cron.Schedules.
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
// Parser
// Description: Parser converts cron spec strings into cron.Schedules.
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
//
// Chain
// Description: Wrap submitted jobs to customize behavior.
// Default: A chain that recovers panics and logs them to stderr.
// Chain
// Description: Wrap submitted jobs to customize behavior.
// Default: A chain that recovers panics and logs them to stderr.
//
// See "cron.With*" to modify the default behavior.
func New(opts ...Option) *Cron {
Expand Down Expand Up @@ -353,3 +354,43 @@ func (c *Cron) removeEntry(id EntryID) {
}
c.entries = entries
}

// AddJobWithID adds a Job to the Cron to be run on the given schedule with a predefined ID.
func (c *Cron) AddJobWithID(id EntryID, spec string, cmd Job) error {
schedule, err := c.parser.Parse(spec)
if err != nil {
return err
}
return c.ScheduleWithID(id, schedule, cmd)
}

// ScheduleWithID adds a Job to the Cron with a predefined ID to be run on the given schedule.
// The job is wrapped with the configured Chain.
func (c *Cron) ScheduleWithID(id EntryID, schedule Schedule, cmd Job) error {
c.runningMu.Lock()
defer c.runningMu.Unlock()

// Check if an entry with the same ID already exists
for _, entry := range c.entries {
if entry.ID == id {
return fmt.Errorf("an entry with this ID already exists: %d", id)
}
}

entry := &Entry{
ID: id, // Use the provided ID
Schedule: schedule,
WrappedJob: c.chain.Then(cmd),
Job: cmd,
}
if !c.running {
c.entries = append(c.entries, entry)
} else {
c.add <- entry
}
return nil
}

func (c *Cron) AddFuncWithID(id EntryID, spec string, cmd func()) error {
return c.AddJobWithID(id, spec, FuncJob(cmd))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/robfig/cron/v3
module github.com/Yunnie-pin/cron

go 1.12