6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-20 13:58:22 +00:00
prologic-saltyim/internal/jobs.go
James Mills be4b4a5e9d Add a basic structure for a go-app PWA + integrated Broker (#36)
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
2022-03-21 22:27:35 +00:00

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