https://github.com/bitleak/go-lmstfy-client
Lmstfy client in Go
https://github.com/bitleak/go-lmstfy-client
Last synced: 4 months ago
JSON representation
Lmstfy client in Go
- Host: GitHub
- URL: https://github.com/bitleak/go-lmstfy-client
- Owner: bitleak
- License: bsd-3-clause
- Created: 2021-04-22T13:45:15.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-10T13:25:26.000Z (about 5 years ago)
- Last Synced: 2024-06-20T00:27:34.898Z (almost 2 years ago)
- Language: Go
- Size: 53.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Lmstfy Client  [](https://codecov.io/gh/bitleak/go-lmstfy-client) [](https://goreportcard.com/report/github.com/bitleak/go-lmstfy-client) [](https://github.com/bitleak/go-lmstfy-client/releases) [](https://github.com/bitleak/go-lmstfy-client/blob/master/LICENSE) [](https://godoc.org/github.com/bitleak/go-lmstfy-client/client)
## Usage
### Setup Lmstfy
As simple as:
```shell
$ make setup
```
it would run the Redis server and lmstfy using docker-compose, and create the new namespace `test-ns`
with token `01F4CKPFY8XYVH6WVEQB3747CW`.
### Producer Example
```go
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)
}
```
[examples/producer](https://github.com/bitleak/go-lmstfy-client/tree/master/examples/producer/main.go)
### Consumer Example
```go
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())
}
})
}
```
[examples/consumer](https://github.com/bitleak/go-lmstfy-client/tree/master/examples/consumer/main.go)
API documentation are available via [godoc](https://godoc.org/github.com/bitleak/go-lmstfy-client/client)