From 138bb25e2b8d874165e5e70778d67572aaa77870 Mon Sep 17 00:00:00 2001 From: Arifin Yunianta <72777947+Yunnie-pin@users.noreply.github.com> Date: Thu, 17 Oct 2024 20:40:41 +0700 Subject: [PATCH 1/5] feat: add AddFuncWithID and AddJobWithID to schedule jobs with predefined IDs --- .gitignore | 1 + cron.go | 59 +++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 9 deletions(-) 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 From 501d9b81b64215c4a4c51a264c463c528e079b30 Mon Sep 17 00:00:00 2001 From: Arifin Yunianta Date: Tue, 3 Dec 2024 17:12:59 +0700 Subject: [PATCH 2/5] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8c95bf47..cdb781ec 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/robfig/cron/v3 +module github.com/Yunnie-pin/cron/v3 go 1.12 From ad4af28474022334805aa18eca31e422769afd0b Mon Sep 17 00:00:00 2001 From: Arifin Yunianta Date: Wed, 4 Dec 2024 08:56:15 +0700 Subject: [PATCH 3/5] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cdb781ec..e7e1d783 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/Yunnie-pin/cron/v3 +module github.com/Yunnie-pin/cron go 1.12 From 1065147d2bf64e11dab034d0d4fc5919b18c4af8 Mon Sep 17 00:00:00 2001 From: Arifin Yunianta Date: Wed, 4 Dec 2024 09:08:19 +0700 Subject: [PATCH 4/5] revert --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index e7e1d783..8c95bf47 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/Yunnie-pin/cron +module github.com/robfig/cron/v3 go 1.12 From 5cf1587d9d1bdcb536ca9636ade6c5f7ede3848d Mon Sep 17 00:00:00 2001 From: Arifin Yunianta <72777947+Yunnie-pin@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:31:52 +0700 Subject: [PATCH 5/5] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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