Provides future scheduling built over Google's pubsub.
Pub/Sub is a fully managed realtime messaging service. This package provides functionality of future scheduling of messages built over google's pubsub. For example - send a reminder for an event which is going to happen in future.
The package exposes a simple Publish
method which takes a 'time of message' as an input. If the time is within the specified window the message is published immediately, else a new message is created which wraps the original message and pushed over a new topic named 'scheduler'.
The package also provides a worker which listens on this topic and receives the message as soon as it is published. The worker stores the message in a datastore and publishes accordingly when the correct time has arrived.
The worker currently stores the messages in a boltdb datastore. Any datastore which can implement the DataStore
interface will work. Using boltdb has several advantages though:
- No extra dependency of maintaining a separate datastore
- The operations needed are very nicely handled by boltdb as it stores the key in a lexicographic order and we need the keys in time sorted order. (So, that we simple store key the time in RFC3339 format as the key)
- Workers will work even in a distributed environment. Every worker will have their own subset of data and will schedule them accordingly.
- Set the "GOOGLE_APPLICATION_CREDENTIALS" environment variable.
- Start the worker
go build && ./worker -projectId <your_project_id>
- Use the publisher from your application like this:
func main() {
pubsubscheduler.InitPubSub(<projectId>, time.Second)
}
func someFunc() {
_, err := pubsubscheduler.Publish(<your_topic>, time.Now().Add(time.Hour), data_bytes, attributesMap)
...
}
Licensed under the MIT License. See the LICENSE file for more information.