Skip to content

Commit

Permalink
Merge pull request #311 from yohamta/feat/multiple-schedule
Browse files Browse the repository at this point in the history
internal/dag: feat: Allow multiple start/stop schedule
  • Loading branch information
yohamta authored Sep 2, 2022
2 parents bcbc5c3 + 1dc65ff commit 7aea586
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ steps:
command: job.sh
```

You can also set multiple start/stop schedules. In the following example, the process will run at 0:00-12:00 and 5:00-17:00.

```yaml
schedule:
start:
- "0 0 * * *"
- "12 0 * * *"
stop:
- "5 0 * * *"
- "17 0 * * *"
steps:
- name: some long-process
command: main.sh
```

### Run Scheduler as a daemon

The easiest way to make sure the process is always running on your system is to create the script below and execute it every minute using cron (you don't need `root` account in this way):
Expand Down
35 changes: 25 additions & 10 deletions internal/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,32 @@ func (b *builder) buildSchedule(def *configDefinition, d *DAG) error {
if _, ok := k.(string); !ok {
return fmt.Errorf("schedule key must be a string")
}
key := k.(string)
switch key {
kk := k.(string)
switch kk {
case "start", "stop":
if _, ok := v.(string); !ok {
return fmt.Errorf("schedule value must be a string")
}
switch key {
case "start":
starts = append(starts, v.(string))
case "stop":
stops = append(stops, v.(string))
switch (v).(type) {
case string:
switch kk {
case "start":
starts = append(starts, v.(string))
case "stop":
stops = append(stops, v.(string))
}
case []interface{}:
for _, vv := range v.([]interface{}) {
if vvv, ok := vv.(string); ok {
switch kk {
case "start":
starts = append(starts, vvv)
case "stop":
stops = append(stops, vvv)
}
} else {
return fmt.Errorf("schedule must be a string or an array of strings")
}
}
default:
return fmt.Errorf("schedule must be a string or an array of strings")
}
default:
return fmt.Errorf("schedule key must be start or stop")
Expand Down
41 changes: 28 additions & 13 deletions internal/dag/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,24 @@ func TestSchedule(t *testing.T) {
Err: true,
},
} {
l := &Loader{}
m, err := l.unmarshalData([]byte(tc.Def))
require.NoError(t, err)
t.Run(tc.Name, func(t *testing.T) {
l := &Loader{}
m, err := l.unmarshalData([]byte(tc.Def))
require.NoError(t, err)

def, err := l.decode(m)
require.NoError(t, err)
def, err := l.decode(m)
require.NoError(t, err)

b := &builder{}
d, err := b.buildFromDefinition(def, nil)
b := &builder{}
d, err := b.buildFromDefinition(def, nil)

if tc.Err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.Want, len(d.Schedule))
}
if tc.Err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.Want, len(d.Schedule))
}
})
}
}

Expand Down Expand Up @@ -341,6 +343,19 @@ func TestScheduleStop(t *testing.T) {
WantStart: 0,
WantStop: 1,
},
{
Name: "multiple schedule",
Def: `schedule:
start:
- "0 1 * * *"
- "0 18 * * *"
stop:
- "0 2 * * *"
- "0 20 * * *"
`,
WantStart: 2,
WantStop: 2,
},
{
Name: "invalid expression",
Def: `schedule:
Expand Down

0 comments on commit 7aea586

Please sign in to comment.