Skip to content

Commit

Permalink
prevent Task.Cron and Task.Gate waiting to be set together (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd authored Oct 17, 2023
1 parent 1b31ef0 commit 90f4ad6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
6 changes: 4 additions & 2 deletions engine/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (e *Engine) verCre(tas *task.Task) (*ticker.Ticker, error) {
return nil, tracer.Maskf(taskCronError, "Task.Cron and Task.Root 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")
return nil, tracer.Maskf(taskGateError, "Task.Gate and Task.Root must not be configured together")
}
}

Expand Down Expand Up @@ -151,7 +151,9 @@ func (e *Engine) verCre(tas *task.Task) (*ticker.Ticker, error) {
if tri && wai {
return nil, tracer.Maskf(labelValueError, "Task.Gate must not contain both of the reserved values [trigger waiting] together")
}

if wai && tas.Cron != nil {
return nil, tracer.Maskf(labelValueError, "Task.Gate must not contain the reserved value [waiting] if Task.Cron is configured")
}
if v != task.Trigger && v != task.Waiting {
return nil, tracer.Maskf(labelValueError, "Task.Gate must only contain one of the reserved values [trigger waiting]")
}
Expand Down
39 changes: 36 additions & 3 deletions engine/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,25 @@ func Test_Engine_Create_Gate_No_Error(t *testing.T) {
},
},
},
// Case 001 ensures that trigger tasks can define a multiple trigger labels.
// Case 001 ensures that task templates defining Task.Cron can schedule
// trigger tasks with Task.Gate definitions containing the reserved value
// "trigger".
{
tas: &task.Task{
Cron: &task.Cron{
task.Aevery: "hour",
},
Meta: &task.Meta{
"foo": "bar",
},
Gate: &task.Gate{
"foo": task.Trigger,
"bar": task.Trigger,
"baz": task.Trigger,
},
},
},
// Case 002 ensures that trigger tasks can define a multiple trigger labels.
{
tas: &task.Task{
Meta: &task.Meta{
Expand All @@ -67,7 +85,7 @@ func Test_Engine_Create_Gate_No_Error(t *testing.T) {
},
},
},
// Case 002 ensures that task templates can define a single waiting label.
// Case 003 ensures that task templates can define a single waiting label.
{
tas: &task.Task{
Meta: &task.Meta{
Expand All @@ -78,7 +96,7 @@ func Test_Engine_Create_Gate_No_Error(t *testing.T) {
},
},
},
// Case 003 ensures that task templates can define a multiple waiting
// Case 004 ensures that task templates can define a multiple waiting
// labels.
{
tas: &task.Task{
Expand Down Expand Up @@ -260,6 +278,21 @@ func Test_Engine_Create_Gate_Error(t *testing.T) {
},
},
},
// Case 012 ensures that the reserved value "waiting" in Task.Gate cannot be
// used together with task.Cron.
{
tas: &task.Task{
Cron: &task.Cron{
task.Aevery: "hour",
},
Meta: &task.Meta{
"foo": "bar",
},
Gate: &task.Gate{
"baz": task.Waiting,
},
},
},
}

for i, tc := range testCases {
Expand Down
4 changes: 2 additions & 2 deletions engine/engine_lifecycle_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,8 @@ func Test_Engine_Lifecycle_Cron_Resolve(t *testing.T) {
}
}

// Verify the scheduled task that should contain Task.Gate, Task.Root and
// Task.Sync according to the task template emitting it.
// Verify the scheduled task that should contain Task.Gate, Task.Meta,
// Task.Root and Task.Sync according to the task template emitting it.
{
var tas *task.Task
{
Expand Down
8 changes: 8 additions & 0 deletions engine/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func IsTaskEmpty(err error) bool {
return errors.Is(err, taskEmptyError)
}

var taskGateError = &tracer.Error{
Kind: "taskGateError",
}

func IsTaskGate(err error) bool {
return errors.Is(err, taskGateError)
}

var taskMetaEmptyError = &tracer.Error{
Kind: "taskMetaEmptyError",
}
Expand Down
10 changes: 5 additions & 5 deletions engine/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ func (e *Engine) ticker() error {
// still exist, then we cannot schedule another one. Regardless, we have to
// update the task template's tick+1 below.
if !exi {
// Create a new scheduled task with the template's Task.Meta, Task.Root
// and Task.Sync reference. Note that scheduled tasks have a reserved
// label reference of the parents object ID in their root directory,
// pointing to the task template that defines their job description and
// scheduling information.
// Create a new scheduled task with the template's Task.Gate, Task.Meta,
// Task.Root and Task.Sync reference. Note that scheduled tasks have a
// reserved label reference of the parents object ID in their root
// directory, pointing to the task template that defines their job
// description and scheduling information.
var t *task.Task
{
t = &task.Task{
Expand Down

0 comments on commit 90f4ad6

Please sign in to comment.