-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4665e32
commit 70efca9
Showing
7 changed files
with
202 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/nats-io/nats.go" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type OutDatedImagesJob struct { | ||
config *rest.Config | ||
js nats.JetStreamContext | ||
frequency string | ||
} | ||
|
||
func NewOutDatedImagesJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*OutDatedImagesJob, error) { | ||
return &OutDatedImagesJob{ | ||
config: config, | ||
js: js, | ||
frequency: frequency, | ||
}, nil | ||
} | ||
func (v *OutDatedImagesJob) CronSpec() string { | ||
return v.frequency | ||
} | ||
|
||
func (j *OutDatedImagesJob) Run() { | ||
// Call the outDatedImages function with the provided config and js | ||
err := OutDatedImages(j.config, j.js) | ||
LogErr(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/robfig/cron/v3" | ||
|
||
"github.com/intelops/go-common/logging" | ||
) | ||
|
||
type jobHandler interface { | ||
CronSpec() string | ||
Run() | ||
} | ||
|
||
type Scheduler struct { | ||
log logging.Logger | ||
jobs map[string]jobHandler | ||
cronIDs map[string]cron.EntryID | ||
c *cron.Cron | ||
cronMutex *sync.Mutex | ||
} | ||
|
||
func NewScheduler(log logging.Logger) *Scheduler { | ||
clog := cron.VerbosePrintfLogger(log.(logging.StdLogger)) | ||
return &Scheduler{ | ||
log: log, | ||
c: cron.New(cron.WithChain(cron.SkipIfStillRunning(clog), cron.Recover(clog))), | ||
jobs: map[string]jobHandler{}, | ||
cronIDs: map[string]cron.EntryID{}, | ||
cronMutex: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (t *Scheduler) AddJob(jobName string, job jobHandler) error { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
_, ok := t.cronIDs[jobName] | ||
if ok { | ||
return errors.Errorf("%s job already exists", jobName) | ||
} | ||
spec := job.CronSpec() | ||
if spec == "" { | ||
return errors.Errorf("%s job has no cron spec", jobName) | ||
} | ||
entryID, err := t.c.AddJob(spec, job) | ||
if err != nil { | ||
return errors.WithMessagef(err, "%s job cron spec not valid", jobName) | ||
} | ||
|
||
t.jobs[jobName] = job | ||
t.cronIDs[jobName] = entryID | ||
t.log.Infof("%s job added with cron '%s'", jobName, spec) | ||
return nil | ||
} | ||
|
||
// RemoveJob ... | ||
func (t *Scheduler) RemoveJob(jobName string) error { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
entryID, ok := t.cronIDs[jobName] | ||
if !ok { | ||
return errors.Errorf("%s job not exist", jobName) | ||
} | ||
|
||
t.c.Remove(entryID) | ||
delete(t.jobs, jobName) | ||
delete(t.cronIDs, jobName) | ||
t.log.Infof("%s job removed", jobName) | ||
return nil | ||
} | ||
|
||
func (t *Scheduler) Start() { | ||
t.c.Start() | ||
t.log.Infof("Job scheduler started") | ||
} | ||
|
||
func (t *Scheduler) Stop() { | ||
t.c.Stop() | ||
t.log.Infof("Job scheduler stopped") | ||
} | ||
|
||
func (t *Scheduler) GetJobs() map[string]jobHandler { | ||
t.cronMutex.Lock() | ||
defer t.cronMutex.Unlock() | ||
return t.jobs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.