sneaker

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: MIT Imports: 2 Imported by: 0

README

go-sneaker

A fast background processing framework for Golang and RabbitMQ

Usage

Use go get

go get github.com/grantchen/go-sneaker

Then import the go-sneaker package into your own code.

import "github.com/grantchen/go-sneaker"
Publisher

send background task code sample

Publisher, err := sneaker.NewPublisher(amqp_url, exchange_key))
if err != nil {
  panic(err)
}

var json = jsoniter.ConfigCompatibleWithStandardLibrary
dataJsonByte, _ := json.Marshal(map[string]string{"key": "value"})
err = Publisher.Publish("queue_name", "text/json", dataJsonByte)
if err != nil {
  panic(err)
}

// if not use can close it
Publisher.Close()
Publish Parameters

Publish(queueName, bodyContentType string, body []byte)

queueName - Queue Name
bodyContentType - send body content type. Default is text/json
body - send data body byte
Consumer

background task handle worker sample

Consumer, err := sneaker.NewConsumer(amqp_url, exchange_key))
if err != nil {
  panic(err)
}

err = Consumer.Consume("queue_name", map[string]interface{}{}, handleWorker)
if err != nil {
  panic(err)
}

func handleWorker(body []byte){
  //...
}
Consume Parameters

Consume(queueName string, args map[string]interface{}, f fn)

queueName - Queue Name
args - consume releated params. Deafult values:

     {"durable": true, "autoDelete": false, "autoAck": false,
		 "exclusive": false, "noWait": false,
		 "noLocal": false, "consumer": "", "threads": 5}
    
     durable - is queue durable
     autoDelete - is queue delete when unused
     autoAck - is consume auto ack
     exclusive - is queue and consume exclusive
     noWait - is queue and consume no wait
     noLocal - is consume no local
     consumer - consumer name
     threads - how many threads to handle worker method f

f - handle body method

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consumer

type Consumer struct {
	Connection   *amqp.Connection
	Channel      *amqp.Channel
	ExchangeName string
}

func NewConsumer

func NewConsumer(amqpUrl, exchangeName string) (*Consumer, error)

func (*Consumer) Consume

func (c *Consumer) Consume(queueName string, args map[string]interface{}, f fn) error

consume a worker queue exchaneName - exchange Name queueName - queueName

type Publisher

type Publisher struct {
	Connection   *amqp.Connection
	Channel      *amqp.Channel
	ExchangeName string
}

func NewPublisher

func NewPublisher(amqpUrl, exchangeName string) (*Publisher, error)

func (*Publisher) Close

func (c *Publisher) Close()

func (*Publisher) Publish

func (c *Publisher) Publish(queueName, bodyContentType string, body []byte) error

publish a worker queue

Jump to

Keyboard shortcuts

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