Skip to content

Instantly share code, notes, and snippets.

@oyamo
Created May 17, 2022 14:47
Show Gist options
  • Save oyamo/cd5dad2fab3dbac091ba944bc913a641 to your computer and use it in GitHub Desktop.
Save oyamo/cd5dad2fab3dbac091ba944bc913a641 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/go-co-op/gocron"
"time"
)
func ScheduleTask(quit chan bool, seconds int, task func()) {
s := gocron.NewScheduler(time.UTC)
// Quit the scheduler when the quit channel is returns true
for {
select {
case <-quit:
s.Clear()
return
default:
s.CronWithSeconds("0/1 * * * * *").
StartAt(time.Now().Add(time.Second * time.Duration(seconds))).Do(func() {
task()
quit <- true
})
s.StartBlocking()
}
}
}
func main() {
quit := make(chan bool, 2)
// Run the scheduler in a go routine.
go ScheduleTask(quit, 2, func() {
println("hello")
})
<-quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment