-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
727be2f
commit 8d21ade
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters