diff --git a/.gitignore b/.gitignore index 00268614..62c7b353 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ _cgo_export.* _testmain.go *.exe +/main \ No newline at end of file diff --git a/cron.go b/cron.go index c7e91766..8c27b182 100644 --- a/cron.go +++ b/cron.go @@ -2,6 +2,7 @@ package cron import ( "context" + "fmt" "sort" "sync" "time" @@ -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 { @@ -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)) +} \ No newline at end of file diff --git a/go.mod b/go.mod index 8c95bf47..e7e1d783 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/robfig/cron/v3 +module github.com/Yunnie-pin/cron go 1.12