Replies: 1 comment
-
There is no guaranteed way within gocron to get deterministic order of the jobs. The closest way I can think of with the current capabilities would be to use a duration job and offset the starts using the For example: package main
import (
"log"
"time"
"github.com/go-co-op/gocron/v2"
)
func main() {
s, err := gocron.NewScheduler()
if err != nil {
log.Fatal(err)
}
now := time.Now()
_, err = s.NewJob(
gocron.DurationJob(1*time.Second),
gocron.NewTask(func() {
log.Println("job 1 ran")
}),
gocron.WithStartAt(
gocron.WithStartDateTime(
now.Add(100*time.Millisecond),
),
),
)
_, err = s.NewJob(
gocron.DurationJob(2*time.Second),
gocron.NewTask(func() {
log.Println("job 2 ran")
}),
gocron.WithStartAt(
gocron.WithStartDateTime(
now.Add(2*time.Second+200*time.Millisecond),
),
),
)
_, err = s.NewJob(
gocron.DurationJob(3*time.Second),
gocron.NewTask(func() {
log.Println("job 3 ran")
}),
gocron.WithStartAt(
gocron.WithStartDateTime(
now.Add(3*time.Second+300*time.Millisecond),
),
),
)
if err != nil {
log.Fatal(err)
}
log.Println("starting")
s.Start()
select {}
} Output:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm implementing a system where people supply multiple scripts to run, with a cron specification for scheduling.
To ensure the scripts can operate on some shared data, I would like to specify an ordering for job tasks to execute in, sequentially. For example, given:
Every 3rd second, all three tasks will run, but task_one must run (to completion) before task_two, which must finish before task_three.
Using
gocron.NewScheduler(gocron.WithLimitConcurrentJobs(1, gocron.LimitModeWait)
seems to ensure that the tasks will not run simultaneously, but testing indicates the ordering is not deterministic:(each of these test tasks has a 100ms sleep to test this).
Is there any way I can get the execution ordering at a particular point in time to be deterministic?
Beta Was this translation helpful? Give feedback.
All reactions