Yaacov Observer Save

Golang event emitter and listener with builtin file watcher package.

Project README

Observer

Go event emitter and listener with builtin file watcher package.

Go Report Card Build Status GoDoc License

Code snippets

// Create a new observer
o := observer.Observer{}
o.Open()

// Register a listener method.
o.AddListener(func(e interface{}) {
  log.Printf("Received: %v.\n", e)
})

// Emit an event.
o.Emit("Hello")

// Watch and emit events on file modifications.
o.Watch([]string{"*.html", "css/*.scss"})

Description

This observer implements event emitter and listener pattern in go, the observer register a list of listener functions and implement an event emitter, once an event is emitted, all listener functions will be called.

This observer also abstracts watching for file changes, users can register a list for files to watch, once a file is watched, events will be emitted automatically on each file modification. Common use cases are watching for changing in config files, and wating for code changes.

This observer is using golang channels for emiting events and fsnotify for watching file changes.

Examples:

Develop

$ go get -u github.com/golang/dep/cmd/dep
$ make vendor
$ make
$ ./observe

Install

go get -u github.com/yaacov/observer

Install using go get will install the package and the CLI tool observer

CLI

observer is a cli tool for watching files and executing shell commands on file modification, it is used to call an action on file change, examples of use can be restart and app when config file changes, recompile code when code updates and send image to server when image change.

inotify-tools, are a set of linux tools that monitor files for changes, they may be better choice for file monitoring on more complex cases.

Get help:

observer -h

Call a server api when config file chage:

observer -r "curl -X POST http://127.0.0.1:8000/api/v1/-/restart" -w "/root/.aws/config"

API

See examples for usage examples.

Method Description
Open() Open the observer channels
Close() Close the observer channels
AddListener(callback Listener) Add a listener function to run on event
Emit(event interface{}) Emit event
Watch(files []string) Watch for file changes, and emit a file change events
SetBufferDuration(d time.Duration) Set the event buffer damping duration
Type Description
WatchEvent struct{ Name string, Op uint32 } Event type emitted by file watcher
Listener func(interface{}) Function type for listeners
Observer struct{ Verbose bool } The observer object

Watching files for modifications

Watching files can be done using exact file name, or shell pattern matching.

Watching for exact file names:

Watch([]string{"./aws/config", "./aws/credentials"})

Watching files using shell pattern matching:

Watch([]string{"./kube/*.yml"})

Note:

We can not expand tilde to home directory, ~/.config will not work as expected. If needed users can use golang's os/user/ package.

Examples

Emit string events

Example of event listener and emitter.

emit.go

// Open an observer and start running
o := observer.Observer{}
o.Open()
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  log.Printf("Received: %s.\n", e.(string))
})

// This event will be picked by the listener
o.Emit("Hello")

Watch files, emit file change events

Example of file watching and listener.

file-watch.go

// Open an observer and start watching for files by file name
o := observer.Observer{}
o.Watch([]string{"../LICENSE"})
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  watchEvent := e.(observer.WatchEvent)
  log.Printf("File modified: %s.\n", e.Name)
})

file-watch-pattern.go

// Open an observer and start watching for file matching shell pattern
o := observer.Observer{}
o.Watch([]string{"*.html", "css/*.scss"})
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  log.Printf("File modified: %v.\n", e)
})

Group events by time, event groups will be sent once as an array of events.

emit-buffered.go

o.Open()
o.SetBufferDuration(1 * time.Second)

o.AddListener(func(e interface{}) {
	output = e.([]interface{}) // => ["done", "done", "done", "done"]
})

// All this events will be grouped together, and sent only once to the listener.
o.Emit("done")
o.Emit("done")
o.Emit("done")
o.Emit("done")
Open Source Agenda is not affiliated with "Yaacov Observer" Project. README Source: yaacov/observer
Stars
35
Open Issues
1
Last Commit
5 years ago
Repository
License

Open Source Agenda Badge

Open Source Agenda Rating