scheduler icon indicating copy to clipboard operation
scheduler copied to clipboard

Job specs from external configuration

Open mkozjak opened this issue 7 years ago • 1 comments

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?

mkozjak avatar Jul 30 '17 23:07 mkozjak

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)
		},
	}
}

tohidplus avatar Apr 17 '20 15:04 tohidplus