Documentation ¶
Overview ¶
Package watcher defines a watcher daemon that handles events from different event sources. This package defines the interfaces for the WatcherDaemon and EventSource. Implementations for file-based event sources and watcher based on inotify is provided as well.
An example on watching a file for changes:
import ( "github.com/theia-log/selene/watcher" ) // Tail-like watcher for a log file func main() { daemon := watcher.NewWatchDaemon() src := watcher.NewFileSource("/var/log/nginx/error.log") // Add handler to print the diff. src.OnSourceEvent(func(file string, diff []byte) { fmt.Printf("%s: %s\n", file, string(diff)) }); // Attach the event source to the daemon. if _, err := daemon.AddSource(src); err != nil { panic(err) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type EventHandler ¶
EventHandler defines a type for handling a change in a particular source. The function takes two parameters: a source, the event source name, and the difference, array of bytes of the actual change. This is designed to handle changes that primarily append to the event source, so the default interpretation is that the diff is the value that have been appended to the previous value of the source:
State(event_now) - State(event_before)
type EventSource ¶
type EventSource interface { // OnSourceEvent adds an EventHandler for the generated events by this // source. OnSourceEvent(handler EventHandler) // Trigger triggers an event on this source, passing the diff value. // Note that this function, although exposed, it is mainly used internally // or by EventSource managers that can keep track of the underlying sources // like watching for file changes or reacting to appends in the system log. Trigger(diff []byte) }
EventSource defines the general interface for an event source.
func NewFileSource ¶
func NewFileSource(filePath string) EventSource
NewFileSource creates new file event source for the given file path.
type FSNotifyEventSource ¶
type FSNotifyEventSource struct { *GenericEventSource // AbsFilePath absolute path to the file. AbsFilePath string // ParentDir the parent directory of the file. ParentDir string // contains filtered or unexported fields }
FSNotifyEventSource is a file event source, based on inotify for changes notifications.
func (*FSNotifyEventSource) AttachToWatcher ¶
func (s *FSNotifyEventSource) AttachToWatcher(watcher *fsnotify.Watcher) error
AttachToWatcher attaches the parent dir to the inotify watcher.
func (*FSNotifyEventSource) DetachFromWatcher ¶
func (s *FSNotifyEventSource) DetachFromWatcher(watcher *fsnotify.Watcher) error
DetachFromWatcher deatches the parent dir from the inotify watcher.
type FSNotifyWatcher ¶
type FSNotifyWatcher struct {
// contains filtered or unexported fields
}
FSNotifyWatcher implements WatcherDaemon based on inotify events.
func (*FSNotifyWatcher) AddSource ¶
func (f *FSNotifyWatcher) AddSource(source string, eventSource EventSource) (EventSource, error)
AddSource adds an event source to be managed by this watcher dameon. If the event source is an FSNotifyEventSource, it is attached to the underlying fsnotify watcher.
func (*FSNotifyWatcher) RemoveSource ¶
func (f *FSNotifyWatcher) RemoveSource(source string) error
RemoveSource removes the event source and it is no longer managed by this daemon. If the event source is also FSNotifyEventSource it is detached from the underlying fsnotify watcher as well.
func (*FSNotifyWatcher) Start ¶
func (f *FSNotifyWatcher) Start() error
Start the watcher. When called, the watcher starts to listen for changes in the registered file sources.
func (*FSNotifyWatcher) Stop ¶
func (f *FSNotifyWatcher) Stop() error
Stop stop the watcher. No more event are going to be handled after this call. The inotify watcher is closed and resources are released.
type GenericEventSource ¶
type GenericEventSource struct { // FilePath is the source name identified by a file path. FilePath string // contains filtered or unexported fields }
GenericEventSource implements the abstract and common functionalities of an EventSource.
func NewEventSource ¶
func NewEventSource(filePath string) *GenericEventSource
NewEventSource builds new GenericEventSource for the given file path.
func (*GenericEventSource) OnSourceEvent ¶
func (g *GenericEventSource) OnSourceEvent(handler EventHandler)
OnSourceEvent registers an EventHandler to be triggered when this source is changed (produces an event).
func (*GenericEventSource) Trigger ¶
func (g *GenericEventSource) Trigger(diff []byte)
Trigger an event over this source. Used mostly internally.
type WatchDaemon ¶
type WatchDaemon interface { // Start the daemon. Events from the event sources shall be handled after // this point. Start() error // Stop the daemon. No events will be handled after this call and the daemon // will try to close all managed EventSources. Stop() error // AddSource adds an EventSource to be managed by this WatchDaemon. // Each source is identified by its name. This name can later be used to // remove the source event from this daemon. AddSource(source string, eventSource EventSource) (EventSource, error) // Removes the source event from this daemon. The underlying source for the // source event shall not be managed after this point. RemoveSource(source string) error }
WatchDaemon is a general interface for a manager of EventSource sources.