# README
Worker
distributed async task worker based on asynq.
Usage
go get -u github.com/go-cinch/common/worker
import (
"context"
"fmt"
"github.com/go-cinch/common/worker"
"time"
)
func main() {
wk := worker.New(
worker.WithRedisURI("redis://127.0.0.1:6379/0"),
worker.WithHandler(process),
)
err := wk.Error
if err != nil {
panic(err)
}
// 1. cron task
wk.Cron(
worker.WithRunUUID("order1"),
worker.WithRunGroup("task1"),
worker.WithRunExpr("0/1 * * * ?"),
)
// 2. once task
wk.Once(
worker.WithRunUUID("order2"),
worker.WithRunGroup("task2"),
worker.WithRunNow(true),
)
time.Sleep(time.Hour)
}
func process(ctx context.Context, p worker.Payload) (err error) {
switch p.Group {
case "task1":
fmt.Println(ctx, p.Uid)
case "task2":
fmt.Println(ctx, p.Uid)
}
return
}
Options
WorkerOptions
WithGroup
- group name, default taskWithRedisURI
- redis uri, default redis://127.0.0.1:6379/0WithRedisPeriodKey
- cron task cache keyWithRetention
- success task store time, default 60s, if this option is provided, the task will be stored as a completed task after successful processingWithMaxRetry
- max retry count when task has error, default 3WithHandler
- callback handlerWithCallback
- http callback uriWithClearArchived
- clear archived task internal, default 300sWithTimeout
- task timeout, default 10s
RunOptions
Cron
cron task, can be executed multiple times
WithRunUUID
- task unique idWithRunGroup
- group prefix, default groupWithRunPayload
- task payloadWithRunExpr
- cron expr, mini is one minute, refer to gorhill/cronexprWithRunMaxRetry
- max retry count when task has errorWithRunTimeout
- task timeout, default 60
Once
once task, execute only once
WithRunUUID
- task unique idWithRunGroup
- group prefix, default groupWithRunPayload
- task payloadWithRunMaxRetry
- max retry count when task has errorWithRunTimeout
- task timeout, default 60WithRunCtx
- contextWithRunIn
- run in xxx secondsWithRunAt
- run atWithRunNow
- run nowWithRunRetention
- success task store timeWithRunReplace
- remove old one and create new one when uid repeat, default false
# Functions
New is create a task worker, implemented by asynq: https://github.com/hibiken/asynq.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithRunReplace remove old one and create new one when uid repeat.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author