2022-03-25 07:03:17 +00:00
|
|
|
package appier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Job ...
|
|
|
|
type Job struct {
|
|
|
|
Spec string
|
|
|
|
Name string
|
|
|
|
Cmd func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scheduler ...
|
|
|
|
type Scheduler struct {
|
|
|
|
cron *cron.Cron
|
|
|
|
jobs []*Job
|
|
|
|
}
|
|
|
|
|
|
|
|
// New ...
|
|
|
|
func newSchedule(jobs ...*Job) *Scheduler {
|
|
|
|
l, _ := time.LoadLocation("Asia/Ho_Chi_Minh")
|
2022-03-25 07:26:17 +00:00
|
|
|
c := cron.New(cron.WithSeconds(), cron.WithLocation(l))
|
2022-03-25 07:03:17 +00:00
|
|
|
|
|
|
|
return &Scheduler{
|
|
|
|
cron: c,
|
|
|
|
jobs: jobs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startSchedule ...
|
|
|
|
func (s *Scheduler) startSchedule() {
|
|
|
|
for _, job := range s.jobs {
|
|
|
|
if _, err := s.cron.AddFunc(job.Spec, job.Cmd); err != nil {
|
|
|
|
log.Fatalf("Add job err: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Job %s is started: %s\n", job.Name, job.Spec)
|
|
|
|
}
|
|
|
|
s.cron.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initSchedule ..
|
|
|
|
func initSchedule() {
|
|
|
|
jobs := []*Job{
|
|
|
|
{
|
2022-03-25 07:26:17 +00:00
|
|
|
Spec: "*/ * * * *",
|
2022-03-25 07:03:17 +00:00
|
|
|
Name: "Start job sync data to service appier",
|
|
|
|
Cmd: SyncToService{}.syncData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s := newSchedule(jobs...)
|
|
|
|
s.startSchedule()
|
|
|
|
}
|