2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
// Package base defines foundational types and constants used in asynq package.
|
|
|
|
package base
|
|
|
|
|
2019-12-23 21:33:48 +08:00
|
|
|
import (
|
2020-01-31 22:48:58 +08:00
|
|
|
"fmt"
|
2020-01-11 13:32:15 +08:00
|
|
|
"strings"
|
2019-12-23 21:33:48 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
2019-12-22 23:15:45 +08:00
|
|
|
|
2020-01-06 22:53:40 +08:00
|
|
|
// DefaultQueueName is the queue name used if none are specified by user.
|
|
|
|
const DefaultQueueName = "default"
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
// Redis keys
|
|
|
|
const (
|
2020-01-31 22:48:58 +08:00
|
|
|
psPrefix = "asynq:ps:" // HASH
|
2020-01-06 22:53:40 +08:00
|
|
|
processedPrefix = "asynq:processed:" // STRING - asynq:processed:<yyyy-mm-dd>
|
|
|
|
failurePrefix = "asynq:failure:" // STRING - asynq:failure:<yyyy-mm-dd>
|
2020-01-07 13:27:51 +08:00
|
|
|
QueuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
|
2020-01-09 22:21:43 +08:00
|
|
|
AllQueues = "asynq:queues" // SET
|
2020-01-07 13:27:51 +08:00
|
|
|
DefaultQueue = QueuePrefix + DefaultQueueName // LIST
|
2020-01-06 22:53:40 +08:00
|
|
|
ScheduledQueue = "asynq:scheduled" // ZSET
|
|
|
|
RetryQueue = "asynq:retry" // ZSET
|
|
|
|
DeadQueue = "asynq:dead" // ZSET
|
|
|
|
InProgressQueue = "asynq:in_progress" // LIST
|
2019-12-22 23:15:45 +08:00
|
|
|
)
|
|
|
|
|
2020-01-06 22:53:40 +08:00
|
|
|
// QueueKey returns a redis key string for the given queue name.
|
|
|
|
func QueueKey(qname string) string {
|
2020-01-11 13:32:15 +08:00
|
|
|
return QueuePrefix + strings.ToLower(qname)
|
2020-01-06 22:53:40 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 13:13:34 +08:00
|
|
|
// ProcessedKey returns a redis key string for processed count
|
2019-12-23 21:33:48 +08:00
|
|
|
// for the given day.
|
|
|
|
func ProcessedKey(t time.Time) string {
|
|
|
|
return processedPrefix + t.UTC().Format("2006-01-02")
|
|
|
|
}
|
|
|
|
|
|
|
|
// FailureKey returns a redis key string for failure count
|
|
|
|
// for the given day.
|
|
|
|
func FailureKey(t time.Time) string {
|
|
|
|
return failurePrefix + t.UTC().Format("2006-01-02")
|
|
|
|
}
|
|
|
|
|
2020-01-31 22:48:58 +08:00
|
|
|
// ProcessStatusKey returns a redis key string for process status.
|
|
|
|
func ProcessStatusKey(hostname string, pid int) string {
|
|
|
|
return fmt.Sprintf("%s%s:%d", psPrefix, hostname, pid)
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
// TaskMessage is the internal representation of a task with additional metadata fields.
|
2019-12-28 12:37:15 +08:00
|
|
|
// Serialized data of this type gets written to redis.
|
2019-12-22 23:15:45 +08:00
|
|
|
type TaskMessage struct {
|
2019-12-28 12:37:15 +08:00
|
|
|
// Type indicates the kind of the task to be performed.
|
2019-12-22 23:15:45 +08:00
|
|
|
Type string
|
2019-12-28 12:37:15 +08:00
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
// Payload holds data needed to process the task.
|
|
|
|
Payload map[string]interface{}
|
|
|
|
|
2019-12-28 12:37:15 +08:00
|
|
|
// ID is a unique identifier for each task.
|
2019-12-22 23:15:45 +08:00
|
|
|
ID xid.ID
|
2019-12-28 12:37:15 +08:00
|
|
|
|
|
|
|
// Queue is a name this message should be enqueued to.
|
2019-12-22 23:15:45 +08:00
|
|
|
Queue string
|
2019-12-28 12:37:15 +08:00
|
|
|
|
2019-12-22 23:15:45 +08:00
|
|
|
// Retry is the max number of retry for this task.
|
|
|
|
Retry int
|
2019-12-28 12:37:15 +08:00
|
|
|
|
|
|
|
// Retried is the number of times we've retried this task so far.
|
2019-12-22 23:15:45 +08:00
|
|
|
Retried int
|
2019-12-28 12:37:15 +08:00
|
|
|
|
|
|
|
// ErrorMsg holds the error message from the last failure.
|
2019-12-22 23:15:45 +08:00
|
|
|
ErrorMsg string
|
|
|
|
}
|
2020-01-31 22:48:58 +08:00
|
|
|
|
|
|
|
// ProcessStatus holds information about running background worker process.
|
|
|
|
type ProcessStatus struct {
|
|
|
|
Concurrency int
|
|
|
|
Queues map[string]uint
|
|
|
|
PID int
|
|
|
|
Host string
|
|
|
|
State string
|
|
|
|
Started time.Time
|
|
|
|
}
|