Created
May 17, 2022 14:47
-
-
Save oyamo/cd5dad2fab3dbac091ba944bc913a641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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