Skip to content

Commit

Permalink
orchestrator
Browse files Browse the repository at this point in the history
  • Loading branch information
MickStanciu committed Oct 2, 2024
1 parent 5e72724 commit 4aa71bb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
23 changes: 23 additions & 0 deletions orchestrator/executors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package orchestrator

import (
"fmt"
"time"
)

type execFn func() error

// ExecuteWithTimeout - executes a function with a timeout
func ExecuteWithTimeout(fn execFn, name string, timeOutSeconds int) error {
res := make(chan error, 1)
go func() {
res <- fn()
}()

select {
case <-time.After(time.Duration(timeOutSeconds) * time.Second):
return fmt.Errorf("timeout occurred after %d seconds, while executing function %q", timeOutSeconds, name)
case result := <-res:
return result
}
}
2 changes: 1 addition & 1 deletion orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (o *Orchestrator) AddStep(step *Step) {
}

if step.stepFn == nil {
panic(errors.New("stepFn cannot be nil"))
panic(errors.New("execFn cannot be nil"))
}

o.steps = append(o.steps, step)
Expand Down
33 changes: 6 additions & 27 deletions orchestrator/step.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package orchestrator

import (
"fmt"
"time"
)

type stepFn func() error

// Step - struct that will contain instructions of Step execution and rollback
type Step struct {
name string
stepFn stepFn
rollbackStepFn stepFn
stepFn execFn
rollbackStepFn execFn
//retryStrategy - nice to have
timeOutSeconds int
}
Expand All @@ -26,7 +19,7 @@ func WithTimeout(timeOutSeconds int) BuildStepOption {
}

// WithRollbackStepFn - adds a rollback function
func WithRollbackStepFn(fn stepFn) BuildStepOption {
func WithRollbackStepFn(fn execFn) BuildStepOption {
return func(s *Step) {
if fn != nil {
s.rollbackStepFn = fn
Expand All @@ -35,7 +28,7 @@ func WithRollbackStepFn(fn stepFn) BuildStepOption {
}

// NewStep - builds a new step
func NewStep(name string, fn stepFn, opts ...BuildStepOption) *Step {
func NewStep(name string, fn execFn, opts ...BuildStepOption) *Step {
s := &Step{
name: name,
stepFn: fn,
Expand All @@ -57,7 +50,7 @@ func (s *Step) ExecStep() error {
}

// executing with timeout
return executeWithTimeout(s.name, s.stepFn, s.timeOutSeconds)
return ExecuteWithTimeout(s.stepFn, s.name, s.timeOutSeconds)
}

// ExecRollback - executes the rollback step
Expand All @@ -71,19 +64,5 @@ func (s *Step) ExecRollback() error {
}

// executing with timeout
return executeWithTimeout(s.name, s.rollbackStepFn, s.timeOutSeconds)
}

func executeWithTimeout(name string, fn stepFn, timeOutSeconds int) error {
res := make(chan error, 1)
go func() {
res <- fn()
}()

select {
case <-time.After(time.Duration(timeOutSeconds) * time.Second): //guess need to test this
return fmt.Errorf("timeout in step %q after %d seconds", name, timeOutSeconds)
case result := <-res:
return result
}
return ExecuteWithTimeout(s.rollbackStepFn, s.name, s.timeOutSeconds)
}

0 comments on commit 4aa71bb

Please sign in to comment.