From d3b23f4a79ec04486d8f2b3e438735985840cd52 Mon Sep 17 00:00:00 2001 From: jtbry Date: Sat, 9 Apr 2022 01:59:31 -0400 Subject: [PATCH 1/3] add run-on-startup optional param --- core/common.go | 1 + core/job.go | 11 ++++++++--- core/scheduler.go | 7 +++++++ core/scheduler_test.go | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/core/common.go b/core/common.go index f9fc7f1c3..cd577affc 100644 --- a/core/common.go +++ b/core/common.go @@ -31,6 +31,7 @@ type Job interface { GetName() string GetSchedule() string GetCommand() string + GetRunOnStartup() string Middlewares() []Middleware Use(...Middleware) Run(*Context) error diff --git a/core/job.go b/core/job.go index 90dcc8fd2..2940eb5a6 100644 --- a/core/job.go +++ b/core/job.go @@ -6,9 +6,10 @@ import ( ) type BareJob struct { - Schedule string - Name string - Command string + Schedule string + Name string + Command string + RunOnStartup string `default:"false" gcfg:"run-on-startup" mapstructure:"run-on-startup"` middlewareContainer running int32 @@ -28,6 +29,10 @@ func (j *BareJob) GetCommand() string { return j.Command } +func (j *BareJob) GetRunOnStartup() string { + return j.RunOnStartup +} + func (j *BareJob) Running() int32 { return atomic.LoadInt32(&j.running) } diff --git a/core/scheduler.go b/core/scheduler.go index 7733889df..21d260f48 100644 --- a/core/scheduler.go +++ b/core/scheduler.go @@ -3,6 +3,7 @@ package core import ( "errors" "fmt" + "strconv" "sync" "github.com/robfig/cron" @@ -42,6 +43,12 @@ func (s *Scheduler) AddJob(j Job) error { return err } + runOnStartup, _ := strconv.ParseBool(j.GetRunOnStartup()) + if runOnStartup { + jw := &jobWrapper{s, j} + jw.Run() + } + s.Jobs = append(s.Jobs, j) return nil } diff --git a/core/scheduler_test.go b/core/scheduler_test.go index 31d957b3b..1d9ce973f 100644 --- a/core/scheduler_test.go +++ b/core/scheduler_test.go @@ -57,3 +57,18 @@ func (s *SuiteScheduler) TestMergeMiddlewaresSame(c *C) { c.Assert(m, HasLen, 1) c.Assert(m[0], Equals, mB) } + +func (s *SuiteScheduler) TestRunOnStartup(c *C) { + job := &TestJob{} + job.Schedule = "@hourly" + job.RunOnStartup = "true" + + sc := NewScheduler(&TestLogger{}) + sc.AddJob(job) + c.Assert(job.Called, Equals, 1) + + jobTwo := &TestJob{} + jobTwo.Schedule = "@hourly" + sc.AddJob(jobTwo) + c.Assert(jobTwo.Called, Equals, 0) +} From 07587a1ac22a0a78c62ad1ead6e13efc9c94e6bb Mon Sep 17 00:00:00 2001 From: jtbry Date: Sat, 9 Apr 2022 02:03:58 -0400 Subject: [PATCH 2/3] document run-on-startup --- docs/jobs.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/jobs.md b/docs/jobs.md index d4bf0bd3f..02db6e586 100644 --- a/docs/jobs.md +++ b/docs/jobs.md @@ -37,6 +37,10 @@ This job is executed inside a running container. Similar to `docker exec` - **INI config**: `Environment` setting can be provided multiple times for multiple environment variables. - **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]` - *default*: Optional field, no default. +- **run-on-startup** + - *description*: Runs the job once on ofelia's startup and then schedules the job normally + - *value*: Boolean, either false or true + - *default*: false ### INI-file example @@ -116,6 +120,10 @@ This job can be used in 2 situations: - **INI config**: `Environment` setting can be provided multiple times for multiple environment variables. - **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]` - *default*: Optional field, no default. +- **run-on-startup** + - *description*: Runs the job once on ofelia's startup and then schedules the job normally + - *value*: Boolean, either false or true + - *default*: false ### INI-file example @@ -172,6 +180,10 @@ Runs the command on the host running Ofelia. - **INI config**: `Environment` setting can be provided multiple times for multiple environment variables. - **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]` - *default*: Optional field, no default. +- **run-on-startup** + - *description*: Runs the job once on ofelia's startup and then schedules the job normally + - *value*: Boolean, either false or true + - *default*: false ### INI-file example @@ -233,6 +245,10 @@ This job can be used to: - *description*: Allocate a pseudo-tty, similar to `docker exec -t`. See this [Stack Overflow answer](https://stackoverflow.com/questions/30137135/confused-about-docker-t-option-to-allocate-a-pseudo-tty) for more info. - *value*: Boolean, either `true` or `false` - *default*: `false` +- **run-on-startup** + - *description*: Runs the job once on ofelia's startup and then schedules the job normally + - *value*: Boolean, either false or true + - *default*: false ### INI-file example From b2dc99b93de6d18632611632af429e2fae033355 Mon Sep 17 00:00:00 2001 From: jtbry Date: Fri, 8 Jul 2022 14:24:26 -0400 Subject: [PATCH 3/3] fix run-on-startup now runs in goroutine --- core/scheduler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/scheduler.go b/core/scheduler.go index 21d260f48..485b9f106 100644 --- a/core/scheduler.go +++ b/core/scheduler.go @@ -46,7 +46,7 @@ func (s *Scheduler) AddJob(j Job) error { runOnStartup, _ := strconv.ParseBool(j.GetRunOnStartup()) if runOnStartup { jw := &jobWrapper{s, j} - jw.Run() + go jw.Run() } s.Jobs = append(s.Jobs, j)