mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
48 lines
867 B
Go
48 lines
867 B
Go
![]() |
package internal
|
||
|
|
||
|
import (
|
||
|
"github.com/robfig/cron"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
// JobSpec ...
|
||
|
type JobSpec struct {
|
||
|
Schedule string
|
||
|
Factory JobFactory
|
||
|
}
|
||
|
|
||
|
func NewJobSpec(schedule string, factory JobFactory) JobSpec {
|
||
|
return JobSpec{schedule, factory}
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
Jobs map[string]JobSpec
|
||
|
StartupJobs map[string]JobSpec
|
||
|
)
|
||
|
|
||
|
func InitJobs(conf *Config) {
|
||
|
Jobs = map[string]JobSpec{
|
||
|
"SyncStore": NewJobSpec("@every 1m", NewSyncStoreJob),
|
||
|
}
|
||
|
|
||
|
StartupJobs = map[string]JobSpec{}
|
||
|
}
|
||
|
|
||
|
type JobFactory func(conf *Config, store Store) cron.Job
|
||
|
|
||
|
type SyncStoreJob struct {
|
||
|
conf *Config
|
||
|
db Store
|
||
|
}
|
||
|
|
||
|
func NewSyncStoreJob(conf *Config, db Store) cron.Job {
|
||
|
return &SyncStoreJob{conf: conf, db: db}
|
||
|
}
|
||
|
|
||
|
func (job *SyncStoreJob) Run() {
|
||
|
if err := job.db.Sync(); err != nil {
|
||
|
log.WithError(err).Warn("error sycning store")
|
||
|
}
|
||
|
log.Info("synced store")
|
||
|
}
|