How do I stop gocron jobs efficiently?

Logan :

I am doing something like this:

package main

import (
    "fmt"
    "time"

    "github.com/jasonlvhit/gocron"
)

func jobs(quit <-chan bool) {
    for {
        select {
        case <-quit:
            return
        default:
            //cron jobs
            g := gocron.NewScheduler()
            g.Every(1).Second().Do(stuff)
            <-g.Start()
        }
    }
}

func stuff() {
    fmt.Println("doing job")
}

func main() {
    q := make(chan bool)
    go jobs(q)
    time.Sleep(3 * time.Second)

 //to quit the goroutine
    q <- true    
    close(q)
    fmt.Println("main")
}

I'm trying to stop the gocrons by killing the goroutine by closing the channel but I'm not able to stop gocron jobs. I am getting output

            doing job
            doing job
            doing job
            doing job
            doing job
            doing job
            doing job
            .
            .

Instead of

            doing job
            doing job
            doing job
            main

What am I doing wrong? Is there any better solution to stop gocron jobs?

Zak :

Your problem is in the select block here:

    select {
    case <-quit:
        return
    default:
        //cron jobs
        g := gocron.NewScheduler()
        g.Every(1).Second().Do(stuff)
        <-g.Start()
    }

This code says: select the case we can read from quit and exit, or do the default case.

Entering the default part of the case will block the goroutine on <-g.Start() until all the jobs are done. We have to wait here for the jobs to finish. While we are still waiting on <-g.Start() we do not consider the quit channel.

Instead do:

func jobs(quit <-chan bool) {
    for {
        //cron jobs
        g := gocron.NewScheduler()
        g.Every(1).Second().Do(stuff)

        select {
        case <-quit:
            // here we receive from quit and exit
            // if `g` has started, we may want to `g.Clear()`
            // or the scheduled jobs will continue, we will just not be waiting for them to finish.
            return
        case <-g.Start():
            // here we know all the jobs are done, and go round the loop again
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I run Google Dataprep jobs automatically?

分類Dev

How do I create delayed_job jobs with hooks/callbacks?

分類Dev

How do I kill zombie/phantom scheduler jobs in laravel forge?

分類Dev

How do I stop spaces appearing in this program?

分類Dev

How do I stop and repeat a function?

分類Dev

How do I stop all processes in a chroot?

分類Dev

How do I remove all tabs BEFORE a string (not after) efficiently?

分類Dev

How do I efficiently track the history of a tensorflow tensor?

分類Dev

How do I efficiently control normalisation in a keyman keyboard using groups?

分類Dev

How do I efficiently web-scrape these abnormal values?

分類Dev

How do I stop user input until I need it?

分類Dev

How do I start/stop and reset a setInterval animation with an if statement?

分類Dev

How do I stop my css from overlapping in django?

分類Dev

How do I stop opening a .whl file with the notepad?

分類Dev

How do I stop users from booking in the past DateTime Ruby?

分類Dev

How do I stop the hiding of a node when it is active?

分類Dev

How do I stop my python turtle from moving?

分類Dev

How do I stop show() from disrupting my radio buttons?

分類Dev

How do I STOP an Azure BizTalk Service without deleting it?

分類Dev

Windows Loads Webpage on Startup - How do I stop this?

分類Dev

How do I make the clock stop chiming every hour on the hour?

分類Dev

How do I stop the screen from being locked on suspend?

分類Dev

how do I stop automysqlbackup throwing LOCK TABLES error?

分類Dev

How do I stop Apache from running as root?

分類Dev

How do I stop each letter from printing on different line?

分類Dev

How do I stop SMPlayer from using volume buttons?

分類Dev

How do I stop elements in an array being replaced every input?

分類Dev

How do I link records to a large table efficiently using python Dedupe?

分類Dev

In Java, how do I most efficiently find an array within a JSON array based on the value of an element of the subarray?

Related 関連記事

  1. 1

    How do I run Google Dataprep jobs automatically?

  2. 2

    How do I create delayed_job jobs with hooks/callbacks?

  3. 3

    How do I kill zombie/phantom scheduler jobs in laravel forge?

  4. 4

    How do I stop spaces appearing in this program?

  5. 5

    How do I stop and repeat a function?

  6. 6

    How do I stop all processes in a chroot?

  7. 7

    How do I remove all tabs BEFORE a string (not after) efficiently?

  8. 8

    How do I efficiently track the history of a tensorflow tensor?

  9. 9

    How do I efficiently control normalisation in a keyman keyboard using groups?

  10. 10

    How do I efficiently web-scrape these abnormal values?

  11. 11

    How do I stop user input until I need it?

  12. 12

    How do I start/stop and reset a setInterval animation with an if statement?

  13. 13

    How do I stop my css from overlapping in django?

  14. 14

    How do I stop opening a .whl file with the notepad?

  15. 15

    How do I stop users from booking in the past DateTime Ruby?

  16. 16

    How do I stop the hiding of a node when it is active?

  17. 17

    How do I stop my python turtle from moving?

  18. 18

    How do I stop show() from disrupting my radio buttons?

  19. 19

    How do I STOP an Azure BizTalk Service without deleting it?

  20. 20

    Windows Loads Webpage on Startup - How do I stop this?

  21. 21

    How do I make the clock stop chiming every hour on the hour?

  22. 22

    How do I stop the screen from being locked on suspend?

  23. 23

    how do I stop automysqlbackup throwing LOCK TABLES error?

  24. 24

    How do I stop Apache from running as root?

  25. 25

    How do I stop each letter from printing on different line?

  26. 26

    How do I stop SMPlayer from using volume buttons?

  27. 27

    How do I stop elements in an array being replaced every input?

  28. 28

    How do I link records to a large table efficiently using python Dedupe?

  29. 29

    In Java, how do I most efficiently find an array within a JSON array based on the value of an element of the subarray?

ホットタグ

アーカイブ