2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00

conventions

This commit is contained in:
Binaek Sarkar 2022-02-10 15:26:34 +05:30 committed by Ken Hibino
parent 55d0610a03
commit ebd7a32c0f
4 changed files with 14 additions and 21 deletions

View File

@ -7,11 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.22.0] - 2022-01-29
### Added ### Added
- `BaseContext` introduced in `Config` to specify callback hook to provide a base `context` from which `Handler` `context` is derived - `BaseContext` is introduced in `Config` to specify callback hook to provide a base `context` from which `Handler` `context` is derived
## [0.21.0] - 2022-01-22 ## [0.21.0] - 2022-01-22

View File

@ -54,10 +54,15 @@ func TestCreateContextWithFutureDeadline(t *testing.T) {
} }
func TestCreateContextWithBaseContext(t *testing.T) { func TestCreateContextWithBaseContext(t *testing.T) {
type ctxKey string
type ctxValue string
var key ctxKey = "key"
var value ctxValue = "value"
tests := []struct { tests := []struct {
deadline time.Time baseCtx context.Context
}{ }{
{time.Now().Add(-2 * time.Hour)}, {context.WithValue(context.Background(), key, value)},
} }
for _, tc := range tests { for _, tc := range tests {
@ -66,13 +71,8 @@ func TestCreateContextWithBaseContext(t *testing.T) {
ID: uuid.NewString(), ID: uuid.NewString(),
Payload: nil, Payload: nil,
} }
type ctxKey string
type ctxValue string
var key ctxKey = "key"
var value ctxValue = "value"
baseCtx := context.WithValue(context.Background(), key, value)
ctx, cancel := New(baseCtx, msg, tc.deadline) ctx, cancel := New(tc.baseCtx, msg, time.Now().Add(30*time.Minute))
defer cancel() defer cancel()
select { select {
@ -82,10 +82,11 @@ func TestCreateContextWithBaseContext(t *testing.T) {
} }
v, ok := ctx.Value(key).(ctxValue) v, ok := ctx.Value(key).(ctxValue)
original, _ := tc.baseCtx.Value(key).(ctxValue)
if !ok { if !ok {
t.Errorf("ctx.Value().(ctxValue) returned false, expected to be true") t.Errorf("ctx.Value().(ctxValue) returned false, expected to be true")
} }
if v != value { if v != original {
t.Errorf("ctx.Value().(ctxValue) returned unknown value (%v), expected to be %s", v, value) t.Errorf("ctx.Value().(ctxValue) returned unknown value (%v), expected to be %s", v, value)
} }
} }

View File

@ -27,7 +27,7 @@ type processor struct {
broker base.Broker broker base.Broker
handler Handler handler Handler
baseCtxFn BaseCtxFn baseCtxFn func() context.Context
queueConfig map[string]int queueConfig map[string]int
@ -72,7 +72,7 @@ type processor struct {
type processorParams struct { type processorParams struct {
logger *log.Logger logger *log.Logger
broker base.Broker broker base.Broker
baseCtxFn BaseCtxFn baseCtxFn func() context.Context
retryDelayFunc RetryDelayFunc retryDelayFunc RetryDelayFunc
isFailureFunc func(error) bool isFailureFunc func(error) bool
syncCh chan<- *syncRequest syncCh chan<- *syncRequest

View File

@ -101,10 +101,7 @@ type Config struct {
// //
// If BaseContext is nil, the default is context.Background(). // If BaseContext is nil, the default is context.Background().
// If this is defined, then it MUST return a non-nil context // If this is defined, then it MUST return a non-nil context
BaseContext BaseCtxFn BaseContext func() context.Context
// SleepOnEmptyQueue optionally specifies the amount of time to wait before polling again when there are no messages in the queue
SleepOnEmptyQueue time.Duration
// Function to calculate retry delay for a failed task. // Function to calculate retry delay for a failed task.
// //
@ -212,9 +209,6 @@ func (fn ErrorHandlerFunc) HandleError(ctx context.Context, task *Task, err erro
fn(ctx, task, err) fn(ctx, task, err)
} }
// BaseCtxFn provides the root context from where the execution contexts of tasks are derived
type BaseCtxFn func() context.Context
// RetryDelayFunc calculates the retry delay duration for a failed task given // RetryDelayFunc calculates the retry delay duration for a failed task given
// the retry count, error, and the task. // the retry count, error, and the task.
// //