Skip to content

Commit

Permalink
allow Task.Cron and Task.Gate to be used together (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd authored Oct 16, 2023
1 parent 8dc86f9 commit d4b2bc8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
6 changes: 3 additions & 3 deletions engine/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ func (e *Engine) verCre(tas *task.Task) (*ticker.Ticker, error) {
if tas.Cron != nil && tas.Root != nil {
return nil, tracer.Maskf(taskCronError, "Task.Cron and Task.Root must not be configured together")
}
if tas.Cron != nil && tas.Gate != nil {
return nil, tracer.Maskf(taskCronError, "Task.Cron and Task.Gate must not be configured together")
}
if tas.Gate != nil && tas.Root != nil {
return nil, tracer.Maskf(taskCronError, "Task.Gate and Task.Root must not be configured together")
}
Expand Down Expand Up @@ -151,6 +148,9 @@ func (e *Engine) verCre(tas *task.Task) (*ticker.Ticker, error) {
wai = true
}

if tri && tas.Cron != nil {
return nil, tracer.Maskf(labelValueError, "Task.Gate must not contain reserved value [trigger] if Task.Cron is configured")
}
if tri && wai {
return nil, tracer.Maskf(labelValueError, "Task.Gate must not contain both of the reserved values [trigger waiting] together")
}
Expand Down
38 changes: 34 additions & 4 deletions engine/engine_create_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,28 @@ func Test_Engine_Create_Cron(t *testing.T) {
Meta: &task.Meta{
"test.api.io/key": "bar",
},
Gate: &task.Gate{
"test.api.io/k-0": task.Trigger,
},
}

err = eon.Create(tas)
if !IsLabelValue(err) {
t.Fatal(err)
}
}

{
tas := &task.Task{
Cron: &task.Cron{
task.Aevery: "hour",
},
Meta: &task.Meta{
"test.api.io/key": "bar",
},
Gate: &task.Gate{
"test.api.io/k-0": task.Waiting,
},
}

err = eon.Create(tas)
Expand All @@ -301,19 +323,27 @@ func Test_Engine_Create_Cron(t *testing.T) {

{
if len(lis) != 2 {
t.Fatal("expected 2 tasks listed")
t.Fatal("expected", 2, "got", len(lis))
}

if lis[0].Meta.Get("test.api.io/key") != "foo" {
t.Fatal("scheduling failed")
}
if lis[0].Cron != nil {
t.Fatal("scheduling failed")
t.Fatal("expected", nil, "got", lis[0].Cron)
}
if lis[0].Gate != nil {
t.Fatal("expected", nil, "got", lis[0].Gate)
}

if lis[1].Meta.Get("test.api.io/key") != "bar" {
t.Fatal("scheduling failed")
t.Fatal("expected", "bar", "got", lis[1].Meta.Get("test.api.io/key"))
}
if lis[1].Cron.Get().Aevery() != "hour" {
t.Fatal("scheduling failed")
t.Fatal("expected", "hour", "got", lis[1].Cron.Get().Aevery())
}
if lis[1].Gate.Get("test.api.io/k-0") != task.Waiting {
t.Fatal("expected", task.Waiting, "got", lis[1].Gate.Get("test.api.io/k-0"))
}
}

Expand Down

0 comments on commit d4b2bc8

Please sign in to comment.