go-lmstfy-client

module
v0.0.0-...-6ea62f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2021 License: BSD-3-Clause

README

Go Lmstfy Client github actions codecov Go Report Card GitHub release date LICENSE GoDoc

Usage

Setup Lmstfy

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.

Producer Example
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

Consumer Example
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

API documentation are available via godoc

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL