-
Notifications
You must be signed in to change notification settings - Fork 4
/
controller.go
79 lines (69 loc) · 2.59 KB
/
controller.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
package controller
import (
"context"
"github.com/aperturerobotics/controllerbus/config"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
)
// Controller tracks a particular process.
type Controller interface {
// Handler handles directives.
directive.Handler
// GetControllerInfo returns information about the controller.
GetControllerInfo() *Info
// Execute executes the given controller.
// Returning nil ends execution.
// Returning an error triggers a retry with backoff.
Execute(ctx context.Context) error
// Close releases any resources used by the controller.
// Error indicates any issue encountered releasing.
Close() error
}
// ConstructOpts contains optional parameters when constructing a controller.
type ConstructOpts struct {
// Logger is the root logger to use.
Logger *logrus.Entry
}
// GetLogger returns the specified logger or a default one if nil.
func (c *ConstructOpts) GetLogger() *logrus.Entry {
if l := c.Logger; l != nil {
return l
}
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
return logrus.NewEntry(log)
}
// Factory represents an available controller implementation.
// The factory can construct instances of the controller given configuration.
type Factory interface {
// GetConfigID returns the unique config ID for the controller.
GetConfigID() string
// ConstructConfig constructs an instance of the controller configuration.
ConstructConfig() config.Config
// Construct constructs the associated controller given configuration.
Construct(config.Config, ConstructOpts) (Controller, error)
// GetVersion returns the version of this controller.
GetVersion() semver.Version
}
// FactoryWithContext is a factory with a cancelation context.
type FactoryWithContext interface {
Factory
// GetFactoryContext returns a context that is canceled if the factory is unloaded.
GetFactoryContext() context.Context
}
// FactoryResolver looks up factories that match configurations.
type FactoryResolver interface {
// GetResolverID returns the resolver identifier.
// Ex: static, go-plugin
GetResolverID() string
// GetResolverVersion returns the resolver version.
GetResolverVersion() semver.Version
// GetConfigCtorByID returns a config constructor by ID.
// If none found, return nil, nil
GetConfigCtorByID(ctx context.Context, id string) (config.Constructor, error)
// GetFactoryMatchingConfig returns the factory that matches the config.
// If no factory is found, return nil.
// If an unexpected error occurs, return it.
GetFactoryMatchingConfig(ctx context.Context, conf config.Config) (Factory, error)
}