retry

package module
v0.0.0-...-8ab0566 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2021 License: GPL-3.0 Imports: 15 Imported by: 0

README

Retry Go

Retry is a go lib which helps to implement retrying execution mechanics with persistence.

In many situations not everything goes as planned. Retry is designed to run a feature and retry as many times as necessary if it fails.
To retry is also to try each time. Retry is designed to retry indefinitely and implement scheduled job (cron) mechanisms, such as hourly automatic data purging etc.

You can easily implement HTTP POST / GET sends, execute a command, create an email sending mechanic and never miss an event!

As a program must be updated, a persistence in Filesystems, badgerDB, gorm is available to retry exactly where it stopped.

How to use with http call

go get github.com/realPy/retry

import the lib with:

import "github.com/realPy/retry"

Import the storage manager need:

import retrydb "github.com/realPy/retry/store/fs"

Create the queue

rq := retry.RetryQueue{}

Init you queue wity a storage manager

rq.Init(retrydb.NewRStoreFS("./spool", "slrx_"))

Start the queue

rq.Start()

Enqueue the data to POST with and print Success exec if the exec sucessfull

rq.EnqueueExecAndRetry(retry.HTTP{
    Node:         retry.Node{Description: "HTTPRequest", MaxRetry: 4},
    URL:          server.URL,
    Method:       "POST",
    HTTPPostData: map[string][]string{"data1": {"yes"}},
    HTTPGetData:  map[string][]string{"no": {"yes"}},
    HTTPHeader:   map[string]string{"User-Agent": "noneman"},
    WaitingTime:  10,
    SuccessFunc: func(h *retry.HTTP) error {
			fmt.Printf("Success exec\n")
			return nil
	},
})

Delayed POST

	rq.EnqueueExecAndRetry(retry.DelayedNode(retry.HTTP{
		Node:         retry.Node{Description: "HTTPRequest", MaxRetry: 4},
		URL:          server.URL,
		Method:       "POST",
		HTTPPostData: map[string][]string{"data1": {"yes"}},
		HTTPGetData:  map[string][]string{"no": {"yes"}},
		HTTPHeader:   map[string]string{"User-Agent": "noneman"},
		WaitingTime:  10,
		SuccessFunc: func(h *retry.HTTP) error {
			fmt.Printf("Success exec\n")
			return nil
		}}, time.Duration(10)*time.Second),
	)

How to implement my own retry

Implement your own retry is easy.
You need to use struct embbed Node that contain minimum information the system need
You can extended your struct with the elements of your need

Function implementations:
The lib use persist to save your jobs on disk or database. The lib known how the Node is struct but not how yout struct is.
You Must implement the interface BinaryMarshaler ( MarshalBinary() (data []byte, err error) ) and BinaryUnMarshaler (UnmarshalBinary(data []byte) error)
Dont forget to call the encode et decode function of Node and your own encode and decode follow you struct.
(see http implementation)
Implement the nodeRetry interface and voila :)

type NodeRetry interface {
	Init()
	Execute(ctx context.Context) error
	GetData() Node
	SetData(Node) NodeRetry
	OnSuccess() error
	OnFailed(error) error
	TimeToSleepOnNextFailed(int) time.Duration
	OnFinalFailed(error) error
}

How to implement a scheduled job

Scheduled job is a retry infinite that always failed :)
To implement a "cron like" job, pass the parameter MaxRetry to retry.MAXRETRYINFINITE and NoPersist to true.
Dont use persist on sheduled job or you will stack of them on each restart of your service.
OnSuccess method interface always return an error (ErrCronRetryAlways is for that) even the execute function success. Return an error on success ensure to retry your event

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrRetryNotImplemented return error when default behaviour
	ErrRetryNotImplemented = errors.New("The method is not implemented")
)
View Source
var (
	//MAXRETRYINFINITE generate infinite retry (cron)
	MAXRETRYINFINITE = -1
)

Functions

func DelayedNode

func DelayedNode(node interface{}, time time.Duration) interface{}

DelayedNode Create a Delayed node container

Types

type CMD

type CMD struct {
	Node
	CmdLine     string
	Args        []string
	WaitingTime int
}

CMD Struct

func (CMD) Execute

func (c CMD) Execute(ctx context.Context) error

Execute implement retry interface

func (CMD) GetData

func (c CMD) GetData() Node

GetData implement retry interface

func (CMD) Init

func (c CMD) Init()

Init implement retry interface

func (CMD) MarshalBinary

func (c CMD) MarshalBinary() ([]byte, error)

MarshalBinary implement retry interface

func (CMD) OnFailed

func (c CMD) OnFailed(e error) error

OnFailed implement retry interface

func (CMD) OnFinalFailed

func (c CMD) OnFinalFailed(e error) error

OnFinalFailed implement retry interface

func (CMD) OnSuccess

func (c CMD) OnSuccess() error

OnSuccess implement retry interface

func (CMD) SetData

func (c CMD) SetData(n Node) NodeRetry

SetData implement retry interface

func (CMD) TimeToSleepOnNextFailed

func (c CMD) TimeToSleepOnNextFailed(retry int) time.Duration

TimeToSleepOnNextFailed implement retry interface

func (*CMD) UnmarshalBinary

func (c *CMD) UnmarshalBinary(data []byte) error

UnmarshalBinary implement retry interface

type CMDFailed

type CMDFailed func(*CMD, error)

CMDFailed CMDFailed method

type CMDFinalFailed

type CMDFinalFailed func(*CMD, error)

CMDFinalFailed CMDFinalFailed method

type CMDSuccess

type CMDSuccess func(*CMD) error

CMDSuccess CMDSuccess method

type HTTP

type HTTP struct {
	Node
	URL             string
	Method          string
	HTTPPostData    map[string][]string
	HTTPGetData     map[string][]string
	HTTPHeader      map[string]string
	WaitingTime     int
	SuccessFunc     HTTPSuccess
	FailedFunc      HTTPFailed
	FinalFailedFunc HTTPFinalFailed
}

HTTP Struct

func (HTTP) Execute

func (h HTTP) Execute(ctx context.Context) error

Execute implement retryit interface

func (HTTP) GetData

func (h HTTP) GetData() Node

GetData implement retryit interface

func (HTTP) Init

func (h HTTP) Init()

Init implement retryit interface

func (HTTP) MarshalBinary

func (h HTTP) MarshalBinary() ([]byte, error)

MarshalBinary implement retryit interface

func (HTTP) OnFailed

func (h HTTP) OnFailed(e error) error

OnFailed implement retryit interface

func (HTTP) OnFinalFailed

func (h HTTP) OnFinalFailed(e error) error

OnFinalFailed implement retryit interface

func (HTTP) OnSuccess

func (h HTTP) OnSuccess() error

OnSuccess implement retryit interface

func (HTTP) SetData

func (h HTTP) SetData(n Node) NodeRetry

SetData implement retryit interface

func (HTTP) TimeToSleepOnNextFailed

func (h HTTP) TimeToSleepOnNextFailed(retry int) time.Duration

TimeToSleepOnNextFailed implement retryit interface

func (*HTTP) UnmarshalBinary

func (h *HTTP) UnmarshalBinary(data []byte) error

UnmarshalBinary implement retryit interface

type HTTPFailed

type HTTPFailed func(*HTTP, error)

HTTPFailed HTTPFailed method

type HTTPFinalFailed

type HTTPFinalFailed func(*HTTP, error)

HTTPFinalFailed HTTPFinalFailed method

type HTTPSuccess

type HTTPSuccess func(*HTTP) error

HTTPSuccess HTTPSuccess method

type Node

type Node struct {
	//public
	Description string
	MaxRetry    int
	NoPersist   bool
	Name        string
	// contains filtered or unexported fields
}

Node Node struct

func (Node) Execute

func (n Node) Execute(ctx context.Context) error

Execute implement retryit interface

func (*Node) GetAttempt

func (n *Node) GetAttempt() int

GetAttempt number of retry

func (Node) GetData

func (n Node) GetData() Node

GetData implement retryit interface

func (Node) Init

func (n Node) Init()

Init implement retryit interface

func (Node) MarshalBinary

func (n Node) MarshalBinary() ([]byte, error)

MarshalBinary implement marshal

func (*Node) NodeGobDecode

func (n *Node) NodeGobDecode(dec *gob.Decoder) error

NodeGobDecode Decode for deserialised

func (*Node) NodeGobEncode

func (n *Node) NodeGobEncode(enc *gob.Encoder) error

NodeGobEncode Encode for serialise

func (Node) OnFailed

func (n Node) OnFailed(e error) error

OnFailed implement retryit interface

func (Node) OnFinalFailed

func (n Node) OnFinalFailed(e error) error

OnFinalFailed implement retryit interface

func (Node) OnSuccess

func (n Node) OnSuccess() error

OnSuccess implement retryit interface

func (Node) SetData

func (n Node) SetData(value Node) NodeRetry

SetData implement retryit interface

func (Node) String

func (n Node) String() string

func (Node) TimeToSleepOnNextFailed

func (n Node) TimeToSleepOnNextFailed(retry int) time.Duration

TimeToSleepOnNextFailed implement retryit interface

func (*Node) UnmarshalBinary

func (n *Node) UnmarshalBinary(data []byte) error

UnmarshalBinary Implement unmarshall

type NodeRemove

type NodeRemove struct {
	// contains filtered or unexported fields
}

NodeRemove type of struct to remove node by name

type NodeRetry

type NodeRetry interface {
	Init()
	Execute(context.Context) error
	GetData() Node
	SetData(Node) NodeRetry
	OnSuccess() error
	OnFailed(error) error
	TimeToSleepOnNextFailed(int) time.Duration
	OnFinalFailed(error) error
}

NodeRetry NodeRetry struct

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue Queue struct

func (*Queue) EnqueueExecAndRetry

func (rq *Queue) EnqueueExecAndRetry(n interface{})

EnqueueExecAndRetry Enqueue node and exec

func (*Queue) Init

func (rq *Queue) Init(store RStore)

Init init queue and store

func (*Queue) Persist

func (rq *Queue) Persist(uuid string, data interface{}) error

Persist save all queue on storage

func (*Queue) Register

func (rq *Queue) Register(i interface{}, success func(interface{}) error, failed func(interface{}, error), finalFailed func(interface{}, error))

Register Register default for type

func (*Queue) RemoveByName

func (rq *Queue) RemoveByName(name string)

RemoveByName Remove a node by name

func (*Queue) Start

func (rq *Queue) Start()

Start Load in store and run loop

func (*Queue) Stop

func (rq *Queue) Stop()

Stop Enqueue Stop the current queue

type RStore

type RStore interface {
	ParseAll(func(string, []byte) error)
	Delete(string) error
	Store(string, func() (*bytes.Buffer, error)) error
}

RStore RStore struct

Directories

Path Synopsis
example
store
fs

Jump to

Keyboard shortcuts

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