Browse Source

Adds example

master
Noah Pederson 2 years ago
parent
commit
e244e29215
  1. 74
      README.md
  2. 58
      examples/basic.go

74
README.md

@ -1,5 +1,75 @@
# goalpost
[![Build Status](https://build.packetlostandfound.us/api/badges/chiefnoah/goalpost/status.svg)](https://build.packetlostandfound.us/chiefnoah/goalpost)
[![Build Status](https://build.packetlostandfound.us/api/badges/chiefnoah/goalpost/status.svg)](https://build.packetlostandfound.us/chiefnoah/goalpost) [![](https://godoc.org/git.packetlostandfound.us/chiefnoah/goalpost?status.svg)](http://godoc.org/git.packetlostandfound.us/chiefnoah/goalpost)
Goalpost is a durable, embedable, worker queue for Golang
It makes use of boltdb/bbolt to provide durability so messages don't get lost when the process get's killed.
## Quickstart
Below is a simple use of goalpost:
```golang
package main
import (
"context"
"fmt"
"time"
)
import "git.packetlostandfound.us/chiefnoah/goalpost"
const eventQueueID = "event-queue"
//Define a type that implements the goalpost.Worker interface
type worker struct {
id string
}
func (w *worker) ID() string {
return w.id
}
func (w *worker) DoWork(ctx context.Context, j *goalpost.Job) error {
//do something cool!
fmt.Printf("Hello, %s\n", j.Data)
//Something broke, but we should retry it...
if j.RetryCount < 9 { //try 10 times
return goalpost.NewRecoverableWorkerError("Something broke, try again")
}
//Something *really* broke, don't retry
//return errors.New("Something broke, badly")
//Everything's fine, we're done here
return nil
}
func main() {
//Init a queue
q, _ := goalpost.Init(eventQueueID)
//remember to handle your errors :)
//Create a worker with id "worker-id"
w := &worker{
id: "worker-1",
}
//register the worker, so it can do work
q.RegisterWorker(w)
//Let's do some work...
q.PushBytes([]byte("World"))
//You should see "Hello, World" printed 10 times
//Make sure your process doesn't exit before your workers can do their work
time.Sleep(10 * time.Second)
//Remember to close your queue when you're done using it
q.Close()
}
```
Goalpost is a durable, embedable, worker queue for Golang.

58
examples/basic.go

@ -0,0 +1,58 @@
package main
import (
"context"
"fmt"
"time"
)
import "git.packetlostandfound.us/chiefnoah/goalpost"
const eventQueueID = "event-queue"
//Define a type that implements the goalpost.Worker interface
type worker struct {
id string
}
func (w *worker) ID() string {
return w.id
}
func (w *worker) DoWork(ctx context.Context, j *goalpost.Job) error {
//do something cool!
fmt.Printf("Hello, %s\n", j.Data)
//Something broke, but we should retry it...
if j.RetryCount < 9 { //try 10 times
return goalpost.NewRecoverableWorkerError("Something broke, try again")
}
//Something *really* broke, don't retry
//return errors.New("Something broke, badly")
//Everything's fine, we're done here
return nil
}
func main() {
//Init a queue
q, _ := goalpost.Init(eventQueueID)
//remember to handle your errors :)
//Create a worker with id "worker-id"
w := &worker{
id: "worker-1",
}
//register the worker, so it can do work
q.RegisterWorker(w)
//Let's do some work...
q.PushBytes([]byte("World"))
//You should see "Hello, World" printed 10 times
//Make sure your process doesn't exit before your workers can do their work
time.Sleep(10 * time.Second)
//Remember to close your queue when you're done using it
q.Close()
}