-
Notifications
You must be signed in to change notification settings - Fork 0
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
6f77195
commit 6d9ac00
Showing
3 changed files
with
104 additions
and
32 deletions.
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,30 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
|
||
// Description: This file contains a compatibility layer with https://github.com/getoutreach/gobox/blob/main/pkg/async/async.go | ||
package plumber | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
// AsyncRunner provides a compability adapter with async.Runner interface | ||
func AsyncRunner(runner interface { | ||
Run(ctx context.Context) error | ||
}) RunnerCloser { | ||
type Closer interface { | ||
Close(ctx context.Context) error | ||
} | ||
return GracefulRunner(func(ctx context.Context, ready ReadyFunc) error { | ||
go ready() | ||
return runner.Run(ctx) | ||
}, func(ctx context.Context) error { | ||
switch r := runner.(type) { | ||
case Closer: | ||
return r.Close(ctx) | ||
case io.Closer: | ||
return r.Close() | ||
} | ||
return nil | ||
}) | ||
} |
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,40 @@ | ||
// Copyright 2024 Outreach Corporation. All Rights Reserved. | ||
|
||
// Description: This file contains looper structs | ||
package plumber | ||
|
||
import "context" | ||
|
||
// BaseLooper is a looper struct that can be used in composition as following example: | ||
// | ||
// type Looper struct { | ||
// *plumber.BaseLooper | ||
// } | ||
// | ||
// s := &Looper{} | ||
// s.BaseLooper = plumber.NewBaseLooper(s.loop) | ||
// | ||
// func (s *Looper) loop(ctx context.Context, l *plumber.Loop) error { | ||
// .... | ||
// } | ||
type BaseLooper struct { | ||
runner RunnerCloser | ||
} | ||
|
||
// Run executes runners workload. Pipelines are starting Run method in separated goroutine. | ||
// Runner must report its readiness using given callback | ||
func (l *BaseLooper) Run(ctx context.Context, ready ReadyFunc) error { | ||
return l.runner.Run(ctx, ready) | ||
} | ||
|
||
// Close method triggers graceful shutdown on the task. It should block till task is properly closed. | ||
// When Close timeout is exceeded then given context is canceled. | ||
func (l *BaseLooper) Close(ctx context.Context) error { | ||
return l.runner.Close(ctx) | ||
} | ||
|
||
func NewBaseLooper(looper func(ctx context.Context, loop *Loop) error) *BaseLooper { | ||
return &BaseLooper{ | ||
runner: Looper(looper), | ||
} | ||
} |
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