/
manifold.go
70 lines (57 loc) · 1.74 KB
/
manifold.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package simplesignalhandler
import (
"fmt"
"os"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
)
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
}
// ManifoldConfig is responsible for configuring this worker.
type ManifoldConfig struct {
// SignalCh is a preconfigured channel listening for signals.
SignalCh <-chan os.Signal
// DefaultHandlerError is the default error to return from this worker when
// there is no mapping for the received signal. Value must be specified.
DefaultHandlerError error
// HandlerErrors is a map of os.Signal to error returns from this worker.
// Valid for this map to be nil or empty.
HandlerErrors map[os.Signal]error
// Logger to use for this worker
Logger Logger
}
// Manifold returns the dependency manifold for this worker based on the config
// provided.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: nil,
Output: nil,
Start: config.Start,
}
}
// Start is responsible for creating a new worker for the manifold config.
func (m ManifoldConfig) Start(context dependency.Context) (worker.Worker, error) {
if err := m.Validate(); err != nil {
return nil, err
}
return NewSignalWatcher(
m.Logger,
m.SignalCh,
SignalHandler(m.DefaultHandlerError, m.HandlerErrors),
)
}
// Validate validates the manifold config.
func (m ManifoldConfig) Validate() error {
if m.SignalCh == nil {
return fmt.Errorf("%w SignalCh cannot be nil", errors.NotValid)
}
if m.DefaultHandlerError == nil {
return fmt.Errorf("%w DefaultHandlerError cannot be nil", errors.NotValid)
}
return nil
}