100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package crontab
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
|
|
"arno/skeletor/service"
|
|
"arno/skeletor/repository"
|
|
"arno/skeletor/entity"
|
|
"arno/skeletor/tool"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
type stubMapping map[string]interface{}
|
|
var StubStorage = stubMapping{}
|
|
|
|
func init() {
|
|
StubStorage = map[string]interface{}{
|
|
"AppClear": AppClear,
|
|
"AppSetPassword": AppSetPassword,
|
|
}
|
|
}
|
|
|
|
func Lauch(ctn *service.Container) {
|
|
mydb := repository.Must(ctn)
|
|
|
|
tool.LogTitle("CRON")
|
|
tool.LogInfo("Création des Jobs Cron")
|
|
c := cron.New()
|
|
|
|
var crons []entity.Cron
|
|
var command entity.Command
|
|
mydb.Find(&crons)
|
|
|
|
for _, cron := range crons {
|
|
mydb.First(&command, "id = ?", cron.CommandId)
|
|
tool.Log("["+command.Name+"] "+cron.Every)
|
|
c.AddFunc(cron.Every, func() {
|
|
var prntA string
|
|
|
|
if(cron.Arg05!="") {
|
|
resA,_ := Call(command.Name,ctn,cron.Arg01,cron.Arg02,cron.Arg03,cron.Arg04,cron.Arg05)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
|
|
if(cron.Arg04!="") {
|
|
resA,_ := Call(command.Name,ctn,cron.Arg01,cron.Arg02,cron.Arg03,cron.Arg04)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
|
|
if(cron.Arg03!="") {
|
|
resA,_ := Call(command.Name,ctn,cron.Arg01,cron.Arg02,cron.Arg03)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
|
|
if(cron.Arg02!="") {
|
|
resA,_ := Call(command.Name,ctn,cron.Arg01,cron.Arg02)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
|
|
if(cron.Arg01!="") {
|
|
resA,_ := Call(command.Name,ctn,cron.Arg01)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
|
|
if(cron.Arg01=="") {
|
|
resA,_ := Call(command.Name,ctn)
|
|
prntA = resA.(string)
|
|
tool.Log(prntA)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Start cron with one scheduled job
|
|
c.Start()
|
|
|
|
}
|
|
|
|
func Call(funcName string, params ... interface{}) (result interface{}, err error) {
|
|
f := reflect.ValueOf(StubStorage[funcName])
|
|
if len(params) != f.Type().NumIn() {
|
|
err = errors.New("The number of params is out of index.")
|
|
return
|
|
}
|
|
in := make([]reflect.Value, len(params))
|
|
for k, param := range params {
|
|
in[k] = reflect.ValueOf(param)
|
|
}
|
|
var res []reflect.Value
|
|
res = f.Call(in)
|
|
result = res[0].Interface()
|
|
return
|
|
}
|