🤨 What Random

Using Goroutines to run a poor mans Cron Job

Lately, I wanted to push some stuff to a statsD server from within a golang application, without setting up a whole new init for a cron job.

Turns out, it's pretty easy when you're using goroutines to get that working.

func Start() chan bool {
    ticker := time.NewTicker(10 * time.Minute)
    quit := make(chan bool, 1)

    go func() {
    	for {
    		select {
    		case <-ticker.C:
    			report()
    		case <-quit:
    			ticker.Stop()
    		}
    	}
    }()

    return quit
}

func report() {
    // do your thing here
}

Explanation

Sidenotes

#programming #golang