scheduler
scheduler copied to clipboard
Job specs from external configuration
Hi,
What if you want to store your task configuration parameters in a database or a yml/ini/js file? How would you map to those functions?
I had this problem and this is how I solved it:
func main(){
scheduleMethods := scheduleMethods()
job:=func(){
fmt.Println("Hello world")
}
// Call the methods and pass the parameters dynamically
scheduleMethods["seconds"](10,job)
}
func scheduleMethods() map[string]func(every uint, function func()) {
return map[string]func(every uint, function func()){
"day": func(every uint, function func()) {
_, _ = scheduler.Every(int(every)).Day().Run(function)
},
"hours": func(every uint, function func()) {
_, _ = scheduler.Every(int(every)).Hours().Run(function)
},
"minutes": func(every uint, function func()) {
_, _ = scheduler.Every(int(every)).Minutes().Run(function)
},
"seconds": func(every uint, function func()) {
_, _ = scheduler.Every(int(every)).Seconds().Run(function)
},
}
}