Skip to content

Instantly share code, notes, and snippets.

@jasonlvhit
Created August 17, 2014 03:25
Show Gist options
  • Save jasonlvhit/dbb4db8aaf5c2bf42a8e to your computer and use it in GitHub Desktop.
Save jasonlvhit/dbb4db8aaf5c2bf42a8e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/jasonlvhit/gocron"
)
func task() {
fmt.Println("I am runnning task.")
}
func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
func main() {
// Do jobs with params
gocron.Every(1).Second().Do(taskWithParams, 1, "hello")
// Do jobs without params
gocron.Every(1).Second().Do(task)
gocron.Every(2).Seconds().Do(task)
gocron.Every(1).Minute().Do(task)
gocron.Every(2).Minutes().Do(task)
gocron.Every(1).Hour().Do(task)
gocron.Every(2).Hours().Do(task)
gocron.Every(1).Day().Do(task)
gocron.Every(2).Days().Do(task)
// Do jobs on specific weekday
gocron.Every(1).Monday().Do(task)
gocron.Every(1).Thursday().Do(task)
// function At() take a string like 'hour:min'
gocron.Every(1).Day().At("10:30").Do(task)
gocron.Every(1).Monday().At("18:30").Do(task)
// remove, clear and next_run
_, time := gocron.NextRun()
fmt.Println(time)
gocron.Remove(task)
gocron.Clear()
// function Start start all the pending jobs
gocron.Start()
// Run all and Run pending , Run all with delay
for {
gocron.RunAll()
}
for {
gocron.RunPending()
}
for {
gocron.RunAllwithDelay(2) // in second
}
// also , you can create a your new scheduler,
// to run two scheduler concurrently
s := gocron.NewScheduler()
s.Every(3).Seconds().Do(task)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment