An event store persistently records and retrieves task lifecycle events. It gives the manager a dedicated place to save events without exposing the underlying map or datastore. section 11.10 section 11.3
A small event-store implementation exposes a consistent set of responsibilities:
- Create and close the backing store, and create its bucket. section 11.10
- Put an event under a string key, retrieve it with Get, enumerate events with List, and count them with Count. section 11.10
- Use task.TaskEvent values rather than task.Task values, while presenting the same basic interface as the task store. section 11.4 section 11.10
The implementation can wrap a Go map for simple in-memory operation or BoltDB for file-backed persistence. The persistent version serializes events as JSON inside a bucket and restores them across manager or worker restarts. section 11.4 section 11.12
Switching storage implementations
A program can select either memory or persistent storage through the store.Store interface. The manager calls Put, Get, List, and Count without knowing whether events live in a map or an on-disk BoltDB file. section 11.12 section 11.3
type EventStore struct {
DbFile string
FileMode os.FileMode
Db *bolt.DB
Bucket string
}
func NewEventStore(file string, mode os.FileMode, bucket string)
(*EventStore, error) {
db, err := bolt.Open(file, mode, nil)
if err != nil {
return nil, fmt.Errorf("unable to open %v", file)
}
e := EventStore{
DbFile: file,
FileMode: mode,
Db: db,
Bucket: bucket,
}
err = e.CreateBucket()
if err != nil {
log.Printf("bucket already exists, will use it instead
of creating new one")
}
return &e, nil
}
Inspect the EventStore structure and constructor to see how a BoltDB file, file mode, database handle, and bucket are assembled. section 11.10
