Skip to content

Commit 2816b89

Browse files
authored
Merge pull request #1 from jtbry/master
PR mcuadros#184 run-on-startup optional param for all jobs
2 parents 94edcdf + b2dc99b commit 2816b89

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

core/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Job interface {
3131
GetName() string
3232
GetSchedule() string
3333
GetCommand() string
34+
GetRunOnStartup() string
3435
Middlewares() []Middleware
3536
Use(...Middleware)
3637
Run(*Context) error

core/job.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
)
77

88
type BareJob struct {
9-
Schedule string
10-
Name string
11-
Command string
9+
Schedule string
10+
Name string
11+
Command string
12+
RunOnStartup string `default:"false" gcfg:"run-on-startup" mapstructure:"run-on-startup"`
1213

1314
middlewareContainer
1415
running int32
@@ -28,6 +29,10 @@ func (j *BareJob) GetCommand() string {
2829
return j.Command
2930
}
3031

32+
func (j *BareJob) GetRunOnStartup() string {
33+
return j.RunOnStartup
34+
}
35+
3136
func (j *BareJob) Running() int32 {
3237
return atomic.LoadInt32(&j.running)
3338
}

core/scheduler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"errors"
55
"fmt"
6+
"strconv"
67
"sync"
78

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

46+
runOnStartup, _ := strconv.ParseBool(j.GetRunOnStartup())
47+
if runOnStartup {
48+
jw := &jobWrapper{s, j}
49+
go jw.Run()
50+
}
51+
4552
s.Jobs = append(s.Jobs, j)
4653
return nil
4754
}

core/scheduler_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,18 @@ func (s *SuiteScheduler) TestMergeMiddlewaresSame(c *C) {
5757
c.Assert(m, HasLen, 1)
5858
c.Assert(m[0], Equals, mB)
5959
}
60+
61+
func (s *SuiteScheduler) TestRunOnStartup(c *C) {
62+
job := &TestJob{}
63+
job.Schedule = "@hourly"
64+
job.RunOnStartup = "true"
65+
66+
sc := NewScheduler(&TestLogger{})
67+
sc.AddJob(job)
68+
c.Assert(job.Called, Equals, 1)
69+
70+
jobTwo := &TestJob{}
71+
jobTwo.Schedule = "@hourly"
72+
sc.AddJob(jobTwo)
73+
c.Assert(jobTwo.Called, Equals, 0)
74+
}

docs/jobs.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ This job is executed inside a running container. Similar to `docker exec`
3737
- **INI config**: `Environment` setting can be provided multiple times for multiple environment variables.
3838
- **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]`
3939
- *default*: Optional field, no default.
40+
- **run-on-startup**
41+
- *description*: Runs the job once on ofelia's startup and then schedules the job normally
42+
- *value*: Boolean, either false or true
43+
- *default*: false
4044

4145
### INI-file example
4246

@@ -116,6 +120,10 @@ This job can be used in 2 situations:
116120
- **INI config**: `Environment` setting can be provided multiple times for multiple environment variables.
117121
- **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]`
118122
- *default*: Optional field, no default.
123+
- **run-on-startup**
124+
- *description*: Runs the job once on ofelia's startup and then schedules the job normally
125+
- *value*: Boolean, either false or true
126+
- *default*: false
119127

120128
### INI-file example
121129

@@ -172,6 +180,10 @@ Runs the command on the host running Ofelia.
172180
- **INI config**: `Environment` setting can be provided multiple times for multiple environment variables.
173181
- **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]`
174182
- *default*: Optional field, no default.
183+
- **run-on-startup**
184+
- *description*: Runs the job once on ofelia's startup and then schedules the job normally
185+
- *value*: Boolean, either false or true
186+
- *default*: false
175187

176188
### INI-file example
177189

@@ -233,6 +245,10 @@ This job can be used to:
233245
- *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.
234246
- *value*: Boolean, either `true` or `false`
235247
- *default*: `false`
248+
- **run-on-startup**
249+
- *description*: Runs the job once on ofelia's startup and then schedules the job normally
250+
- *value*: Boolean, either false or true
251+
- *default*: false
236252

237253
### INI-file example
238254

0 commit comments

Comments
 (0)