mirror of
https://github.com/hibiken/asynq.git
synced 2025-09-19 05:17:30 +08:00
Update processor to adapt for deadlines set change
- Processor dequeues tasks only when it's available to process - Processor retries a task when its context's Done channel is closed
This commit is contained in:
18
context.go
18
context.go
@@ -27,28 +27,14 @@ type ctxKey int
|
||||
const metadataCtxKey ctxKey = 0
|
||||
|
||||
// createContext returns a context and cancel function for a given task message.
|
||||
func createContext(msg *base.TaskMessage) (ctx context.Context, cancel context.CancelFunc) {
|
||||
func createContext(msg *base.TaskMessage, deadline time.Time) (ctx context.Context, cancel context.CancelFunc) {
|
||||
metadata := taskMetadata{
|
||||
id: msg.ID.String(),
|
||||
maxRetry: msg.Retry,
|
||||
retryCount: msg.Retried,
|
||||
}
|
||||
ctx = context.WithValue(context.Background(), metadataCtxKey, metadata)
|
||||
if msg.Timeout == 0 && msg.Deadline == 0 {
|
||||
panic("asynq: internal error: missing both timeout and deadline")
|
||||
}
|
||||
if msg.Timeout != 0 {
|
||||
timeout := time.Duration(msg.Timeout) * time.Second
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
}
|
||||
if msg.Deadline != 0 {
|
||||
deadline := time.Unix(int64(msg.Deadline), 0)
|
||||
ctx, cancel = context.WithDeadline(ctx, deadline)
|
||||
}
|
||||
if cancel == nil {
|
||||
ctx, cancel = context.WithCancel(ctx)
|
||||
}
|
||||
return ctx, cancel
|
||||
return context.WithDeadline(ctx, deadline)
|
||||
}
|
||||
|
||||
// GetTaskID extracts a task ID from a context, if any.
|
||||
|
Reference in New Issue
Block a user