As simple as:
$ make setup
it would run the Redis server and lmstfy using docker-compose, and create the new namespace test-ns
with token 01F4CKPFY8XYVH6WVEQB3747CW
.
import (
"fmt"
"github.com/bitleak/go-lmstfy-client/client"
"os"
)
func main() {
c := client.NewLmstfyClient("127.0.0.1", 7777, "test-ns", "01F4CKPFY8XYVH6WVEQB3747CW")
// Optional, config the client to retry when some errors happened. retry 3 times with 50ms interval
c.ConfigRetry(3, 50)
// Publish a job with ttl==forever, tries==3, delay==1s
jobID, err := c.Publish("test-queue", []byte("hello"), 0, 3, 1)
if err != nil {
fmt.Printf("Failed to publish the new job, err: %s\n", jobID)
os.Exit(1)
}
fmt.Printf("Published the new job with id: %s\n", jobID)
}
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/bitleak/go-lmstfy-client/client"
)
func registerSignal(shutdownFn func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, []os.Signal{
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
}...)
go func() {
for range c {
shutdownFn()
os.Exit(0)
}
}()
}
func main() {
consumer := client.NewConsumer(&client.ConsumerConfig{
Host: "127.0.0.1",
Port: 7777,
Namespace: "test-ns",
Token: "01F4CKPFY8XYVH6WVEQB3747CW",
Queues: []string{"test-queue"},
Threads: 4, // how many polling threads which would affect the performance
TTR: 30, // unit was second, determine when the job would be visible again
})
registerSignal(func() {
consumer.Close()
fmt.Println("Bye, Going to shutdown the consumer")
})
fmt.Println("Hello, Going to consume jobs from queue")
_ = consumer.Receive(context.Background(), func(ctx context.Context, job *client.Job) {
fmt.Printf("Got new job: %s\n", job.ID)
if err := job.Ack(); err != nil {
fmt.Printf("Failed to ack the job: %s, err: %s\n", job.ID, err.Error())
}
})
}
API documentation are available via godoc