-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwatcher.go
59 lines (47 loc) · 2.44 KB
/
watcher.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
package sarah
import (
"context"
"errors"
"fmt"
)
// ErrWatcherNotRunning is returned when ConfigWatcher.Unwatch is called but the context is already canceled.
var ErrWatcherNotRunning = errors.New("context is already canceled")
// ErrAlreadySubscribing is returned when duplicated calls to ConfigWatcher.Watch occur.
var ErrAlreadySubscribing = errors.New("already subscribing")
// ConfigNotFoundError is returned when a corresponding configuration is not found.
// This is typically returned when the caller tries to see if there is any configuration available via ConfigWatcher.Read.
type ConfigNotFoundError struct {
BotType BotType
ID string
}
// Error returns stringified representation of the error.
func (err *ConfigNotFoundError) Error() string {
return fmt.Sprintf("no configuration found for %s:%s", err.BotType, err.ID)
}
var _ error = (*ConfigNotFoundError)(nil)
// ConfigWatcher defines an interface that all "watcher" implementations must satisfy.
// A watcher subscribes to any change on the configuration setting of Command or ScheduledTask.
// When a change is detected, ConfigWatcher calls the callback function to apply the change to the configuration values Command or ScheduledTask is referring to.
// One example could be watchers.fileWatcher that subscribes to configuration file changes;
// while another reference implementation -- https://github.com/oklahomer/go-sarah-githubconfig -- subscribes to changes on a given GitHub repository.
type ConfigWatcher interface {
// Read reads the latest configuration value and apply that value to configPtr.
Read(botCtx context.Context, botType BotType, id string, configPtr interface{}) error
// Watch subscribes to given id's configuration.
// When a change to the corresponding configuration value occurs, callback is called.
// A call to callback function triggers go-sarah's core to call Read() to reflect the latest configuration value.
Watch(botCtx context.Context, botType BotType, id string, callback func()) error
// Unwatch is called when Bot is stopped and subscription is no longer required.
Unwatch(botType BotType) error
}
type nullConfigWatcher struct{}
var _ ConfigWatcher = (*nullConfigWatcher)(nil)
func (*nullConfigWatcher) Read(_ context.Context, _ BotType, _ string, _ interface{}) error {
return nil
}
func (*nullConfigWatcher) Watch(_ context.Context, _ BotType, _ string, _ func()) error {
return nil
}
func (*nullConfigWatcher) Unwatch(_ BotType) error {
return nil
}