mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
Merge pull request #25 from jwetzell/feat/sleep-processor
add a processor that sleeps
This commit is contained in:
47
internal/processor/time-sleep.go
Normal file
47
internal/processor/time-sleep.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type MetaDelay struct {
|
||||
config config.ProcessorConfig
|
||||
logger *slog.Logger
|
||||
Duration time.Duration
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Process(ctx context.Context, payload any) (any, error) {
|
||||
time.Sleep(md.Duration)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Type() string {
|
||||
return md.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "time.sleep",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep duration must be number")
|
||||
}
|
||||
|
||||
return &MetaDelay{config: config, Duration: time.Millisecond * time.Duration(durationNum), logger: slog.Default().With("component", "processor")}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user