mirror of
https://github.com/hibiken/asynq.git
synced 2026-06-22 00:44:01 +08:00
Compare commits
2 Commits
master
...
sohail/v0.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
646f9b7f8c | ||
|
|
5dfbf92903 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -7,7 +7,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest]
|
os: [ubuntu-latest]
|
||||||
go-version: [1.24.x, 1.25.x]
|
go-version: [1.25.x, 1.26.x]
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
services:
|
services:
|
||||||
redis:
|
redis:
|
||||||
@@ -45,7 +45,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest]
|
os: [ubuntu-latest]
|
||||||
go-version: [1.24.x, 1.25.x]
|
go-version: [1.25.x, 1.26.x]
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
services:
|
services:
|
||||||
redis:
|
redis:
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ Initialize your project by creating a folder and then running `go mod init githu
|
|||||||
go get -u github.com/hibiken/asynq
|
go get -u github.com/hibiken/asynq
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You may use the latest features not available in the last tagged release by installing with:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go get -u github.com/hibiken/asynq@master
|
||||||
|
```
|
||||||
|
|
||||||
Make sure you're running a Redis server locally or from a [Docker](https://hub.docker.com/_/redis) container. Version `4.0` or higher is required.
|
Make sure you're running a Redis server locally or from a [Docker](https://hub.docker.com/_/redis) container. Version `4.0` or higher is required.
|
||||||
|
|
||||||
Next, write a package that encapsulates task creation and task handling.
|
Next, write a package that encapsulates task creation and task handling.
|
||||||
|
|||||||
14
asynq.go
14
asynq.go
@@ -40,7 +40,6 @@ type Task struct {
|
|||||||
func (t *Task) Type() string { return t.typename }
|
func (t *Task) Type() string { return t.typename }
|
||||||
func (t *Task) Payload() []byte { return t.payload }
|
func (t *Task) Payload() []byte { return t.payload }
|
||||||
func (t *Task) Headers() map[string]string { return t.headers }
|
func (t *Task) Headers() map[string]string { return t.headers }
|
||||||
func (t *Task) Options() []Option { return t.opts }
|
|
||||||
|
|
||||||
// ResultWriter returns a pointer to the ResultWriter associated with the task.
|
// ResultWriter returns a pointer to the ResultWriter associated with the task.
|
||||||
//
|
//
|
||||||
@@ -472,7 +471,7 @@ func (opt RedisClusterClientOpt) MakeRedisClient() interface{} {
|
|||||||
// redis://[:password@]host[:port][/dbnumber]
|
// redis://[:password@]host[:port][/dbnumber]
|
||||||
// rediss://[:password@]host[:port][/dbnumber]
|
// rediss://[:password@]host[:port][/dbnumber]
|
||||||
// redis-socket://[:password@]path[?db=dbnumber]
|
// redis-socket://[:password@]path[?db=dbnumber]
|
||||||
// redis-sentinel://[:password@]host1[:port][,host2:[:port]][,hostN:[:port]][/dbnumber][?master=masterName]
|
// redis-sentinel://[:password@]host1[:port][,host2:[:port]][,hostN:[:port]][?master=masterName]
|
||||||
func ParseRedisURI(uri string) (RedisConnOpt, error) {
|
func ParseRedisURI(uri string) (RedisConnOpt, error) {
|
||||||
u, err := url.Parse(uri)
|
u, err := url.Parse(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -546,20 +545,11 @@ func parseRedisSocketURI(u *url.URL) (RedisConnOpt, error) {
|
|||||||
func parseRedisSentinelURI(u *url.URL) (RedisConnOpt, error) {
|
func parseRedisSentinelURI(u *url.URL) (RedisConnOpt, error) {
|
||||||
addrs := strings.Split(u.Host, ",")
|
addrs := strings.Split(u.Host, ",")
|
||||||
master := u.Query().Get("master")
|
master := u.Query().Get("master")
|
||||||
var db int
|
|
||||||
var err error
|
|
||||||
if len(u.Path) > 0 {
|
|
||||||
xs := strings.Split(strings.Trim(u.Path, "/"), "/")
|
|
||||||
db, err = strconv.Atoi(xs[0])
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("asynq: could not parse redis sentinel uri: database number should be the first segment of the path")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var password string
|
var password string
|
||||||
if v, ok := u.User.Password(); ok {
|
if v, ok := u.User.Password(); ok {
|
||||||
password = v
|
password = v
|
||||||
}
|
}
|
||||||
return RedisFailoverClientOpt{MasterName: master, SentinelAddrs: addrs, SentinelPassword: password, DB: db}, nil
|
return RedisFailoverClientOpt{MasterName: master, SentinelAddrs: addrs, SentinelPassword: password}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResultWriter is a client interface to write result data for a task.
|
// ResultWriter is a client interface to write result data for a task.
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
@@ -149,23 +148,6 @@ func TestParseRedisURI(t *testing.T) {
|
|||||||
SentinelPassword: "mypassword",
|
SentinelPassword: "mypassword",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"redis-sentinel://localhost:5000,localhost:5001,localhost:5002/3?master=mymaster",
|
|
||||||
RedisFailoverClientOpt{
|
|
||||||
MasterName: "mymaster",
|
|
||||||
SentinelAddrs: []string{"localhost:5000", "localhost:5001", "localhost:5002"},
|
|
||||||
DB: 3,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"redis-sentinel://:mypassword@localhost:5000,localhost:5001,localhost:5002/7?master=mymaster",
|
|
||||||
RedisFailoverClientOpt{
|
|
||||||
MasterName: "mymaster",
|
|
||||||
SentinelAddrs: []string{"localhost:5000", "localhost:5001", "localhost:5002"},
|
|
||||||
SentinelPassword: "mypassword",
|
|
||||||
DB: 7,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -206,10 +188,6 @@ func TestParseRedisURIErrors(t *testing.T) {
|
|||||||
"non integer for db numbers for socket",
|
"non integer for db numbers for socket",
|
||||||
"redis-socket:///some/path/to/redis?db=one",
|
"redis-socket:///some/path/to/redis?db=one",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"non integer for db number for sentinel",
|
|
||||||
"redis-sentinel://localhost:5000/abc?master=mymaster",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -220,30 +198,3 @@ func TestParseRedisURIErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTaskOptions(t *testing.T) {
|
|
||||||
opts := []Option{
|
|
||||||
MaxRetry(3),
|
|
||||||
Queue("critical"),
|
|
||||||
Timeout(5 * time.Minute),
|
|
||||||
}
|
|
||||||
task := NewTask("mytask", []byte("payload"), opts...)
|
|
||||||
|
|
||||||
got := task.Options()
|
|
||||||
if len(got) != len(opts) {
|
|
||||||
t.Fatalf("task.Options() returned %d options, want %d", len(got), len(opts))
|
|
||||||
}
|
|
||||||
for i, o := range opts {
|
|
||||||
if got[i].String() != o.String() {
|
|
||||||
t.Errorf("task.Options()[%d] = %v, want %v", i, got[i], o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTaskOptionsNil(t *testing.T) {
|
|
||||||
task := NewTask("mytask", []byte("payload"))
|
|
||||||
got := task.Options()
|
|
||||||
if got != nil {
|
|
||||||
t.Errorf("task.Options() = %v, want nil for task with no options", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
174
client.go
174
client.go
@@ -6,9 +6,7 @@ package asynq
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -62,7 +60,6 @@ const (
|
|||||||
TaskIDOpt
|
TaskIDOpt
|
||||||
RetentionOpt
|
RetentionOpt
|
||||||
GroupOpt
|
GroupOpt
|
||||||
HeaderOpt
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option specifies the task processing behavior.
|
// Option specifies the task processing behavior.
|
||||||
@@ -89,7 +86,6 @@ type (
|
|||||||
processInOption time.Duration
|
processInOption time.Duration
|
||||||
retentionOption time.Duration
|
retentionOption time.Duration
|
||||||
groupOption string
|
groupOption string
|
||||||
headerOption [2]string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxRetry returns an option to specify the max number of times
|
// MaxRetry returns an option to specify the max number of times
|
||||||
@@ -221,27 +217,6 @@ func (name groupOption) String() string { return fmt.Sprintf("Group(%q)", st
|
|||||||
func (name groupOption) Type() OptionType { return GroupOpt }
|
func (name groupOption) Type() OptionType { return GroupOpt }
|
||||||
func (name groupOption) Value() interface{} { return string(name) }
|
func (name groupOption) Value() interface{} { return string(name) }
|
||||||
|
|
||||||
// Header returns an option to associate the key-value header to the task.
|
|
||||||
//
|
|
||||||
// This option is composable with other Client options and can be used together
|
|
||||||
// with other options like MaxRetry, Queue, etc. For use cases where headers
|
|
||||||
// need to be combined with other options, using Header option is recommended.
|
|
||||||
//
|
|
||||||
// Alternatively, NewTaskWithHeaders can be used to create a task with headers
|
|
||||||
// directly, which may be preferable when headers are an intrinsic part of the
|
|
||||||
// task definition rather than enqueue-time configuration.
|
|
||||||
func Header(key, value string) Option {
|
|
||||||
return headerOption{key, value}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h headerOption) String() string {
|
|
||||||
var bytes []byte
|
|
||||||
bytes, _ = json.Marshal(h)
|
|
||||||
return fmt.Sprintf("Header(%s)", bytes)
|
|
||||||
}
|
|
||||||
func (h headerOption) Type() OptionType { return HeaderOpt }
|
|
||||||
func (h headerOption) Value() interface{} { return [2]string{h[0], h[1]} }
|
|
||||||
|
|
||||||
// ErrDuplicateTask indicates that the given task could not be enqueued since it's a duplicate of another task.
|
// ErrDuplicateTask indicates that the given task could not be enqueued since it's a duplicate of another task.
|
||||||
//
|
//
|
||||||
// ErrDuplicateTask error only applies to tasks enqueued with a Unique option.
|
// ErrDuplicateTask error only applies to tasks enqueued with a Unique option.
|
||||||
@@ -262,7 +237,6 @@ type option struct {
|
|||||||
processAt time.Time
|
processAt time.Time
|
||||||
retention time.Duration
|
retention time.Duration
|
||||||
group string
|
group string
|
||||||
headers map[string]string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// composeOptions merges user provided options into the default options
|
// composeOptions merges user provided options into the default options
|
||||||
@@ -277,7 +251,6 @@ func composeOptions(opts ...Option) (option, error) {
|
|||||||
timeout: 0, // do not set to defaultTimeout here
|
timeout: 0, // do not set to defaultTimeout here
|
||||||
deadline: time.Time{},
|
deadline: time.Time{},
|
||||||
processAt: time.Now(),
|
processAt: time.Now(),
|
||||||
headers: make(map[string]string),
|
|
||||||
}
|
}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
switch opt := opt.(type) {
|
switch opt := opt.(type) {
|
||||||
@@ -317,9 +290,6 @@ func composeOptions(opts ...Option) (option, error) {
|
|||||||
return option{}, errors.New("group key cannot be empty")
|
return option{}, errors.New("group key cannot be empty")
|
||||||
}
|
}
|
||||||
res.group = key
|
res.group = key
|
||||||
case headerOption:
|
|
||||||
key, value := opt[0], opt[1]
|
|
||||||
res.headers[key] = value
|
|
||||||
default:
|
default:
|
||||||
// ignore unexpected option
|
// ignore unexpected option
|
||||||
}
|
}
|
||||||
@@ -415,7 +385,7 @@ func (c *Client) EnqueueContext(ctx context.Context, task *Task, opts ...Option)
|
|||||||
ID: opt.taskID,
|
ID: opt.taskID,
|
||||||
Type: task.Type(),
|
Type: task.Type(),
|
||||||
Payload: task.Payload(),
|
Payload: task.Payload(),
|
||||||
Headers: maps.Clone(task.Headers()),
|
Headers: task.Headers(),
|
||||||
Queue: opt.queue,
|
Queue: opt.queue,
|
||||||
Retry: opt.retry,
|
Retry: opt.retry,
|
||||||
Deadline: deadline.Unix(),
|
Deadline: deadline.Unix(),
|
||||||
@@ -424,12 +394,6 @@ func (c *Client) EnqueueContext(ctx context.Context, task *Task, opts ...Option)
|
|||||||
GroupKey: opt.group,
|
GroupKey: opt.group,
|
||||||
Retention: int64(opt.retention.Seconds()),
|
Retention: int64(opt.retention.Seconds()),
|
||||||
}
|
}
|
||||||
if len(opt.headers) > 0 {
|
|
||||||
if msg.Headers == nil {
|
|
||||||
msg.Headers = make(map[string]string)
|
|
||||||
}
|
|
||||||
maps.Copy(msg.Headers, opt.headers)
|
|
||||||
}
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
var state base.TaskState
|
var state base.TaskState
|
||||||
if opt.processAt.After(now) {
|
if opt.processAt.After(now) {
|
||||||
@@ -456,142 +420,6 @@ func (c *Client) EnqueueContext(ctx context.Context, task *Task, opts ...Option)
|
|||||||
return newTaskInfo(msg, state, opt.processAt, nil), nil
|
return newTaskInfo(msg, state, opt.processAt, nil), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BatchEnqueueResult holds the result of enqueuing a single task within a batch.
|
|
||||||
type BatchEnqueueResult struct {
|
|
||||||
TaskInfo *TaskInfo
|
|
||||||
Err error
|
|
||||||
}
|
|
||||||
|
|
||||||
// BatchEnqueueContext enqueues multiple tasks in a single Redis pipeline round-trip,
|
|
||||||
// returning a per-task result slice aligned with the input tasks slice.
|
|
||||||
//
|
|
||||||
// # Atomicity Guarantees
|
|
||||||
//
|
|
||||||
// There is no all-or-nothing guarantee across the batch. Each task is executed as
|
|
||||||
// an independent Lua script inside a Redis pipeline. Individual scripts are atomic
|
|
||||||
// (the existence check, hash write, and list/sorted-set push for one task cannot
|
|
||||||
// be partially applied), but the pipeline as a whole is not wrapped in a
|
|
||||||
// MULTI/EXEC transaction. This means:
|
|
||||||
//
|
|
||||||
// - Partial success is possible: some tasks may be enqueued while others are not.
|
|
||||||
// - A task whose ID already exists in Redis is silently skipped (treated as a
|
|
||||||
// no-op by the Lua script), and its result will still show success.
|
|
||||||
// - If the Redis pipeline call itself fails (e.g. connection lost, context
|
|
||||||
// cancelled), every task that passed client-side validation receives that
|
|
||||||
// error — none of them can be assumed to have been enqueued.
|
|
||||||
//
|
|
||||||
// # Validation Errors (pre-pipeline)
|
|
||||||
//
|
|
||||||
// The following are caught before any Redis call and rejected in the
|
|
||||||
// corresponding BatchEnqueueResult.Err without affecting other tasks:
|
|
||||||
//
|
|
||||||
// - nil task
|
|
||||||
// - empty task type name
|
|
||||||
// - invalid options
|
|
||||||
// - group tasks (not supported in batch mode)
|
|
||||||
// - unique tasks (not supported in batch mode)
|
|
||||||
//
|
|
||||||
// # Supported Task Types
|
|
||||||
//
|
|
||||||
// Immediate and scheduled (via [ProcessAt] or [ProcessIn]) tasks are supported.
|
|
||||||
// Group and unique tasks are rejected as described above.
|
|
||||||
func (c *Client) BatchEnqueueContext(ctx context.Context, tasks []*Task, opts ...Option) []BatchEnqueueResult {
|
|
||||||
results := make([]BatchEnqueueResult, len(tasks))
|
|
||||||
if len(tasks) == 0 {
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
type itemMeta struct {
|
|
||||||
state base.TaskState
|
|
||||||
processAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
items := make([]base.BatchEnqueueItem, 0, len(tasks))
|
|
||||||
itemIndexes := make([]int, 0, len(tasks))
|
|
||||||
itemMetas := make([]itemMeta, 0, len(tasks))
|
|
||||||
|
|
||||||
for i, task := range tasks {
|
|
||||||
if task == nil {
|
|
||||||
results[i] = BatchEnqueueResult{Err: fmt.Errorf("task cannot be nil")}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(task.Type()) == "" {
|
|
||||||
results[i] = BatchEnqueueResult{Err: fmt.Errorf("task typename cannot be empty")}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
merged := append(task.opts, opts...)
|
|
||||||
opt, err := composeOptions(merged...)
|
|
||||||
if err != nil {
|
|
||||||
results[i] = BatchEnqueueResult{Err: err}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if opt.group != "" {
|
|
||||||
results[i] = BatchEnqueueResult{Err: fmt.Errorf("batch enqueue does not support group tasks")}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if opt.uniqueTTL > 0 {
|
|
||||||
results[i] = BatchEnqueueResult{Err: fmt.Errorf("batch enqueue does not support unique tasks")}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
deadline := noDeadline
|
|
||||||
if !opt.deadline.IsZero() {
|
|
||||||
deadline = opt.deadline
|
|
||||||
}
|
|
||||||
timeout := noTimeout
|
|
||||||
if opt.timeout != 0 {
|
|
||||||
timeout = opt.timeout
|
|
||||||
}
|
|
||||||
if deadline.Equal(noDeadline) && timeout == noTimeout {
|
|
||||||
timeout = defaultTimeout
|
|
||||||
}
|
|
||||||
msg := &base.TaskMessage{
|
|
||||||
ID: opt.taskID,
|
|
||||||
Type: task.Type(),
|
|
||||||
Payload: task.Payload(),
|
|
||||||
Headers: task.Headers(),
|
|
||||||
Queue: opt.queue,
|
|
||||||
Retry: opt.retry,
|
|
||||||
Deadline: deadline.Unix(),
|
|
||||||
Timeout: int64(timeout.Seconds()),
|
|
||||||
Retention: int64(opt.retention.Seconds()),
|
|
||||||
}
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
scheduled := opt.processAt.After(now)
|
|
||||||
|
|
||||||
item := base.BatchEnqueueItem{Msg: msg}
|
|
||||||
var meta itemMeta
|
|
||||||
if scheduled {
|
|
||||||
item.ProcessAt = opt.processAt
|
|
||||||
meta = itemMeta{state: base.TaskStateScheduled, processAt: opt.processAt}
|
|
||||||
} else {
|
|
||||||
meta = itemMeta{state: base.TaskStatePending, processAt: now}
|
|
||||||
}
|
|
||||||
|
|
||||||
items = append(items, item)
|
|
||||||
itemIndexes = append(itemIndexes, i)
|
|
||||||
itemMetas = append(itemMetas, meta)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(items) == 0 {
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.broker.BatchEnqueue(ctx, items)
|
|
||||||
if err != nil {
|
|
||||||
for _, idx := range itemIndexes {
|
|
||||||
results[idx] = BatchEnqueueResult{Err: err}
|
|
||||||
}
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
for j, idx := range itemIndexes {
|
|
||||||
info := newTaskInfo(items[j].Msg, itemMetas[j].state, itemMetas[j].processAt, nil)
|
|
||||||
results[idx] = BatchEnqueueResult{TaskInfo: info}
|
|
||||||
}
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ping performs a ping against the redis connection.
|
// Ping performs a ping against the redis connection.
|
||||||
func (c *Client) Ping() error {
|
func (c *Client) Ping() error {
|
||||||
return c.broker.Ping()
|
return c.broker.Ping()
|
||||||
|
|||||||
272
client_test.go
272
client_test.go
@@ -13,8 +13,6 @@ import (
|
|||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
|
||||||
"github.com/hibiken/asynq/internal/testbroker"
|
|
||||||
h "github.com/hibiken/asynq/internal/testutil"
|
h "github.com/hibiken/asynq/internal/testutil"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
@@ -1341,70 +1339,6 @@ func TestClientEnqueueWithHeaders(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
desc: "Task with header option",
|
|
||||||
task: NewTask("store_data", []byte("data"), Header("channel", "email"), Header("user-id", "bob1234")),
|
|
||||||
opts: []Option{},
|
|
||||||
wantInfo: &TaskInfo{
|
|
||||||
Queue: "default",
|
|
||||||
Type: "store_data",
|
|
||||||
Payload: []byte("data"),
|
|
||||||
Headers: map[string]string{"channel": "email", "user-id": "bob1234"},
|
|
||||||
State: TaskStatePending,
|
|
||||||
MaxRetry: 25,
|
|
||||||
Retried: 0,
|
|
||||||
LastErr: "",
|
|
||||||
LastFailedAt: time.Time{},
|
|
||||||
Timeout: defaultTimeout,
|
|
||||||
Deadline: time.Time{},
|
|
||||||
NextProcessAt: now,
|
|
||||||
},
|
|
||||||
wantPending: map[string][]*base.TaskMessage{
|
|
||||||
"default": {
|
|
||||||
{
|
|
||||||
Type: "store_data",
|
|
||||||
Payload: []byte("data"),
|
|
||||||
Headers: map[string]string{"channel": "email", "user-id": "bob1234"},
|
|
||||||
Retry: 25,
|
|
||||||
Queue: "default",
|
|
||||||
Timeout: int64(defaultTimeout.Seconds()),
|
|
||||||
Deadline: noDeadline.Unix(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "Enqueue task with header option",
|
|
||||||
task: NewTask("store_data", []byte("data")),
|
|
||||||
opts: []Option{Header("channel", "email"), Header("user-id", "bob1234"), MaxRetry(5)},
|
|
||||||
wantInfo: &TaskInfo{
|
|
||||||
Queue: "default",
|
|
||||||
Type: "store_data",
|
|
||||||
Payload: []byte("data"),
|
|
||||||
Headers: map[string]string{"channel": "email", "user-id": "bob1234"},
|
|
||||||
State: TaskStatePending,
|
|
||||||
MaxRetry: 5,
|
|
||||||
Retried: 0,
|
|
||||||
LastErr: "",
|
|
||||||
LastFailedAt: time.Time{},
|
|
||||||
Timeout: defaultTimeout,
|
|
||||||
Deadline: time.Time{},
|
|
||||||
NextProcessAt: now,
|
|
||||||
},
|
|
||||||
wantPending: map[string][]*base.TaskMessage{
|
|
||||||
"default": {
|
|
||||||
{
|
|
||||||
Type: "store_data",
|
|
||||||
Payload: []byte("data"),
|
|
||||||
Headers: map[string]string{"channel": "email", "user-id": "bob1234"},
|
|
||||||
Retry: 5,
|
|
||||||
Queue: "default",
|
|
||||||
Timeout: int64(defaultTimeout.Seconds()),
|
|
||||||
Deadline: noDeadline.Unix(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -1727,209 +1661,3 @@ func TestClientEnqueueWithHeadersAndGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBatchEnqueueContext_ImmediateTasks(t *testing.T) {
|
|
||||||
r := setup(t)
|
|
||||||
client := NewClient(getRedisConnOpt(t))
|
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
tasks := []*Task{
|
|
||||||
NewTask("task1", []byte("payload1")),
|
|
||||||
NewTask("task2", []byte("payload2")),
|
|
||||||
NewTask("task3", []byte("payload3")),
|
|
||||||
}
|
|
||||||
|
|
||||||
results := client.BatchEnqueueContext(context.Background(), tasks)
|
|
||||||
if len(results) != 3 {
|
|
||||||
t.Fatalf("BatchEnqueueContext returned %d results, want 3", len(results))
|
|
||||||
}
|
|
||||||
for i, res := range results {
|
|
||||||
if res.Err != nil {
|
|
||||||
t.Errorf("results[%d].Err = %v, want nil", i, res.Err)
|
|
||||||
}
|
|
||||||
if res.TaskInfo == nil {
|
|
||||||
t.Errorf("results[%d].TaskInfo is nil, want non-nil", i)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if res.TaskInfo.Queue != "default" {
|
|
||||||
t.Errorf("results[%d].TaskInfo.Queue = %q, want %q", i, res.TaskInfo.Queue, "default")
|
|
||||||
}
|
|
||||||
if res.TaskInfo.State != TaskStatePending {
|
|
||||||
t.Errorf("results[%d].TaskInfo.State = %v, want %v", i, res.TaskInfo.State, TaskStatePending)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gotPending := h.GetPendingMessages(t, r, "default")
|
|
||||||
if len(gotPending) != 3 {
|
|
||||||
t.Errorf("len(pending) = %d, want 3", len(gotPending))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBatchEnqueueContext_ScheduledTask(t *testing.T) {
|
|
||||||
r := setup(t)
|
|
||||||
client := NewClient(getRedisConnOpt(t))
|
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
future := time.Now().Add(1 * time.Hour)
|
|
||||||
tasks := []*Task{
|
|
||||||
NewTask("scheduled_task", []byte("payload"), ProcessAt(future)),
|
|
||||||
}
|
|
||||||
|
|
||||||
results := client.BatchEnqueueContext(context.Background(), tasks)
|
|
||||||
if len(results) != 1 {
|
|
||||||
t.Fatalf("BatchEnqueueContext returned %d results, want 1", len(results))
|
|
||||||
}
|
|
||||||
if results[0].Err != nil {
|
|
||||||
t.Fatalf("results[0].Err = %v, want nil", results[0].Err)
|
|
||||||
}
|
|
||||||
if results[0].TaskInfo == nil {
|
|
||||||
t.Fatal("results[0].TaskInfo is nil, want non-nil")
|
|
||||||
}
|
|
||||||
if results[0].TaskInfo.State != TaskStateScheduled {
|
|
||||||
t.Errorf("results[0].TaskInfo.State = %v, want %v", results[0].TaskInfo.State, TaskStateScheduled)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotScheduled := h.GetScheduledMessages(t, r, "default")
|
|
||||||
if len(gotScheduled) != 1 {
|
|
||||||
t.Errorf("len(scheduled) = %d, want 1", len(gotScheduled))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBatchEnqueueContext_MixedBatch(t *testing.T) {
|
|
||||||
r := setup(t)
|
|
||||||
client := NewClient(getRedisConnOpt(t))
|
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
future := time.Now().Add(1 * time.Hour)
|
|
||||||
tasks := []*Task{
|
|
||||||
NewTask("immediate1", []byte("p1")),
|
|
||||||
NewTask("scheduled1", []byte("p2"), ProcessAt(future)),
|
|
||||||
NewTask("immediate2", []byte("p3")),
|
|
||||||
NewTask("grouped1", []byte("p4"), Group("mygroup")),
|
|
||||||
NewTask("immediate3", []byte("p5")),
|
|
||||||
}
|
|
||||||
|
|
||||||
results := client.BatchEnqueueContext(context.Background(), tasks)
|
|
||||||
if len(results) != 5 {
|
|
||||||
t.Fatalf("BatchEnqueueContext returned %d results, want 5", len(results))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Immediate tasks (indices 0, 2, 4) should succeed with Pending state.
|
|
||||||
for _, idx := range []int{0, 2, 4} {
|
|
||||||
if results[idx].Err != nil {
|
|
||||||
t.Errorf("results[%d].Err = %v, want nil (immediate task)", idx, results[idx].Err)
|
|
||||||
}
|
|
||||||
if results[idx].TaskInfo == nil {
|
|
||||||
t.Errorf("results[%d].TaskInfo is nil, want non-nil", idx)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if results[idx].TaskInfo.State != TaskStatePending {
|
|
||||||
t.Errorf("results[%d].TaskInfo.State = %v, want %v", idx, results[idx].TaskInfo.State, TaskStatePending)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scheduled task (index 1) should succeed with Scheduled state.
|
|
||||||
if results[1].Err != nil {
|
|
||||||
t.Errorf("results[1].Err = %v, want nil (scheduled task)", results[1].Err)
|
|
||||||
}
|
|
||||||
if results[1].TaskInfo != nil && results[1].TaskInfo.State != TaskStateScheduled {
|
|
||||||
t.Errorf("results[1].TaskInfo.State = %v, want %v", results[1].TaskInfo.State, TaskStateScheduled)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grouped task (index 3) should be rejected.
|
|
||||||
if results[3].Err == nil {
|
|
||||||
t.Error("results[3].Err is nil, want error for group task")
|
|
||||||
}
|
|
||||||
|
|
||||||
gotPending := h.GetPendingMessages(t, r, "default")
|
|
||||||
if len(gotPending) != 3 {
|
|
||||||
t.Errorf("len(pending) = %d, want 3", len(gotPending))
|
|
||||||
}
|
|
||||||
|
|
||||||
gotScheduled := h.GetScheduledMessages(t, r, "default")
|
|
||||||
if len(gotScheduled) != 1 {
|
|
||||||
t.Errorf("len(scheduled) = %d, want 1", len(gotScheduled))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBatchEnqueueContext_ValidationErrors(t *testing.T) {
|
|
||||||
setup(t)
|
|
||||||
client := NewClient(getRedisConnOpt(t))
|
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
desc string
|
|
||||||
tasks []*Task
|
|
||||||
opts []Option
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
desc: "nil task",
|
|
||||||
tasks: []*Task{nil},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "empty task typename",
|
|
||||||
tasks: []*Task{NewTask("", []byte("payload"))},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "blank task typename",
|
|
||||||
tasks: []*Task{NewTask(" ", []byte("payload"))},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "invalid option: unique TTL less than 1s",
|
|
||||||
tasks: []*Task{NewTask("foo", nil)},
|
|
||||||
opts: []Option{Unique(300 * time.Millisecond)},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "group task rejected",
|
|
||||||
tasks: []*Task{NewTask("foo", nil, Group("mygroup"))},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "unique task rejected",
|
|
||||||
tasks: []*Task{NewTask("foo", nil, Unique(time.Hour))},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
results := client.BatchEnqueueContext(context.Background(), tc.tasks, tc.opts...)
|
|
||||||
if len(results) != len(tc.tasks) {
|
|
||||||
t.Errorf("%s: got %d results, want %d", tc.desc, len(results), len(tc.tasks))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for i, res := range results {
|
|
||||||
if res.Err == nil {
|
|
||||||
t.Errorf("%s: results[%d].Err = nil, want non-nil error", tc.desc, i)
|
|
||||||
}
|
|
||||||
if res.TaskInfo != nil {
|
|
||||||
t.Errorf("%s: results[%d].TaskInfo = %v, want nil", tc.desc, i, res.TaskInfo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBatchEnqueueContext_BrokerError(t *testing.T) {
|
|
||||||
r := rdb.NewRDB(setup(t))
|
|
||||||
defer r.Close()
|
|
||||||
testBroker := testbroker.NewTestBroker(r)
|
|
||||||
client := &Client{broker: testBroker, sharedConnection: true}
|
|
||||||
|
|
||||||
tasks := []*Task{
|
|
||||||
NewTask("task1", []byte("p1")),
|
|
||||||
NewTask("task2", []byte("p2")),
|
|
||||||
}
|
|
||||||
|
|
||||||
testBroker.Sleep()
|
|
||||||
results := client.BatchEnqueueContext(context.Background(), tasks)
|
|
||||||
testBroker.Wakeup()
|
|
||||||
|
|
||||||
if len(results) != 2 {
|
|
||||||
t.Fatalf("BatchEnqueueContext returned %d results, want 2", len(results))
|
|
||||||
}
|
|
||||||
for i, res := range results {
|
|
||||||
if res.Err == nil {
|
|
||||||
t.Errorf("results[%d].Err = nil, want non-nil error when broker is down", i)
|
|
||||||
}
|
|
||||||
if res.TaskInfo != nil {
|
|
||||||
t.Errorf("results[%d].TaskInfo = %v, want nil on broker error", i, res.TaskInfo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
8
codecov.yml
Normal file
8
codecov.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
coverage:
|
||||||
|
status:
|
||||||
|
project:
|
||||||
|
default:
|
||||||
|
informational: true
|
||||||
|
patch:
|
||||||
|
default:
|
||||||
|
informational: true
|
||||||
11
go.mod
11
go.mod
@@ -1,20 +1,21 @@
|
|||||||
module github.com/hibiken/asynq
|
module github.com/hibiken/asynq
|
||||||
|
|
||||||
go 1.24.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/go-cmp v0.7.0
|
github.com/google/go-cmp v0.7.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/redis/go-redis/v9 v9.14.1
|
github.com/redis/go-redis/v9 v9.18.0
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/spf13/cast v1.10.0
|
github.com/spf13/cast v1.10.0
|
||||||
go.uber.org/goleak v1.3.0
|
go.uber.org/goleak v1.3.0
|
||||||
golang.org/x/sys v0.37.0
|
golang.org/x/sys v0.43.0
|
||||||
golang.org/x/time v0.14.0
|
golang.org/x/time v0.15.0
|
||||||
google.golang.org/protobuf v1.36.10
|
google.golang.org/protobuf v1.36.11
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
22
go.sum
22
go.sum
@@ -14,14 +14,16 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/redis/go-redis/v9 v9.14.1 h1:nDCrEiJmfOWhD76xlaw+HXT0c9hfNWeXgl0vIRYSDvQ=
|
github.com/redis/go-redis/v9 v9.18.0 h1:pMkxYPkEbMPwRdenAzUNyFNrDgHx9U+DrBabWNfSRQs=
|
||||||
github.com/redis/go-redis/v9 v9.14.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
|
github.com/redis/go-redis/v9 v9.18.0/go.mod h1:k3ufPphLU5YXwNTUcCRXGxUoF1fqxnhFQmscfkCoDA0=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
@@ -30,13 +32,17 @@ github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
|||||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
|
||||||
|
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
||||||
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||||
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||||
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
|
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
package asynq
|
package asynq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -1003,13 +1002,6 @@ func parseOption(s string) (Option, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return Retention(d), nil
|
return Retention(d), nil
|
||||||
case "Header":
|
|
||||||
var h [2]string
|
|
||||||
err := json.Unmarshal([]byte(arg), &h)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return Header(h[0], h[1]), nil
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("cannot not parse option string %q", s)
|
return nil, fmt.Errorf("cannot not parse option string %q", s)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3526,7 +3526,6 @@ func TestParseOption(t *testing.T) {
|
|||||||
{ProcessAt(oneHourFromNow).String(), ProcessAtOpt, oneHourFromNow},
|
{ProcessAt(oneHourFromNow).String(), ProcessAtOpt, oneHourFromNow},
|
||||||
{`ProcessIn(10m)`, ProcessInOpt, 10 * time.Minute},
|
{`ProcessIn(10m)`, ProcessInOpt, 10 * time.Minute},
|
||||||
{`Retention(24h)`, RetentionOpt, 24 * time.Hour},
|
{`Retention(24h)`, RetentionOpt, 24 * time.Hour},
|
||||||
{`Header(["email", "hello@example.com"])`, HeaderOpt, [2]string{"email", "hello@example.com"}},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -3574,14 +3573,6 @@ func TestParseOption(t *testing.T) {
|
|||||||
if cmp.Equal(gotVal, tc.wantVal.(time.Time)) {
|
if cmp.Equal(gotVal, tc.wantVal.(time.Time)) {
|
||||||
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
||||||
}
|
}
|
||||||
case HeaderOpt:
|
|
||||||
gotVal, ok := got.Value().([2]string)
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("returned Option with non array value")
|
|
||||||
}
|
|
||||||
if gotVal != tc.wantVal.([2]string) {
|
|
||||||
t.Fatalf("got value %v, want %v", gotVal, tc.wantVal)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
t.Fatalf("returned Option with unexpected type: %v", got.Type())
|
t.Fatalf("returned Option with unexpected type: %v", got.Type())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Version of asynq library and CLI.
|
// Version of asynq library and CLI.
|
||||||
const Version = "0.26.0"
|
const Version = "0.27.0"
|
||||||
|
|
||||||
// DefaultQueueName is the queue name used if none are specified by user.
|
// DefaultQueueName is the queue name used if none are specified by user.
|
||||||
const DefaultQueueName = "default"
|
const DefaultQueueName = "default"
|
||||||
@@ -684,14 +684,6 @@ func (l *Lease) IsValid() bool {
|
|||||||
return l.expireAt.After(now) || l.expireAt.Equal(now)
|
return l.expireAt.After(now) || l.expireAt.Equal(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BatchEnqueueItem pairs a task message with optional scheduling metadata for
|
|
||||||
// batch enqueue operations. If ProcessAt is zero, the task is enqueued for
|
|
||||||
// immediate processing; otherwise it is added to the scheduled set.
|
|
||||||
type BatchEnqueueItem struct {
|
|
||||||
Msg *TaskMessage
|
|
||||||
ProcessAt time.Time // zero value → immediate
|
|
||||||
}
|
|
||||||
|
|
||||||
// Broker is a message broker that supports operations to manage task queues.
|
// Broker is a message broker that supports operations to manage task queues.
|
||||||
//
|
//
|
||||||
// See rdb.RDB as a reference implementation.
|
// See rdb.RDB as a reference implementation.
|
||||||
@@ -700,12 +692,6 @@ type Broker interface {
|
|||||||
Close() error
|
Close() error
|
||||||
Enqueue(ctx context.Context, msg *TaskMessage) error
|
Enqueue(ctx context.Context, msg *TaskMessage) error
|
||||||
EnqueueUnique(ctx context.Context, msg *TaskMessage, ttl time.Duration) error
|
EnqueueUnique(ctx context.Context, msg *TaskMessage, ttl time.Duration) error
|
||||||
// BatchEnqueue enqueues multiple tasks in a single round-trip. It returns the
|
|
||||||
// count of newly enqueued tasks; duplicate IDs are silently skipped. The error
|
|
||||||
// is non-nil only on infrastructure failure (e.g. lost connection), in which
|
|
||||||
// case the count is meaningless. Individual task scripts are atomic but the
|
|
||||||
// batch as a whole is not transactional — partial success is possible.
|
|
||||||
BatchEnqueue(ctx context.Context, items []BatchEnqueueItem) (int, error)
|
|
||||||
Dequeue(qnames ...string) (*TaskMessage, time.Time, error)
|
Dequeue(qnames ...string) (*TaskMessage, time.Time, error)
|
||||||
Done(ctx context.Context, msg *TaskMessage) error
|
Done(ctx context.Context, msg *TaskMessage) error
|
||||||
MarkAsComplete(ctx context.Context, msg *TaskMessage) error
|
MarkAsComplete(ctx context.Context, msg *TaskMessage) error
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ package rdb
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -138,8 +137,6 @@ table.insert(res, aggregating_count)
|
|||||||
return res`)
|
return res`)
|
||||||
|
|
||||||
// CurrentStats returns a current state of the queues.
|
// CurrentStats returns a current state of the queues.
|
||||||
// if DISABLE_MEMORY_USAGE_PROFILING is set to "true" or any non empty value, memory usage profiling will be disabled
|
|
||||||
// which is otherwise always performed..
|
|
||||||
func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
||||||
var op errors.Op = "rdb.CurrentStats"
|
var op errors.Op = "rdb.CurrentStats"
|
||||||
exists, err := r.queueExists(qname)
|
exists, err := r.queueExists(qname)
|
||||||
@@ -231,15 +228,11 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
stats.Size = size
|
stats.Size = size
|
||||||
disableMemUsageProfiling := os.Getenv("DISABLE_MEMORY_USAGE_PROFILING")
|
memusg, err := r.memoryUsage(qname)
|
||||||
if disableMemUsageProfiling == "false" || disableMemUsageProfiling == "" {
|
if err != nil {
|
||||||
memusg, err := r.memoryUsage(qname)
|
return nil, errors.E(op, errors.CanonicalCode(err), err)
|
||||||
if err != nil {
|
|
||||||
return nil, errors.E(op, errors.CanonicalCode(err), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
stats.MemoryUsage = memusg
|
|
||||||
}
|
}
|
||||||
|
stats.MemoryUsage = memusg
|
||||||
return stats, nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -139,118 +139,6 @@ func (r *RDB) Enqueue(ctx context.Context, msg *base.TaskMessage) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BatchEnqueue adds all given tasks to Redis using a single pipeline round-trip.
|
|
||||||
// Each item is either enqueued immediately (ProcessAt is zero) or added to the
|
|
||||||
// scheduled sorted set.
|
|
||||||
//
|
|
||||||
// WARNING: tasks whose IDs already exist in Redis are silently skipped.
|
|
||||||
//
|
|
||||||
// The pipeline executes independent Lua scripts per task — there is no
|
|
||||||
// MULTI/EXEC wrapping the batch, so individual tasks may succeed or fail
|
|
||||||
// independently. The returned int is the number of tasks actually written;
|
|
||||||
// skipped duplicates do not count. The returned error is non-nil only when the
|
|
||||||
// pipeline call itself fails (network error, context cancellation, etc.), in
|
|
||||||
// which case no individual result should be trusted.
|
|
||||||
//
|
|
||||||
// Message encoding errors cause an immediate return before any Redis I/O.
|
|
||||||
func (r *RDB) BatchEnqueue(ctx context.Context, items []base.BatchEnqueueItem) (int, error) {
|
|
||||||
var op errors.Op = "rdb.BatchEnqueue"
|
|
||||||
if len(items) == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preload Lua scripts so that EVALSHA inside the pipeline does not fail with
|
|
||||||
// NOSCRIPT. Script.Run on a pipeline only sends EVALSHA (unlike non-pipeline
|
|
||||||
// Run which retries with EVAL on NOSCRIPT).
|
|
||||||
needsEnqueue, needsSchedule := false, false
|
|
||||||
for _, item := range items {
|
|
||||||
if item.ProcessAt.IsZero() {
|
|
||||||
needsEnqueue = true
|
|
||||||
} else {
|
|
||||||
needsSchedule = true
|
|
||||||
}
|
|
||||||
if needsEnqueue && needsSchedule {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needsEnqueue {
|
|
||||||
if err := enqueueCmd.Load(ctx, r.client).Err(); err != nil {
|
|
||||||
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("failed to load enqueue script: %v", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needsSchedule {
|
|
||||||
if err := scheduleCmd.Load(ctx, r.client).Err(); err != nil {
|
|
||||||
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("failed to load schedule script: %v", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pipe := r.client.Pipeline()
|
|
||||||
|
|
||||||
// Track which pipeline slot holds each item's script result.
|
|
||||||
scriptIdxs := make([]int, 0, len(items))
|
|
||||||
pipeLen := 0
|
|
||||||
|
|
||||||
// Track queues we add to AllQueues in this pipeline so we can roll back the
|
|
||||||
// in-memory cache on failure.
|
|
||||||
var newQueues []string
|
|
||||||
|
|
||||||
now := r.clock.Now().UnixNano()
|
|
||||||
|
|
||||||
for _, item := range items {
|
|
||||||
encoded, err := base.EncodeMessage(item.Msg)
|
|
||||||
if err != nil {
|
|
||||||
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
|
|
||||||
}
|
|
||||||
if _, found := r.queuesPublished.Load(item.Msg.Queue); !found {
|
|
||||||
pipe.SAdd(ctx, base.AllQueues, item.Msg.Queue)
|
|
||||||
r.queuesPublished.Store(item.Msg.Queue, true)
|
|
||||||
newQueues = append(newQueues, item.Msg.Queue)
|
|
||||||
pipeLen++
|
|
||||||
}
|
|
||||||
|
|
||||||
if item.ProcessAt.IsZero() {
|
|
||||||
keys := []string{
|
|
||||||
base.TaskKey(item.Msg.Queue, item.Msg.ID),
|
|
||||||
base.PendingKey(item.Msg.Queue),
|
|
||||||
}
|
|
||||||
argv := []interface{}{encoded, item.Msg.ID, now}
|
|
||||||
enqueueCmd.Run(ctx, pipe, keys, argv...)
|
|
||||||
} else {
|
|
||||||
keys := []string{
|
|
||||||
base.TaskKey(item.Msg.Queue, item.Msg.ID),
|
|
||||||
base.ScheduledKey(item.Msg.Queue),
|
|
||||||
}
|
|
||||||
argv := []interface{}{encoded, item.ProcessAt.Unix(), item.Msg.ID}
|
|
||||||
scheduleCmd.Run(ctx, pipe, keys, argv...)
|
|
||||||
}
|
|
||||||
scriptIdxs = append(scriptIdxs, pipeLen)
|
|
||||||
pipeLen++
|
|
||||||
}
|
|
||||||
|
|
||||||
cmds, err := pipe.Exec(ctx)
|
|
||||||
if err != nil && err != redis.Nil {
|
|
||||||
for _, q := range newQueues {
|
|
||||||
r.queuesPublished.Delete(q)
|
|
||||||
}
|
|
||||||
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("redis pipeline error: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
enqueued := 0
|
|
||||||
for _, idx := range scriptIdxs {
|
|
||||||
if idx >= len(cmds) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
res, err := cmds[idx].(*redis.Cmd).Result()
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if n, ok := res.(int64); ok && n == 1 {
|
|
||||||
enqueued++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return enqueued, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// enqueueUniqueCmd enqueues the task message if the task is unique.
|
// enqueueUniqueCmd enqueues the task message if the task is unique.
|
||||||
//
|
//
|
||||||
// KEYS[1] -> unique key
|
// KEYS[1] -> unique key
|
||||||
@@ -1600,7 +1488,6 @@ func (r *RDB) CancelationPubSub() (*redis.PubSub, error) {
|
|||||||
pubsub := r.client.Subscribe(ctx, base.CancelChannel)
|
pubsub := r.client.Subscribe(ctx, base.CancelChannel)
|
||||||
_, err := pubsub.Receive(ctx)
|
_, err := pubsub.Receive(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pubsub.Close()
|
|
||||||
return nil, errors.E(op, errors.Unknown, fmt.Sprintf("redis pubsub receive error: %v", err))
|
return nil, errors.E(op, errors.Unknown, fmt.Sprintf("redis pubsub receive error: %v", err))
|
||||||
}
|
}
|
||||||
return pubsub, nil
|
return pubsub, nil
|
||||||
|
|||||||
@@ -160,156 +160,6 @@ func TestEnqueueTaskIdConflictError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBatchEnqueue(t *testing.T) {
|
|
||||||
r := setup(t)
|
|
||||||
defer r.Close()
|
|
||||||
|
|
||||||
t1 := h.NewTaskMessage("send_email", h.JSON(map[string]interface{}{"to": "user@example.com"}))
|
|
||||||
t2 := h.NewTaskMessageWithQueue("generate_csv", h.JSON(map[string]interface{}{}), "csv")
|
|
||||||
t3 := h.NewTaskMessageWithQueue("sync", nil, "low")
|
|
||||||
|
|
||||||
enqueueTime := time.Now()
|
|
||||||
r.SetClock(timeutil.NewSimulatedClock(enqueueTime))
|
|
||||||
|
|
||||||
t.Run("enqueue multiple tasks", func(t *testing.T) {
|
|
||||||
h.FlushDB(t, r.client)
|
|
||||||
items := []base.BatchEnqueueItem{
|
|
||||||
{Msg: t1},
|
|
||||||
{Msg: t2},
|
|
||||||
{Msg: t3},
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := r.BatchEnqueue(context.Background(), items)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("BatchEnqueue returned error: %v", err)
|
|
||||||
}
|
|
||||||
if n != 3 {
|
|
||||||
t.Errorf("BatchEnqueue returned %d, want 3", n)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range items {
|
|
||||||
msg := item.Msg
|
|
||||||
pendingKey := base.PendingKey(msg.Queue)
|
|
||||||
pendingIDs := r.client.LRange(context.Background(), pendingKey, 0, -1).Val()
|
|
||||||
found := false
|
|
||||||
for _, id := range pendingIDs {
|
|
||||||
if id == msg.ID {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Errorf("task %s not found in pending list %s", msg.ID, pendingKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
taskKey := base.TaskKey(msg.Queue, msg.ID)
|
|
||||||
state := r.client.HGet(context.Background(), taskKey, "state").Val()
|
|
||||||
if state != "pending" {
|
|
||||||
t.Errorf("state for task %s = %q, want %q", msg.ID, state, "pending")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("empty batch", func(t *testing.T) {
|
|
||||||
h.FlushDB(t, r.client)
|
|
||||||
|
|
||||||
n, err := r.BatchEnqueue(context.Background(), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("BatchEnqueue(nil) returned error: %v", err)
|
|
||||||
}
|
|
||||||
if n != 0 {
|
|
||||||
t.Errorf("BatchEnqueue(nil) returned %d, want 0", n)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("duplicate IDs skipped", func(t *testing.T) {
|
|
||||||
h.FlushDB(t, r.client)
|
|
||||||
|
|
||||||
if err := r.Enqueue(context.Background(), t1); err != nil {
|
|
||||||
t.Fatalf("pre-enqueue failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dup := *t1
|
|
||||||
newMsg := h.NewTaskMessage("new_task", nil)
|
|
||||||
|
|
||||||
items := []base.BatchEnqueueItem{
|
|
||||||
{Msg: &dup},
|
|
||||||
{Msg: newMsg},
|
|
||||||
}
|
|
||||||
n, err := r.BatchEnqueue(context.Background(), items)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("BatchEnqueue returned error: %v", err)
|
|
||||||
}
|
|
||||||
if n != 1 {
|
|
||||||
t.Errorf("BatchEnqueue returned %d, want 1 (duplicate should be skipped)", n)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("scheduled tasks", func(t *testing.T) {
|
|
||||||
h.FlushDB(t, r.client)
|
|
||||||
|
|
||||||
future := time.Now().Add(1 * time.Hour)
|
|
||||||
s1 := h.NewTaskMessage("deferred_email", nil)
|
|
||||||
items := []base.BatchEnqueueItem{
|
|
||||||
{Msg: t1},
|
|
||||||
{Msg: s1, ProcessAt: future},
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := r.BatchEnqueue(context.Background(), items)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("BatchEnqueue returned error: %v", err)
|
|
||||||
}
|
|
||||||
if n != 2 {
|
|
||||||
t.Errorf("BatchEnqueue returned %d, want 2", n)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Immediate task should be in pending.
|
|
||||||
pendingIDs := r.client.LRange(context.Background(), base.PendingKey(t1.Queue), 0, -1).Val()
|
|
||||||
foundPending := false
|
|
||||||
for _, id := range pendingIDs {
|
|
||||||
if id == t1.ID {
|
|
||||||
foundPending = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !foundPending {
|
|
||||||
t.Errorf("immediate task %s not found in pending list", t1.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scheduled task should be in scheduled set.
|
|
||||||
scheduledIDs := r.client.ZRange(context.Background(), base.ScheduledKey(s1.Queue), 0, -1).Val()
|
|
||||||
foundScheduled := false
|
|
||||||
for _, id := range scheduledIDs {
|
|
||||||
if id == s1.ID {
|
|
||||||
foundScheduled = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !foundScheduled {
|
|
||||||
t.Errorf("scheduled task %s not found in scheduled set", s1.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
taskKey := base.TaskKey(s1.Queue, s1.ID)
|
|
||||||
state := r.client.HGet(context.Background(), taskKey, "state").Val()
|
|
||||||
if state != "scheduled" {
|
|
||||||
t.Errorf("state for scheduled task %s = %q, want %q", s1.ID, state, "scheduled")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("pipeline error from cancelled context", func(t *testing.T) {
|
|
||||||
h.FlushDB(t, r.client)
|
|
||||||
|
|
||||||
msg := h.NewTaskMessage("pipeline_error_task", nil)
|
|
||||||
items := []base.BatchEnqueueItem{{Msg: msg}}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
_, err := r.BatchEnqueue(ctx, items)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("BatchEnqueue with cancelled context returned nil error, want non-nil")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEnqueueQueueCache(t *testing.T) {
|
func TestEnqueueQueueCache(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
@@ -3424,29 +3274,6 @@ func TestCancelationPubSub(t *testing.T) {
|
|||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCancelationPubSubReceiveError(t *testing.T) {
|
|
||||||
// Use a client connected to a non-existent Redis server to trigger
|
|
||||||
// a Receive() error. This verifies that the pubsub connection is
|
|
||||||
// closed on error, preventing connection leaks.
|
|
||||||
client := redis.NewClient(&redis.Options{
|
|
||||||
Addr: "localhost:0", // invalid port — connection will fail
|
|
||||||
})
|
|
||||||
r := NewRDB(client)
|
|
||||||
defer r.Close()
|
|
||||||
|
|
||||||
pubsub, err := r.CancelationPubSub()
|
|
||||||
if err == nil {
|
|
||||||
// If no error, we must clean up the pubsub.
|
|
||||||
if pubsub != nil {
|
|
||||||
pubsub.Close()
|
|
||||||
}
|
|
||||||
t.Fatal("(*RDB).CancelationPubSub() expected to return an error when redis is unreachable")
|
|
||||||
}
|
|
||||||
if pubsub != nil {
|
|
||||||
t.Error("(*RDB).CancelationPubSub() expected nil pubsub on error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteResult(t *testing.T) {
|
func TestWriteResult(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|||||||
@@ -64,15 +64,6 @@ func (tb *TestBroker) EnqueueUnique(ctx context.Context, msg *base.TaskMessage,
|
|||||||
return tb.real.EnqueueUnique(ctx, msg, ttl)
|
return tb.real.EnqueueUnique(ctx, msg, ttl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tb *TestBroker) BatchEnqueue(ctx context.Context, items []base.BatchEnqueueItem) (int, error) {
|
|
||||||
tb.mu.Lock()
|
|
||||||
defer tb.mu.Unlock()
|
|
||||||
if tb.sleeping {
|
|
||||||
return 0, errRedisDown
|
|
||||||
}
|
|
||||||
return tb.real.BatchEnqueue(ctx, items)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tb *TestBroker) Dequeue(qnames ...string) (*base.TaskMessage, time.Time, error) {
|
func (tb *TestBroker) Dequeue(qnames ...string) (*base.TaskMessage, time.Time, error) {
|
||||||
tb.mu.Lock()
|
tb.mu.Lock()
|
||||||
defer tb.mu.Unlock()
|
defer tb.mu.Unlock()
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -35,14 +36,14 @@ var cronListCmd = &cobra.Command{
|
|||||||
Use: "list",
|
Use: "list",
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Short: "List cron entries",
|
Short: "List cron entries",
|
||||||
RunE: cronList,
|
Run: cronList,
|
||||||
}
|
}
|
||||||
|
|
||||||
var cronHistoryCmd = &cobra.Command{
|
var cronHistoryCmd = &cobra.Command{
|
||||||
Use: "history <entry_id> [<entry_id>...]",
|
Use: "history <entry_id> [<entry_id>...]",
|
||||||
Short: "Show history of each cron tasks",
|
Short: "Show history of each cron tasks",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: cronHistory,
|
Run: cronHistory,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1
|
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1
|
||||||
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1 bf6a8594-cd03-4968-b36a-8572c5e160dd
|
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1 bf6a8594-cd03-4968-b36a-8572c5e160dd
|
||||||
@@ -50,16 +51,17 @@ var cronHistoryCmd = &cobra.Command{
|
|||||||
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1 --page=2`),
|
$ asynq cron history 7837f142-6337-4217-9276-8f27281b67d1 --page=2`),
|
||||||
}
|
}
|
||||||
|
|
||||||
func cronList(cmd *cobra.Command, args []string) error {
|
func cronList(cmd *cobra.Command, args []string) {
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
|
|
||||||
entries, err := inspector.SchedulerEntries()
|
entries, err := inspector.SchedulerEntries()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch scheduler entries: %v", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(entries) == 0 {
|
if len(entries) == 0 {
|
||||||
fmt.Println("No scheduler entries")
|
fmt.Println("No scheduler entries")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort entries by spec.
|
// Sort entries by spec.
|
||||||
@@ -76,7 +78,6 @@ func cronList(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
printTable(cols, printRows)
|
printTable(cols, printRows)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a string describing when the next enqueue will happen.
|
// Returns a string describing when the next enqueue will happen.
|
||||||
@@ -96,14 +97,16 @@ func prevEnqueue(prevEnqueuedAt time.Time) string {
|
|||||||
return fmt.Sprintf("%v ago", time.Since(prevEnqueuedAt).Round(time.Second))
|
return fmt.Sprintf("%v ago", time.Since(prevEnqueuedAt).Round(time.Second))
|
||||||
}
|
}
|
||||||
|
|
||||||
func cronHistory(cmd *cobra.Command, args []string) error {
|
func cronHistory(cmd *cobra.Command, args []string) {
|
||||||
pageNum, err := cmd.Flags().GetInt("page")
|
pageNum, err := cmd.Flags().GetInt("page")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
pageSize, err := cmd.Flags().GetInt("size")
|
pageSize, err := cmd.Flags().GetInt("size")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
for i, entryID := range args {
|
for i, entryID := range args {
|
||||||
@@ -133,5 +136,4 @@ func cronHistory(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
printTable(cols, printRows)
|
printTable(cols, printRows)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/MakeNowJust/heredoc/v2"
|
"github.com/MakeNowJust/heredoc/v2"
|
||||||
@@ -31,14 +32,14 @@ var dashCmd = &cobra.Command{
|
|||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq dash
|
$ asynq dash
|
||||||
$ asynq dash --refresh=3s`),
|
$ asynq dash --refresh=3s`),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if flagPollInterval < 1*time.Second {
|
if flagPollInterval < 1*time.Second {
|
||||||
return fmt.Errorf("--refresh cannot be less than 1s")
|
fmt.Println("error: --refresh cannot be less than 1s")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
dash.Run(dash.Options{
|
dash.Run(dash.Options{
|
||||||
PollInterval: flagPollInterval,
|
PollInterval: flagPollInterval,
|
||||||
RedisConnOpt: getRedisConnOpt(),
|
RedisConnOpt: getRedisConnOpt(),
|
||||||
})
|
})
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/MakeNowJust/heredoc/v2"
|
"github.com/MakeNowJust/heredoc/v2"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -30,25 +31,22 @@ var groupListCmd = &cobra.Command{
|
|||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Short: "List groups",
|
Short: "List groups",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: groupLists,
|
Run: groupLists,
|
||||||
}
|
}
|
||||||
|
|
||||||
func groupLists(cmd *cobra.Command, args []string) error {
|
func groupLists(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
groups, err := inspector.Groups(qname)
|
groups, err := inspector.Groups(qname)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not fetch groups: %v", err)
|
|
||||||
}
|
|
||||||
if len(groups) == 0 {
|
if len(groups) == 0 {
|
||||||
fmt.Printf("No groups found in queue %q\n", qname)
|
fmt.Printf("No groups found in queue %q\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
for _, g := range groups {
|
for _, g := range groups {
|
||||||
fmt.Println(g.Group)
|
fmt.Println(g.Group)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/MakeNowJust/heredoc/v2"
|
"github.com/MakeNowJust/heredoc/v2"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@@ -43,14 +44,16 @@ var queueListCmd = &cobra.Command{
|
|||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List queues",
|
Short: "List queues",
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
RunE: queueList,
|
// TODO: Use RunE instead?
|
||||||
|
Run: queueList,
|
||||||
}
|
}
|
||||||
|
|
||||||
var queueInspectCmd = &cobra.Command{
|
var queueInspectCmd = &cobra.Command{
|
||||||
Use: "inspect <queue> [<queue>...]",
|
Use: "inspect <queue> [<queue>...]",
|
||||||
Short: "Display detailed information on one or more queues",
|
Short: "Display detailed information on one or more queues",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: queueInspect,
|
// TODO: Use RunE instead?
|
||||||
|
Run: queueInspect,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq queue inspect myqueue
|
$ asynq queue inspect myqueue
|
||||||
$ asynq queue inspect queue1 queue2 queue3`),
|
$ asynq queue inspect queue1 queue2 queue3`),
|
||||||
@@ -59,8 +62,8 @@ var queueInspectCmd = &cobra.Command{
|
|||||||
var queueHistoryCmd = &cobra.Command{
|
var queueHistoryCmd = &cobra.Command{
|
||||||
Use: "history <queue> [<queue>...]",
|
Use: "history <queue> [<queue>...]",
|
||||||
Short: "Display historical aggregate data from one or more queues",
|
Short: "Display historical aggregate data from one or more queues",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: queueHistory,
|
Run: queueHistory,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq queue history myqueue
|
$ asynq queue history myqueue
|
||||||
$ asynq queue history queue1 queue2 queue3
|
$ asynq queue history queue1 queue2 queue3
|
||||||
@@ -71,7 +74,7 @@ var queuePauseCmd = &cobra.Command{
|
|||||||
Use: "pause <queue> [<queue>...]",
|
Use: "pause <queue> [<queue>...]",
|
||||||
Short: "Pause one or more queues",
|
Short: "Pause one or more queues",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: queuePause,
|
Run: queuePause,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq queue pause myqueue
|
$ asynq queue pause myqueue
|
||||||
$ asynq queue pause queue1 queue2 queue3`),
|
$ asynq queue pause queue1 queue2 queue3`),
|
||||||
@@ -82,7 +85,7 @@ var queueUnpauseCmd = &cobra.Command{
|
|||||||
Short: "Resume (unpause) one or more queues",
|
Short: "Resume (unpause) one or more queues",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Aliases: []string{"unpause"},
|
Aliases: []string{"unpause"},
|
||||||
RunE: queueUnpause,
|
Run: queueUnpause,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq queue resume myqueue
|
$ asynq queue resume myqueue
|
||||||
$ asynq queue resume queue1 queue2 queue3`),
|
$ asynq queue resume queue1 queue2 queue3`),
|
||||||
@@ -93,14 +96,14 @@ var queueRemoveCmd = &cobra.Command{
|
|||||||
Short: "Remove one or more queues",
|
Short: "Remove one or more queues",
|
||||||
Aliases: []string{"rm", "delete"},
|
Aliases: []string{"rm", "delete"},
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: queueRemove,
|
Run: queueRemove,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq queue rm myqueue
|
$ asynq queue rm myqueue
|
||||||
$ asynq queue rm queue1 queue2 queue3
|
$ asynq queue rm queue1 queue2 queue3
|
||||||
$ asynq queue rm myqueue --force`),
|
$ asynq queue rm myqueue --force`),
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueList(cmd *cobra.Command, args []string) error {
|
func queueList(cmd *cobra.Command, args []string) {
|
||||||
type queueInfo struct {
|
type queueInfo struct {
|
||||||
name string
|
name string
|
||||||
keyslot int64
|
keyslot int64
|
||||||
@@ -109,7 +112,8 @@ func queueList(cmd *cobra.Command, args []string) error {
|
|||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
queues, err := inspector.Queues()
|
queues, err := inspector.Queues()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch list of queues: %v", err)
|
fmt.Printf("error: Could not fetch list of queues: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
var qs []*queueInfo
|
var qs []*queueInfo
|
||||||
for _, qname := range queues {
|
for _, qname := range queues {
|
||||||
@@ -117,13 +121,13 @@ func queueList(cmd *cobra.Command, args []string) error {
|
|||||||
if useRedisCluster {
|
if useRedisCluster {
|
||||||
keyslot, err := inspector.ClusterKeySlot(qname)
|
keyslot, err := inspector.ClusterKeySlot(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: could not get cluster keyslot for %q\n", qname)
|
fmt.Errorf("error: Could not get cluster keyslot for %q\n", qname)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
q.keyslot = keyslot
|
q.keyslot = keyslot
|
||||||
nodes, err := inspector.ClusterNodes(qname)
|
nodes, err := inspector.ClusterNodes(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: could not get cluster nodes for %q\n", qname)
|
fmt.Errorf("error: Could not get cluster nodes for %q\n", qname)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
q.nodes = nodes
|
q.nodes = nodes
|
||||||
@@ -144,10 +148,9 @@ func queueList(cmd *cobra.Command, args []string) error {
|
|||||||
fmt.Println(q.name)
|
fmt.Println(q.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueInspect(cmd *cobra.Command, args []string) error {
|
func queueInspect(cmd *cobra.Command, args []string) {
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
for i, qname := range args {
|
for i, qname := range args {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
@@ -160,7 +163,6 @@ func queueInspect(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
printQueueInfo(info)
|
printQueueInfo(info)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printQueueInfo(info *asynq.QueueInfo) {
|
func printQueueInfo(info *asynq.QueueInfo) {
|
||||||
@@ -193,10 +195,11 @@ func printQueueInfo(info *asynq.QueueInfo) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueHistory(cmd *cobra.Command, args []string) error {
|
func queueHistory(cmd *cobra.Command, args []string) {
|
||||||
days, err := cmd.Flags().GetInt("days")
|
days, err := cmd.Flags().GetInt("days")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: Internal error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
for i, qname := range args {
|
for i, qname := range args {
|
||||||
@@ -211,7 +214,6 @@ func queueHistory(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
printDailyStats(stats)
|
printDailyStats(stats)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printDailyStats(stats []*asynq.DailyStats) {
|
func printDailyStats(stats []*asynq.DailyStats) {
|
||||||
@@ -231,63 +233,49 @@ func printDailyStats(stats []*asynq.DailyStats) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func queuePause(cmd *cobra.Command, args []string) error {
|
func queuePause(cmd *cobra.Command, args []string) {
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
var firstErr error
|
|
||||||
for _, qname := range args {
|
for _, qname := range args {
|
||||||
err := inspector.PauseQueue(qname)
|
err := inspector.PauseQueue(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
if firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("Successfully paused queue %q\n", qname)
|
fmt.Printf("Successfully paused queue %q\n", qname)
|
||||||
}
|
}
|
||||||
return firstErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueUnpause(cmd *cobra.Command, args []string) error {
|
func queueUnpause(cmd *cobra.Command, args []string) {
|
||||||
inspector := createInspector()
|
inspector := createInspector()
|
||||||
var firstErr error
|
|
||||||
for _, qname := range args {
|
for _, qname := range args {
|
||||||
err := inspector.UnpauseQueue(qname)
|
err := inspector.UnpauseQueue(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
if firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("Successfully unpaused queue %q\n", qname)
|
fmt.Printf("Successfully unpaused queue %q\n", qname)
|
||||||
}
|
}
|
||||||
return firstErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueRemove(cmd *cobra.Command, args []string) error {
|
func queueRemove(cmd *cobra.Command, args []string) {
|
||||||
// TODO: Use inspector once RemoveQueue become public API.
|
// TODO: Use inspector once RemoveQueue become public API.
|
||||||
force, err := cmd.Flags().GetBool("force")
|
force, err := cmd.Flags().GetBool("force")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: Internal error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := createRDB()
|
r := createRDB()
|
||||||
var firstErr error
|
|
||||||
for _, qname := range args {
|
for _, qname := range args {
|
||||||
err = r.RemoveQueue(qname, force)
|
err = r.RemoveQueue(qname, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.IsQueueNotEmpty(err) {
|
if errors.IsQueueNotEmpty(err) {
|
||||||
fmt.Printf("error: %v\nIf you are sure you want to delete it, run 'asynq queue rm --force %s'\n", err, qname)
|
fmt.Printf("error: %v\nIf you are sure you want to delete it, run 'asynq queue rm --force %s'\n", err, qname)
|
||||||
} else {
|
continue
|
||||||
fmt.Printf("error: %v\n", err)
|
|
||||||
}
|
|
||||||
if firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
}
|
||||||
|
fmt.Printf("error: %v\n", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("Successfully removed queue %q\n", qname)
|
fmt.Printf("Successfully removed queue %q\n", qname)
|
||||||
}
|
}
|
||||||
return firstErr
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -487,31 +487,35 @@ func isPrintable(data []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper to turn a command line flag into a duration
|
// Helper to turn a command line flag into a duration
|
||||||
func getDuration(cmd *cobra.Command, arg string) (time.Duration, error) {
|
func getDuration(cmd *cobra.Command, arg string) time.Duration {
|
||||||
durationStr, err := cmd.Flags().GetString(arg)
|
durationStr, err := cmd.Flags().GetString(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
duration, err := time.ParseDuration(durationStr)
|
duration, err := time.ParseDuration(durationStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return duration, nil
|
return duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to turn a command line flag into a time
|
// Helper to turn a command line flag into a time
|
||||||
func getTime(cmd *cobra.Command, arg string) (time.Time, error) {
|
func getTime(cmd *cobra.Command, arg string) time.Time {
|
||||||
timeStr, err := cmd.Flags().GetString(arg)
|
timeStr, err := cmd.Flags().GetString(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
timeVal, err := time.Parse(time.RFC3339, timeStr)
|
timeVal, err := time.Parse(time.RFC3339, timeStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return timeVal, nil
|
return timeVal
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -43,19 +44,20 @@ The command shows the following for each server:
|
|||||||
|
|
||||||
A "active" server is pulling tasks from queues and processing them.
|
A "active" server is pulling tasks from queues and processing them.
|
||||||
A "stopped" server is no longer pulling new tasks from queues`,
|
A "stopped" server is no longer pulling new tasks from queues`,
|
||||||
RunE: serverList,
|
Run: serverList,
|
||||||
}
|
}
|
||||||
|
|
||||||
func serverList(cmd *cobra.Command, args []string) error {
|
func serverList(cmd *cobra.Command, args []string) {
|
||||||
r := createRDB()
|
r := createRDB()
|
||||||
|
|
||||||
servers, err := r.ListServers()
|
servers, err := r.ListServers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch list of servers: %v", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(servers) == 0 {
|
if len(servers) == 0 {
|
||||||
fmt.Println("No running servers")
|
fmt.Println("No running servers")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort by hostname and pid
|
// sort by hostname and pid
|
||||||
@@ -78,7 +80,6 @@ func serverList(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
printTable(cols, printRows)
|
printTable(cols, printRows)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatQueues(qmap map[string]int) string {
|
func formatQueues(qmap map[string]int) string {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ var statsCmd = &cobra.Command{
|
|||||||
* Aggregate data for the current day
|
* Aggregate data for the current day
|
||||||
* Basic information about the running redis instance`),
|
* Basic information about the running redis instance`),
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: stats,
|
Run: stats,
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonFlag bool
|
var jsonFlag bool
|
||||||
@@ -74,12 +74,13 @@ type FullStats struct {
|
|||||||
RedisInfo map[string]string `json:"redis"`
|
RedisInfo map[string]string `json:"redis"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func stats(cmd *cobra.Command, args []string) error {
|
func stats(cmd *cobra.Command, args []string) {
|
||||||
r := createRDB()
|
r := createRDB()
|
||||||
|
|
||||||
queues, err := r.AllQueues()
|
queues, err := r.AllQueues()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch queues: %v", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var aggStats AggregateStats
|
var aggStats AggregateStats
|
||||||
@@ -87,7 +88,8 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
for _, qname := range queues {
|
for _, qname := range queues {
|
||||||
s, err := r.CurrentStats(qname)
|
s, err := r.CurrentStats(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch stats for queue %q: %v", qname, err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
aggStats.Active += s.Active
|
aggStats.Active += s.Active
|
||||||
aggStats.Pending += s.Pending
|
aggStats.Pending += s.Pending
|
||||||
@@ -108,7 +110,8 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
info, err = r.RedisInfo()
|
info, err = r.RedisInfo()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not fetch redis info: %v", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonFlag {
|
if jsonFlag {
|
||||||
@@ -119,11 +122,12 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not marshal stats to JSON: %v", err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(statsJSON))
|
fmt.Println(string(statsJSON))
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bold := color.New(color.Bold)
|
bold := color.New(color.Bold)
|
||||||
@@ -147,7 +151,6 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
printInfo(info)
|
printInfo(info)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printStatsByState(s *AggregateStats) {
|
func printStatsByState(s *AggregateStats) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/MakeNowJust/heredoc/v2"
|
"github.com/MakeNowJust/heredoc/v2"
|
||||||
@@ -119,14 +120,14 @@ var taskListCmd = &cobra.Command{
|
|||||||
$ asynq task list --queue=myqueue --state=pending
|
$ asynq task list --queue=myqueue --state=pending
|
||||||
$ asynq task list --queue=myqueue --state=aggregating --group=mygroup
|
$ asynq task list --queue=myqueue --state=aggregating --group=mygroup
|
||||||
$ asynq task list --queue=myqueue --state=scheduled --page=2`),
|
$ asynq task list --queue=myqueue --state=scheduled --page=2`),
|
||||||
RunE: taskList,
|
Run: taskList,
|
||||||
}
|
}
|
||||||
|
|
||||||
var taskInspectCmd = &cobra.Command{
|
var taskInspectCmd = &cobra.Command{
|
||||||
Use: "inspect --queue=<queue> --id=<task_id>",
|
Use: "inspect --queue=<queue> --id=<task_id>",
|
||||||
Short: "Display detailed information on the specified task",
|
Short: "Display detailed information on the specified task",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskInspect,
|
Run: taskInspect,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task inspect --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
$ asynq task inspect --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
||||||
}
|
}
|
||||||
@@ -135,7 +136,7 @@ var taskCancelCmd = &cobra.Command{
|
|||||||
Use: "cancel <task_id> [<task_id>...]",
|
Use: "cancel <task_id> [<task_id>...]",
|
||||||
Short: "Cancel one or more active tasks",
|
Short: "Cancel one or more active tasks",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: taskCancel,
|
Run: taskCancel,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task cancel f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
$ asynq task cancel f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
||||||
}
|
}
|
||||||
@@ -144,7 +145,7 @@ var taskArchiveCmd = &cobra.Command{
|
|||||||
Use: "archive --queue=<queue> --id=<task_id>",
|
Use: "archive --queue=<queue> --id=<task_id>",
|
||||||
Short: "Archive a task with the given id",
|
Short: "Archive a task with the given id",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskArchive,
|
Run: taskArchive,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task archive --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
$ asynq task archive --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
||||||
}
|
}
|
||||||
@@ -154,7 +155,7 @@ var taskDeleteCmd = &cobra.Command{
|
|||||||
Aliases: []string{"remove", "rm"},
|
Aliases: []string{"remove", "rm"},
|
||||||
Short: "Delete a task with the given id",
|
Short: "Delete a task with the given id",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskDelete,
|
Run: taskDelete,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task delete --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
$ asynq task delete --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
||||||
}
|
}
|
||||||
@@ -163,7 +164,7 @@ var taskRunCmd = &cobra.Command{
|
|||||||
Use: "run --queue=<queue> --id=<task_id>",
|
Use: "run --queue=<queue> --id=<task_id>",
|
||||||
Short: "Run a task with the given id",
|
Short: "Run a task with the given id",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskRun,
|
Run: taskRun,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task run --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
$ asynq task run --queue=myqueue --id=f1720682-f5a6-4db1-8953-4f48ae541d0f`),
|
||||||
}
|
}
|
||||||
@@ -172,7 +173,7 @@ var taskEnqueueCmd = &cobra.Command{
|
|||||||
Use: "enqueue --type_name=footype --payload=barpayload",
|
Use: "enqueue --type_name=footype --payload=barpayload",
|
||||||
Short: "Enqueue a task",
|
Short: "Enqueue a task",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskEnqueue,
|
Run: taskEnqueue,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task enqueue -t footype -l barpayload
|
$ asynq task enqueue -t footype -l barpayload
|
||||||
$ asynq task enqueue -t footask -l barpayload --retry 3 --id f1720682-f5a6-4db1-8953-4f48ae541d0f --queue bazqueue --timeout 100s --deadline 2024-12-14T01:23:45Z --unique 100s --process_at 2024-12-14T01:22:05Z --process_in 100s --retention 5h --group baygroup`),
|
$ asynq task enqueue -t footask -l barpayload --retry 3 --id f1720682-f5a6-4db1-8953-4f48ae541d0f --queue bazqueue --timeout 100s --deadline 2024-12-14T01:23:45Z --unique 100s --process_at 2024-12-14T01:22:05Z --process_in 100s --retention 5h --group baygroup`),
|
||||||
@@ -182,7 +183,7 @@ var taskArchiveAllCmd = &cobra.Command{
|
|||||||
Use: "archiveall --queue=<queue> --state=<state>",
|
Use: "archiveall --queue=<queue> --state=<state>",
|
||||||
Short: "Archive all tasks in the given state",
|
Short: "Archive all tasks in the given state",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskArchiveAll,
|
Run: taskArchiveAll,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task archiveall --queue=myqueue --state=retry
|
$ asynq task archiveall --queue=myqueue --state=retry
|
||||||
$ asynq task archiveall --queue=myqueue --state=aggregating --group=mygroup`),
|
$ asynq task archiveall --queue=myqueue --state=aggregating --group=mygroup`),
|
||||||
@@ -192,7 +193,7 @@ var taskDeleteAllCmd = &cobra.Command{
|
|||||||
Use: "deleteall --queue=<queue> --state=<state>",
|
Use: "deleteall --queue=<queue> --state=<state>",
|
||||||
Short: "Delete all tasks in the given state",
|
Short: "Delete all tasks in the given state",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskDeleteAll,
|
Run: taskDeleteAll,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task deleteall --queue=myqueue --state=archived
|
$ asynq task deleteall --queue=myqueue --state=archived
|
||||||
$ asynq task deleteall --queue=myqueue --state=aggregating --group=mygroup`),
|
$ asynq task deleteall --queue=myqueue --state=aggregating --group=mygroup`),
|
||||||
@@ -202,66 +203,74 @@ var taskRunAllCmd = &cobra.Command{
|
|||||||
Use: "runall --queue=<queue> --state=<state>",
|
Use: "runall --queue=<queue> --state=<state>",
|
||||||
Short: "Run all tasks in the given state",
|
Short: "Run all tasks in the given state",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: taskRunAll,
|
Run: taskRunAll,
|
||||||
Example: heredoc.Doc(`
|
Example: heredoc.Doc(`
|
||||||
$ asynq task runall --queue=myqueue --state=retry
|
$ asynq task runall --queue=myqueue --state=retry
|
||||||
$ asynq task runall --queue=myqueue --state=aggregating --group=mygroup`),
|
$ asynq task runall --queue=myqueue --state=aggregating --group=mygroup`),
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskList(cmd *cobra.Command, args []string) error {
|
func taskList(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
state, err := cmd.Flags().GetString("state")
|
state, err := cmd.Flags().GetString("state")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
pageNum, err := cmd.Flags().GetInt("page")
|
pageNum, err := cmd.Flags().GetInt("page")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
pageSize, err := cmd.Flags().GetInt("size")
|
pageSize, err := cmd.Flags().GetInt("size")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch state {
|
switch state {
|
||||||
case "active":
|
case "active":
|
||||||
return listActiveTasks(qname, pageNum, pageSize)
|
listActiveTasks(qname, pageNum, pageSize)
|
||||||
case "pending":
|
case "pending":
|
||||||
return listPendingTasks(qname, pageNum, pageSize)
|
listPendingTasks(qname, pageNum, pageSize)
|
||||||
case "scheduled":
|
case "scheduled":
|
||||||
return listScheduledTasks(qname, pageNum, pageSize)
|
listScheduledTasks(qname, pageNum, pageSize)
|
||||||
case "retry":
|
case "retry":
|
||||||
return listRetryTasks(qname, pageNum, pageSize)
|
listRetryTasks(qname, pageNum, pageSize)
|
||||||
case "archived":
|
case "archived":
|
||||||
return listArchivedTasks(qname, pageNum, pageSize)
|
listArchivedTasks(qname, pageNum, pageSize)
|
||||||
case "completed":
|
case "completed":
|
||||||
return listCompletedTasks(qname, pageNum, pageSize)
|
listCompletedTasks(qname, pageNum, pageSize)
|
||||||
case "aggregating":
|
case "aggregating":
|
||||||
group, err := cmd.Flags().GetString("group")
|
group, err := cmd.Flags().GetString("group")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return fmt.Errorf("flag --group is required for listing aggregating tasks")
|
fmt.Println("Flag --group is required for listing aggregating tasks")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return listAggregatingTasks(qname, group, pageNum, pageSize)
|
listAggregatingTasks(qname, group, pageNum, pageSize)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("state=%q is not supported", state)
|
fmt.Printf("error: state=%q is not supported\n", state)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listActiveTasks(qname string, pageNum, pageSize int) error {
|
func listActiveTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListActiveTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListActiveTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No active tasks in %q queue\n", qname)
|
fmt.Printf("No active tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload"},
|
[]string{"ID", "Type", "Payload"},
|
||||||
@@ -271,18 +280,18 @@ func listActiveTasks(qname string, pageNum, pageSize int) error {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listPendingTasks(qname string, pageNum, pageSize int) error {
|
func listPendingTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListPendingTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListPendingTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No pending tasks in %q queue\n", qname)
|
fmt.Printf("No pending tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload"},
|
[]string{"ID", "Type", "Payload"},
|
||||||
@@ -292,18 +301,18 @@ func listPendingTasks(qname string, pageNum, pageSize int) error {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listScheduledTasks(qname string, pageNum, pageSize int) error {
|
func listScheduledTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListScheduledTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListScheduledTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No scheduled tasks in %q queue\n", qname)
|
fmt.Printf("No scheduled tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload", "Process In"},
|
[]string{"ID", "Type", "Payload", "Process In"},
|
||||||
@@ -313,7 +322,6 @@ func listScheduledTasks(qname string, pageNum, pageSize int) error {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatProcessAt formats next process at time to human friendly string.
|
// formatProcessAt formats next process at time to human friendly string.
|
||||||
@@ -327,15 +335,16 @@ func formatProcessAt(processAt time.Time) string {
|
|||||||
return fmt.Sprintf("in %v", d.Round(time.Second))
|
return fmt.Sprintf("in %v", d.Round(time.Second))
|
||||||
}
|
}
|
||||||
|
|
||||||
func listRetryTasks(qname string, pageNum, pageSize int) error {
|
func listRetryTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListRetryTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListRetryTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No retry tasks in %q queue\n", qname)
|
fmt.Printf("No retry tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload", "Next Retry", "Last Error", "Last Failed", "Retried", "Max Retry"},
|
[]string{"ID", "Type", "Payload", "Next Retry", "Last Error", "Last Failed", "Retried", "Max Retry"},
|
||||||
@@ -346,18 +355,18 @@ func listRetryTasks(qname string, pageNum, pageSize int) error {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listArchivedTasks(qname string, pageNum, pageSize int) error {
|
func listArchivedTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListArchivedTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListArchivedTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No archived tasks in %q queue\n", qname)
|
fmt.Printf("No archived tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload", "Last Failed", "Last Error"},
|
[]string{"ID", "Type", "Payload", "Last Failed", "Last Error"},
|
||||||
@@ -366,18 +375,18 @@ func listArchivedTasks(qname string, pageNum, pageSize int) error {
|
|||||||
fmt.Fprintf(w, tmpl, t.ID, t.Type, sprintBytes(t.Payload), formatPastTime(t.LastFailedAt), t.LastErr)
|
fmt.Fprintf(w, tmpl, t.ID, t.Type, sprintBytes(t.Payload), formatPastTime(t.LastFailedAt), t.LastErr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listCompletedTasks(qname string, pageNum, pageSize int) error {
|
func listCompletedTasks(qname string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListCompletedTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListCompletedTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No completed tasks in %q queue\n", qname)
|
fmt.Printf("No completed tasks in %q queue\n", qname)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload", "CompletedAt", "Result"},
|
[]string{"ID", "Type", "Payload", "CompletedAt", "Result"},
|
||||||
@@ -386,18 +395,18 @@ func listCompletedTasks(qname string, pageNum, pageSize int) error {
|
|||||||
fmt.Fprintf(w, tmpl, t.ID, t.Type, sprintBytes(t.Payload), formatPastTime(t.CompletedAt), sprintBytes(t.Result))
|
fmt.Fprintf(w, tmpl, t.ID, t.Type, sprintBytes(t.Payload), formatPastTime(t.CompletedAt), sprintBytes(t.Result))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listAggregatingTasks(qname, group string, pageNum, pageSize int) error {
|
func listAggregatingTasks(qname, group string, pageNum, pageSize int) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
tasks, err := i.ListAggregatingTasks(qname, group, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListAggregatingTasks(qname, group, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(tasks) == 0 {
|
if len(tasks) == 0 {
|
||||||
fmt.Printf("No aggregating tasks in group %q \n", group)
|
fmt.Printf("No aggregating tasks in group %q \n", group)
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
printTable(
|
printTable(
|
||||||
[]string{"ID", "Type", "Payload", "Group"},
|
[]string{"ID", "Type", "Payload", "Group"},
|
||||||
@@ -407,42 +416,38 @@ func listAggregatingTasks(qname, group string, pageNum, pageSize int) error {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskCancel(cmd *cobra.Command, args []string) error {
|
func taskCancel(cmd *cobra.Command, args []string) {
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
var firstErr error
|
|
||||||
for _, id := range args {
|
for _, id := range args {
|
||||||
if err := i.CancelProcessing(id); err != nil {
|
if err := i.CancelProcessing(id); err != nil {
|
||||||
fmt.Printf("error: could not send cancelation signal: %v\n", err)
|
fmt.Printf("error: could not send cancelation signal: %v\n", err)
|
||||||
if firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("Sent cancelation signal for task %s\n", id)
|
fmt.Printf("Sent cancelation signal for task %s\n", id)
|
||||||
}
|
}
|
||||||
return firstErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskInspect(cmd *cobra.Command, args []string) error {
|
func taskInspect(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
id, err := cmd.Flags().GetString("id")
|
id, err := cmd.Flags().GetString("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
info, err := i.GetTaskInfo(qname, id)
|
info, err := i.GetTaskInfo(qname, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get task info: %v", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
printTaskInfo(info)
|
printTaskInfo(info)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printTaskInfo(info *asynq.TaskInfo) {
|
func printTaskInfo(info *asynq.TaskInfo) {
|
||||||
@@ -481,72 +486,80 @@ func formatPastTime(t time.Time) string {
|
|||||||
return t.Format(time.UnixDate)
|
return t.Format(time.UnixDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskArchive(cmd *cobra.Command, args []string) error {
|
func taskArchive(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
id, err := cmd.Flags().GetString("id")
|
id, err := cmd.Flags().GetString("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
err = i.ArchiveTask(qname, id)
|
err = i.ArchiveTask(qname, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not archive task: %v", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println("task archived")
|
fmt.Println("task archived")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskDelete(cmd *cobra.Command, args []string) error {
|
func taskDelete(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
id, err := cmd.Flags().GetString("id")
|
id, err := cmd.Flags().GetString("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
err = i.DeleteTask(qname, id)
|
err = i.DeleteTask(qname, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not delete task: %v", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println("task deleted")
|
fmt.Println("task deleted")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskRun(cmd *cobra.Command, args []string) error {
|
func taskRun(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
id, err := cmd.Flags().GetString("id")
|
id, err := cmd.Flags().GetString("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
err = i.RunTask(qname, id)
|
err = i.RunTask(qname, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not run task: %v", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println("task is now pending")
|
fmt.Println("task is now pending")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskEnqueue(cmd *cobra.Command, args []string) error {
|
func taskEnqueue(cmd *cobra.Command, args []string) {
|
||||||
typeName, err := cmd.Flags().GetString("type_name")
|
typeName, err := cmd.Flags().GetString("type_name")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := cmd.Flags().GetString("payload")
|
payload, err := cmd.Flags().GetString("payload")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// For all of the optional flags, we need to explicitly check whether they were set or
|
// For all of the optional flags, we need to explicitly check whether they were set or
|
||||||
@@ -556,7 +569,8 @@ func taskEnqueue(cmd *cobra.Command, args []string) error {
|
|||||||
if cmd.Flags().Changed("retry") {
|
if cmd.Flags().Changed("retry") {
|
||||||
retry, err := cmd.Flags().GetInt("retry")
|
retry, err := cmd.Flags().GetInt("retry")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
opts = append(opts, asynq.MaxRetry(retry))
|
opts = append(opts, asynq.MaxRetry(retry))
|
||||||
}
|
}
|
||||||
@@ -564,7 +578,8 @@ func taskEnqueue(cmd *cobra.Command, args []string) error {
|
|||||||
if cmd.Flags().Changed("queue") {
|
if cmd.Flags().Changed("queue") {
|
||||||
queue, err := cmd.Flags().GetString("queue")
|
queue, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
opts = append(opts, asynq.Queue(queue))
|
opts = append(opts, asynq.Queue(queue))
|
||||||
}
|
}
|
||||||
@@ -572,63 +587,41 @@ func taskEnqueue(cmd *cobra.Command, args []string) error {
|
|||||||
if cmd.Flags().Changed("id") {
|
if cmd.Flags().Changed("id") {
|
||||||
id, err := cmd.Flags().GetString("id")
|
id, err := cmd.Flags().GetString("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
opts = append(opts, asynq.TaskID(id))
|
opts = append(opts, asynq.TaskID(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("timeout") {
|
if cmd.Flags().Changed("timeout") {
|
||||||
d, err := getDuration(cmd, "timeout")
|
opts = append(opts, asynq.Timeout(getDuration(cmd, "timeout")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.Timeout(d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("deadline") {
|
if cmd.Flags().Changed("deadline") {
|
||||||
t, err := getTime(cmd, "deadline")
|
opts = append(opts, asynq.Deadline(getTime(cmd, "deadline")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.Deadline(t))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("unique") {
|
if cmd.Flags().Changed("unique") {
|
||||||
d, err := getDuration(cmd, "unique")
|
opts = append(opts, asynq.Unique(getDuration(cmd, "unique")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.Unique(d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("process_at") {
|
if cmd.Flags().Changed("process_at") {
|
||||||
t, err := getTime(cmd, "process_at")
|
opts = append(opts, asynq.ProcessAt(getTime(cmd, "process_at")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.ProcessAt(t))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("process_in") {
|
if cmd.Flags().Changed("process_in") {
|
||||||
d, err := getDuration(cmd, "process_in")
|
opts = append(opts, asynq.ProcessIn(getDuration(cmd, "process_in")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.ProcessIn(d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("retention") {
|
if cmd.Flags().Changed("retention") {
|
||||||
d, err := getDuration(cmd, "retention")
|
opts = append(opts, asynq.Retention(getDuration(cmd, "retention")))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts = append(opts, asynq.Retention(d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Flags().Changed("group") {
|
if cmd.Flags().Changed("group") {
|
||||||
group, err := cmd.Flags().GetString("group")
|
group, err := cmd.Flags().GetString("group")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
opts = append(opts, asynq.Group(group))
|
opts = append(opts, asynq.Group(group))
|
||||||
}
|
}
|
||||||
@@ -638,21 +631,23 @@ func taskEnqueue(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
taskInfo, err := c.Enqueue(task)
|
taskInfo, err := c.Enqueue(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not enqueue task: %v", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Enqueued task %s to queue %s\n", taskInfo.ID, taskInfo.Queue)
|
fmt.Printf("Enqueued task %s to queue %s\n", taskInfo.ID, taskInfo.Queue)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskArchiveAll(cmd *cobra.Command, args []string) error {
|
func taskArchiveAll(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
state, err := cmd.Flags().GetString("state")
|
state, err := cmd.Flags().GetString("state")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
@@ -667,35 +662,35 @@ func taskArchiveAll(cmd *cobra.Command, args []string) error {
|
|||||||
case "aggregating":
|
case "aggregating":
|
||||||
group, err := cmd.Flags().GetString("group")
|
group, err := cmd.Flags().GetString("group")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return fmt.Errorf("flag --group is required for aggregating tasks")
|
fmt.Println("error: Flag --group is required for aggregating tasks")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
n, err = i.ArchiveAllAggregatingTasks(qname, group)
|
n, err = i.ArchiveAllAggregatingTasks(qname, group)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("%d tasks archived\n", n)
|
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported state %q", state)
|
fmt.Printf("error: unsupported state %q\n", state)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("%d tasks archived\n", n)
|
fmt.Printf("%d tasks archived\n", n)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskDeleteAll(cmd *cobra.Command, args []string) error {
|
func taskDeleteAll(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
state, err := cmd.Flags().GetString("state")
|
state, err := cmd.Flags().GetString("state")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
@@ -714,35 +709,35 @@ func taskDeleteAll(cmd *cobra.Command, args []string) error {
|
|||||||
case "aggregating":
|
case "aggregating":
|
||||||
group, err := cmd.Flags().GetString("group")
|
group, err := cmd.Flags().GetString("group")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return fmt.Errorf("flag --group is required for aggregating tasks")
|
fmt.Println("error: Flag --group is required for aggregating tasks")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
n, err = i.DeleteAllAggregatingTasks(qname, group)
|
n, err = i.DeleteAllAggregatingTasks(qname, group)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("%d tasks deleted\n", n)
|
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported state %q", state)
|
fmt.Printf("error: unsupported state %q\n", state)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("%d tasks deleted\n", n)
|
fmt.Printf("%d tasks deleted\n", n)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskRunAll(cmd *cobra.Command, args []string) error {
|
func taskRunAll(cmd *cobra.Command, args []string) {
|
||||||
qname, err := cmd.Flags().GetString("queue")
|
qname, err := cmd.Flags().GetString("queue")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
state, err := cmd.Flags().GetString("state")
|
state, err := cmd.Flags().GetString("state")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
i := createInspector()
|
i := createInspector()
|
||||||
@@ -757,23 +752,21 @@ func taskRunAll(cmd *cobra.Command, args []string) error {
|
|||||||
case "aggregating":
|
case "aggregating":
|
||||||
group, err := cmd.Flags().GetString("group")
|
group, err := cmd.Flags().GetString("group")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return fmt.Errorf("flag --group is required for aggregating tasks")
|
fmt.Println("error: Flag --group is required for aggregating tasks")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
n, err = i.RunAllAggregatingTasks(qname, group)
|
n, err = i.RunAllAggregatingTasks(qname, group)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("%d tasks are now pending\n", n)
|
|
||||||
return nil
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported state %q", state)
|
fmt.Printf("error: unsupported state %q\n", state)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Printf("error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("%d tasks are now pending\n", n)
|
fmt.Printf("%d tasks are now pending\n", n)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user