Skip to content

Commit

Permalink
add generic executor (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrik-haase authored Oct 10, 2024
1 parent 727be2f commit 8d21ade
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions generic_executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package goload

import (
"context"
"time"
)

type genericExecutor struct {
name string
handler func(ctx context.Context) error
options *ExecutorOptions
}

type ExecutorOption func(options *ExecutorOptions)

func WithWeight(weight int) ExecutorOption {
return func(options *ExecutorOptions) {
options.Weight = weight
}
}

func WithTimeout(timeout time.Duration) ExecutorOption {
return func(options *ExecutorOptions) {
options.Timeout = timeout
}
}

func NewGenericExecutor(name string, handler func(ctx context.Context) error, opts ...ExecutorOption) Executor {
options := &ExecutorOptions{
Weight: 1,
Timeout: 0,
}

for _, opt := range opts {
opt(options)
}

return &genericExecutor{
name: name,
handler: handler,
options: options,
}
}

func (d *genericExecutor) Execute(ctx context.Context) ExecutionResponse {
err := d.handler(ctx)
return ExecutionResponse{
Identifier: d.Name(),
Err: err,
AdditionalData: nil,
}
}

func (d *genericExecutor) Name() string {
return d.name
}

func (d *genericExecutor) Options() *ExecutorOptions {
return d.options
}
2 changes: 1 addition & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r *Runner) hit(ex Executor, began time.Time) *Result {
defer cancel()
} else if r.defaultTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), ex.Options().Timeout)
ctx, cancel = context.WithTimeout(context.Background(), r.defaultTimeout)
defer cancel()
}

Expand Down

0 comments on commit 8d21ade

Please sign in to comment.