admin管理员组

文章数量:1399490

I'm trying to create some cron tasks to test out the library. I'm exploring the singleton mode where if the same instance of a task is being executed, then it's rescheduled, however, I noticed that it's still getting picked up and executed. And overall, my main concern is with the growing number of goroutines even after execution is completed, which might be consuming memory too. And I'm confused as to why this's happening. Here's a sample of what I was trying:

s, _ := gocron.NewScheduler()
    defer func() { _ = s.Shutdown() }()

    s.Start()

    for i := 0; i < 10; i++ {
        t.Logf("num goroutines @ [%v] : [%v]", time.Now(), runtime.NumGoroutine())
        _, _ = s.NewJob(
            gocron.OneTimeJob(
                gocron.OneTimeJobStartImmediately(),
            ),
            gocron.NewTask(
                func() {
                    t.Logf("One time job [%d] started @ [%v] with [%d] goroutines", i, time.Now(), runtime.NumGoroutine())
                    time.Sleep(5 * time.Second)
                    t.Logf("One time job [%d] finished @ [%v] with [%d] goroutines", i, time.Now(), runtime.NumGoroutine())
                },
            ),
            gocron.WithSingletonMode(gocron.LimitModeReschedule),
            gocron.WithTags(fmt.Sprintf("tag-%d", i%2)),
        )
        time.Sleep(2 * time.Second)
    }

And the result looks something like this :

num goroutines @ [2025-03-25 19:08:57.513718 -0700 PDT m=+0.016469959] : [5]
One time job [0] started @ [2025-03-25 19:08:57.513933 -0700 PDT m=+0.016685584] with [6] goroutines
num goroutines @ [2025-03-25 19:08:59.515005 -0700 PDT m=+2.017860793] : [6]
One time job [1] started @ [2025-03-25 19:08:59.516081 -0700 PDT m=+2.018936709] with [7] goroutines
num goroutines @ [2025-03-25 19:09:01.516332 -0700 PDT m=+4.019291043] : [7]
One time job [2] started @ [2025-03-25 19:09:01.517226 -0700 PDT m=+4.020184834] with [8] goroutines
One time job [0] finished @ [2025-03-25 19:09:02.514315 -0700 PDT m=+5.017326043] with [8] goroutines
num goroutines @ [2025-03-25 19:09:03.517435 -0700 PDT m=+6.020497668] : [8]
One time job [3] started @ [2025-03-25 19:09:03.518384 -0700 PDT m=+6.021447126] with [9] goroutines
One time job [1] finished @ [2025-03-25 19:09:04.51691 -0700 PDT m=+7.020024043] with [9] goroutines
num goroutines @ [2025-03-25 19:09:05.518519 -0700 PDT m=+8.021685043] : [9]
One time job [4] started @ [2025-03-25 19:09:05.51925 -0700 PDT m=+8.022416001] with [10] goroutines
One time job [2] finished @ [2025-03-25 19:09:06.517808 -0700 PDT m=+9.021026084] with [10] goroutines
num goroutines @ [2025-03-25 19:09:07.519834 -0700 PDT m=+10.023103501] : [10]
One time job [5] started @ [2025-03-25 19:09:07.52033 -0700 PDT m=+10.023598876] with [11] goroutines
One time job [3] finished @ [2025-03-25 19:09:08.519277 -0700 PDT m=+11.022598126] with [11] goroutines
num goroutines @ [2025-03-25 19:09:09.521043 -0700 PDT m=+12.024415543] : [11]
One time job [6] started @ [2025-03-25 19:09:09.521674 -0700 PDT m=+12.025046959] with [12] goroutines
One time job [4] finished @ [2025-03-25 19:09:10.520093 -0700 PDT m=+13.023516918] with [12] goroutines
num goroutines @ [2025-03-25 19:09:11.522255 -0700 PDT m=+14.025730834] : [12]
One time job [7] started @ [2025-03-25 19:09:11.522742 -0700 PDT m=+14.026217834] with [13] goroutines
One time job [5] finished @ [2025-03-25 19:09:12.521151 -0700 PDT m=+15.024679168] with [13] goroutines
num goroutines @ [2025-03-25 19:09:13.522546 -0700 PDT m=+16.026125584] : [13]
One time job [8] started @ [2025-03-25 19:09:13.522965 -0700 PDT m=+16.026544334] with [14] goroutines
One time job [6] finished @ [2025-03-25 19:09:14.522656 -0700 PDT m=+17.026286751] with [14] goroutines
num goroutines @ [2025-03-25 19:09:15.523729 -0700 PDT m=+18.027411376] : [14]
One time job [9] started @ [2025-03-25 19:09:15.526384 -0700 PDT m=+18.030066876] with [15] goroutines
One time job [7] finished @ [2025-03-25 19:09:16.52378 -0700 PDT m=+19.027514293] with [15] goroutines
One time job [8] finished @ [2025-03-25 19:09:18.523861 -0700 PDT m=+21.027699251] with [15] goroutines
One time job [9] finished @ [2025-03-25 19:09:20.527062 -0700 PDT m=+23.031002959] with [15] goroutines

As you can see job[2] with the same tags is started before job[0] finished - so wouldn't that mean that 2 instances of the same job are running at the same time contrary to what is expected from using singleton mode to run these one time tasks? Moreover I can't understand why the number of goroutines keeps on increasing even after the task completes.

This's the scenario I'm trying to replicate here : I have a periodic gocron task which actually spins up these one time gocron tasks every 10 min each with tags i. To replicate that case in a simpler way, I added tags as i%2

for reference, I'm using go 1.22 and github/go-co-op/gocron/v2 v2.3.0

I'm trying to create some cron tasks to test out the https://github/go-co-op/gocron library. I'm exploring the singleton mode where if the same instance of a task is being executed, then it's rescheduled, however, I noticed that it's still getting picked up and executed. And overall, my main concern is with the growing number of goroutines even after execution is completed, which might be consuming memory too. And I'm confused as to why this's happening. Here's a sample of what I was trying:

s, _ := gocron.NewScheduler()
    defer func() { _ = s.Shutdown() }()

    s.Start()

    for i := 0; i < 10; i++ {
        t.Logf("num goroutines @ [%v] : [%v]", time.Now(), runtime.NumGoroutine())
        _, _ = s.NewJob(
            gocron.OneTimeJob(
                gocron.OneTimeJobStartImmediately(),
            ),
            gocron.NewTask(
                func() {
                    t.Logf("One time job [%d] started @ [%v] with [%d] goroutines", i, time.Now(), runtime.NumGoroutine())
                    time.Sleep(5 * time.Second)
                    t.Logf("One time job [%d] finished @ [%v] with [%d] goroutines", i, time.Now(), runtime.NumGoroutine())
                },
            ),
            gocron.WithSingletonMode(gocron.LimitModeReschedule),
            gocron.WithTags(fmt.Sprintf("tag-%d", i%2)),
        )
        time.Sleep(2 * time.Second)
    }

And the result looks something like this :

num goroutines @ [2025-03-25 19:08:57.513718 -0700 PDT m=+0.016469959] : [5]
One time job [0] started @ [2025-03-25 19:08:57.513933 -0700 PDT m=+0.016685584] with [6] goroutines
num goroutines @ [2025-03-25 19:08:59.515005 -0700 PDT m=+2.017860793] : [6]
One time job [1] started @ [2025-03-25 19:08:59.516081 -0700 PDT m=+2.018936709] with [7] goroutines
num goroutines @ [2025-03-25 19:09:01.516332 -0700 PDT m=+4.019291043] : [7]
One time job [2] started @ [2025-03-25 19:09:01.517226 -0700 PDT m=+4.020184834] with [8] goroutines
One time job [0] finished @ [2025-03-25 19:09:02.514315 -0700 PDT m=+5.017326043] with [8] goroutines
num goroutines @ [2025-03-25 19:09:03.517435 -0700 PDT m=+6.020497668] : [8]
One time job [3] started @ [2025-03-25 19:09:03.518384 -0700 PDT m=+6.021447126] with [9] goroutines
One time job [1] finished @ [2025-03-25 19:09:04.51691 -0700 PDT m=+7.020024043] with [9] goroutines
num goroutines @ [2025-03-25 19:09:05.518519 -0700 PDT m=+8.021685043] : [9]
One time job [4] started @ [2025-03-25 19:09:05.51925 -0700 PDT m=+8.022416001] with [10] goroutines
One time job [2] finished @ [2025-03-25 19:09:06.517808 -0700 PDT m=+9.021026084] with [10] goroutines
num goroutines @ [2025-03-25 19:09:07.519834 -0700 PDT m=+10.023103501] : [10]
One time job [5] started @ [2025-03-25 19:09:07.52033 -0700 PDT m=+10.023598876] with [11] goroutines
One time job [3] finished @ [2025-03-25 19:09:08.519277 -0700 PDT m=+11.022598126] with [11] goroutines
num goroutines @ [2025-03-25 19:09:09.521043 -0700 PDT m=+12.024415543] : [11]
One time job [6] started @ [2025-03-25 19:09:09.521674 -0700 PDT m=+12.025046959] with [12] goroutines
One time job [4] finished @ [2025-03-25 19:09:10.520093 -0700 PDT m=+13.023516918] with [12] goroutines
num goroutines @ [2025-03-25 19:09:11.522255 -0700 PDT m=+14.025730834] : [12]
One time job [7] started @ [2025-03-25 19:09:11.522742 -0700 PDT m=+14.026217834] with [13] goroutines
One time job [5] finished @ [2025-03-25 19:09:12.521151 -0700 PDT m=+15.024679168] with [13] goroutines
num goroutines @ [2025-03-25 19:09:13.522546 -0700 PDT m=+16.026125584] : [13]
One time job [8] started @ [2025-03-25 19:09:13.522965 -0700 PDT m=+16.026544334] with [14] goroutines
One time job [6] finished @ [2025-03-25 19:09:14.522656 -0700 PDT m=+17.026286751] with [14] goroutines
num goroutines @ [2025-03-25 19:09:15.523729 -0700 PDT m=+18.027411376] : [14]
One time job [9] started @ [2025-03-25 19:09:15.526384 -0700 PDT m=+18.030066876] with [15] goroutines
One time job [7] finished @ [2025-03-25 19:09:16.52378 -0700 PDT m=+19.027514293] with [15] goroutines
One time job [8] finished @ [2025-03-25 19:09:18.523861 -0700 PDT m=+21.027699251] with [15] goroutines
One time job [9] finished @ [2025-03-25 19:09:20.527062 -0700 PDT m=+23.031002959] with [15] goroutines

As you can see job[2] with the same tags is started before job[0] finished - so wouldn't that mean that 2 instances of the same job are running at the same time contrary to what is expected from using singleton mode to run these one time tasks? Moreover I can't understand why the number of goroutines keeps on increasing even after the task completes.

This's the scenario I'm trying to replicate here : I have a periodic gocron task which actually spins up these one time gocron tasks every 10 min each with tags i. To replicate that case in a simpler way, I added tags as i%2

for reference, I'm using go 1.22 and github/go-co-op/gocron/v2 v2.3.0

Share Improve this question edited Mar 26 at 19:12 mmind asked Mar 26 at 2:15 mmindmmind 111 bronze badge 3
  • WithSingletonMode "keeps the job from running again if it is already running", this applies to a single job (in case the task is still be running when retriggered). As you are creating with OneTimeJob the jobs will only run once anyway. It looks like you want to setup multiple jobs, but limit the number of jobs running concurrently, so WithLimitConcurrentJobs might be what you want? – Brits Commented Mar 26 at 6:15
  • @Brits i actually have a periodic grocron job that creates these one time tasks every 10 min or so. And I want to avoid the same one time job from being created with the same tags. to replicate that, i used tags i%2 here. – mmind Commented Mar 26 at 18:03
  • Sorry, I'm not seeing the relevance of Tags here ("Tags provide a way to identify jobs by a set of tags and remove multiple jobs by tag"). My understanding is that tags have no influence on the running of jobs (they just provide a convenient way to remove all jobs with a tag). – Brits Commented Mar 26 at 19:51
Add a comment  | 

1 Answer 1

Reset to default 1

Wouldn't that mean that 2 instances of the same job are running at the same time?

No. Every call to NewJob creates a distinct job. In your case you create ten distinct jobs that just happen to do the same thing.

To limit concurrency across all jobs use WithLimitConcurrentJobs.

s, _ := gocron.NewScheduler(
    gocron.WithLimitConcurrentJobs(1, gocron.LimitModeReschedule),
)

本文标签: cronincreasing number of goroutines when using gocoopgocronStack Overflow