tickler

package module
v0.0.0-...-c9e7073 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 7 Imported by: 0

README

tickler

Tickler, is a library for long-running services to enqueue and process jobs in the background with simplicity and performance in mind.

Installation

With Go module support (Go 1.11+), simply add the following import

import "github.com/goodjobtech/tickler"

Otherwise, to install the tickler, run the following command:

go get -u github.com/goodjobtech/tickler

Usage

Enqueue a job

package main

import (
	"fmt"
	"github.com/goodjobtech/tickler"
)

func main() {
	tl := tickler.New()
	tl.Start()

	request := tickler.Request{
		Job: func() error {
			fmt.Println("Hello World!")
			return nil
		},
		Name: "hello-world",
	}

	tl.Enqueue(request)
}

Wait for another job to be completed

package main

import (
	"fmt"
	"github.com/goodjobtech/tickler"
	"time"
)

func main() {
	tl := tickler.New()
	tl.Start()

	request := tickler.Request{
		Job: func() error {
			time.Sleep(time.Second * 5)
			return nil
		},
		Name: "cpu-heavy-job",
	}

	tl.Enqueue(request)

	waitRequest := tickler.Request{
		Job: func() error {
			fmt.Println("CPU heavy job is done!")
			return nil
		},
		Name: "waiting",
	}

	tl.Enqueue(waitRequest, tickler.WaitFor("cpu-heavy-job"))
}

Enqueue a job if a given job is successfully completed

If a job returns with no error, i.e. err == nil, it is succeeded.

package main

import (
	"fmt"
	"github.com/goodjobtech/tickler"
)

func main() {
	tl := tickler.New()
	tl.Start()

	request := tickler.Request{
		Job: func() error {
			fmt.Println("Hello World!")
			return nil
		},
		Name: "hello-world",
	}

	tl.Enqueue(request)

	waitRequest := tickler.Request{
		Job: func() error {
			fmt.Println("I am waiting")
			return nil
		},
		Name: "waiting",
	}
	
	// This will be executed
	tl.Enqueue(waitRequest, tickler.IfSuccess("hello-world"))
}

Enqueue a job if a given job is failed

If a job returns with error, i.e. err != nil, it is failed.

package main

import (
	"errors"
	"fmt"
	"github.com/goodjobtech/tickler"
)

func main() {
	tl := tickler.New()
	tl.Start()

	request := tickler.Request{
		Job: func() error {
			fmt.Println("Hello World!")
			return errors.New("I failed")
		},
		Name: "hello-world",
	}

	tl.Enqueue(request)

	waitRequest := tickler.Request{
		Job: func() error {
			fmt.Println("I am waiting")
			return nil
		},
		Name: "waiting",
	}

	// This will be executed
	tl.Enqueue(waitRequest, tickler.IfFailure("hello-world"))
}

Retry a failed job

tickler uses exponential backoff mechanism to retry failed jobs

package main

import (
	"errors"
	"fmt"
	"github.com/goodjobtech/tickler"
)

func main() {
	tl := tickler.New()
	tl.Start()

	counter := 3

	request := tickler.Request{
		Job: func() error {
			if counter == 0 {
				fmt.Println("beat that counter")
				return nil
			}

			counter--
			return errors.New("still got some counter")
		},
		Name: "retry",
	}

	// This job will be called maximum 4 times.
	tl.Enqueue(request, tickler.WithRetry(4))
}

Contributing

All kinds of pull request and feature requests are welcomed!

License

Tickler's source code is licensed under MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackgroundFunction

type BackgroundFunction = func() error

type EventOption

type EventOption interface {
	// contains filtered or unexported methods
}

func IfFailure

func IfFailure(jobNames ...JobName) EventOption

IfFailure runs the given function if the job(s) fails.

func IfSuccess

func IfSuccess(jobNames ...JobName) EventOption

IfSuccess runs the given function if the job(s) succeeds.

func WaitFor

func WaitFor(jobNames ...JobName) EventOption

func WithRetry

func WithRetry(maxRetries int) EventOption

WithRetry runs the given function with the given number of retries. It uses the truncated exponential backoff algorithm to determine the delay between retries.

type JobName

type JobName = string

type Options

type Options struct {
	Limit int64
	Ctx   context.Context
	// contains filtered or unexported fields
}

type Request

type Request struct {
	Job  BackgroundFunction
	Name JobName
}

Request is a request to be executed

type Tickler

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

Tickler is a service that can be used to schedule and run the background tasks/jobs

func New

func New() *Tickler

New creates a new Tickler with default settings. The Tickler will not start until the Start method is called.

func (*Tickler) Enqueue

func (s *Tickler) Enqueue(request Request, opts ...EventOption)

func (*Tickler) EnqueueWithContext

func (s *Tickler) EnqueueWithContext(ctx context.Context, request Request, opts ...EventOption)

func (*Tickler) GetContext

func (s *Tickler) GetContext() context.Context

GetContext returns the context of the tickler

func (*Tickler) GetCurrentJobs

func (s *Tickler) GetCurrentJobs() map[string]bool

GetCurrentJobs returns a list of jobs that are currently running

func (*Tickler) GetQueueLength

func (s *Tickler) GetQueueLength() int

GetQueueLength returns the length of the queue

func (*Tickler) Limit

func (s *Tickler) Limit(newLimit int)

Limit the number of jobs that can be run at the same time the default size is 100.

func (*Tickler) SetContext

func (s *Tickler) SetContext(ctx context.Context)

SetContext sets the context for the tickler

func (*Tickler) Start

func (s *Tickler) Start()

Start the tickler service

func (*Tickler) Stop

func (s *Tickler) Stop()

Jump to

Keyboard shortcuts

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