Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run-on-startup optional param for all jobs #184

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Job interface {
GetName() string
GetSchedule() string
GetCommand() string
GetRunOnStartup() string
Middlewares() []Middleware
Use(...Middleware)
Run(*Context) error
Expand Down
11 changes: 8 additions & 3 deletions core/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions core/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"errors"
"fmt"
"strconv"
"sync"

"github.com/robfig/cron"
Expand Down Expand Up @@ -42,6 +43,12 @@ func (s *Scheduler) AddJob(j Job) error {
return err
}

runOnStartup, _ := strconv.ParseBool(j.GetRunOnStartup())
if runOnStartup {
jw := &jobWrapper{s, j}
go jw.Run()
}

s.Jobs = append(s.Jobs, j)
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions core/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions docs/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down