-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplayer.go
More file actions
31 lines (25 loc) · 763 Bytes
/
player.go
File metadata and controls
31 lines (25 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package orchestra
import (
"context"
"os"
"os/signal"
"github.com/cenkalti/backoff/v4"
)
// Player is a long running background worker
type Player interface {
Play(context.Context) error
}
// PlayerWithBackoff is a player that can be restarted with a backoff strategy
type PlayerWithBackoff interface {
Player
// A backoff strategy to use when the player fails but returns ErrRestart
// NOTE: This is only called once before the player is started, so it should be
// idempotent
Backoff() backoff.BackOff
}
// PlayUntilSignal starts the player and stops when it receives os.Signals
func PlayUntilSignal(ctx context.Context, p Player, sig ...os.Signal) error {
ctx, cancel := signal.NotifyContext(ctx, sig...)
defer cancel()
return p.Play(ctx)
}