TasQ is a background task worker with high performance.
You can use TasQ if your application requires some background operations. If a task requires some logic which not needs for client You can move this logic to background.
For example, there is HTTP server which requires doing some logic (send mail, exec DB queries etc.) after handling of HTTP request. In this case, you may use TasQ.
-
Task (go type) must implement tasq.Task interface which has one method Do. Signature of Do func:
func Do() error
-
Configure TasQ for your application using the following settings:
WorkersPollSize
- count of workers (go-routines) which will be run after start TasQTaskMaxRetry
- number of function calls Do when that function returnstasq.ErrRetryTask
. That's mean If functionDo
returnstasq.ErrRetryTask
background worker try to call this function again until the number of trying less thantasq.TaskMaxRetry
option orDo
function returns something else.
-
Create new instance of TasQ:
tq := tasq.New()
; -
Set up
TaskDone
andTaskFailed
functions if need:TaskDone
- the function which will be called when the task is done. Parameters:id int64
- task ID;TaskFailed
- the function which will be called when the task is failed. Parameters:id int64
- task ID,err error
- error;
-
Run TasQ:
tq.Start()
; -
Use
tq.Enqueue(task)
for adding a task to background queue; -
Use
tq.Close()
for stopping workers and for closing channel;
-
Need to print request URL parameters in one second after request:
-
Implement custom task type
custom.go
.package task import ( "log" "time" ) type CustomTask struct { logData interface{} } func NewCustomTask(logData interface{}) *CustomTask { return &CustomTask{ logData: logData, } } func (c *CustomTask) Do() error { // imitation some huge logic time.Sleep(time.Second) log.Println("parameters:", c.logData) return nil }
-
Create and run server
main.go
. Calltq.Enqueue(NewCustomTask(request.URL.Query().Encode()))
for enqueue task.package main import ( "net/http" "github.com/begmaroman/tasq" ) var ( tq *tasq.TasQ ) func init() { tq = tasq.New() } func main() { // start tasq background workers tq.Start() defer tq.Close() http.HandleFunc("/tasq", handler) log.Fatal(http.ListenAndServe(":7575", nil)) } func handler(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("add logging to background")) // add some task to background task queue // If a task requires some logic which not needs for client You can move this logic to background. backgroundTask := NewCustomTask(request.URL.Query().Encode()) tq.Enqueue(backgroundTask) }
-
Request http://localhost:7575/tasq?parameter=value and check server logs.
-
pkg: github.com/begmaroman/tasq
Test name | Iteration count | Time |
---|---|---|
BenchmarkTasQ_Enqueue_SuccessDone-4 | 3000000 | 565 ns/op |
BenchmarkTasQ_Enqueue_CustomErrorDone-4 | 2000000 | 609 ns/op |
BenchmarkTasQ_Enqueue_RetryError3-4 | 3000000 | 569 ns/op |