distributed async task worker based on asynq.
go get -u github.com/go-cinch/common/worker
import (
"context"
"fmt"
"github.com/go-cinch/common/worker"
)
func main() {
wk := worker.New(
// redis://[:password@]host[:port][/dbnumber]
// worker.WithRedisURI("redis://:password@127.0.0.1:6379/0"),
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(
context.Background(),
worker.WithRunUUID("order1"),
worker.WithRunGroup("task1"),
worker.WithRunExpr("0/1 * * * ?"),
)
// 2. once task
_ = wk.Once(
context.Background(),
worker.WithRunUUID("order2"),
worker.WithRunGroup("task2"),
worker.WithRunNow(true),
)
ch := make(chan struct{})
<-ch
}
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
}
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
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 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