mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-12-22 17:57:32 +01:00
Mohammed Sohail
4d13a14dc2
Squashed commit of the following: commit05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 10:03:44 2023 +0300 feat: add status types to dispatcher commit397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 09:39:31 2023 +0300 deps: bump -> cic-celo-sdk commitf2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 16:53:53 2023 +0300 snapshot: 12-ebening commit4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 12:50:43 2023 +0300 xnapshot: 12-02 commit773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 14:23:37 2023 +0300 update: deps initializers commit8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 10:42:15 2023 +0300 wip: refactor taskers commit8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Fri Feb 3 12:29:27 2023 +0300 refactor: decouple sql queries, remove transfer * add inline docs * removed transfer taks in prep for re-write commitb4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 2 12:29:43 2023 +0000 refactor: cmd/service/* and api
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package tasker
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/bsm/redislock"
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/logg"
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
|
|
"github.com/hibiken/asynq"
|
|
"github.com/zerodha/logf"
|
|
)
|
|
|
|
const (
|
|
fixedRetryCount = 25
|
|
fixedRetryPeriod = time.Second * 1
|
|
)
|
|
|
|
type TaskerServerOpts struct {
|
|
Concurrency int
|
|
Logg logf.Logger
|
|
LogLevel asynq.LogLevel
|
|
RedisPool *redis.RedisPool
|
|
SystemContainer *SystemContainer
|
|
TaskerClient *TaskerClient
|
|
}
|
|
|
|
type TaskerServer struct {
|
|
mux *asynq.ServeMux
|
|
server *asynq.Server
|
|
}
|
|
|
|
func NewTaskerServer(o TaskerServerOpts) *TaskerServer {
|
|
server := asynq.NewServer(
|
|
o.RedisPool,
|
|
asynq.Config{
|
|
Concurrency: o.Concurrency,
|
|
IsFailure: expectedFailures,
|
|
Logger: logg.NewAsynqLogg(o.Logg),
|
|
LogLevel: o.LogLevel,
|
|
Queues: map[string]int{
|
|
string(HighPriority): 5,
|
|
string(DefaultPriority): 2,
|
|
},
|
|
RetryDelayFunc: retryDelay,
|
|
},
|
|
)
|
|
|
|
mux := asynq.NewServeMux()
|
|
|
|
return &TaskerServer{
|
|
mux: mux,
|
|
server: server,
|
|
}
|
|
}
|
|
|
|
func (ts *TaskerServer) RegisterHandlers(taskName TaskName, taskHandler func(context.Context, *asynq.Task) error) {
|
|
ts.mux.HandleFunc(string(taskName), taskHandler)
|
|
}
|
|
|
|
func (ts *TaskerServer) Start() error {
|
|
if err := ts.server.Start(ts.mux); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ts *TaskerServer) Stop() {
|
|
ts.server.Stop()
|
|
ts.server.Shutdown()
|
|
}
|
|
|
|
func expectedFailures(err error) bool {
|
|
switch err {
|
|
// Ignore lock contention errors; retry until lock obtain.
|
|
case redislock.ErrNotObtained:
|
|
return false
|
|
default:
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Immidiatel
|
|
func retryDelay(count int, err error, task *asynq.Task) time.Duration {
|
|
if count < fixedRetryCount {
|
|
return fixedRetryPeriod
|
|
} else {
|
|
return asynq.DefaultRetryDelayFunc(count, err, task)
|
|
}
|
|
}
|