/go-taskqueue-lite

Lite Task/Job queue library using go rutines and channels

Primary LanguageGoMIT LicenseMIT

Go-TaskQueue-Lite

GoDoc Go

An easy way to perform background processing in go.

  • Thread-Safe
  • Custom workers pool size
  • Supported background tasks/jobs
    • only once
    • scheduled
    • recurring

Installation

Install Go-TaskQueue-Lite using go get

$ go get github.com/ssubedir/go-taskqueue-lite

Example

Import this package and write

package main
import (
	"fmt"
	tq "github.com/ssubedir/go-taskqueue-lite"
)
type TestTask struct {
	ID int
}
func (t *TestTask) Run() {
	fmt.Printf("Task - '%d'\n", t.ID)
}
func main() {
	// queue with 8 workers
	queue := tq.NewQueue(8)
	queue.Start()
	defer queue.Stop()
	// Run Tasks
	...
	...
	...
}

Task Interface:

// Task interface
type Task interface {
	Run()
}

All tasks must implement Run()

type TestTask struct {
    // task struct
}
func (t *TestTask) Run() {
    // do task
}

Only once task

Enqueue Parameters

Enqueue(Task Task)

only once tasks are executed only once and almost immediately after creation.

queue := tq.NewQueue(8)
queue.Start()
defer queue.Stop()
queue.Enqueue(&TestTask{1})

Delayed task

Schedule(Task Task, duration_string  string)

A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "2h45m". Valid time units are "s", "m", "h".


// example duration_string inputs

"10h"
"1h10m10s"
"1h1m"
"20s"

Delayed tasks are executed only once too, but not immediately, after a certain time interval.

queue := NewQueue(8)
queue.Start()
defer queue.Stop()
queue.Schedule(&TestTask{1}, "10s")

Recurring task

Recurring(Task Task, duration_string  string)

A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "2h45m". Valid time units are "s", "m", "h".

// example duration_string inputs

"10h"
"1h10m10s"
"1h1m"
"20s"

Recurring task are executed many times on the specified schedule.

queue := NewQueue(8)
queue.Start()
defer queue.Stop()
queue.Recurring(&TestTask{1}, "1m10s")

Todos

  • Batch tasks

Built With

  • GO - Programming language

License

This project is licensed under the MIT License - see the LICENSE.md file for details