mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-20 13:58:22 +00:00
![James Mills](/assets/img/avatar_default.png)
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Co-authored-by: Jon Lundy <jon@xuu.cc> Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/36
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")
|
|
}
|