-
Notifications
You must be signed in to change notification settings - Fork 491
/
manifold.go
97 lines (85 loc) · 2.52 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package filenotifywatcher
import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/juju/juju/worker/common"
)
// Logger represents the logging methods called.
type Logger interface {
Errorf(message string, args ...interface{})
Warningf(message string, args ...interface{})
Infof(message string, args ...interface{})
Debugf(message string, args ...interface{})
Tracef(message string, args ...interface{})
IsTraceEnabled() bool
}
// WatcherFn is a function that returns a new Watcher.
type WatcherFn = func(string, ...Option) (FileWatcher, error)
// ManifoldConfig defines the names of the manifolds on which a Manifold will
// depend.
type ManifoldConfig struct {
Clock clock.Clock
Logger Logger
NewWatcher WatcherFn
NewINotifyWatcher func() (INotifyWatcher, error)
}
func (cfg ManifoldConfig) Validate() error {
if cfg.Clock == nil {
return errors.NotValidf("nil Clock")
}
if cfg.Logger == nil {
return errors.NotValidf("nil Logger")
}
if cfg.NewWatcher == nil {
return errors.NotValidf("nil NewWatcher")
}
if cfg.NewINotifyWatcher == nil {
return errors.NotValidf("nil NewINotifyWatcher")
}
return nil
}
// Manifold returns a dependency manifold that runs the filenotifywatcher
// worker, using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{},
Output: fileNotifyWatcherOutput,
Start: func(context dependency.Context) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
cfg := WorkerConfig{
Clock: config.Clock,
Logger: config.Logger,
NewWatcher: config.NewWatcher,
NewINotifyWatcher: config.NewINotifyWatcher,
}
w, err := newWorker(cfg)
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
},
}
}
func fileNotifyWatcherOutput(in worker.Worker, out interface{}) error {
if w, ok := in.(*common.CleanupWorker); ok {
in = w.Worker
}
w, ok := in.(*fileNotifyWorker)
if !ok {
return errors.Errorf("in should be a *fileNotifyWorker; got %T", in)
}
switch out := out.(type) {
case *FileNotifyWatcher:
var target FileNotifyWatcher = w
*out = target
default:
return errors.Errorf("out should be a *filenotifywatcher.FileNotifyWatcher; got %T", out)
}
return nil
}