Compare commits
75 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
aef0775c05 | ||
|
de146993d2 | ||
|
60cbf8dc5a | ||
|
fb38086590 | ||
|
cfcd19a222 | ||
|
24ee4b9693 | ||
|
7849b395bd | ||
|
fa3082e5bb | ||
|
d13f7e900f | ||
|
b63476ddc8 | ||
|
210b026b01 | ||
|
556b2103fe | ||
|
0289bc7a10 | ||
|
ae942c93e5 | ||
|
0faf97f146 | ||
|
711bfa371f | ||
|
73d62844e6 | ||
|
00b82904c6 | ||
|
a866369866 | ||
|
26b78136ba | ||
|
44aad7f037 | ||
|
9884d5f2fa | ||
|
826f1ecff4 | ||
|
24f2b64c6c | ||
|
1c1474c55c | ||
|
5161b9368a | ||
|
0c998a8e17 | ||
|
49160f2536 | ||
|
e33d297d8e | ||
|
eb8ced6bdd | ||
|
789a9fd711 | ||
|
5924cdac33 | ||
|
442c9275a0 | ||
|
a0865df33c | ||
|
431a96a1f7 | ||
|
74e5582cfc | ||
|
bf542a781c | ||
|
7c7f8e5f30 | ||
|
46ab4417dd | ||
|
f8a94fb839 | ||
|
42453280f4 | ||
|
4ec2dc9e47 | ||
|
45933eb6b0 | ||
|
4df372b369 | ||
|
c688b8f4f9 | ||
|
eef2f5f3cb | ||
|
239ef27a6e | ||
|
24da281aa7 | ||
|
b086e88a47 | ||
|
cf61911a49 | ||
|
aafd8a5b74 | ||
|
4f11e52558 | ||
|
b14c73809e | ||
|
779065c269 | ||
|
f9842ba914 | ||
|
022dc29701 | ||
|
40d1889ba0 | ||
|
7e96e893fe | ||
|
84b0c76c8b | ||
|
60b887b8e3 | ||
|
7864bea55c | ||
|
47220554ca | ||
|
f91c05b92c | ||
|
9b4438347e | ||
|
c33dd447ac | ||
|
6df2c3ae2b | ||
|
37554fd23c | ||
|
77f5a38453 | ||
|
8d2b9d6be7 | ||
|
1b7d557c66 | ||
|
30b68728d4 | ||
|
310d38620d | ||
|
1a53bbf21b | ||
|
9c79a7d507 | ||
|
516f95edff |
6
.gitignore
vendored
@@ -15,7 +15,7 @@
|
|||||||
/examples
|
/examples
|
||||||
|
|
||||||
# Ignore command binary
|
# Ignore command binary
|
||||||
/tools/asynqmon/asynqmon
|
/tools/asynq/asynq
|
||||||
|
|
||||||
# Ignore asynqmon config file
|
# Ignore asynq config file
|
||||||
.asynqmon.*
|
.asynq.*
|
@@ -2,9 +2,7 @@ language: go
|
|||||||
go_import_path: github.com/hibiken/asynq
|
go_import_path: github.com/hibiken/asynq
|
||||||
git:
|
git:
|
||||||
depth: 1
|
depth: 1
|
||||||
env:
|
go: [1.13.x, 1.14.x]
|
||||||
- GO111MODULE=on # go modules are the default
|
|
||||||
go: [1.12.x, 1.13.x, 1.14.x]
|
|
||||||
script:
|
script:
|
||||||
- go test -race -v -coverprofile=coverage.txt -covermode=atomic ./...
|
- go test -race -v -coverprofile=coverage.txt -covermode=atomic ./...
|
||||||
services:
|
services:
|
||||||
|
67
CHANGELOG.md
@@ -7,6 +7,73 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.9.0] - 2020-05-16
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `Logger` interface has changed. Please see the godoc for the new interface.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `LogLevel` type is added. Server's log level can be specified through `LogLevel` field in `Config`.
|
||||||
|
|
||||||
|
## [0.8.3] - 2020-05-08
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `Close` method is added to `Client`.
|
||||||
|
|
||||||
|
## [0.8.2] - 2020-05-03
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- [Fixed cancelfunc leak](https://github.com/hibiken/asynq/pull/145)
|
||||||
|
|
||||||
|
## [0.8.1] - 2020-04-27
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `ParseRedisURI` helper function is added to create a `RedisConnOpt` from a URI string.
|
||||||
|
- `SetDefaultOptions` method is added to `Client`.
|
||||||
|
|
||||||
|
## [0.8.0] - 2020-04-19
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `Background` type is renamed to `Server`.
|
||||||
|
- To upgrade from the previous version, Update `NewBackground` to `NewServer` and pass `Config` by value.
|
||||||
|
- CLI is renamed to `asynq`.
|
||||||
|
- To upgrade the CLI to the latest version run `go get -u github.com/hibiken/tools/asynq`
|
||||||
|
- The `ps` command in CLI is renamed to `servers`
|
||||||
|
- `Concurrency` defaults to the number of CPUs when unset or set to a negative value.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `ShutdownTimeout` field is added to `Config` to speicfy timeout duration used during graceful shutdown.
|
||||||
|
- New `Server` type exposes `Start`, `Stop`, and `Quiet` as well as `Run`.
|
||||||
|
|
||||||
|
## [0.7.1] - 2020-04-05
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed signal handling for windows.
|
||||||
|
|
||||||
|
## [0.7.0] - 2020-03-22
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Support Go v1.13+, dropped support for go v1.12
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `Unique` option was added to allow client to enqueue a task only if it's unique within a certain time period.
|
||||||
|
|
||||||
|
## [0.6.2] - 2020-03-15
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `Use` method was added to `ServeMux` to apply middlewares to all handlers.
|
||||||
|
|
||||||
## [0.6.1] - 2020-03-12
|
## [0.6.1] - 2020-03-12
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
254
README.md
@@ -7,12 +7,41 @@
|
|||||||
[](https://gitter.im/go-asynq/community)
|
[](https://gitter.im/go-asynq/community)
|
||||||
[](https://codecov.io/gh/hibiken/asynq)
|
[](https://codecov.io/gh/hibiken/asynq)
|
||||||
|
|
||||||
Asynq is a simple Go library for queueing tasks and processing them in the background with workers.
|
## Overview
|
||||||
It is backed by Redis and it is designed to have a low barrier to entry. It should be integrated in your web stack easily.
|
|
||||||
|
|
||||||
**Important Note**: Current major version is zero (v0.x.x) to accomodate rapid development and fast iteration while getting early feedback from users. The public API could change without a major version update before v1.0.0 release.
|
Asynq is a Go library for queueing tasks and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It should be integrated in your web stack easily.
|
||||||
|
|
||||||

|
Highlevel overview of how Asynq works:
|
||||||
|
|
||||||
|
- Client puts task on a queue
|
||||||
|
- Server pulls task off queues and starts a worker goroutine for each task
|
||||||
|
- Tasks are processed concurrently by multiple workers
|
||||||
|
|
||||||
|
Task queues are used as a mechanism to distribute work across multiple machines.
|
||||||
|
A system can consist of multiple worker servers and brokers, giving way to high availability and horizontal scaling.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Stability and Compatibility
|
||||||
|
|
||||||
|
**Important Note**: Current major version is zero (v0.x.x) to accomodate rapid development and fast iteration while getting early feedback from users (Feedback on APIs are appreciated!). The public API could change without a major version update before v1.0.0 release.
|
||||||
|
|
||||||
|
**Status**: The library is currently undergoing heavy development with frequent, breaking API changes.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Guaranteed [at least one execution](https://www.cloudcomputingpatterns.org/at_least_once_delivery/) of a task
|
||||||
|
- Scheduling of tasks
|
||||||
|
- Durability since tasks are written to Redis
|
||||||
|
- [Retries](https://github.com/hibiken/asynq/wiki/Task-Retry) of failed tasks
|
||||||
|
- [Weighted priority queues](https://github.com/hibiken/asynq/wiki/Priority-Queues#weighted-priority-queues)
|
||||||
|
- [Strict priority queues](https://github.com/hibiken/asynq/wiki/Priority-Queues#strict-priority-queues)
|
||||||
|
- Low latency to add a task since writes are fast in Redis
|
||||||
|
- De-duplication of tasks using [unique option](https://github.com/hibiken/asynq/wiki/Unique-Tasks)
|
||||||
|
- Allow [timeout and deadline per task](https://github.com/hibiken/asynq/wiki/Task-Timeout-and-Cancelation)
|
||||||
|
- [Flexible handler interface with support for middlewares](https://github.com/hibiken/asynq/wiki/Handler-Deep-Dive)
|
||||||
|
- [Support Redis Sentinels](https://github.com/hibiken/asynq/wiki/Automatic-Failover) for HA
|
||||||
|
- [CLI](#command-line-tool) to inspect and remote-control queues and tasks
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
@@ -22,63 +51,175 @@ First, make sure you are running a Redis server locally.
|
|||||||
$ redis-server
|
$ redis-server
|
||||||
```
|
```
|
||||||
|
|
||||||
To create and schedule tasks, use `Client` and provide a task and when to enqueue the task.
|
Next, write a package that encapsulates task creation and task handling.
|
||||||
A task will be processed by a background worker as soon as the task gets enqueued.
|
|
||||||
Scheduled tasks will be stored in Redis and will be enqueued at the specified time.
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
package tasks
|
||||||
r := &asynq.RedisClientOpt{
|
|
||||||
Addr: "127.0.0.1:6379",
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A list of task types.
|
||||||
|
const (
|
||||||
|
EmailDelivery = "email:deliver"
|
||||||
|
ImageProcessing = "image:process"
|
||||||
|
)
|
||||||
|
|
||||||
|
//--------------------------------------------
|
||||||
|
// Write function NewXXXTask to create a task.
|
||||||
|
//--------------------------------------------
|
||||||
|
|
||||||
|
func NewEmailDeliveryTask(userID int, tmplID string) *asynq.Task {
|
||||||
|
payload := map[string]interface{}{"user_id": userID, "template_id": tmplID}
|
||||||
|
return asynq.NewTask(EmailDelivery, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewImageProcessingTask(src, dst string) *asynq.Task {
|
||||||
|
payload := map[string]interface{}{"src": src, "dst": dst}
|
||||||
|
return asynq.NewTask(ImageProcessing, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Write function HandleXXXTask to handle the given task.
|
||||||
|
// NOTE: It satisfies the asynq.HandlerFunc interface.
|
||||||
|
//
|
||||||
|
// Handler doesn't need to be a function. You can define a type
|
||||||
|
// that satisfies asynq.Handler interface. See example below.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
func HandleEmailDeliveryTask(ctx context.Context, t *asynq.Task) error {
|
||||||
|
userID, err := t.Payload.GetInt("user_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
tmplID, err := t.Payload.GetString("template_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("Send Email to User: user_id = %d, template_id = %s\n", userID, tmplID)
|
||||||
|
// Email delivery logic ...
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
client := asynq.NewClient(r)
|
type ImageProcesser struct {
|
||||||
|
// ... fields for struct
|
||||||
|
}
|
||||||
|
|
||||||
// Create a task with task type and payload.
|
// ImageProcessor implements asynq.Handler.
|
||||||
t1 := asynq.NewTask("email:signup", map[string]interface{}{"user_id": 42})
|
func (p *ImageProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error {
|
||||||
|
src, err := t.Payload.GetString("src")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dst, err := t.Payload.GetString("dst")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("Process image: src = %s, dst = %s\n", src, dst)
|
||||||
|
// Image processing logic ...
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
t2 := asynq.NewTask("email:reminder", map[string]interface{}{"user_id": 42})
|
func NewImageProcessor() *ImageProcessor {
|
||||||
|
// ... return an instance
|
||||||
// Enqueue immediately.
|
|
||||||
err := client.Enqueue(t1)
|
|
||||||
|
|
||||||
// Enqueue 24 hrs later.
|
|
||||||
err = client.EnqueueIn(24*time.Hour, t2)
|
|
||||||
|
|
||||||
// Enqueue at specific time.
|
|
||||||
err = client.EnqueueAt(time.Date(2020, time.March, 6, 10, 0, 0, 0, time.UTC), t2)
|
|
||||||
|
|
||||||
// Pass vararg options to specify processing behavior for the given task.
|
|
||||||
//
|
|
||||||
// MaxRetry specifies the max number of retry if the task fails (Default is 25).
|
|
||||||
// Queue specifies which queue to enqueue this task to (Default is "default" queue).
|
|
||||||
// Timeout specifies the the task timeout (Default is no timeout).
|
|
||||||
err = client.Enqueue(t1, asynq.MaxRetry(10), asynq.Queue("critical"), asynq.Timeout(time.Minute))
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
To start the background workers, use `Background` and provide your `Handler` to process the tasks.
|
In your web application code, import the above package and use [`Client`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#Client) to put tasks on the queue.
|
||||||
|
A task will be processed asynchronously by a background worker as soon as the task gets enqueued.
|
||||||
`Handler` is an interface with one method `ProcessTask` with the following signature.
|
Scheduled tasks will be stored in Redis and will be enqueued at the specified time.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// ProcessTask should return nil if the processing of a task is successful.
|
package main
|
||||||
//
|
|
||||||
// If ProcessTask return a non-nil error or panics, the task will be retried after delay.
|
import (
|
||||||
type Handler interface {
|
"time"
|
||||||
ProcessTask(context.Context, *asynq.Task) error
|
|
||||||
|
"github.com/hibiken/asynq"
|
||||||
|
"your/app/package/tasks"
|
||||||
|
)
|
||||||
|
|
||||||
|
const redisAddr = "127.0.0.1:6379"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := asynq.RedisClientOpt{Addr: redisAddr}
|
||||||
|
c := asynq.NewClient(r)
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
// ----------------------------------------------------
|
||||||
|
// Example 1: Enqueue task to be processed immediately.
|
||||||
|
// Use (*Client).Enqueue method.
|
||||||
|
// ----------------------------------------------------
|
||||||
|
|
||||||
|
t := tasks.NewEmailDeliveryTask(42, "some:template:id")
|
||||||
|
err := c.Enqueue(t)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not enqueue task: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
// Example 2: Schedule task to be processed in the future.
|
||||||
|
// Use (*Client).EnqueueIn or (*Client).EnqueueAt.
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
|
||||||
|
t = tasks.NewEmailDeliveryTask(42, "other:template:id")
|
||||||
|
err = c.EnqueueIn(24*time.Hour, t)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not schedule task: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
// Example 3: Set options to tune task processing behavior.
|
||||||
|
// Options include MaxRetry, Queue, Timeout, Deadline, Unique etc.
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
c.SetDefaultOptions(tasks.ImageProcessing, asynq.MaxRetry(10), asynq.Timeout(time.Minute))
|
||||||
|
|
||||||
|
t = tasks.NewImageProcessingTask("some/blobstore/url", "other/blobstore/url")
|
||||||
|
err = c.Enqueue(t)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not enqueue task: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
// Example 4: Pass options to tune task processing behavior at enqueue time.
|
||||||
|
// Options passed at enqueue time override default ones, if any.
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
t = tasks.NewImageProcessingTask("some/blobstore/url", "other/blobstore/url")
|
||||||
|
err = c.Enqueue(t, asynq.Queue("critical"), asynq.Timeout(30*time.Second))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not enqueue task: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can optionally use `ServeMux` to create a handler, just as you would with `"net/http"` Handler.
|
Next, create a worker server to process these tasks in the background.
|
||||||
|
To start the background workers, use [`Server`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#Server) and provide your [`Handler`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#Handler) to process the tasks.
|
||||||
|
|
||||||
|
You can optionally use [`ServeMux`](https://pkg.go.dev/github.com/hibiken/asynq?tab=doc#ServeMux) to create a handler, just as you would with [`"net/http"`](https://golang.org/pkg/net/http/) Handler.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
package main
|
||||||
r := &asynq.RedisClientOpt{
|
|
||||||
Addr: "127.0.0.1:6379",
|
|
||||||
}
|
|
||||||
|
|
||||||
bg := asynq.NewBackground(r, &asynq.Config{
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq"
|
||||||
|
"your/app/package/tasks"
|
||||||
|
)
|
||||||
|
|
||||||
|
const redisAddr = "127.0.0.1:6379"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := asynq.RedisClientOpt{Addr: redisAddr}
|
||||||
|
|
||||||
|
srv := asynq.NewServer(r, asynq.Config{
|
||||||
// Specify how many concurrent workers to use
|
// Specify how many concurrent workers to use
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
// Optionally specify multiple queues with different priority.
|
// Optionally specify multiple queues with different priority.
|
||||||
@@ -92,22 +233,13 @@ func main() {
|
|||||||
|
|
||||||
// mux maps a type to a handler
|
// mux maps a type to a handler
|
||||||
mux := asynq.NewServeMux()
|
mux := asynq.NewServeMux()
|
||||||
mux.HandleFunc("email:signup", signupEmailHandler)
|
mux.HandleFunc(tasks.EmailDelivery, tasks.HandleEmailDeliveryTask)
|
||||||
mux.HandleFunc("email:reminder", reminderEmailHandler)
|
mux.Handle(tasks.ImageProcessing, tasks.NewImageProcessor())
|
||||||
// ...register other handlers...
|
// ...register other handlers...
|
||||||
|
|
||||||
bg.Run(mux)
|
if err := srv.Run(mux); err != nil {
|
||||||
}
|
log.Fatalf("could not run server: %v", err)
|
||||||
|
|
||||||
// function with the same signature as the ProcessTask method for the Handler interface.
|
|
||||||
func signupEmailHandler(ctx context.Context, t *asynq.Task) error {
|
|
||||||
id, err := t.Payload.GetInt("user_id")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
fmt.Printf("Send welcome email to user %d\n", id)
|
|
||||||
// ...your email sending logic...
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -123,7 +255,7 @@ Here's an example of running the `stats` command.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
For details on how to use the tool, refer to the tool's [README](/tools/asynqmon/README.md).
|
For details on how to use the tool, refer to the tool's [README](/tools/asynq/README.md).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -136,7 +268,7 @@ go get -u github.com/hibiken/asynq
|
|||||||
To install the CLI tool, run the following command:
|
To install the CLI tool, run the following command:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get -u github.com/hibiken/asynq/tools/asynqmon
|
go get -u github.com/hibiken/asynq/tools/asynq
|
||||||
```
|
```
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
@@ -144,7 +276,7 @@ go get -u github.com/hibiken/asynq/tools/asynqmon
|
|||||||
| Dependency | Version |
|
| Dependency | Version |
|
||||||
| -------------------------- | ------- |
|
| -------------------------- | ------- |
|
||||||
| [Redis](https://redis.io/) | v2.8+ |
|
| [Redis](https://redis.io/) | v2.8+ |
|
||||||
| [Go](https://golang.org/) | v1.12+ |
|
| [Go](https://golang.org/) | v1.13+ |
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
@@ -155,7 +287,7 @@ Please see the [Contribution Guide](/CONTRIBUTING.md) before contributing.
|
|||||||
|
|
||||||
- [Sidekiq](https://github.com/mperham/sidekiq) : Many of the design ideas are taken from sidekiq and its Web UI
|
- [Sidekiq](https://github.com/mperham/sidekiq) : Many of the design ideas are taken from sidekiq and its Web UI
|
||||||
- [RQ](https://github.com/rq/rq) : Client APIs are inspired by rq library.
|
- [RQ](https://github.com/rq/rq) : Client APIs are inspired by rq library.
|
||||||
- [Cobra](https://github.com/spf13/cobra) : Asynqmon CLI is built with cobra
|
- [Cobra](https://github.com/spf13/cobra) : Asynq CLI is built with cobra
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
76
asynq.go
@@ -7,6 +7,9 @@ package asynq
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v7"
|
"github.com/go-redis/redis/v7"
|
||||||
)
|
)
|
||||||
@@ -94,6 +97,79 @@ type RedisFailoverClientOpt struct {
|
|||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseRedisURI parses redis uri string and returns RedisConnOpt if uri is valid.
|
||||||
|
// It returns a non-nil error if uri cannot be parsed.
|
||||||
|
//
|
||||||
|
// Three URI schemes are supported, which are redis:, redis-socket:, and redis-sentinel:.
|
||||||
|
// Supported formats are:
|
||||||
|
// redis://[:password@]host[:port][/dbnumber]
|
||||||
|
// redis-socket://[:password@]path[?db=dbnumber]
|
||||||
|
// redis-sentinel://[:password@]host1[:port][,host2:[:port]][,hostN:[:port]][?master=masterName]
|
||||||
|
func ParseRedisURI(uri string) (RedisConnOpt, error) {
|
||||||
|
u, err := url.Parse(uri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("asynq: could not parse redis uri: %v", err)
|
||||||
|
}
|
||||||
|
switch u.Scheme {
|
||||||
|
case "redis":
|
||||||
|
return parseRedisURI(u)
|
||||||
|
case "redis-socket":
|
||||||
|
return parseRedisSocketURI(u)
|
||||||
|
case "redis-sentinel":
|
||||||
|
return parseRedisSentinelURI(u)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("asynq: unsupported uri scheme: %q", u.Scheme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRedisURI(u *url.URL) (RedisConnOpt, error) {
|
||||||
|
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 uri: database number should be the first segment of the path")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var password string
|
||||||
|
if v, ok := u.User.Password(); ok {
|
||||||
|
password = v
|
||||||
|
}
|
||||||
|
return RedisClientOpt{Addr: u.Host, DB: db, Password: password}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRedisSocketURI(u *url.URL) (RedisConnOpt, error) {
|
||||||
|
const errPrefix = "asynq: could not parse redis socket uri"
|
||||||
|
if len(u.Path) == 0 {
|
||||||
|
return nil, fmt.Errorf("%s: path does not exist", errPrefix)
|
||||||
|
}
|
||||||
|
q := u.Query()
|
||||||
|
var db int
|
||||||
|
var err error
|
||||||
|
if n := q.Get("db"); n != "" {
|
||||||
|
db, err = strconv.Atoi(n)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s: query param `db` should be a number", errPrefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var password string
|
||||||
|
if v, ok := u.User.Password(); ok {
|
||||||
|
password = v
|
||||||
|
}
|
||||||
|
return RedisClientOpt{Network: "unix", Addr: u.Path, DB: db, Password: password}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRedisSentinelURI(u *url.URL) (RedisConnOpt, error) {
|
||||||
|
addrs := strings.Split(u.Host, ",")
|
||||||
|
master := u.Query().Get("master")
|
||||||
|
var password string
|
||||||
|
if v, ok := u.User.Password(); ok {
|
||||||
|
password = v
|
||||||
|
}
|
||||||
|
return RedisFailoverClientOpt{MasterName: master, SentinelAddrs: addrs, Password: password}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// createRedisClient returns a redis client given a redis connection configuration.
|
// createRedisClient returns a redis client given a redis connection configuration.
|
||||||
//
|
//
|
||||||
// Passing an unexpected type as a RedisConnOpt argument will cause panic.
|
// Passing an unexpected type as a RedisConnOpt argument will cause panic.
|
||||||
|
131
asynq_test.go
@@ -5,7 +5,7 @@
|
|||||||
package asynq
|
package asynq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"flag"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -15,16 +15,28 @@ import (
|
|||||||
"github.com/hibiken/asynq/internal/log"
|
"github.com/hibiken/asynq/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This file defines test helper functions used by
|
//============================================================================
|
||||||
// other test files.
|
// This file defines helper functions and variables used in other test files.
|
||||||
|
//============================================================================
|
||||||
|
|
||||||
// redis used for package testing.
|
// variables used for package testing.
|
||||||
const (
|
var (
|
||||||
redisAddr = "localhost:6379"
|
redisAddr string
|
||||||
redisDB = 14
|
redisDB int
|
||||||
|
|
||||||
|
testLogLevel = FatalLevel
|
||||||
)
|
)
|
||||||
|
|
||||||
var testLogger = log.NewLogger(os.Stderr)
|
var testLogger *log.Logger
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&redisAddr, "redis_addr", "localhost:6379", "redis address to use in testing")
|
||||||
|
flag.IntVar(&redisDB, "redis_db", 14, "redis db number to use in testing")
|
||||||
|
flag.Var(&testLogLevel, "loglevel", "log level to use in testing")
|
||||||
|
|
||||||
|
testLogger = log.NewLogger(nil)
|
||||||
|
testLogger.SetLevel(toInternalLogLevel(testLogLevel))
|
||||||
|
}
|
||||||
|
|
||||||
func setup(tb testing.TB) *redis.Client {
|
func setup(tb testing.TB) *redis.Client {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
@@ -44,3 +56,106 @@ var sortTaskOpt = cmp.Transformer("SortMsg", func(in []*Task) []*Task {
|
|||||||
})
|
})
|
||||||
return out
|
return out
|
||||||
})
|
})
|
||||||
|
|
||||||
|
func TestParseRedisURI(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
uri string
|
||||||
|
want RedisConnOpt
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"redis://localhost:6379",
|
||||||
|
RedisClientOpt{Addr: "localhost:6379"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis://localhost:6379/3",
|
||||||
|
RedisClientOpt{Addr: "localhost:6379", DB: 3},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis://:mypassword@localhost:6379",
|
||||||
|
RedisClientOpt{Addr: "localhost:6379", Password: "mypassword"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis://:mypassword@127.0.0.1:6379/11",
|
||||||
|
RedisClientOpt{Addr: "127.0.0.1:6379", Password: "mypassword", DB: 11},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-socket:///var/run/redis/redis.sock",
|
||||||
|
RedisClientOpt{Network: "unix", Addr: "/var/run/redis/redis.sock"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-socket://:mypassword@/var/run/redis/redis.sock",
|
||||||
|
RedisClientOpt{Network: "unix", Addr: "/var/run/redis/redis.sock", Password: "mypassword"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-socket:///var/run/redis/redis.sock?db=7",
|
||||||
|
RedisClientOpt{Network: "unix", Addr: "/var/run/redis/redis.sock", DB: 7},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-socket://:mypassword@/var/run/redis/redis.sock?db=12",
|
||||||
|
RedisClientOpt{Network: "unix", Addr: "/var/run/redis/redis.sock", Password: "mypassword", DB: 12},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-sentinel://localhost:5000,localhost:5001,localhost:5002?master=mymaster",
|
||||||
|
RedisFailoverClientOpt{
|
||||||
|
MasterName: "mymaster",
|
||||||
|
SentinelAddrs: []string{"localhost:5000", "localhost:5001", "localhost:5002"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"redis-sentinel://:mypassword@localhost:5000,localhost:5001,localhost:5002?master=mymaster",
|
||||||
|
RedisFailoverClientOpt{
|
||||||
|
MasterName: "mymaster",
|
||||||
|
SentinelAddrs: []string{"localhost:5000", "localhost:5001", "localhost:5002"},
|
||||||
|
Password: "mypassword",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got, err := ParseRedisURI(tc.uri)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ParseRedisURI(%q) returned an error: %v", tc.uri, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||||
|
t.Errorf("ParseRedisURI(%q) = %+v, want %+v\n(-want,+got)\n%s", tc.uri, got, tc.want, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseRedisURIErrors(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
desc string
|
||||||
|
uri string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"unsupported scheme",
|
||||||
|
"rdb://localhost:6379",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"missing scheme",
|
||||||
|
"localhost:6379",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"multiple db numbers",
|
||||||
|
"redis://localhost:6379/1,2,3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"missing path for socket connection",
|
||||||
|
"redis-socket://?db=one",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"non integer for db numbers for socket",
|
||||||
|
"redis-socket:///some/path/to/redis?db=one",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
_, err := ParseRedisURI(tc.uri)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("%s: ParseRedisURI(%q) succeeded for malformed input, want error",
|
||||||
|
tc.desc, tc.uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
313
background.go
@@ -1,313 +0,0 @@
|
|||||||
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT license
|
|
||||||
// that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package asynq
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"sync"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
|
||||||
"github.com/hibiken/asynq/internal/log"
|
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Background is responsible for managing the background-task processing.
|
|
||||||
//
|
|
||||||
// Background manages task queues to process tasks.
|
|
||||||
// If the processing of a task is unsuccessful, background will
|
|
||||||
// schedule it for a retry until either the task gets processed successfully
|
|
||||||
// or it exhausts its max retry count.
|
|
||||||
//
|
|
||||||
// Once a task exhausts its retries, it will be moved to the "dead" queue and
|
|
||||||
// will be kept in the queue for some time until a certain condition is met
|
|
||||||
// (e.g., queue size reaches a certain limit, or the task has been in the
|
|
||||||
// queue for a certain amount of time).
|
|
||||||
type Background struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
running bool
|
|
||||||
|
|
||||||
ps *base.ProcessState
|
|
||||||
|
|
||||||
// wait group to wait for all goroutines to finish.
|
|
||||||
wg sync.WaitGroup
|
|
||||||
|
|
||||||
logger Logger
|
|
||||||
|
|
||||||
rdb *rdb.RDB
|
|
||||||
scheduler *scheduler
|
|
||||||
processor *processor
|
|
||||||
syncer *syncer
|
|
||||||
heartbeater *heartbeater
|
|
||||||
subscriber *subscriber
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config specifies the background-task processing behavior.
|
|
||||||
type Config struct {
|
|
||||||
// Maximum number of concurrent processing of tasks.
|
|
||||||
//
|
|
||||||
// If set to a zero or negative value, NewBackground will overwrite the value to one.
|
|
||||||
Concurrency int
|
|
||||||
|
|
||||||
// Function to calculate retry delay for a failed task.
|
|
||||||
//
|
|
||||||
// By default, it uses exponential backoff algorithm to calculate the delay.
|
|
||||||
//
|
|
||||||
// n is the number of times the task has been retried.
|
|
||||||
// e is the error returned by the task handler.
|
|
||||||
// t is the task in question.
|
|
||||||
RetryDelayFunc func(n int, e error, t *Task) time.Duration
|
|
||||||
|
|
||||||
// List of queues to process with given priority value. Keys are the names of the
|
|
||||||
// queues and values are associated priority value.
|
|
||||||
//
|
|
||||||
// If set to nil or not specified, the background will process only the "default" queue.
|
|
||||||
//
|
|
||||||
// Priority is treated as follows to avoid starving low priority queues.
|
|
||||||
//
|
|
||||||
// Example:
|
|
||||||
// Queues: map[string]int{
|
|
||||||
// "critical": 6,
|
|
||||||
// "default": 3,
|
|
||||||
// "low": 1,
|
|
||||||
// }
|
|
||||||
// With the above config and given that all queues are not empty, the tasks
|
|
||||||
// in "critical", "default", "low" should be processed 60%, 30%, 10% of
|
|
||||||
// the time respectively.
|
|
||||||
//
|
|
||||||
// If a queue has a zero or negative priority value, the queue will be ignored.
|
|
||||||
Queues map[string]int
|
|
||||||
|
|
||||||
// StrictPriority indicates whether the queue priority should be treated strictly.
|
|
||||||
//
|
|
||||||
// If set to true, tasks in the queue with the highest priority is processed first.
|
|
||||||
// The tasks in lower priority queues are processed only when those queues with
|
|
||||||
// higher priorities are empty.
|
|
||||||
StrictPriority bool
|
|
||||||
|
|
||||||
// ErrorHandler handles errors returned by the task handler.
|
|
||||||
//
|
|
||||||
// HandleError is invoked only if the task handler returns a non-nil error.
|
|
||||||
//
|
|
||||||
// Example:
|
|
||||||
// func reportError(task *asynq.Task, err error, retried, maxRetry int) {
|
|
||||||
// if retried >= maxRetry {
|
|
||||||
// err = fmt.Errorf("retry exhausted for task %s: %w", task.Type, err)
|
|
||||||
// }
|
|
||||||
// errorReportingService.Notify(err)
|
|
||||||
// })
|
|
||||||
//
|
|
||||||
// ErrorHandler: asynq.ErrorHandlerFunc(reportError)
|
|
||||||
ErrorHandler ErrorHandler
|
|
||||||
|
|
||||||
// Logger specifies the logger used by the background instance.
|
|
||||||
//
|
|
||||||
// If unset, default logger is used.
|
|
||||||
Logger Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
// An ErrorHandler handles errors returned by the task handler.
|
|
||||||
type ErrorHandler interface {
|
|
||||||
HandleError(task *Task, err error, retried, maxRetry int)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The ErrorHandlerFunc type is an adapter to allow the use of ordinary functions as a ErrorHandler.
|
|
||||||
// If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.
|
|
||||||
type ErrorHandlerFunc func(task *Task, err error, retried, maxRetry int)
|
|
||||||
|
|
||||||
// HandleError calls fn(task, err, retried, maxRetry)
|
|
||||||
func (fn ErrorHandlerFunc) HandleError(task *Task, err error, retried, maxRetry int) {
|
|
||||||
fn(task, err, retried, maxRetry)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logger implements logging with various log levels.
|
|
||||||
type Logger interface {
|
|
||||||
// Debug logs a message at Debug level.
|
|
||||||
Debug(format string, args ...interface{})
|
|
||||||
|
|
||||||
// Info logs a message at Info level.
|
|
||||||
Info(format string, args ...interface{})
|
|
||||||
|
|
||||||
// Warn logs a message at Warning level.
|
|
||||||
Warn(format string, args ...interface{})
|
|
||||||
|
|
||||||
// Error logs a message at Error level.
|
|
||||||
Error(format string, args ...interface{})
|
|
||||||
|
|
||||||
// Fatal logs a message at Fatal level
|
|
||||||
// and process will exit with status set to 1.
|
|
||||||
Fatal(format string, args ...interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Formula taken from https://github.com/mperham/sidekiq.
|
|
||||||
func defaultDelayFunc(n int, e error, t *Task) time.Duration {
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
s := int(math.Pow(float64(n), 4)) + 15 + (r.Intn(30) * (n + 1))
|
|
||||||
return time.Duration(s) * time.Second
|
|
||||||
}
|
|
||||||
|
|
||||||
var defaultQueueConfig = map[string]int{
|
|
||||||
base.DefaultQueueName: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBackground returns a new Background given a redis connection option
|
|
||||||
// and background processing configuration.
|
|
||||||
func NewBackground(r RedisConnOpt, cfg *Config) *Background {
|
|
||||||
n := cfg.Concurrency
|
|
||||||
if n < 1 {
|
|
||||||
n = 1
|
|
||||||
}
|
|
||||||
delayFunc := cfg.RetryDelayFunc
|
|
||||||
if delayFunc == nil {
|
|
||||||
delayFunc = defaultDelayFunc
|
|
||||||
}
|
|
||||||
queues := make(map[string]int)
|
|
||||||
for qname, p := range cfg.Queues {
|
|
||||||
if p > 0 {
|
|
||||||
queues[qname] = p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(queues) == 0 {
|
|
||||||
queues = defaultQueueConfig
|
|
||||||
}
|
|
||||||
logger := cfg.Logger
|
|
||||||
if logger == nil {
|
|
||||||
logger = log.NewLogger(os.Stderr)
|
|
||||||
}
|
|
||||||
|
|
||||||
host, err := os.Hostname()
|
|
||||||
if err != nil {
|
|
||||||
host = "unknown-host"
|
|
||||||
}
|
|
||||||
pid := os.Getpid()
|
|
||||||
|
|
||||||
rdb := rdb.NewRDB(createRedisClient(r))
|
|
||||||
ps := base.NewProcessState(host, pid, n, queues, cfg.StrictPriority)
|
|
||||||
syncCh := make(chan *syncRequest)
|
|
||||||
cancels := base.NewCancelations()
|
|
||||||
syncer := newSyncer(logger, syncCh, 5*time.Second)
|
|
||||||
heartbeater := newHeartbeater(logger, rdb, ps, 5*time.Second)
|
|
||||||
scheduler := newScheduler(logger, rdb, 5*time.Second, queues)
|
|
||||||
processor := newProcessor(logger, rdb, ps, delayFunc, syncCh, cancels, cfg.ErrorHandler)
|
|
||||||
subscriber := newSubscriber(logger, rdb, cancels)
|
|
||||||
return &Background{
|
|
||||||
logger: logger,
|
|
||||||
rdb: rdb,
|
|
||||||
ps: ps,
|
|
||||||
scheduler: scheduler,
|
|
||||||
processor: processor,
|
|
||||||
syncer: syncer,
|
|
||||||
heartbeater: heartbeater,
|
|
||||||
subscriber: subscriber,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Handler processes tasks.
|
|
||||||
//
|
|
||||||
// ProcessTask should return nil if the processing of a task
|
|
||||||
// is successful.
|
|
||||||
//
|
|
||||||
// If ProcessTask return a non-nil error or panics, the task
|
|
||||||
// will be retried after delay.
|
|
||||||
type Handler interface {
|
|
||||||
ProcessTask(context.Context, *Task) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// The HandlerFunc type is an adapter to allow the use of
|
|
||||||
// ordinary functions as a Handler. If f is a function
|
|
||||||
// with the appropriate signature, HandlerFunc(f) is a
|
|
||||||
// Handler that calls f.
|
|
||||||
type HandlerFunc func(context.Context, *Task) error
|
|
||||||
|
|
||||||
// ProcessTask calls fn(ctx, task)
|
|
||||||
func (fn HandlerFunc) ProcessTask(ctx context.Context, task *Task) error {
|
|
||||||
return fn(ctx, task)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run starts the background-task processing and blocks until
|
|
||||||
// an os signal to exit the program is received. Once it receives
|
|
||||||
// a signal, it gracefully shuts down all pending workers and other
|
|
||||||
// goroutines to process the tasks.
|
|
||||||
func (bg *Background) Run(handler Handler) {
|
|
||||||
type prefixLogger interface {
|
|
||||||
SetPrefix(prefix string)
|
|
||||||
}
|
|
||||||
// If logger supports setting prefix, then set prefix for log output.
|
|
||||||
if l, ok := bg.logger.(prefixLogger); ok {
|
|
||||||
l.SetPrefix(fmt.Sprintf("asynq: pid=%d ", os.Getpid()))
|
|
||||||
}
|
|
||||||
bg.logger.Info("Starting processing")
|
|
||||||
|
|
||||||
bg.start(handler)
|
|
||||||
defer bg.stop()
|
|
||||||
|
|
||||||
bg.logger.Info("Send signal TSTP to stop processing new tasks")
|
|
||||||
bg.logger.Info("Send signal TERM or INT to terminate the process")
|
|
||||||
|
|
||||||
// Wait for a signal to terminate.
|
|
||||||
sigs := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGTSTP)
|
|
||||||
for {
|
|
||||||
sig := <-sigs
|
|
||||||
if sig == syscall.SIGTSTP {
|
|
||||||
bg.processor.stop()
|
|
||||||
bg.ps.SetStatus(base.StatusStopped)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
bg.logger.Info("Starting graceful shutdown")
|
|
||||||
}
|
|
||||||
|
|
||||||
// starts the background-task processing.
|
|
||||||
func (bg *Background) start(handler Handler) {
|
|
||||||
bg.mu.Lock()
|
|
||||||
defer bg.mu.Unlock()
|
|
||||||
if bg.running {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
bg.running = true
|
|
||||||
bg.processor.handler = handler
|
|
||||||
|
|
||||||
bg.heartbeater.start(&bg.wg)
|
|
||||||
bg.subscriber.start(&bg.wg)
|
|
||||||
bg.syncer.start(&bg.wg)
|
|
||||||
bg.scheduler.start(&bg.wg)
|
|
||||||
bg.processor.start(&bg.wg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// stops the background-task processing.
|
|
||||||
func (bg *Background) stop() {
|
|
||||||
bg.mu.Lock()
|
|
||||||
defer bg.mu.Unlock()
|
|
||||||
if !bg.running {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: The order of termination is important.
|
|
||||||
// Sender goroutines should be terminated before the receiver goroutines.
|
|
||||||
//
|
|
||||||
// processor -> syncer (via syncCh)
|
|
||||||
bg.scheduler.terminate()
|
|
||||||
bg.processor.terminate()
|
|
||||||
bg.syncer.terminate()
|
|
||||||
bg.subscriber.terminate()
|
|
||||||
bg.heartbeater.terminate()
|
|
||||||
|
|
||||||
bg.wg.Wait()
|
|
||||||
|
|
||||||
bg.rdb.Close()
|
|
||||||
bg.running = false
|
|
||||||
|
|
||||||
bg.logger.Info("Bye!")
|
|
||||||
}
|
|
@@ -1,128 +0,0 @@
|
|||||||
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT license
|
|
||||||
// that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package asynq
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"go.uber.org/goleak"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBackground(t *testing.T) {
|
|
||||||
// https://github.com/go-redis/redis/issues/1029
|
|
||||||
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
|
|
||||||
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
|
||||||
|
|
||||||
r := &RedisClientOpt{
|
|
||||||
Addr: "localhost:6379",
|
|
||||||
DB: 15,
|
|
||||||
}
|
|
||||||
client := NewClient(r)
|
|
||||||
bg := NewBackground(r, &Config{
|
|
||||||
Concurrency: 10,
|
|
||||||
})
|
|
||||||
|
|
||||||
// no-op handler
|
|
||||||
h := func(ctx context.Context, task *Task) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
bg.start(HandlerFunc(h))
|
|
||||||
|
|
||||||
err := client.Enqueue(NewTask("send_email", map[string]interface{}{"recipient_id": 123}))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("could not enqueue a task: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = client.EnqueueAt(time.Now().Add(time.Hour), NewTask("send_email", map[string]interface{}{"recipient_id": 456}))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("could not enqueue a task: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bg.stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGCD(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input []int
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
{[]int{6, 2, 12}, 2},
|
|
||||||
{[]int{3, 3, 3}, 3},
|
|
||||||
{[]int{6, 3, 1}, 1},
|
|
||||||
{[]int{1}, 1},
|
|
||||||
{[]int{1, 0, 2}, 1},
|
|
||||||
{[]int{8, 0, 4}, 4},
|
|
||||||
{[]int{9, 12, 18, 30}, 3},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
got := gcd(tc.input...)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("gcd(%v) = %d, want %d", tc.input, got, tc.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNormalizeQueueCfg(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
input map[string]int
|
|
||||||
want map[string]int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
input: map[string]int{
|
|
||||||
"high": 100,
|
|
||||||
"default": 20,
|
|
||||||
"low": 5,
|
|
||||||
},
|
|
||||||
want: map[string]int{
|
|
||||||
"high": 20,
|
|
||||||
"default": 4,
|
|
||||||
"low": 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: map[string]int{
|
|
||||||
"default": 10,
|
|
||||||
},
|
|
||||||
want: map[string]int{
|
|
||||||
"default": 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: map[string]int{
|
|
||||||
"critical": 5,
|
|
||||||
"default": 1,
|
|
||||||
},
|
|
||||||
want: map[string]int{
|
|
||||||
"critical": 5,
|
|
||||||
"default": 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: map[string]int{
|
|
||||||
"critical": 6,
|
|
||||||
"default": 3,
|
|
||||||
"low": 0,
|
|
||||||
},
|
|
||||||
want: map[string]int{
|
|
||||||
"critical": 2,
|
|
||||||
"default": 1,
|
|
||||||
"low": 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
got := normalizeQueueCfg(tc.input)
|
|
||||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
||||||
t.Errorf("normalizeQueueCfg(%v) = %v, want %v; (-want, +got):\n%s",
|
|
||||||
tc.input, got, tc.want, diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -24,7 +24,7 @@ func BenchmarkEndToEndSimple(b *testing.B) {
|
|||||||
DB: redisDB,
|
DB: redisDB,
|
||||||
}
|
}
|
||||||
client := NewClient(redis)
|
client := NewClient(redis)
|
||||||
bg := NewBackground(redis, &Config{
|
srv := NewServer(redis, Config{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
||||||
return time.Second
|
return time.Second
|
||||||
@@ -46,11 +46,11 @@ func BenchmarkEndToEndSimple(b *testing.B) {
|
|||||||
}
|
}
|
||||||
b.StartTimer() // end setup
|
b.StartTimer() // end setup
|
||||||
|
|
||||||
bg.start(HandlerFunc(handler))
|
srv.Start(HandlerFunc(handler))
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
b.StopTimer() // begin teardown
|
b.StopTimer() // begin teardown
|
||||||
bg.stop()
|
srv.Stop()
|
||||||
b.StartTimer() // end teardown
|
b.StartTimer() // end teardown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ func BenchmarkEndToEnd(b *testing.B) {
|
|||||||
DB: redisDB,
|
DB: redisDB,
|
||||||
}
|
}
|
||||||
client := NewClient(redis)
|
client := NewClient(redis)
|
||||||
bg := NewBackground(redis, &Config{
|
srv := NewServer(redis, Config{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
RetryDelayFunc: func(n int, err error, t *Task) time.Duration {
|
||||||
return time.Second
|
return time.Second
|
||||||
@@ -99,11 +99,11 @@ func BenchmarkEndToEnd(b *testing.B) {
|
|||||||
}
|
}
|
||||||
b.StartTimer() // end setup
|
b.StartTimer() // end setup
|
||||||
|
|
||||||
bg.start(HandlerFunc(handler))
|
srv.Start(HandlerFunc(handler))
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
b.StopTimer() // begin teardown
|
b.StopTimer() // begin teardown
|
||||||
bg.stop()
|
srv.Stop()
|
||||||
b.StartTimer() // end teardown
|
b.StartTimer() // end teardown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,7 @@ func BenchmarkEndToEndMultipleQueues(b *testing.B) {
|
|||||||
DB: redisDB,
|
DB: redisDB,
|
||||||
}
|
}
|
||||||
client := NewClient(redis)
|
client := NewClient(redis)
|
||||||
bg := NewBackground(redis, &Config{
|
srv := NewServer(redis, Config{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
Queues: map[string]int{
|
Queues: map[string]int{
|
||||||
"high": 6,
|
"high": 6,
|
||||||
@@ -160,11 +160,11 @@ func BenchmarkEndToEndMultipleQueues(b *testing.B) {
|
|||||||
}
|
}
|
||||||
b.StartTimer() // end setup
|
b.StartTimer() // end setup
|
||||||
|
|
||||||
bg.start(HandlerFunc(handler))
|
srv.Start(HandlerFunc(handler))
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
b.StopTimer() // begin teardown
|
b.StopTimer() // begin teardown
|
||||||
bg.stop()
|
srv.Stop()
|
||||||
b.StartTimer() // end teardown
|
b.StartTimer() // end teardown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
155
client.go
@@ -5,7 +5,11 @@
|
|||||||
package asynq
|
package asynq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
@@ -20,13 +24,18 @@ import (
|
|||||||
//
|
//
|
||||||
// Clients are safe for concurrent use by multiple goroutines.
|
// Clients are safe for concurrent use by multiple goroutines.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
rdb *rdb.RDB
|
mu sync.Mutex
|
||||||
|
opts map[string][]Option
|
||||||
|
rdb *rdb.RDB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient and returns a new Client given a redis connection option.
|
// NewClient and returns a new Client given a redis connection option.
|
||||||
func NewClient(r RedisConnOpt) *Client {
|
func NewClient(r RedisConnOpt) *Client {
|
||||||
rdb := rdb.NewRDB(createRedisClient(r))
|
rdb := rdb.NewRDB(createRedisClient(r))
|
||||||
return &Client{rdb}
|
return &Client{
|
||||||
|
opts: make(map[string][]Option),
|
||||||
|
rdb: rdb,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option specifies the task processing behavior.
|
// Option specifies the task processing behavior.
|
||||||
@@ -38,6 +47,7 @@ type (
|
|||||||
queueOption string
|
queueOption string
|
||||||
timeoutOption time.Duration
|
timeoutOption time.Duration
|
||||||
deadlineOption time.Time
|
deadlineOption time.Time
|
||||||
|
uniqueOption time.Duration
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxRetry returns an option to specify the max number of times
|
// MaxRetry returns an option to specify the max number of times
|
||||||
@@ -70,11 +80,30 @@ func Deadline(t time.Time) Option {
|
|||||||
return deadlineOption(t)
|
return deadlineOption(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unique returns an option to enqueue a task only if the given task is unique.
|
||||||
|
// Task enqueued with this option is guaranteed to be unique within the given ttl.
|
||||||
|
// Once the task gets processed successfully or once the TTL has expired, another task with the same uniqueness may be enqueued.
|
||||||
|
// ErrDuplicateTask error is returned when enqueueing a duplicate task.
|
||||||
|
//
|
||||||
|
// Uniqueness of a task is based on the following properties:
|
||||||
|
// - Task Type
|
||||||
|
// - Task Payload
|
||||||
|
// - Queue Name
|
||||||
|
func Unique(ttl time.Duration) Option {
|
||||||
|
return uniqueOption(ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
var ErrDuplicateTask = errors.New("task already exists")
|
||||||
|
|
||||||
type option struct {
|
type option struct {
|
||||||
retry int
|
retry int
|
||||||
queue string
|
queue string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
deadline time.Time
|
deadline time.Time
|
||||||
|
uniqueTTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func composeOptions(opts ...Option) option {
|
func composeOptions(opts ...Option) option {
|
||||||
@@ -94,6 +123,8 @@ func composeOptions(opts ...Option) option {
|
|||||||
res.timeout = time.Duration(opt)
|
res.timeout = time.Duration(opt)
|
||||||
case deadlineOption:
|
case deadlineOption:
|
||||||
res.deadline = time.Time(opt)
|
res.deadline = time.Time(opt)
|
||||||
|
case uniqueOption:
|
||||||
|
res.uniqueTTL = time.Duration(opt)
|
||||||
default:
|
default:
|
||||||
// ignore unexpected option
|
// ignore unexpected option
|
||||||
}
|
}
|
||||||
@@ -101,10 +132,52 @@ func composeOptions(opts ...Option) option {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
// uniqueKey computes the redis key used for the given task.
|
||||||
// Max retry count by default
|
// It returns an empty string if ttl is zero.
|
||||||
defaultMaxRetry = 25
|
func uniqueKey(t *Task, ttl time.Duration, qname string) string {
|
||||||
)
|
if ttl == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%s:%s", t.Type, serializePayload(t.Payload.data), qname)
|
||||||
|
}
|
||||||
|
|
||||||
|
func serializePayload(payload map[string]interface{}) string {
|
||||||
|
if payload == nil {
|
||||||
|
return "nil"
|
||||||
|
}
|
||||||
|
type entry struct {
|
||||||
|
k string
|
||||||
|
v interface{}
|
||||||
|
}
|
||||||
|
var es []entry
|
||||||
|
for k, v := range payload {
|
||||||
|
es = append(es, entry{k, v})
|
||||||
|
}
|
||||||
|
// sort entries by key
|
||||||
|
sort.Slice(es, func(i, j int) bool { return es[i].k < es[j].k })
|
||||||
|
var b strings.Builder
|
||||||
|
for _, e := range es {
|
||||||
|
if b.Len() > 0 {
|
||||||
|
b.WriteString(",")
|
||||||
|
}
|
||||||
|
b.WriteString(fmt.Sprintf("%s=%v", e.k, e.v))
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default max retry count used if nothing is specified.
|
||||||
|
const defaultMaxRetry = 25
|
||||||
|
|
||||||
|
// SetDefaultOptions sets options to be used for a given task type.
|
||||||
|
// The argument opts specifies the behavior of task processing.
|
||||||
|
// If there are conflicting Option values the last one overrides others.
|
||||||
|
//
|
||||||
|
// Default options can be overridden by options passed at enqueue time.
|
||||||
|
func (c *Client) SetDefaultOptions(taskType string, opts ...Option) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
c.opts[taskType] = opts
|
||||||
|
}
|
||||||
|
|
||||||
// EnqueueAt schedules task to be enqueued at the specified time.
|
// EnqueueAt schedules task to be enqueued at the specified time.
|
||||||
//
|
//
|
||||||
@@ -113,17 +186,7 @@ const (
|
|||||||
// The argument opts specifies the behavior of task processing.
|
// The argument opts specifies the behavior of task processing.
|
||||||
// If there are conflicting Option values the last one overrides others.
|
// If there are conflicting Option values the last one overrides others.
|
||||||
func (c *Client) EnqueueAt(t time.Time, task *Task, opts ...Option) error {
|
func (c *Client) EnqueueAt(t time.Time, task *Task, opts ...Option) error {
|
||||||
opt := composeOptions(opts...)
|
return c.enqueueAt(t, task, opts...)
|
||||||
msg := &base.TaskMessage{
|
|
||||||
ID: xid.New(),
|
|
||||||
Type: task.Type,
|
|
||||||
Payload: task.Payload.data,
|
|
||||||
Queue: opt.queue,
|
|
||||||
Retry: opt.retry,
|
|
||||||
Timeout: opt.timeout.String(),
|
|
||||||
Deadline: opt.deadline.Format(time.RFC3339),
|
|
||||||
}
|
|
||||||
return c.enqueue(msg, t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue enqueues task to be processed immediately.
|
// Enqueue enqueues task to be processed immediately.
|
||||||
@@ -133,7 +196,7 @@ func (c *Client) EnqueueAt(t time.Time, task *Task, opts ...Option) error {
|
|||||||
// The argument opts specifies the behavior of task processing.
|
// The argument opts specifies the behavior of task processing.
|
||||||
// If there are conflicting Option values the last one overrides others.
|
// If there are conflicting Option values the last one overrides others.
|
||||||
func (c *Client) Enqueue(task *Task, opts ...Option) error {
|
func (c *Client) Enqueue(task *Task, opts ...Option) error {
|
||||||
return c.EnqueueAt(time.Now(), task, opts...)
|
return c.enqueueAt(time.Now(), task, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnqueueIn schedules task to be enqueued after the specified delay.
|
// EnqueueIn schedules task to be enqueued after the specified delay.
|
||||||
@@ -143,12 +206,54 @@ func (c *Client) Enqueue(task *Task, opts ...Option) error {
|
|||||||
// The argument opts specifies the behavior of task processing.
|
// The argument opts specifies the behavior of task processing.
|
||||||
// If there are conflicting Option values the last one overrides others.
|
// If there are conflicting Option values the last one overrides others.
|
||||||
func (c *Client) EnqueueIn(d time.Duration, task *Task, opts ...Option) error {
|
func (c *Client) EnqueueIn(d time.Duration, task *Task, opts ...Option) error {
|
||||||
return c.EnqueueAt(time.Now().Add(d), task, opts...)
|
return c.enqueueAt(time.Now().Add(d), task, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) enqueue(msg *base.TaskMessage, t time.Time) error {
|
// Close closes the connection with redis server.
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
return c.rdb.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) enqueueAt(t time.Time, task *Task, opts ...Option) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if defaults, ok := c.opts[task.Type]; ok {
|
||||||
|
opts = append(defaults, opts...)
|
||||||
|
}
|
||||||
|
opt := composeOptions(opts...)
|
||||||
|
msg := &base.TaskMessage{
|
||||||
|
ID: xid.New(),
|
||||||
|
Type: task.Type,
|
||||||
|
Payload: task.Payload.data,
|
||||||
|
Queue: opt.queue,
|
||||||
|
Retry: opt.retry,
|
||||||
|
Timeout: opt.timeout.String(),
|
||||||
|
Deadline: opt.deadline.Format(time.RFC3339),
|
||||||
|
UniqueKey: uniqueKey(task, opt.uniqueTTL, opt.queue),
|
||||||
|
}
|
||||||
|
var err error
|
||||||
if time.Now().After(t) {
|
if time.Now().After(t) {
|
||||||
return c.rdb.Enqueue(msg)
|
err = c.enqueue(msg, opt.uniqueTTL)
|
||||||
|
} else {
|
||||||
|
err = c.schedule(msg, t, opt.uniqueTTL)
|
||||||
|
}
|
||||||
|
if err == rdb.ErrDuplicateTask {
|
||||||
|
return fmt.Errorf("%w", ErrDuplicateTask)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) enqueue(msg *base.TaskMessage, uniqueTTL time.Duration) error {
|
||||||
|
if uniqueTTL > 0 {
|
||||||
|
return c.rdb.EnqueueUnique(msg, uniqueTTL)
|
||||||
|
}
|
||||||
|
return c.rdb.Enqueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) schedule(msg *base.TaskMessage, t time.Time, uniqueTTL time.Duration) error {
|
||||||
|
if uniqueTTL > 0 {
|
||||||
|
ttl := t.Add(uniqueTTL).Sub(time.Now())
|
||||||
|
return c.rdb.ScheduleUnique(msg, t, ttl)
|
||||||
}
|
}
|
||||||
return c.rdb.Schedule(msg, t)
|
return c.rdb.Schedule(msg, t)
|
||||||
}
|
}
|
||||||
|
343
client_test.go
@@ -5,14 +5,21 @@
|
|||||||
package asynq
|
package asynq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
h "github.com/hibiken/asynq/internal/asynqtest"
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
noTimeout = time.Duration(0).String()
|
||||||
|
noDeadline = time.Time{}.Format(time.RFC3339)
|
||||||
|
)
|
||||||
|
|
||||||
func TestClientEnqueueAt(t *testing.T) {
|
func TestClientEnqueueAt(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
client := NewClient(RedisClientOpt{
|
client := NewClient(RedisClientOpt{
|
||||||
@@ -25,9 +32,6 @@ func TestClientEnqueueAt(t *testing.T) {
|
|||||||
var (
|
var (
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
oneHourLater = now.Add(time.Hour)
|
oneHourLater = now.Add(time.Hour)
|
||||||
|
|
||||||
noTimeout = time.Duration(0).String()
|
|
||||||
noDeadline = time.Time{}.Format(time.RFC3339)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@@ -44,8 +48,8 @@ func TestClientEnqueueAt(t *testing.T) {
|
|||||||
processAt: now,
|
processAt: now,
|
||||||
opts: []Option{},
|
opts: []Option{},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -111,11 +115,6 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
|
|
||||||
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
||||||
|
|
||||||
var (
|
|
||||||
noTimeout = time.Duration(0).String()
|
|
||||||
noDeadline = time.Time{}.Format(time.RFC3339)
|
|
||||||
)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
desc string
|
desc string
|
||||||
task *Task
|
task *Task
|
||||||
@@ -129,8 +128,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
MaxRetry(3),
|
MaxRetry(3),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: 3,
|
Retry: 3,
|
||||||
@@ -148,8 +147,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
MaxRetry(-2),
|
MaxRetry(-2),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: 0, // Retry count should be set to zero
|
Retry: 0, // Retry count should be set to zero
|
||||||
@@ -168,8 +167,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
MaxRetry(10),
|
MaxRetry(10),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: 10, // Last option takes precedence
|
Retry: 10, // Last option takes precedence
|
||||||
@@ -187,8 +186,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
Queue("custom"),
|
Queue("custom"),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"custom": []*base.TaskMessage{
|
"custom": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -206,8 +205,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
Queue("HIGH"),
|
Queue("HIGH"),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"high": []*base.TaskMessage{
|
"high": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -225,8 +224,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
Timeout(20 * time.Second),
|
Timeout(20 * time.Second),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -244,8 +243,8 @@ func TestClientEnqueue(t *testing.T) {
|
|||||||
Deadline(time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC)),
|
Deadline(time.Date(2020, time.June, 24, 0, 0, 0, 0, time.UTC)),
|
||||||
},
|
},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -285,11 +284,6 @@ func TestClientEnqueueIn(t *testing.T) {
|
|||||||
|
|
||||||
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
task := NewTask("send_email", map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"})
|
||||||
|
|
||||||
var (
|
|
||||||
noTimeout = time.Duration(0).String()
|
|
||||||
noDeadline = time.Time{}.Format(time.RFC3339)
|
|
||||||
)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
desc string
|
desc string
|
||||||
task *Task
|
task *Task
|
||||||
@@ -324,8 +318,8 @@ func TestClientEnqueueIn(t *testing.T) {
|
|||||||
delay: 0,
|
delay: 0,
|
||||||
opts: []Option{},
|
opts: []Option{},
|
||||||
wantEnqueued: map[string][]*base.TaskMessage{
|
wantEnqueued: map[string][]*base.TaskMessage{
|
||||||
"default": []*base.TaskMessage{
|
"default": {
|
||||||
&base.TaskMessage{
|
{
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Payload: task.Payload.data,
|
Payload: task.Payload.data,
|
||||||
Retry: defaultMaxRetry,
|
Retry: defaultMaxRetry,
|
||||||
@@ -361,3 +355,290 @@ func TestClientEnqueueIn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClientDefaultOptions(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
desc string
|
||||||
|
defaultOpts []Option // options set at the client level.
|
||||||
|
opts []Option // options used at enqueue time.
|
||||||
|
task *Task
|
||||||
|
queue string // queue that the message should go into.
|
||||||
|
want *base.TaskMessage
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "With queue routing option",
|
||||||
|
defaultOpts: []Option{Queue("feed")},
|
||||||
|
opts: []Option{},
|
||||||
|
task: NewTask("feed:import", nil),
|
||||||
|
queue: "feed",
|
||||||
|
want: &base.TaskMessage{
|
||||||
|
Type: "feed:import",
|
||||||
|
Payload: nil,
|
||||||
|
Retry: defaultMaxRetry,
|
||||||
|
Queue: "feed",
|
||||||
|
Timeout: noTimeout,
|
||||||
|
Deadline: noDeadline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "With multiple options",
|
||||||
|
defaultOpts: []Option{Queue("feed"), MaxRetry(5)},
|
||||||
|
opts: []Option{},
|
||||||
|
task: NewTask("feed:import", nil),
|
||||||
|
queue: "feed",
|
||||||
|
want: &base.TaskMessage{
|
||||||
|
Type: "feed:import",
|
||||||
|
Payload: nil,
|
||||||
|
Retry: 5,
|
||||||
|
Queue: "feed",
|
||||||
|
Timeout: noTimeout,
|
||||||
|
Deadline: noDeadline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "With overriding options at enqueue time",
|
||||||
|
defaultOpts: []Option{Queue("feed"), MaxRetry(5)},
|
||||||
|
opts: []Option{Queue("critical")},
|
||||||
|
task: NewTask("feed:import", nil),
|
||||||
|
queue: "critical",
|
||||||
|
want: &base.TaskMessage{
|
||||||
|
Type: "feed:import",
|
||||||
|
Payload: nil,
|
||||||
|
Retry: 5,
|
||||||
|
Queue: "critical",
|
||||||
|
Timeout: noTimeout,
|
||||||
|
Deadline: noDeadline,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r)
|
||||||
|
c := NewClient(RedisClientOpt{Addr: redisAddr, DB: redisDB})
|
||||||
|
c.SetDefaultOptions(tc.task.Type, tc.defaultOpts...)
|
||||||
|
err := c.Enqueue(tc.task, tc.opts...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
enqueued := h.GetEnqueuedMessages(t, r, tc.queue)
|
||||||
|
if len(enqueued) != 1 {
|
||||||
|
t.Errorf("%s;\nexpected queue %q to have one message; got %d messages in the queue.",
|
||||||
|
tc.desc, tc.queue, len(enqueued))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
got := enqueued[0]
|
||||||
|
if diff := cmp.Diff(tc.want, got, h.IgnoreIDOpt); diff != "" {
|
||||||
|
t.Errorf("%s;\nmismatch found in enqueued task message; (-want,+got)\n%s",
|
||||||
|
tc.desc, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUniqueKey(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
desc string
|
||||||
|
task *Task
|
||||||
|
ttl time.Duration
|
||||||
|
qname string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"with zero TTL",
|
||||||
|
NewTask("email:send", map[string]interface{}{"a": 123, "b": "hello", "c": true}),
|
||||||
|
0,
|
||||||
|
"default",
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with primitive types",
|
||||||
|
NewTask("email:send", map[string]interface{}{"a": 123, "b": "hello", "c": true}),
|
||||||
|
10 * time.Minute,
|
||||||
|
"default",
|
||||||
|
"email:send:a=123,b=hello,c=true:default",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with unsorted keys",
|
||||||
|
NewTask("email:send", map[string]interface{}{"b": "hello", "c": true, "a": 123}),
|
||||||
|
10 * time.Minute,
|
||||||
|
"default",
|
||||||
|
"email:send:a=123,b=hello,c=true:default",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with composite types",
|
||||||
|
NewTask("email:send",
|
||||||
|
map[string]interface{}{
|
||||||
|
"address": map[string]string{"line": "123 Main St", "city": "Boston", "state": "MA"},
|
||||||
|
"names": []string{"bob", "mike", "rob"}}),
|
||||||
|
10 * time.Minute,
|
||||||
|
"default",
|
||||||
|
"email:send:address=map[city:Boston line:123 Main St state:MA],names=[bob mike rob]:default",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with complex types",
|
||||||
|
NewTask("email:send",
|
||||||
|
map[string]interface{}{
|
||||||
|
"time": time.Date(2020, time.July, 28, 0, 0, 0, 0, time.UTC),
|
||||||
|
"duration": time.Hour}),
|
||||||
|
10 * time.Minute,
|
||||||
|
"default",
|
||||||
|
"email:send:duration=1h0m0s,time=2020-07-28 00:00:00 +0000 UTC:default",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"with nil payload",
|
||||||
|
NewTask("reindex", nil),
|
||||||
|
10 * time.Minute,
|
||||||
|
"default",
|
||||||
|
"reindex:nil:default",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got := uniqueKey(tc.task, tc.ttl, tc.qname)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("%s: uniqueKey(%v, %v, %q) = %q, want %q", tc.desc, tc.task, tc.ttl, tc.qname, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueUnique(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
c := NewClient(RedisClientOpt{
|
||||||
|
Addr: redisAddr,
|
||||||
|
DB: redisDB,
|
||||||
|
})
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
task *Task
|
||||||
|
ttl time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
NewTask("email", map[string]interface{}{"user_id": 123}),
|
||||||
|
time.Hour,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r) // clean up db before each test case.
|
||||||
|
|
||||||
|
// Enqueue the task first. It should succeed.
|
||||||
|
err := c.Enqueue(tc.task, Unique(tc.ttl))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTTL := r.TTL(uniqueKey(tc.task, tc.ttl, base.DefaultQueueName)).Val()
|
||||||
|
if !cmp.Equal(tc.ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
|
t.Errorf("TTL = %v, want %v", gotTTL, tc.ttl)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue the task again. It should fail.
|
||||||
|
err = c.Enqueue(tc.task, Unique(tc.ttl))
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !errors.Is(err, ErrDuplicateTask) {
|
||||||
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueInUnique(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
c := NewClient(RedisClientOpt{
|
||||||
|
Addr: redisAddr,
|
||||||
|
DB: redisDB,
|
||||||
|
})
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
task *Task
|
||||||
|
d time.Duration
|
||||||
|
ttl time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
NewTask("reindex", nil),
|
||||||
|
time.Hour,
|
||||||
|
10 * time.Minute,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r) // clean up db before each test case.
|
||||||
|
|
||||||
|
// Enqueue the task first. It should succeed.
|
||||||
|
err := c.EnqueueIn(tc.d, tc.task, Unique(tc.ttl))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTTL := r.TTL(uniqueKey(tc.task, tc.ttl, base.DefaultQueueName)).Val()
|
||||||
|
wantTTL := time.Duration(tc.ttl.Seconds()+tc.d.Seconds()) * time.Second
|
||||||
|
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
|
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue the task again. It should fail.
|
||||||
|
err = c.EnqueueIn(tc.d, tc.task, Unique(tc.ttl))
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !errors.Is(err, ErrDuplicateTask) {
|
||||||
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueAtUnique(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
c := NewClient(RedisClientOpt{
|
||||||
|
Addr: redisAddr,
|
||||||
|
DB: redisDB,
|
||||||
|
})
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
task *Task
|
||||||
|
at time.Time
|
||||||
|
ttl time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
NewTask("reindex", nil),
|
||||||
|
time.Now().Add(time.Hour),
|
||||||
|
10 * time.Minute,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r) // clean up db before each test case.
|
||||||
|
|
||||||
|
// Enqueue the task first. It should succeed.
|
||||||
|
err := c.EnqueueAt(tc.at, tc.task, Unique(tc.ttl))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTTL := r.TTL(uniqueKey(tc.task, tc.ttl, base.DefaultQueueName)).Val()
|
||||||
|
wantTTL := tc.at.Add(tc.ttl).Sub(time.Now())
|
||||||
|
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
|
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue the task again. It should fail.
|
||||||
|
err = c.EnqueueAt(tc.at, tc.task, Unique(tc.ttl))
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Enqueueing %+v did not return an error", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !errors.Is(err, ErrDuplicateTask) {
|
||||||
|
t.Errorf("Enqueueing %+v returned an error that is not ErrDuplicateTask", tc.task)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
12
doc.go
@@ -14,7 +14,7 @@ specify the options using one of RedisConnOpt types.
|
|||||||
DB: 3,
|
DB: 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
The Client is used to register a task to be processed at the specified time.
|
The Client is used to enqueue a task to be processed at the specified time.
|
||||||
|
|
||||||
Task is created with two parameters: its type and payload.
|
Task is created with two parameters: its type and payload.
|
||||||
|
|
||||||
@@ -27,18 +27,18 @@ Task is created with two parameters: its type and payload.
|
|||||||
// Enqueue the task to be processed immediately.
|
// Enqueue the task to be processed immediately.
|
||||||
err := client.Enqueue(t)
|
err := client.Enqueue(t)
|
||||||
|
|
||||||
// Schedule the task to be processed in one minute.
|
// Schedule the task to be processed after one minute.
|
||||||
err = client.EnqueueIn(time.Minute, t)
|
err = client.EnqueueIn(time.Minute, t)
|
||||||
|
|
||||||
The Background is used to run the background task processing with a given
|
The Server is used to run the background task processing with a given
|
||||||
handler.
|
handler.
|
||||||
bg := asynq.NewBackground(redis, &asynq.Config{
|
srv := asynq.NewServer(redis, asynq.Config{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
})
|
})
|
||||||
|
|
||||||
bg.Run(handler)
|
srv.Run(handler)
|
||||||
|
|
||||||
Handler is an interface with one method ProcessTask which
|
Handler is an interface type with a method which
|
||||||
takes a task and returns an error. Handler should return nil if
|
takes a task and returns an error. Handler should return nil if
|
||||||
the processing is successful, otherwise return a non-nil error.
|
the processing is successful, otherwise return a non-nil error.
|
||||||
If handler panics or returns a non-nil error, the task will be retried in the future.
|
If handler panics or returns a non-nil error, the task will be retried in the future.
|
||||||
|
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
Before Width: | Height: | Size: 582 KiB After Width: | Height: | Size: 582 KiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
BIN
docs/assets/overview.png
Normal file
After Width: | Height: | Size: 63 KiB |
95
example_test.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT license
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package asynq_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleServer_Run() {
|
||||||
|
srv := asynq.NewServer(
|
||||||
|
asynq.RedisClientOpt{Addr: ":6379"},
|
||||||
|
asynq.Config{Concurrency: 20},
|
||||||
|
)
|
||||||
|
|
||||||
|
h := asynq.NewServeMux()
|
||||||
|
// ... Register handlers
|
||||||
|
|
||||||
|
// Run blocks and waits for os signal to terminate the program.
|
||||||
|
if err := srv.Run(h); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleServer_Stop() {
|
||||||
|
srv := asynq.NewServer(
|
||||||
|
asynq.RedisClientOpt{Addr: ":6379"},
|
||||||
|
asynq.Config{Concurrency: 20},
|
||||||
|
)
|
||||||
|
|
||||||
|
h := asynq.NewServeMux()
|
||||||
|
// ... Register handlers
|
||||||
|
|
||||||
|
if err := srv.Start(h); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT)
|
||||||
|
<-sigs // wait for termination signal
|
||||||
|
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleServer_Quiet() {
|
||||||
|
srv := asynq.NewServer(
|
||||||
|
asynq.RedisClientOpt{Addr: ":6379"},
|
||||||
|
asynq.Config{Concurrency: 20},
|
||||||
|
)
|
||||||
|
|
||||||
|
h := asynq.NewServeMux()
|
||||||
|
// ... Register handlers
|
||||||
|
|
||||||
|
if err := srv.Start(h); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
|
||||||
|
// Handle SIGTERM, SIGINT to exit the program.
|
||||||
|
// Handle SIGTSTP to stop processing new tasks.
|
||||||
|
for {
|
||||||
|
s := <-sigs
|
||||||
|
if s == unix.SIGTSTP {
|
||||||
|
srv.Quiet() // stop processing new tasks
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleParseRedisURI() {
|
||||||
|
rconn, err := asynq.ParseRedisURI("redis://localhost:6379/10")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
r, ok := rconn.(asynq.RedisClientOpt)
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("unexpected type")
|
||||||
|
}
|
||||||
|
fmt.Println(r.Addr)
|
||||||
|
fmt.Println(r.DB)
|
||||||
|
// Output:
|
||||||
|
// localhost:6379
|
||||||
|
// 10
|
||||||
|
}
|
2
go.mod
@@ -8,7 +8,7 @@ require (
|
|||||||
github.com/rs/xid v1.2.1
|
github.com/rs/xid v1.2.1
|
||||||
github.com/spf13/cast v1.3.1
|
github.com/spf13/cast v1.3.1
|
||||||
go.uber.org/goleak v0.10.0
|
go.uber.org/goleak v0.10.0
|
||||||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e // indirect
|
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e
|
||||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
|
||||||
gopkg.in/yaml.v2 v2.2.7 // indirect
|
gopkg.in/yaml.v2 v2.2.7 // indirect
|
||||||
)
|
)
|
||||||
|
28
heartbeat.go
@@ -9,16 +9,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// heartbeater is responsible for writing process info to redis periodically to
|
// heartbeater is responsible for writing process info to redis periodically to
|
||||||
// indicate that the background worker process is up.
|
// indicate that the background worker process is up.
|
||||||
type heartbeater struct {
|
type heartbeater struct {
|
||||||
logger Logger
|
logger *log.Logger
|
||||||
rdb *rdb.RDB
|
broker base.Broker
|
||||||
|
|
||||||
ps *base.ProcessState
|
ss *base.ServerState
|
||||||
|
|
||||||
// channel to communicate back to the long running "heartbeater" goroutine.
|
// channel to communicate back to the long running "heartbeater" goroutine.
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
@@ -27,25 +27,25 @@ type heartbeater struct {
|
|||||||
interval time.Duration
|
interval time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHeartbeater(l Logger, rdb *rdb.RDB, ps *base.ProcessState, interval time.Duration) *heartbeater {
|
func newHeartbeater(l *log.Logger, b base.Broker, ss *base.ServerState, interval time.Duration) *heartbeater {
|
||||||
return &heartbeater{
|
return &heartbeater{
|
||||||
logger: l,
|
logger: l,
|
||||||
rdb: rdb,
|
broker: b,
|
||||||
ps: ps,
|
ss: ss,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
interval: interval,
|
interval: interval,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *heartbeater) terminate() {
|
func (h *heartbeater) terminate() {
|
||||||
h.logger.Info("Heartbeater shutting down...")
|
h.logger.Debug("Heartbeater shutting down...")
|
||||||
// Signal the heartbeater goroutine to stop.
|
// Signal the heartbeater goroutine to stop.
|
||||||
h.done <- struct{}{}
|
h.done <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *heartbeater) start(wg *sync.WaitGroup) {
|
func (h *heartbeater) start(wg *sync.WaitGroup) {
|
||||||
h.ps.SetStarted(time.Now())
|
h.ss.SetStarted(time.Now())
|
||||||
h.ps.SetStatus(base.StatusRunning)
|
h.ss.SetStatus(base.StatusRunning)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@@ -53,8 +53,8 @@ func (h *heartbeater) start(wg *sync.WaitGroup) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-h.done:
|
case <-h.done:
|
||||||
h.rdb.ClearProcessState(h.ps)
|
h.broker.ClearServerState(h.ss)
|
||||||
h.logger.Info("Heartbeater done")
|
h.logger.Debug("Heartbeater done")
|
||||||
return
|
return
|
||||||
case <-time.After(h.interval):
|
case <-time.After(h.interval):
|
||||||
h.beat()
|
h.beat()
|
||||||
@@ -66,8 +66,8 @@ func (h *heartbeater) start(wg *sync.WaitGroup) {
|
|||||||
func (h *heartbeater) beat() {
|
func (h *heartbeater) beat() {
|
||||||
// Note: Set TTL to be long enough so that it won't expire before we write again
|
// Note: Set TTL to be long enough so that it won't expire before we write again
|
||||||
// and short enough to expire quickly once the process is shut down or killed.
|
// and short enough to expire quickly once the process is shut down or killed.
|
||||||
err := h.rdb.WriteProcessState(h.ps, h.interval*2)
|
err := h.broker.WriteServerState(h.ss, h.interval*2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logger.Error("could not write heartbeat data: %v", err)
|
h.logger.Errorf("could not write heartbeat data: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,6 +14,7 @@ import (
|
|||||||
h "github.com/hibiken/asynq/internal/asynqtest"
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
|
"github.com/hibiken/asynq/internal/testbroker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHeartbeater(t *testing.T) {
|
func TestHeartbeater(t *testing.T) {
|
||||||
@@ -31,17 +32,18 @@ func TestHeartbeater(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
timeCmpOpt := cmpopts.EquateApproxTime(10 * time.Millisecond)
|
timeCmpOpt := cmpopts.EquateApproxTime(10 * time.Millisecond)
|
||||||
ignoreOpt := cmpopts.IgnoreUnexported(base.ProcessInfo{})
|
ignoreOpt := cmpopts.IgnoreUnexported(base.ServerInfo{})
|
||||||
|
ignoreFieldOpt := cmpopts.IgnoreFields(base.ServerInfo{}, "ServerID")
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
h.FlushDB(t, r)
|
h.FlushDB(t, r)
|
||||||
|
|
||||||
state := base.NewProcessState(tc.host, tc.pid, tc.concurrency, tc.queues, false)
|
state := base.NewServerState(tc.host, tc.pid, tc.concurrency, tc.queues, false)
|
||||||
hb := newHeartbeater(testLogger, rdbClient, state, tc.interval)
|
hb := newHeartbeater(testLogger, rdbClient, state, tc.interval)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
hb.start(&wg)
|
hb.start(&wg)
|
||||||
|
|
||||||
want := &base.ProcessInfo{
|
want := &base.ServerInfo{
|
||||||
Host: tc.host,
|
Host: tc.host,
|
||||||
PID: tc.pid,
|
PID: tc.pid,
|
||||||
Queues: tc.queues,
|
Queues: tc.queues,
|
||||||
@@ -53,21 +55,21 @@ func TestHeartbeater(t *testing.T) {
|
|||||||
// allow for heartbeater to write to redis
|
// allow for heartbeater to write to redis
|
||||||
time.Sleep(tc.interval * 2)
|
time.Sleep(tc.interval * 2)
|
||||||
|
|
||||||
ps, err := rdbClient.ListProcesses()
|
ss, err := rdbClient.ListServers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not read process status from redis: %v", err)
|
t.Errorf("could not read server info from redis: %v", err)
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ps) != 1 {
|
if len(ss) != 1 {
|
||||||
t.Errorf("(*RDB).ListProcesses returned %d process info, want 1", len(ps))
|
t.Errorf("(*RDB).ListServers returned %d process info, want 1", len(ss))
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(want, ps[0], timeCmpOpt, ignoreOpt); diff != "" {
|
if diff := cmp.Diff(want, ss[0], timeCmpOpt, ignoreOpt, ignoreFieldOpt); diff != "" {
|
||||||
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ps[0], want, diff)
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ss[0], want, diff)
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -79,21 +81,21 @@ func TestHeartbeater(t *testing.T) {
|
|||||||
time.Sleep(tc.interval * 2)
|
time.Sleep(tc.interval * 2)
|
||||||
|
|
||||||
want.Status = "stopped"
|
want.Status = "stopped"
|
||||||
ps, err = rdbClient.ListProcesses()
|
ss, err = rdbClient.ListServers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not read process status from redis: %v", err)
|
t.Errorf("could not read process status from redis: %v", err)
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ps) != 1 {
|
if len(ss) != 1 {
|
||||||
t.Errorf("(*RDB).ListProcesses returned %d process info, want 1", len(ps))
|
t.Errorf("(*RDB).ListProcesses returned %d process info, want 1", len(ss))
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(want, ps[0], timeCmpOpt, ignoreOpt); diff != "" {
|
if diff := cmp.Diff(want, ss[0], timeCmpOpt, ignoreOpt, ignoreFieldOpt); diff != "" {
|
||||||
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ps[0], want, diff)
|
t.Errorf("redis stored process status %+v, want %+v; (-want, +got)\n%s", ss[0], want, diff)
|
||||||
hb.terminate()
|
hb.terminate()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -101,3 +103,26 @@ func TestHeartbeater(t *testing.T) {
|
|||||||
hb.terminate()
|
hb.terminate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHeartbeaterWithRedisDown(t *testing.T) {
|
||||||
|
// Make sure that heartbeater goroutine doesn't panic
|
||||||
|
// if it cannot connect to redis.
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Errorf("panic occurred: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
r := rdb.NewRDB(setup(t))
|
||||||
|
testBroker := testbroker.NewTestBroker(r)
|
||||||
|
ss := base.NewServerState("localhost", 1234, 10, map[string]int{"default": 1}, false)
|
||||||
|
hb := newHeartbeater(testLogger, testBroker, ss, time.Second)
|
||||||
|
|
||||||
|
testBroker.Sleep()
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
hb.start(&wg)
|
||||||
|
|
||||||
|
// wait for heartbeater to try writing data to redis
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
hb.terminate()
|
||||||
|
}
|
||||||
|
@@ -41,9 +41,9 @@ var SortZSetEntryOpt = cmp.Transformer("SortZSetEntries", func(in []ZSetEntry) [
|
|||||||
return out
|
return out
|
||||||
})
|
})
|
||||||
|
|
||||||
// SortProcessInfoOpt is a cmp.Option to sort base.ProcessInfo for comparing slice of process info.
|
// SortServerInfoOpt is a cmp.Option to sort base.ServerInfo for comparing slice of process info.
|
||||||
var SortProcessInfoOpt = cmp.Transformer("SortProcessInfo", func(in []*base.ProcessInfo) []*base.ProcessInfo {
|
var SortServerInfoOpt = cmp.Transformer("SortServerInfo", func(in []*base.ServerInfo) []*base.ServerInfo {
|
||||||
out := append([]*base.ProcessInfo(nil), in...) // Copy input to avoid mutating it
|
out := append([]*base.ServerInfo(nil), in...) // Copy input to avoid mutating it
|
||||||
sort.Slice(out, func(i, j int) bool {
|
sort.Slice(out, func(i, j int) bool {
|
||||||
if out[i].Host != out[j].Host {
|
if out[i].Host != out[j].Host {
|
||||||
return out[i].Host < out[j].Host
|
return out[i].Host < out[j].Host
|
||||||
|
@@ -12,6 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v7"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,10 +21,10 @@ const DefaultQueueName = "default"
|
|||||||
|
|
||||||
// Redis keys
|
// Redis keys
|
||||||
const (
|
const (
|
||||||
AllProcesses = "asynq:ps" // ZSET
|
AllServers = "asynq:servers" // ZSET
|
||||||
psPrefix = "asynq:ps:" // STRING - asynq:ps:<host>:<pid>
|
serversPrefix = "asynq:servers:" // STRING - asynq:ps:<host>:<pid>:<serverid>
|
||||||
AllWorkers = "asynq:workers" // ZSET
|
AllWorkers = "asynq:workers" // ZSET
|
||||||
workersPrefix = "asynq:workers:" // HASH - asynq:workers:<host:<pid>
|
workersPrefix = "asynq:workers:" // HASH - asynq:workers:<host:<pid>:<serverid>
|
||||||
processedPrefix = "asynq:processed:" // STRING - asynq:processed:<yyyy-mm-dd>
|
processedPrefix = "asynq:processed:" // STRING - asynq:processed:<yyyy-mm-dd>
|
||||||
failurePrefix = "asynq:failure:" // STRING - asynq:failure:<yyyy-mm-dd>
|
failurePrefix = "asynq:failure:" // STRING - asynq:failure:<yyyy-mm-dd>
|
||||||
QueuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
|
QueuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
|
||||||
@@ -51,14 +52,14 @@ func FailureKey(t time.Time) string {
|
|||||||
return failurePrefix + t.UTC().Format("2006-01-02")
|
return failurePrefix + t.UTC().Format("2006-01-02")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessInfoKey returns a redis key for process info.
|
// ServerInfoKey returns a redis key for process info.
|
||||||
func ProcessInfoKey(hostname string, pid int) string {
|
func ServerInfoKey(hostname string, pid int, sid string) string {
|
||||||
return fmt.Sprintf("%s%s:%d", psPrefix, hostname, pid)
|
return fmt.Sprintf("%s%s:%d:%s", serversPrefix, hostname, pid, sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WorkersKey returns a redis key for the workers given hostname and pid.
|
// WorkersKey returns a redis key for the workers given hostname, pid, and server ID.
|
||||||
func WorkersKey(hostname string, pid int) string {
|
func WorkersKey(hostname string, pid int, sid string) string {
|
||||||
return fmt.Sprintf("%s%s:%d", workersPrefix, hostname, pid)
|
return fmt.Sprintf("%s%s:%d:%s", workersPrefix, hostname, pid, sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaskMessage is the internal representation of a task with additional metadata fields.
|
// TaskMessage is the internal representation of a task with additional metadata fields.
|
||||||
@@ -97,44 +98,54 @@ type TaskMessage struct {
|
|||||||
//
|
//
|
||||||
// time.Time's zero value means no deadline.
|
// time.Time's zero value means no deadline.
|
||||||
Deadline string
|
Deadline string
|
||||||
|
|
||||||
|
// UniqueKey holds the redis key used for uniqueness lock for this task.
|
||||||
|
//
|
||||||
|
// Empty string indicates that no uniqueness lock was used.
|
||||||
|
UniqueKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessState holds process level information.
|
// ServerState holds process level information.
|
||||||
//
|
//
|
||||||
// ProcessStates are safe for concurrent use by multiple goroutines.
|
// ServerStates are safe for concurrent use by multiple goroutines.
|
||||||
type ProcessState struct {
|
type ServerState struct {
|
||||||
mu sync.Mutex // guards all data fields
|
mu sync.Mutex // guards all data fields
|
||||||
|
id xid.ID
|
||||||
concurrency int
|
concurrency int
|
||||||
queues map[string]int
|
queues map[string]int
|
||||||
strictPriority bool
|
strictPriority bool
|
||||||
pid int
|
pid int
|
||||||
host string
|
host string
|
||||||
status PStatus
|
status ServerStatus
|
||||||
started time.Time
|
started time.Time
|
||||||
workers map[string]*workerStats
|
workers map[string]*workerStats
|
||||||
}
|
}
|
||||||
|
|
||||||
// PStatus represents status of a process.
|
// ServerStatus represents status of a server.
|
||||||
type PStatus int
|
type ServerStatus int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// StatusIdle indicates process is in idle state.
|
// StatusIdle indicates the server is in idle state.
|
||||||
StatusIdle PStatus = iota
|
StatusIdle ServerStatus = iota
|
||||||
|
|
||||||
// StatusRunning indicates process is up and processing tasks.
|
// StatusRunning indicates the servier is up and processing tasks.
|
||||||
StatusRunning
|
StatusRunning
|
||||||
|
|
||||||
// StatusStopped indicates process is up but not processing new tasks.
|
// StatusQuiet indicates the server is up but not processing new tasks.
|
||||||
|
StatusQuiet
|
||||||
|
|
||||||
|
// StatusStopped indicates the server server has been stopped.
|
||||||
StatusStopped
|
StatusStopped
|
||||||
)
|
)
|
||||||
|
|
||||||
var statuses = []string{
|
var statuses = []string{
|
||||||
"idle",
|
"idle",
|
||||||
"running",
|
"running",
|
||||||
|
"quiet",
|
||||||
"stopped",
|
"stopped",
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s PStatus) String() string {
|
func (s ServerStatus) String() string {
|
||||||
if StatusIdle <= s && s <= StatusStopped {
|
if StatusIdle <= s && s <= StatusStopped {
|
||||||
return statuses[s]
|
return statuses[s]
|
||||||
}
|
}
|
||||||
@@ -146,11 +157,12 @@ type workerStats struct {
|
|||||||
started time.Time
|
started time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProcessState returns a new instance of ProcessState.
|
// NewServerState returns a new instance of ServerState.
|
||||||
func NewProcessState(host string, pid, concurrency int, queues map[string]int, strict bool) *ProcessState {
|
func NewServerState(host string, pid, concurrency int, queues map[string]int, strict bool) *ServerState {
|
||||||
return &ProcessState{
|
return &ServerState{
|
||||||
host: host,
|
host: host,
|
||||||
pid: pid,
|
pid: pid,
|
||||||
|
id: xid.New(),
|
||||||
concurrency: concurrency,
|
concurrency: concurrency,
|
||||||
queues: cloneQueueConfig(queues),
|
queues: cloneQueueConfig(queues),
|
||||||
strictPriority: strict,
|
strictPriority: strict,
|
||||||
@@ -159,59 +171,67 @@ func NewProcessState(host string, pid, concurrency int, queues map[string]int, s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStatus updates the state of process.
|
// SetStatus updates the status of server.
|
||||||
func (ps *ProcessState) SetStatus(status PStatus) {
|
func (ss *ServerState) SetStatus(status ServerStatus) {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
ps.status = status
|
ss.status = status
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status returns the status of server.
|
||||||
|
func (ss *ServerState) Status() ServerStatus {
|
||||||
|
ss.mu.Lock()
|
||||||
|
defer ss.mu.Unlock()
|
||||||
|
return ss.status
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStarted records when the process started processing.
|
// SetStarted records when the process started processing.
|
||||||
func (ps *ProcessState) SetStarted(t time.Time) {
|
func (ss *ServerState) SetStarted(t time.Time) {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
ps.started = t
|
ss.started = t
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddWorkerStats records when a worker started and which task it's processing.
|
// AddWorkerStats records when a worker started and which task it's processing.
|
||||||
func (ps *ProcessState) AddWorkerStats(msg *TaskMessage, started time.Time) {
|
func (ss *ServerState) AddWorkerStats(msg *TaskMessage, started time.Time) {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
ps.workers[msg.ID.String()] = &workerStats{msg, started}
|
ss.workers[msg.ID.String()] = &workerStats{msg, started}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteWorkerStats removes a worker's entry from the process state.
|
// DeleteWorkerStats removes a worker's entry from the process state.
|
||||||
func (ps *ProcessState) DeleteWorkerStats(msg *TaskMessage) {
|
func (ss *ServerState) DeleteWorkerStats(msg *TaskMessage) {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
delete(ps.workers, msg.ID.String())
|
delete(ss.workers, msg.ID.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns current state of process as a ProcessInfo.
|
// GetInfo returns current state of server as a ServerInfo.
|
||||||
func (ps *ProcessState) Get() *ProcessInfo {
|
func (ss *ServerState) GetInfo() *ServerInfo {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
return &ProcessInfo{
|
return &ServerInfo{
|
||||||
Host: ps.host,
|
Host: ss.host,
|
||||||
PID: ps.pid,
|
PID: ss.pid,
|
||||||
Concurrency: ps.concurrency,
|
ServerID: ss.id.String(),
|
||||||
Queues: cloneQueueConfig(ps.queues),
|
Concurrency: ss.concurrency,
|
||||||
StrictPriority: ps.strictPriority,
|
Queues: cloneQueueConfig(ss.queues),
|
||||||
Status: ps.status.String(),
|
StrictPriority: ss.strictPriority,
|
||||||
Started: ps.started,
|
Status: ss.status.String(),
|
||||||
ActiveWorkerCount: len(ps.workers),
|
Started: ss.started,
|
||||||
|
ActiveWorkerCount: len(ss.workers),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWorkers returns a list of currently running workers' info.
|
// GetWorkers returns a list of currently running workers' info.
|
||||||
func (ps *ProcessState) GetWorkers() []*WorkerInfo {
|
func (ss *ServerState) GetWorkers() []*WorkerInfo {
|
||||||
ps.mu.Lock()
|
ss.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ss.mu.Unlock()
|
||||||
var res []*WorkerInfo
|
var res []*WorkerInfo
|
||||||
for _, w := range ps.workers {
|
for _, w := range ss.workers {
|
||||||
res = append(res, &WorkerInfo{
|
res = append(res, &WorkerInfo{
|
||||||
Host: ps.host,
|
Host: ss.host,
|
||||||
PID: ps.pid,
|
PID: ss.pid,
|
||||||
ID: w.msg.ID,
|
ID: w.msg.ID,
|
||||||
Type: w.msg.Type,
|
Type: w.msg.Type,
|
||||||
Queue: w.msg.Queue,
|
Queue: w.msg.Queue,
|
||||||
@@ -238,10 +258,11 @@ func clonePayload(payload map[string]interface{}) map[string]interface{} {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessInfo holds information about a running background worker process.
|
// ServerInfo holds information about a running server.
|
||||||
type ProcessInfo struct {
|
type ServerInfo struct {
|
||||||
Host string
|
Host string
|
||||||
PID int
|
PID int
|
||||||
|
ServerID string
|
||||||
Concurrency int
|
Concurrency int
|
||||||
Queues map[string]int
|
Queues map[string]int
|
||||||
StrictPriority bool
|
StrictPriority bool
|
||||||
@@ -308,3 +329,25 @@ func (c *Cancelations) GetAll() []context.CancelFunc {
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Broker is a message broker that supports operations to manage task queues.
|
||||||
|
//
|
||||||
|
// See rdb.RDB as a reference implementation.
|
||||||
|
type Broker interface {
|
||||||
|
Enqueue(msg *TaskMessage) error
|
||||||
|
EnqueueUnique(msg *TaskMessage, ttl time.Duration) error
|
||||||
|
Dequeue(qnames ...string) (*TaskMessage, error)
|
||||||
|
Done(msg *TaskMessage) error
|
||||||
|
Requeue(msg *TaskMessage) error
|
||||||
|
Schedule(msg *TaskMessage, processAt time.Time) error
|
||||||
|
ScheduleUnique(msg *TaskMessage, processAt time.Time, ttl time.Duration) error
|
||||||
|
Retry(msg *TaskMessage, processAt time.Time, errMsg string) error
|
||||||
|
Kill(msg *TaskMessage, errMsg string) error
|
||||||
|
RequeueAll() (int64, error)
|
||||||
|
CheckAndEnqueue(qnames ...string) error
|
||||||
|
WriteServerState(ss *ServerState, ttl time.Duration) error
|
||||||
|
ClearServerState(ss *ServerState) error
|
||||||
|
CancelationPubSub() (*redis.PubSub, error) // TODO: Need to decouple from redis to support other brokers
|
||||||
|
PublishCancelation(id string) error
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
@@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -67,20 +68,22 @@ func TestFailureKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessInfoKey(t *testing.T) {
|
func TestServerInfoKey(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
hostname string
|
hostname string
|
||||||
pid int
|
pid int
|
||||||
|
sid string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"localhost", 9876, "asynq:ps:localhost:9876"},
|
{"localhost", 9876, "server123", "asynq:servers:localhost:9876:server123"},
|
||||||
{"127.0.0.1", 1234, "asynq:ps:127.0.0.1:1234"},
|
{"127.0.0.1", 1234, "server987", "asynq:servers:127.0.0.1:1234:server987"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
got := ProcessInfoKey(tc.hostname, tc.pid)
|
got := ServerInfoKey(tc.hostname, tc.pid, tc.sid)
|
||||||
if got != tc.want {
|
if got != tc.want {
|
||||||
t.Errorf("ProcessInfoKey(%q, %d) = %q, want %q", tc.hostname, tc.pid, got, tc.want)
|
t.Errorf("ServerInfoKey(%q, %d, %q) = %q, want %q",
|
||||||
|
tc.hostname, tc.pid, tc.sid, got, tc.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,48 +92,53 @@ func TestWorkersKey(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
hostname string
|
hostname string
|
||||||
pid int
|
pid int
|
||||||
|
sid string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"localhost", 9876, "asynq:workers:localhost:9876"},
|
{"localhost", 9876, "server1", "asynq:workers:localhost:9876:server1"},
|
||||||
{"127.0.0.1", 1234, "asynq:workers:127.0.0.1:1234"},
|
{"127.0.0.1", 1234, "server2", "asynq:workers:127.0.0.1:1234:server2"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
got := WorkersKey(tc.hostname, tc.pid)
|
got := WorkersKey(tc.hostname, tc.pid, tc.sid)
|
||||||
if got != tc.want {
|
if got != tc.want {
|
||||||
t.Errorf("WorkersKey(%q, %d) = %q, want = %q", tc.hostname, tc.pid, got, tc.want)
|
t.Errorf("WorkersKey(%q, %d, %q) = %q, want = %q",
|
||||||
|
tc.hostname, tc.pid, tc.sid, got, tc.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for process state being accessed by multiple goroutines.
|
// Test for server state being accessed by multiple goroutines.
|
||||||
// Run with -race flag to check for data race.
|
// Run with -race flag to check for data race.
|
||||||
func TestProcessStateConcurrentAccess(t *testing.T) {
|
func TestServerStateConcurrentAccess(t *testing.T) {
|
||||||
ps := NewProcessState("127.0.0.1", 1234, 10, map[string]int{"default": 1}, false)
|
ss := NewServerState("127.0.0.1", 1234, 10, map[string]int{"default": 1}, false)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
started := time.Now()
|
started := time.Now()
|
||||||
msgs := []*TaskMessage{
|
msgs := []*TaskMessage{
|
||||||
&TaskMessage{ID: xid.New(), Type: "type1", Payload: map[string]interface{}{"user_id": 42}},
|
{ID: xid.New(), Type: "type1", Payload: map[string]interface{}{"user_id": 42}},
|
||||||
&TaskMessage{ID: xid.New(), Type: "type2"},
|
{ID: xid.New(), Type: "type2"},
|
||||||
&TaskMessage{ID: xid.New(), Type: "type3"},
|
{ID: xid.New(), Type: "type3"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulate hearbeater calling SetStatus and SetStarted.
|
// Simulate hearbeater calling SetStatus and SetStarted.
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
ps.SetStarted(started)
|
ss.SetStarted(started)
|
||||||
ps.SetStatus(StatusRunning)
|
ss.SetStatus(StatusRunning)
|
||||||
|
if status := ss.Status(); status != StatusRunning {
|
||||||
|
t.Errorf("(*ServerState).Status() = %v, want %v", status, StatusRunning)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Simulate processor starting worker goroutines.
|
// Simulate processor starting worker goroutines.
|
||||||
for _, msg := range msgs {
|
for _, msg := range msgs {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ps.AddWorkerStats(msg, time.Now())
|
ss.AddWorkerStats(msg, time.Now())
|
||||||
go func(msg *TaskMessage) {
|
go func(msg *TaskMessage) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||||
ps.DeleteWorkerStats(msg)
|
ss.DeleteWorkerStats(msg)
|
||||||
}(msg)
|
}(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,15 +147,15 @@ func TestProcessStateConcurrentAccess(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
ps.Get()
|
ss.GetInfo()
|
||||||
ps.GetWorkers()
|
ss.GetWorkers()
|
||||||
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
want := &ProcessInfo{
|
want := &ServerInfo{
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
PID: 1234,
|
PID: 1234,
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
@@ -158,9 +166,9 @@ func TestProcessStateConcurrentAccess(t *testing.T) {
|
|||||||
ActiveWorkerCount: 0,
|
ActiveWorkerCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
got := ps.Get()
|
got := ss.GetInfo()
|
||||||
if diff := cmp.Diff(want, got); diff != "" {
|
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(ServerInfo{}, "ServerID")); diff != "" {
|
||||||
t.Errorf("(*ProcessState).Get() = %+v, want %+v; (-want,+got)\n%s",
|
t.Errorf("(*ServerState).GetInfo() = %+v, want %+v; (-want,+got)\n%s",
|
||||||
got, want, diff)
|
got, want, diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,43 +6,210 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
stdlog "log"
|
stdlog "log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewLogger(out io.Writer) *Logger {
|
// Base supports logging at various log levels.
|
||||||
return &Logger{
|
type Base interface {
|
||||||
stdlog.New(out, "", stdlog.Ldate|stdlog.Ltime|stdlog.Lmicroseconds|stdlog.LUTC),
|
// Debug logs a message at Debug level.
|
||||||
}
|
Debug(args ...interface{})
|
||||||
|
|
||||||
|
// Info logs a message at Info level.
|
||||||
|
Info(args ...interface{})
|
||||||
|
|
||||||
|
// Warn logs a message at Warning level.
|
||||||
|
Warn(args ...interface{})
|
||||||
|
|
||||||
|
// Error logs a message at Error level.
|
||||||
|
Error(args ...interface{})
|
||||||
|
|
||||||
|
// Fatal logs a message at Fatal level
|
||||||
|
// and process will exit with status set to 1.
|
||||||
|
Fatal(args ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Logger struct {
|
// baseLogger is a wrapper object around log.Logger from the standard library.
|
||||||
|
// It supports logging at various log levels.
|
||||||
|
type baseLogger struct {
|
||||||
*stdlog.Logger
|
*stdlog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Debug(format string, args ...interface{}) {
|
// Debug logs a message at Debug level.
|
||||||
format = "DEBUG: " + format
|
func (l *baseLogger) Debug(args ...interface{}) {
|
||||||
l.Printf(format, args...)
|
l.prefixPrint("DEBUG: ", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Info(format string, args ...interface{}) {
|
// Info logs a message at Info level.
|
||||||
format = "INFO: " + format
|
func (l *baseLogger) Info(args ...interface{}) {
|
||||||
l.Printf(format, args...)
|
l.prefixPrint("INFO: ", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Warn(format string, args ...interface{}) {
|
// Warn logs a message at Warning level.
|
||||||
format = "WARN: " + format
|
func (l *baseLogger) Warn(args ...interface{}) {
|
||||||
l.Printf(format, args...)
|
l.prefixPrint("WARN: ", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Error(format string, args ...interface{}) {
|
// Error logs a message at Error level.
|
||||||
format = "ERROR: " + format
|
func (l *baseLogger) Error(args ...interface{}) {
|
||||||
l.Printf(format, args...)
|
l.prefixPrint("ERROR: ", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) Fatal(format string, args ...interface{}) {
|
// Fatal logs a message at Fatal level
|
||||||
format = "FATAL: " + format
|
// and process will exit with status set to 1.
|
||||||
l.Printf(format, args...)
|
func (l *baseLogger) Fatal(args ...interface{}) {
|
||||||
|
l.prefixPrint("FATAL: ", args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *baseLogger) prefixPrint(prefix string, args ...interface{}) {
|
||||||
|
args = append([]interface{}{prefix}, args...)
|
||||||
|
l.Print(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newBase creates and returns a new instance of baseLogger.
|
||||||
|
func newBase(out io.Writer) *baseLogger {
|
||||||
|
prefix := fmt.Sprintf("asynq: pid=%d ", os.Getpid())
|
||||||
|
return &baseLogger{
|
||||||
|
stdlog.New(out, prefix, stdlog.Ldate|stdlog.Ltime|stdlog.Lmicroseconds|stdlog.LUTC),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLogger creates and returns a new instance of Logger.
|
||||||
|
// Log level is set to DebugLevel by default.
|
||||||
|
func NewLogger(base Base) *Logger {
|
||||||
|
if base == nil {
|
||||||
|
base = newBase(os.Stderr)
|
||||||
|
}
|
||||||
|
return &Logger{base: base, level: DebugLevel}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger logs message to io.Writer at various log levels.
|
||||||
|
type Logger struct {
|
||||||
|
base Base
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
// Minimum log level for this logger.
|
||||||
|
// Message with level lower than this level won't be outputted.
|
||||||
|
level Level
|
||||||
|
}
|
||||||
|
|
||||||
|
// Level represents a log level.
|
||||||
|
type Level int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DebugLevel is the lowest level of logging.
|
||||||
|
// Debug logs are intended for debugging and development purposes.
|
||||||
|
DebugLevel Level = iota
|
||||||
|
|
||||||
|
// InfoLevel is used for general informational log messages.
|
||||||
|
InfoLevel
|
||||||
|
|
||||||
|
// WarnLevel is used for undesired but relatively expected events,
|
||||||
|
// which may indicate a problem.
|
||||||
|
WarnLevel
|
||||||
|
|
||||||
|
// ErrorLevel is used for undesired and unexpected events that
|
||||||
|
// the program can recover from.
|
||||||
|
ErrorLevel
|
||||||
|
|
||||||
|
// FatalLevel is used for undesired and unexpected events that
|
||||||
|
// the program cannot recover from.
|
||||||
|
FatalLevel
|
||||||
|
)
|
||||||
|
|
||||||
|
// String is part of the fmt.Stringer interface.
|
||||||
|
//
|
||||||
|
// Used for testing and debugging purposes.
|
||||||
|
func (l Level) String() string {
|
||||||
|
switch l {
|
||||||
|
case DebugLevel:
|
||||||
|
return "debug"
|
||||||
|
case InfoLevel:
|
||||||
|
return "info"
|
||||||
|
case WarnLevel:
|
||||||
|
return "warning"
|
||||||
|
case ErrorLevel:
|
||||||
|
return "error"
|
||||||
|
case FatalLevel:
|
||||||
|
return "fatal"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// canLogAt reports whether logger can log at level v.
|
||||||
|
func (l *Logger) canLogAt(v Level) bool {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
return v >= l.level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Debug(args ...interface{}) {
|
||||||
|
if !l.canLogAt(DebugLevel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.base.Debug(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Info(args ...interface{}) {
|
||||||
|
if !l.canLogAt(InfoLevel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.base.Info(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Warn(args ...interface{}) {
|
||||||
|
if !l.canLogAt(WarnLevel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.base.Warn(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Error(args ...interface{}) {
|
||||||
|
if !l.canLogAt(ErrorLevel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.base.Error(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Fatal(args ...interface{}) {
|
||||||
|
if !l.canLogAt(FatalLevel) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.base.Fatal(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Debugf(format string, args ...interface{}) {
|
||||||
|
l.Debug(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Infof(format string, args ...interface{}) {
|
||||||
|
l.Info(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Warnf(format string, args ...interface{}) {
|
||||||
|
l.Warn(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Errorf(format string, args ...interface{}) {
|
||||||
|
l.Error(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Fatalf(format string, args ...interface{}) {
|
||||||
|
l.Fatal(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLevel sets the logger level.
|
||||||
|
// It panics if v is less than DebugLevel or greater than FatalLevel.
|
||||||
|
func (l *Logger) SetLevel(v Level) {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
|
if v < DebugLevel || v > FatalLevel {
|
||||||
|
panic("log: invalid log level")
|
||||||
|
}
|
||||||
|
l.level = v
|
||||||
|
}
|
||||||
|
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
// regexp for timestamps
|
// regexp for timestamps
|
||||||
const (
|
const (
|
||||||
|
rgxPID = `[0-9]+`
|
||||||
rgxdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
|
rgxdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
|
||||||
rgxtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
|
rgxtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
|
||||||
rgxmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
|
rgxmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
|
||||||
@@ -27,20 +28,22 @@ type tester struct {
|
|||||||
func TestLoggerDebug(t *testing.T) {
|
func TestLoggerDebug(t *testing.T) {
|
||||||
tests := []tester{
|
tests := []tester{
|
||||||
{
|
{
|
||||||
desc: "without trailing newline, logger adds newline",
|
desc: "without trailing newline, logger adds newline",
|
||||||
message: "hello, world!",
|
message: "hello, world!",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s DEBUG: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s DEBUG: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "with trailing newline, logger preserves newline",
|
desc: "with trailing newline, logger preserves newline",
|
||||||
message: "hello, world!\n",
|
message: "hello, world!\n",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s DEBUG: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s DEBUG: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
logger := NewLogger(&buf)
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
logger.Debug(tc.message)
|
logger.Debug(tc.message)
|
||||||
|
|
||||||
@@ -50,7 +53,7 @@ func TestLoggerDebug(t *testing.T) {
|
|||||||
t.Fatal("pattern did not compile:", err)
|
t.Fatal("pattern did not compile:", err)
|
||||||
}
|
}
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Errorf("logger.info(%q) outputted %q, should match pattern %q",
|
t.Errorf("logger.Debug(%q) outputted %q, should match pattern %q",
|
||||||
tc.message, got, tc.wantPattern)
|
tc.message, got, tc.wantPattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,20 +62,22 @@ func TestLoggerDebug(t *testing.T) {
|
|||||||
func TestLoggerInfo(t *testing.T) {
|
func TestLoggerInfo(t *testing.T) {
|
||||||
tests := []tester{
|
tests := []tester{
|
||||||
{
|
{
|
||||||
desc: "without trailing newline, logger adds newline",
|
desc: "without trailing newline, logger adds newline",
|
||||||
message: "hello, world!",
|
message: "hello, world!",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s INFO: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s INFO: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "with trailing newline, logger preserves newline",
|
desc: "with trailing newline, logger preserves newline",
|
||||||
message: "hello, world!\n",
|
message: "hello, world!\n",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s INFO: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s INFO: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
logger := NewLogger(&buf)
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
logger.Info(tc.message)
|
logger.Info(tc.message)
|
||||||
|
|
||||||
@@ -82,7 +87,7 @@ func TestLoggerInfo(t *testing.T) {
|
|||||||
t.Fatal("pattern did not compile:", err)
|
t.Fatal("pattern did not compile:", err)
|
||||||
}
|
}
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Errorf("logger.info(%q) outputted %q, should match pattern %q",
|
t.Errorf("logger.Info(%q) outputted %q, should match pattern %q",
|
||||||
tc.message, got, tc.wantPattern)
|
tc.message, got, tc.wantPattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,20 +96,22 @@ func TestLoggerInfo(t *testing.T) {
|
|||||||
func TestLoggerWarn(t *testing.T) {
|
func TestLoggerWarn(t *testing.T) {
|
||||||
tests := []tester{
|
tests := []tester{
|
||||||
{
|
{
|
||||||
desc: "without trailing newline, logger adds newline",
|
desc: "without trailing newline, logger adds newline",
|
||||||
message: "hello, world!",
|
message: "hello, world!",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s WARN: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s WARN: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "with trailing newline, logger preserves newline",
|
desc: "with trailing newline, logger preserves newline",
|
||||||
message: "hello, world!\n",
|
message: "hello, world!\n",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s WARN: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s WARN: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
logger := NewLogger(&buf)
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
logger.Warn(tc.message)
|
logger.Warn(tc.message)
|
||||||
|
|
||||||
@@ -114,7 +121,7 @@ func TestLoggerWarn(t *testing.T) {
|
|||||||
t.Fatal("pattern did not compile:", err)
|
t.Fatal("pattern did not compile:", err)
|
||||||
}
|
}
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Errorf("logger.info(%q) outputted %q, should match pattern %q",
|
t.Errorf("logger.Warn(%q) outputted %q, should match pattern %q",
|
||||||
tc.message, got, tc.wantPattern)
|
tc.message, got, tc.wantPattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,20 +130,22 @@ func TestLoggerWarn(t *testing.T) {
|
|||||||
func TestLoggerError(t *testing.T) {
|
func TestLoggerError(t *testing.T) {
|
||||||
tests := []tester{
|
tests := []tester{
|
||||||
{
|
{
|
||||||
desc: "without trailing newline, logger adds newline",
|
desc: "without trailing newline, logger adds newline",
|
||||||
message: "hello, world!",
|
message: "hello, world!",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s ERROR: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s ERROR: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "with trailing newline, logger preserves newline",
|
desc: "with trailing newline, logger preserves newline",
|
||||||
message: "hello, world!\n",
|
message: "hello, world!\n",
|
||||||
wantPattern: fmt.Sprintf("^%s %s%s ERROR: hello, world!\n$", rgxdate, rgxtime, rgxmicroseconds),
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s ERROR: hello, world!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
logger := NewLogger(&buf)
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
logger.Error(tc.message)
|
logger.Error(tc.message)
|
||||||
|
|
||||||
@@ -146,8 +155,234 @@ func TestLoggerError(t *testing.T) {
|
|||||||
t.Fatal("pattern did not compile:", err)
|
t.Fatal("pattern did not compile:", err)
|
||||||
}
|
}
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Errorf("logger.info(%q) outputted %q, should match pattern %q",
|
t.Errorf("logger.Error(%q) outputted %q, should match pattern %q",
|
||||||
tc.message, got, tc.wantPattern)
|
tc.message, got, tc.wantPattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type formatTester struct {
|
||||||
|
desc string
|
||||||
|
format string
|
||||||
|
args []interface{}
|
||||||
|
wantPattern string // regexp that log output must match
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerDebugf(t *testing.T) {
|
||||||
|
tests := []formatTester{
|
||||||
|
{
|
||||||
|
desc: "Formats message with DEBUG prefix",
|
||||||
|
format: "hello, %s!",
|
||||||
|
args: []interface{}{"Gopher"},
|
||||||
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s DEBUG: hello, Gopher!\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
|
logger.Debugf(tc.format, tc.args...)
|
||||||
|
|
||||||
|
got := buf.String()
|
||||||
|
matched, err := regexp.MatchString(tc.wantPattern, got)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("pattern did not compile:", err)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("logger.Debugf(%q, %v) outputted %q, should match pattern %q",
|
||||||
|
tc.format, tc.args, got, tc.wantPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerInfof(t *testing.T) {
|
||||||
|
tests := []formatTester{
|
||||||
|
{
|
||||||
|
desc: "Formats message with INFO prefix",
|
||||||
|
format: "%d,%d,%d",
|
||||||
|
args: []interface{}{1, 2, 3},
|
||||||
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s INFO: 1,2,3\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
|
logger.Infof(tc.format, tc.args...)
|
||||||
|
|
||||||
|
got := buf.String()
|
||||||
|
matched, err := regexp.MatchString(tc.wantPattern, got)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("pattern did not compile:", err)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("logger.Infof(%q, %v) outputted %q, should match pattern %q",
|
||||||
|
tc.format, tc.args, got, tc.wantPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerWarnf(t *testing.T) {
|
||||||
|
tests := []formatTester{
|
||||||
|
{
|
||||||
|
desc: "Formats message with WARN prefix",
|
||||||
|
format: "hello, %s",
|
||||||
|
args: []interface{}{"Gophers"},
|
||||||
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s WARN: hello, Gophers\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
|
logger.Warnf(tc.format, tc.args...)
|
||||||
|
|
||||||
|
got := buf.String()
|
||||||
|
matched, err := regexp.MatchString(tc.wantPattern, got)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("pattern did not compile:", err)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("logger.Warnf(%q, %v) outputted %q, should match pattern %q",
|
||||||
|
tc.format, tc.args, got, tc.wantPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerErrorf(t *testing.T) {
|
||||||
|
tests := []formatTester{
|
||||||
|
{
|
||||||
|
desc: "Formats message with ERROR prefix",
|
||||||
|
format: "hello, %s",
|
||||||
|
args: []interface{}{"Gophers"},
|
||||||
|
wantPattern: fmt.Sprintf("^asynq: pid=%s %s %s%s ERROR: hello, Gophers\n$",
|
||||||
|
rgxPID, rgxdate, rgxtime, rgxmicroseconds),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
|
||||||
|
logger.Errorf(tc.format, tc.args...)
|
||||||
|
|
||||||
|
got := buf.String()
|
||||||
|
matched, err := regexp.MatchString(tc.wantPattern, got)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("pattern did not compile:", err)
|
||||||
|
}
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("logger.Errorf(%q, %v) outputted %q, should match pattern %q",
|
||||||
|
tc.format, tc.args, got, tc.wantPattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerWithLowerLevels(t *testing.T) {
|
||||||
|
// Logger should not log messages at a level
|
||||||
|
// lower than the specified level.
|
||||||
|
tests := []struct {
|
||||||
|
level Level
|
||||||
|
op string
|
||||||
|
}{
|
||||||
|
// with level one above
|
||||||
|
{InfoLevel, "Debug"},
|
||||||
|
{InfoLevel, "Debugf"},
|
||||||
|
{WarnLevel, "Info"},
|
||||||
|
{WarnLevel, "Infof"},
|
||||||
|
{ErrorLevel, "Warn"},
|
||||||
|
{ErrorLevel, "Warnf"},
|
||||||
|
{FatalLevel, "Error"},
|
||||||
|
{FatalLevel, "Errorf"},
|
||||||
|
// with skip level
|
||||||
|
{WarnLevel, "Debug"},
|
||||||
|
{ErrorLevel, "Infof"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
logger.SetLevel(tc.level)
|
||||||
|
|
||||||
|
switch tc.op {
|
||||||
|
case "Debug":
|
||||||
|
logger.Debug("hello")
|
||||||
|
case "Debugf":
|
||||||
|
logger.Debugf("hello, %s", "world")
|
||||||
|
case "Info":
|
||||||
|
logger.Info("hello")
|
||||||
|
case "Infof":
|
||||||
|
logger.Infof("hello, %s", "world")
|
||||||
|
case "Warn":
|
||||||
|
logger.Warn("hello")
|
||||||
|
case "Warnf":
|
||||||
|
logger.Warnf("hello, %s", "world")
|
||||||
|
case "Error":
|
||||||
|
logger.Error("hello")
|
||||||
|
case "Errorf":
|
||||||
|
logger.Errorf("hello, %s", "world")
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected op: %q", tc.op)
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf.String() != "" {
|
||||||
|
t.Errorf("logger.%s outputted log message when level is set to %v", tc.op, tc.level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerWithSameOrHigherLevels(t *testing.T) {
|
||||||
|
// Logger should log messages at a level
|
||||||
|
// same as or higher than the specified level.
|
||||||
|
tests := []struct {
|
||||||
|
level Level
|
||||||
|
op string
|
||||||
|
}{
|
||||||
|
// same level
|
||||||
|
{DebugLevel, "Debug"},
|
||||||
|
{InfoLevel, "Infof"},
|
||||||
|
{WarnLevel, "Warn"},
|
||||||
|
{ErrorLevel, "Errorf"},
|
||||||
|
// higher level
|
||||||
|
{DebugLevel, "Info"},
|
||||||
|
{InfoLevel, "Warnf"},
|
||||||
|
{WarnLevel, "Error"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := NewLogger(newBase(&buf))
|
||||||
|
logger.SetLevel(tc.level)
|
||||||
|
|
||||||
|
switch tc.op {
|
||||||
|
case "Debug":
|
||||||
|
logger.Debug("hello")
|
||||||
|
case "Debugf":
|
||||||
|
logger.Debugf("hello, %s", "world")
|
||||||
|
case "Info":
|
||||||
|
logger.Info("hello")
|
||||||
|
case "Infof":
|
||||||
|
logger.Infof("hello, %s", "world")
|
||||||
|
case "Warn":
|
||||||
|
logger.Warn("hello")
|
||||||
|
case "Warnf":
|
||||||
|
logger.Warnf("hello, %s", "world")
|
||||||
|
case "Error":
|
||||||
|
logger.Error("hello")
|
||||||
|
case "Errorf":
|
||||||
|
logger.Errorf("hello, %s", "world")
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected op: %q", tc.op)
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf.String() == "" {
|
||||||
|
t.Errorf("logger.%s did not output log message when level is set to %v", tc.op, tc.level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -759,23 +759,23 @@ func (r *RDB) RemoveQueue(qname string, force bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Note: Script also removes stale keys.
|
// Note: Script also removes stale keys.
|
||||||
var listProcessesCmd = redis.NewScript(`
|
var listServersCmd = redis.NewScript(`
|
||||||
local res = {}
|
local res = {}
|
||||||
local now = tonumber(ARGV[1])
|
local now = tonumber(ARGV[1])
|
||||||
local keys = redis.call("ZRANGEBYSCORE", KEYS[1], now, "+inf")
|
local keys = redis.call("ZRANGEBYSCORE", KEYS[1], now, "+inf")
|
||||||
for _, key in ipairs(keys) do
|
for _, key in ipairs(keys) do
|
||||||
local ps = redis.call("GET", key)
|
local s = redis.call("GET", key)
|
||||||
if ps then
|
if s then
|
||||||
table.insert(res, ps)
|
table.insert(res, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", now-1)
|
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", now-1)
|
||||||
return res`)
|
return res`)
|
||||||
|
|
||||||
// ListProcesses returns the list of process statuses.
|
// ListServers returns the list of server info.
|
||||||
func (r *RDB) ListProcesses() ([]*base.ProcessInfo, error) {
|
func (r *RDB) ListServers() ([]*base.ServerInfo, error) {
|
||||||
res, err := listProcessesCmd.Run(r.client,
|
res, err := listServersCmd.Run(r.client,
|
||||||
[]string{base.AllProcesses}, time.Now().UTC().Unix()).Result()
|
[]string{base.AllServers}, time.Now().UTC().Unix()).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -783,16 +783,16 @@ func (r *RDB) ListProcesses() ([]*base.ProcessInfo, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var processes []*base.ProcessInfo
|
var servers []*base.ServerInfo
|
||||||
for _, s := range data {
|
for _, s := range data {
|
||||||
var ps base.ProcessInfo
|
var info base.ServerInfo
|
||||||
err := json.Unmarshal([]byte(s), &ps)
|
err := json.Unmarshal([]byte(s), &info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue // skip bad data
|
continue // skip bad data
|
||||||
}
|
}
|
||||||
processes = append(processes, &ps)
|
servers = append(servers, &info)
|
||||||
}
|
}
|
||||||
return processes, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Script also removes stale keys.
|
// Note: Script also removes stale keys.
|
||||||
|
@@ -2051,14 +2051,14 @@ func TestRemoveQueueError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListProcesses(t *testing.T) {
|
func TestListServers(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
|
|
||||||
started1 := time.Now().Add(-time.Hour)
|
started1 := time.Now().Add(-time.Hour)
|
||||||
ps1 := base.NewProcessState("do.droplet1", 1234, 10, map[string]int{"default": 1}, false)
|
ss1 := base.NewServerState("do.droplet1", 1234, 10, map[string]int{"default": 1}, false)
|
||||||
ps1.SetStarted(started1)
|
ss1.SetStarted(started1)
|
||||||
ps1.SetStatus(base.StatusRunning)
|
ss1.SetStatus(base.StatusRunning)
|
||||||
info1 := &base.ProcessInfo{
|
info1 := &base.ServerInfo{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
Queues: map[string]int{"default": 1},
|
Queues: map[string]int{"default": 1},
|
||||||
Host: "do.droplet1",
|
Host: "do.droplet1",
|
||||||
@@ -2069,11 +2069,11 @@ func TestListProcesses(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
started2 := time.Now().Add(-2 * time.Hour)
|
started2 := time.Now().Add(-2 * time.Hour)
|
||||||
ps2 := base.NewProcessState("do.droplet2", 9876, 20, map[string]int{"email": 1}, false)
|
ss2 := base.NewServerState("do.droplet2", 9876, 20, map[string]int{"email": 1}, false)
|
||||||
ps2.SetStarted(started2)
|
ss2.SetStarted(started2)
|
||||||
ps2.SetStatus(base.StatusStopped)
|
ss2.SetStatus(base.StatusStopped)
|
||||||
ps2.AddWorkerStats(h.NewTaskMessage("send_email", nil), time.Now())
|
ss2.AddWorkerStats(h.NewTaskMessage("send_email", nil), time.Now())
|
||||||
info2 := &base.ProcessInfo{
|
info2 := &base.ServerInfo{
|
||||||
Concurrency: 20,
|
Concurrency: 20,
|
||||||
Queues: map[string]int{"email": 1},
|
Queues: map[string]int{"email": 1},
|
||||||
Host: "do.droplet2",
|
Host: "do.droplet2",
|
||||||
@@ -2084,41 +2084,42 @@ func TestListProcesses(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
processes []*base.ProcessState
|
serverStates []*base.ServerState
|
||||||
want []*base.ProcessInfo
|
want []*base.ServerInfo
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
processes: []*base.ProcessState{},
|
serverStates: []*base.ServerState{},
|
||||||
want: []*base.ProcessInfo{},
|
want: []*base.ServerInfo{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
processes: []*base.ProcessState{ps1},
|
serverStates: []*base.ServerState{ss1},
|
||||||
want: []*base.ProcessInfo{info1},
|
want: []*base.ServerInfo{info1},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
processes: []*base.ProcessState{ps1, ps2},
|
serverStates: []*base.ServerState{ss1, ss2},
|
||||||
want: []*base.ProcessInfo{info1, info2},
|
want: []*base.ServerInfo{info1, info2},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ignoreOpt := cmpopts.IgnoreUnexported(base.ProcessInfo{})
|
ignoreOpt := cmpopts.IgnoreUnexported(base.ServerInfo{})
|
||||||
|
ignoreFieldOpt := cmpopts.IgnoreFields(base.ServerInfo{}, "ServerID")
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
h.FlushDB(t, r.client)
|
h.FlushDB(t, r.client)
|
||||||
|
|
||||||
for _, ps := range tc.processes {
|
for _, ss := range tc.serverStates {
|
||||||
if err := r.WriteProcessState(ps, 5*time.Second); err != nil {
|
if err := r.WriteServerState(ss, 5*time.Second); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
got, err := r.ListProcesses()
|
got, err := r.ListServers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("r.ListProcesses returned an error: %v", err)
|
t.Errorf("r.ListServers returned an error: %v", err)
|
||||||
}
|
}
|
||||||
if diff := cmp.Diff(tc.want, got, h.SortProcessInfoOpt, ignoreOpt); diff != "" {
|
if diff := cmp.Diff(tc.want, got, h.SortServerInfoOpt, ignoreOpt, ignoreFieldOpt); diff != "" {
|
||||||
t.Errorf("r.ListProcesses returned %v, want %v; (-want,+got)\n%s",
|
t.Errorf("r.ListServers returned %v, want %v; (-want,+got)\n%s",
|
||||||
got, tc.processes, diff)
|
got, tc.serverStates, diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2164,15 +2165,15 @@ func TestListWorkers(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
h.FlushDB(t, r.client)
|
h.FlushDB(t, r.client)
|
||||||
|
|
||||||
ps := base.NewProcessState(host, pid, 10, map[string]int{"default": 1}, false)
|
ss := base.NewServerState(host, pid, 10, map[string]int{"default": 1}, false)
|
||||||
|
|
||||||
for _, w := range tc.workers {
|
for _, w := range tc.workers {
|
||||||
ps.AddWorkerStats(w.msg, w.started)
|
ss.AddWorkerStats(w.msg, w.started)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := r.WriteProcessState(ps, time.Minute)
|
err := r.WriteServerState(ss, time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not write process state to redis: %v", err)
|
t.Errorf("could not write server state to redis: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -22,6 +22,9 @@ var (
|
|||||||
|
|
||||||
// ErrTaskNotFound indicates that a task that matches the given identifier was not found.
|
// ErrTaskNotFound indicates that a task that matches the given identifier was not found.
|
||||||
ErrTaskNotFound = errors.New("could not find a task")
|
ErrTaskNotFound = errors.New("could not find a task")
|
||||||
|
|
||||||
|
// ErrDuplicateTask indicates that another task with the same unique key holds the uniqueness lock.
|
||||||
|
ErrDuplicateTask = errors.New("task already exists")
|
||||||
)
|
)
|
||||||
|
|
||||||
const statsTTL = 90 * 24 * time.Hour // 90 days
|
const statsTTL = 90 * 24 * time.Hour // 90 days
|
||||||
@@ -59,6 +62,46 @@ func (r *RDB) Enqueue(msg *base.TaskMessage) error {
|
|||||||
return enqueueCmd.Run(r.client, []string{key, base.AllQueues}, bytes).Err()
|
return enqueueCmd.Run(r.client, []string{key, base.AllQueues}, bytes).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KEYS[1] -> unique key in the form <type>:<payload>:<qname>
|
||||||
|
// KEYS[2] -> asynq:queues:<qname>
|
||||||
|
// KEYS[2] -> asynq:queues
|
||||||
|
// ARGV[1] -> task ID
|
||||||
|
// ARGV[2] -> uniqueness lock TTL
|
||||||
|
// ARGV[3] -> task message data
|
||||||
|
var enqueueUniqueCmd = redis.NewScript(`
|
||||||
|
local ok = redis.call("SET", KEYS[1], ARGV[1], "NX", "EX", ARGV[2])
|
||||||
|
if not ok then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
redis.call("LPUSH", KEYS[2], ARGV[3])
|
||||||
|
redis.call("SADD", KEYS[3], KEYS[2])
|
||||||
|
return 1
|
||||||
|
`)
|
||||||
|
|
||||||
|
// EnqueueUnique inserts the given task if the task's uniqueness lock can be acquired.
|
||||||
|
// It returns ErrDuplicateTask if the lock cannot be acquired.
|
||||||
|
func (r *RDB) EnqueueUnique(msg *base.TaskMessage, ttl time.Duration) error {
|
||||||
|
bytes, err := json.Marshal(msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
key := base.QueueKey(msg.Queue)
|
||||||
|
res, err := enqueueUniqueCmd.Run(r.client,
|
||||||
|
[]string{msg.UniqueKey, key, base.AllQueues},
|
||||||
|
msg.ID.String(), int(ttl.Seconds()), bytes).Result()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, ok := res.(int64)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("could not cast %v to int64", res)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return ErrDuplicateTask
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Dequeue queries given queues in order and pops a task message if there is one and returns it.
|
// Dequeue queries given queues in order and pops a task message if there is one and returns it.
|
||||||
// If all queues are empty, ErrNoProcessableTask error is returned.
|
// If all queues are empty, ErrNoProcessableTask error is returned.
|
||||||
func (r *RDB) Dequeue(qnames ...string) (*base.TaskMessage, error) {
|
func (r *RDB) Dequeue(qnames ...string) (*base.TaskMessage, error) {
|
||||||
@@ -118,8 +161,10 @@ func (r *RDB) dequeue(queues ...string) (data string, err error) {
|
|||||||
|
|
||||||
// KEYS[1] -> asynq:in_progress
|
// KEYS[1] -> asynq:in_progress
|
||||||
// KEYS[2] -> asynq:processed:<yyyy-mm-dd>
|
// KEYS[2] -> asynq:processed:<yyyy-mm-dd>
|
||||||
|
// KEYS[3] -> unique key in the format <type>:<payload>:<qname>
|
||||||
// ARGV[1] -> base.TaskMessage value
|
// ARGV[1] -> base.TaskMessage value
|
||||||
// ARGV[2] -> stats expiration timestamp
|
// ARGV[2] -> stats expiration timestamp
|
||||||
|
// ARGV[3] -> task ID
|
||||||
// Note: LREM count ZERO means "remove all elements equal to val"
|
// Note: LREM count ZERO means "remove all elements equal to val"
|
||||||
var doneCmd = redis.NewScript(`
|
var doneCmd = redis.NewScript(`
|
||||||
redis.call("LREM", KEYS[1], 0, ARGV[1])
|
redis.call("LREM", KEYS[1], 0, ARGV[1])
|
||||||
@@ -127,10 +172,14 @@ local n = redis.call("INCR", KEYS[2])
|
|||||||
if tonumber(n) == 1 then
|
if tonumber(n) == 1 then
|
||||||
redis.call("EXPIREAT", KEYS[2], ARGV[2])
|
redis.call("EXPIREAT", KEYS[2], ARGV[2])
|
||||||
end
|
end
|
||||||
|
if string.len(KEYS[3]) > 0 and redis.call("GET", KEYS[3]) == ARGV[3] then
|
||||||
|
redis.call("DEL", KEYS[3])
|
||||||
|
end
|
||||||
return redis.status_reply("OK")
|
return redis.status_reply("OK")
|
||||||
`)
|
`)
|
||||||
|
|
||||||
// Done removes the task from in-progress queue to mark the task as done.
|
// Done removes the task from in-progress queue to mark the task as done.
|
||||||
|
// It removes a uniqueness lock acquired by the task, if any.
|
||||||
func (r *RDB) Done(msg *base.TaskMessage) error {
|
func (r *RDB) Done(msg *base.TaskMessage) error {
|
||||||
bytes, err := json.Marshal(msg)
|
bytes, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -140,8 +189,8 @@ func (r *RDB) Done(msg *base.TaskMessage) error {
|
|||||||
processedKey := base.ProcessedKey(now)
|
processedKey := base.ProcessedKey(now)
|
||||||
expireAt := now.Add(statsTTL)
|
expireAt := now.Add(statsTTL)
|
||||||
return doneCmd.Run(r.client,
|
return doneCmd.Run(r.client,
|
||||||
[]string{base.InProgressQueue, processedKey},
|
[]string{base.InProgressQueue, processedKey, msg.UniqueKey},
|
||||||
bytes, expireAt.Unix()).Err()
|
bytes, expireAt.Unix(), msg.ID.String()).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// KEYS[1] -> asynq:in_progress
|
// KEYS[1] -> asynq:in_progress
|
||||||
@@ -164,15 +213,71 @@ func (r *RDB) Requeue(msg *base.TaskMessage) error {
|
|||||||
string(bytes)).Err()
|
string(bytes)).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KEYS[1] -> asynq:scheduled
|
||||||
|
// KEYS[2] -> asynq:queues
|
||||||
|
// ARGV[1] -> score (process_at timestamp)
|
||||||
|
// ARGV[2] -> task message
|
||||||
|
// ARGV[3] -> queue key
|
||||||
|
var scheduleCmd = redis.NewScript(`
|
||||||
|
redis.call("ZADD", KEYS[1], ARGV[1], ARGV[2])
|
||||||
|
redis.call("SADD", KEYS[2], ARGV[3])
|
||||||
|
return 1
|
||||||
|
`)
|
||||||
|
|
||||||
// Schedule adds the task to the backlog queue to be processed in the future.
|
// Schedule adds the task to the backlog queue to be processed in the future.
|
||||||
func (r *RDB) Schedule(msg *base.TaskMessage, processAt time.Time) error {
|
func (r *RDB) Schedule(msg *base.TaskMessage, processAt time.Time) error {
|
||||||
bytes, err := json.Marshal(msg)
|
bytes, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
qkey := base.QueueKey(msg.Queue)
|
||||||
score := float64(processAt.Unix())
|
score := float64(processAt.Unix())
|
||||||
return r.client.ZAdd(base.ScheduledQueue,
|
return scheduleCmd.Run(r.client,
|
||||||
&redis.Z{Member: string(bytes), Score: score}).Err()
|
[]string{base.ScheduledQueue, base.AllQueues},
|
||||||
|
score, bytes, qkey).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// KEYS[1] -> unique key in the format <type>:<payload>:<qname>
|
||||||
|
// KEYS[2] -> asynq:scheduled
|
||||||
|
// KEYS[3] -> asynq:queues
|
||||||
|
// ARGV[1] -> task ID
|
||||||
|
// ARGV[2] -> uniqueness lock TTL
|
||||||
|
// ARGV[3] -> score (process_at timestamp)
|
||||||
|
// ARGV[4] -> task message
|
||||||
|
// ARGV[5] -> queue key
|
||||||
|
var scheduleUniqueCmd = redis.NewScript(`
|
||||||
|
local ok = redis.call("SET", KEYS[1], ARGV[1], "NX", "EX", ARGV[2])
|
||||||
|
if not ok then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
redis.call("ZADD", KEYS[2], ARGV[3], ARGV[4])
|
||||||
|
redis.call("SADD", KEYS[3], ARGV[5])
|
||||||
|
return 1
|
||||||
|
`)
|
||||||
|
|
||||||
|
// ScheduleUnique adds the task to the backlog queue to be processed in the future if the uniqueness lock can be acquired.
|
||||||
|
// It returns ErrDuplicateTask if the lock cannot be acquired.
|
||||||
|
func (r *RDB) ScheduleUnique(msg *base.TaskMessage, processAt time.Time, ttl time.Duration) error {
|
||||||
|
bytes, err := json.Marshal(msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
qkey := base.QueueKey(msg.Queue)
|
||||||
|
score := float64(processAt.Unix())
|
||||||
|
res, err := scheduleUniqueCmd.Run(r.client,
|
||||||
|
[]string{msg.UniqueKey, base.ScheduledQueue, base.AllQueues},
|
||||||
|
msg.ID.String(), int(ttl.Seconds()), score, bytes, qkey).Result()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, ok := res.(int64)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("could not cast %v to int64", res)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return ErrDuplicateTask
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// KEYS[1] -> asynq:in_progress
|
// KEYS[1] -> asynq:in_progress
|
||||||
@@ -358,9 +463,9 @@ func (r *RDB) forwardSingle(src, dst string) error {
|
|||||||
[]string{src, dst}, now).Err()
|
[]string{src, dst}, now).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// KEYS[1] -> asynq:ps:<host:pid>
|
// KEYS[1] -> asynq:servers:<host:pid:sid>
|
||||||
// KEYS[2] -> asynq:ps
|
// KEYS[2] -> asynq:servers
|
||||||
// KEYS[3] -> asynq:workers<host:pid>
|
// KEYS[3] -> asynq:workers<host:pid:sid>
|
||||||
// keys[4] -> asynq:workers
|
// keys[4] -> asynq:workers
|
||||||
// ARGV[1] -> expiration time
|
// ARGV[1] -> expiration time
|
||||||
// ARGV[2] -> TTL in seconds
|
// ARGV[2] -> TTL in seconds
|
||||||
@@ -379,16 +484,16 @@ redis.call("EXPIRE", KEYS[3], ARGV[2])
|
|||||||
redis.call("ZADD", KEYS[4], ARGV[1], KEYS[3])
|
redis.call("ZADD", KEYS[4], ARGV[1], KEYS[3])
|
||||||
return redis.status_reply("OK")`)
|
return redis.status_reply("OK")`)
|
||||||
|
|
||||||
// WriteProcessState writes process state data to redis with expiration set to the value ttl.
|
// WriteServerState writes server state data to redis with expiration set to the value ttl.
|
||||||
func (r *RDB) WriteProcessState(ps *base.ProcessState, ttl time.Duration) error {
|
func (r *RDB) WriteServerState(ss *base.ServerState, ttl time.Duration) error {
|
||||||
info := ps.Get()
|
info := ss.GetInfo()
|
||||||
bytes, err := json.Marshal(info)
|
bytes, err := json.Marshal(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var args []interface{} // args to the lua script
|
var args []interface{} // args to the lua script
|
||||||
exp := time.Now().Add(ttl).UTC()
|
exp := time.Now().Add(ttl).UTC()
|
||||||
workers := ps.GetWorkers()
|
workers := ss.GetWorkers()
|
||||||
args = append(args, float64(exp.Unix()), ttl.Seconds(), bytes)
|
args = append(args, float64(exp.Unix()), ttl.Seconds(), bytes)
|
||||||
for _, w := range workers {
|
for _, w := range workers {
|
||||||
bytes, err := json.Marshal(w)
|
bytes, err := json.Marshal(w)
|
||||||
@@ -397,17 +502,17 @@ func (r *RDB) WriteProcessState(ps *base.ProcessState, ttl time.Duration) error
|
|||||||
}
|
}
|
||||||
args = append(args, w.ID.String(), bytes)
|
args = append(args, w.ID.String(), bytes)
|
||||||
}
|
}
|
||||||
pkey := base.ProcessInfoKey(info.Host, info.PID)
|
skey := base.ServerInfoKey(info.Host, info.PID, info.ServerID)
|
||||||
wkey := base.WorkersKey(info.Host, info.PID)
|
wkey := base.WorkersKey(info.Host, info.PID, info.ServerID)
|
||||||
return writeProcessInfoCmd.Run(r.client,
|
return writeProcessInfoCmd.Run(r.client,
|
||||||
[]string{pkey, base.AllProcesses, wkey, base.AllWorkers},
|
[]string{skey, base.AllServers, wkey, base.AllWorkers},
|
||||||
args...).Err()
|
args...).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// KEYS[1] -> asynq:ps
|
// KEYS[1] -> asynq:servers
|
||||||
// KEYS[2] -> asynq:ps:<host:pid>
|
// KEYS[2] -> asynq:servers:<host:pid:sid>
|
||||||
// KEYS[3] -> asynq:workers
|
// KEYS[3] -> asynq:workers
|
||||||
// KEYS[4] -> asynq:workers<host:pid>
|
// KEYS[4] -> asynq:workers<host:pid:sid>
|
||||||
var clearProcessInfoCmd = redis.NewScript(`
|
var clearProcessInfoCmd = redis.NewScript(`
|
||||||
redis.call("ZREM", KEYS[1], KEYS[2])
|
redis.call("ZREM", KEYS[1], KEYS[2])
|
||||||
redis.call("DEL", KEYS[2])
|
redis.call("DEL", KEYS[2])
|
||||||
@@ -415,14 +520,14 @@ redis.call("ZREM", KEYS[3], KEYS[4])
|
|||||||
redis.call("DEL", KEYS[4])
|
redis.call("DEL", KEYS[4])
|
||||||
return redis.status_reply("OK")`)
|
return redis.status_reply("OK")`)
|
||||||
|
|
||||||
// ClearProcessState deletes process state data from redis.
|
// ClearServerState deletes server state data from redis.
|
||||||
func (r *RDB) ClearProcessState(ps *base.ProcessState) error {
|
func (r *RDB) ClearServerState(ss *base.ServerState) error {
|
||||||
info := ps.Get()
|
info := ss.GetInfo()
|
||||||
host, pid := info.Host, info.PID
|
host, pid, id := info.Host, info.PID, info.ServerID
|
||||||
pkey := base.ProcessInfoKey(host, pid)
|
skey := base.ServerInfoKey(host, pid, id)
|
||||||
wkey := base.WorkersKey(host, pid)
|
wkey := base.WorkersKey(host, pid, id)
|
||||||
return clearProcessInfoCmd.Run(r.client,
|
return clearProcessInfoCmd.Run(r.client,
|
||||||
[]string{base.AllProcesses, pkey, base.AllWorkers, wkey}).Err()
|
[]string{base.AllServers, skey, base.AllWorkers, wkey}).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelationPubSub returns a pubsub for cancelation messages.
|
// CancelationPubSub returns a pubsub for cancelation messages.
|
||||||
|
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
h "github.com/hibiken/asynq/internal/asynqtest"
|
h "github.com/hibiken/asynq/internal/asynqtest"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO(hibiken): Get Redis address and db number from ENV variables.
|
// TODO(hibiken): Get Redis address and db number from ENV variables.
|
||||||
@@ -69,6 +70,48 @@ func TestEnqueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnqueueUnique(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
m1 := base.TaskMessage{
|
||||||
|
ID: xid.New(),
|
||||||
|
Type: "email",
|
||||||
|
Payload: map[string]interface{}{"user_id": 123},
|
||||||
|
Queue: base.DefaultQueueName,
|
||||||
|
UniqueKey: "email:user_id=123:default",
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
msg *base.TaskMessage
|
||||||
|
ttl time.Duration // uniqueness ttl
|
||||||
|
}{
|
||||||
|
{&m1, time.Minute},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r.client) // clean up db before each test case.
|
||||||
|
|
||||||
|
err := r.EnqueueUnique(tc.msg, tc.ttl)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("First message: (*RDB).EnqueueUnique(%v, %v) = %v, want nil",
|
||||||
|
tc.msg, tc.ttl, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
got := r.EnqueueUnique(tc.msg, tc.ttl)
|
||||||
|
if got != ErrDuplicateTask {
|
||||||
|
t.Errorf("Second message: (*RDB).EnqueueUnique(%v, %v) = %v, want %v",
|
||||||
|
tc.msg, tc.ttl, got, ErrDuplicateTask)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTTL := r.client.TTL(tc.msg.UniqueKey).Val()
|
||||||
|
if !cmp.Equal(tc.ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
|
t.Errorf("TTL %q = %v, want %v", tc.msg.UniqueKey, gotTTL, tc.ttl)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDequeue(t *testing.T) {
|
func TestDequeue(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
|
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "hello!"})
|
||||||
@@ -188,6 +231,13 @@ func TestDone(t *testing.T) {
|
|||||||
r := setup(t)
|
r := setup(t)
|
||||||
t1 := h.NewTaskMessage("send_email", nil)
|
t1 := h.NewTaskMessage("send_email", nil)
|
||||||
t2 := h.NewTaskMessage("export_csv", nil)
|
t2 := h.NewTaskMessage("export_csv", nil)
|
||||||
|
t3 := &base.TaskMessage{
|
||||||
|
ID: xid.New(),
|
||||||
|
Type: "reindex",
|
||||||
|
Payload: nil,
|
||||||
|
UniqueKey: "reindex:nil:default",
|
||||||
|
Queue: "default",
|
||||||
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
inProgress []*base.TaskMessage // initial state of the in-progress list
|
inProgress []*base.TaskMessage // initial state of the in-progress list
|
||||||
@@ -204,11 +254,25 @@ func TestDone(t *testing.T) {
|
|||||||
target: t1,
|
target: t1,
|
||||||
wantInProgress: []*base.TaskMessage{},
|
wantInProgress: []*base.TaskMessage{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
inProgress: []*base.TaskMessage{t1, t2, t3},
|
||||||
|
target: t3,
|
||||||
|
wantInProgress: []*base.TaskMessage{t1, t2},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
h.FlushDB(t, r.client) // clean up db before each test case
|
h.FlushDB(t, r.client) // clean up db before each test case
|
||||||
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
h.SeedInProgressQueue(t, r.client, tc.inProgress)
|
||||||
|
for _, msg := range tc.inProgress {
|
||||||
|
// Set uniqueness lock if unique key is present.
|
||||||
|
if len(msg.UniqueKey) > 0 {
|
||||||
|
err := r.client.SetNX(msg.UniqueKey, msg.ID.String(), time.Minute).Err()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err := r.Done(tc.target)
|
err := r.Done(tc.target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -232,6 +296,10 @@ func TestDone(t *testing.T) {
|
|||||||
if gotTTL > statsTTL {
|
if gotTTL > statsTTL {
|
||||||
t.Errorf("TTL %q = %v, want less than or equal to %v", processedKey, gotTTL, statsTTL)
|
t.Errorf("TTL %q = %v, want less than or equal to %v", processedKey, gotTTL, statsTTL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(tc.target.UniqueKey) > 0 && r.client.Exists(tc.target.UniqueKey).Val() != 0 {
|
||||||
|
t.Errorf("Uniqueness lock %q still exists", tc.target.UniqueKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,6 +412,58 @@ func TestSchedule(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestScheduleUnique(t *testing.T) {
|
||||||
|
r := setup(t)
|
||||||
|
m1 := base.TaskMessage{
|
||||||
|
ID: xid.New(),
|
||||||
|
Type: "email",
|
||||||
|
Payload: map[string]interface{}{"user_id": 123},
|
||||||
|
Queue: base.DefaultQueueName,
|
||||||
|
UniqueKey: "email:user_id=123:default",
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
msg *base.TaskMessage
|
||||||
|
processAt time.Time
|
||||||
|
ttl time.Duration // uniqueness lock ttl
|
||||||
|
}{
|
||||||
|
{&m1, time.Now().Add(15 * time.Minute), time.Minute},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
h.FlushDB(t, r.client) // clean up db before each test case
|
||||||
|
|
||||||
|
desc := fmt.Sprintf("(*RDB).ScheduleUnique(%v, %v, %v)", tc.msg, tc.processAt, tc.ttl)
|
||||||
|
err := r.ScheduleUnique(tc.msg, tc.processAt, tc.ttl)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Frist task: %s = %v, want nil", desc, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
gotScheduled := h.GetScheduledEntries(t, r.client)
|
||||||
|
if len(gotScheduled) != 1 {
|
||||||
|
t.Errorf("%s inserted %d items to %q, want 1 items inserted", desc, len(gotScheduled), base.ScheduledQueue)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if int64(gotScheduled[0].Score) != tc.processAt.Unix() {
|
||||||
|
t.Errorf("%s inserted an item with score %d, want %d", desc, int64(gotScheduled[0].Score), tc.processAt.Unix())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
got := r.ScheduleUnique(tc.msg, tc.processAt, tc.ttl)
|
||||||
|
if got != ErrDuplicateTask {
|
||||||
|
t.Errorf("Second task: %s = %v, want %v",
|
||||||
|
desc, got, ErrDuplicateTask)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTTL := r.client.TTL(tc.msg.UniqueKey).Val()
|
||||||
|
if !cmp.Equal(tc.ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
|
t.Errorf("TTL %q = %v, want %v", tc.msg.UniqueKey, gotTTL, tc.ttl)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRetry(t *testing.T) {
|
func TestRetry(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "Hola!"})
|
t1 := h.NewTaskMessage("send_email", map[string]interface{}{"subject": "Hola!"})
|
||||||
@@ -742,61 +862,61 @@ func TestCheckAndEnqueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteProcessState(t *testing.T) {
|
func TestWriteServerState(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
host, pid := "localhost", 98765
|
|
||||||
queues := map[string]int{"default": 2, "email": 5, "low": 1}
|
queues := map[string]int{"default": 2, "email": 5, "low": 1}
|
||||||
|
|
||||||
started := time.Now()
|
started := time.Now()
|
||||||
ps := base.NewProcessState(host, pid, 10, queues, false)
|
ss := base.NewServerState("localhost", 4242, 10, queues, false)
|
||||||
ps.SetStarted(started)
|
ss.SetStarted(started)
|
||||||
ps.SetStatus(base.StatusRunning)
|
ss.SetStatus(base.StatusRunning)
|
||||||
ttl := 5 * time.Second
|
ttl := 5 * time.Second
|
||||||
|
|
||||||
h.FlushDB(t, r.client)
|
h.FlushDB(t, r.client)
|
||||||
|
|
||||||
err := r.WriteProcessState(ps, ttl)
|
err := r.WriteServerState(ss, ttl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("r.WriteProcessState returned an error: %v", err)
|
t.Errorf("r.WriteServerState returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check ProcessInfo was written correctly
|
// Check ServerInfo was written correctly
|
||||||
pkey := base.ProcessInfoKey(host, pid)
|
info := ss.GetInfo()
|
||||||
data := r.client.Get(pkey).Val()
|
skey := base.ServerInfoKey(info.Host, info.PID, info.ServerID)
|
||||||
var got base.ProcessInfo
|
data := r.client.Get(skey).Val()
|
||||||
|
var got base.ServerInfo
|
||||||
err = json.Unmarshal([]byte(data), &got)
|
err = json.Unmarshal([]byte(data), &got)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not decode json: %v", err)
|
t.Fatalf("could not decode json: %v", err)
|
||||||
}
|
}
|
||||||
want := base.ProcessInfo{
|
want := base.ServerInfo{
|
||||||
Host: "localhost",
|
Host: info.Host,
|
||||||
PID: 98765,
|
PID: info.PID,
|
||||||
Concurrency: 10,
|
Concurrency: info.Concurrency,
|
||||||
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
|
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
|
||||||
StrictPriority: false,
|
StrictPriority: false,
|
||||||
Status: "running",
|
Status: "running",
|
||||||
Started: started,
|
Started: started,
|
||||||
ActiveWorkerCount: 0,
|
ActiveWorkerCount: 0,
|
||||||
}
|
}
|
||||||
if diff := cmp.Diff(want, got); diff != "" {
|
ignoreOpt := cmpopts.IgnoreFields(base.ServerInfo{}, "ServerID")
|
||||||
t.Errorf("persisted ProcessInfo was %v, want %v; (-want,+got)\n%s",
|
if diff := cmp.Diff(want, got, ignoreOpt); diff != "" {
|
||||||
|
t.Errorf("persisted ServerInfo was %v, want %v; (-want,+got)\n%s",
|
||||||
got, want, diff)
|
got, want, diff)
|
||||||
}
|
}
|
||||||
// Check ProcessInfo TTL was set correctly
|
// Check ServerInfo TTL was set correctly
|
||||||
gotTTL := r.client.TTL(pkey).Val()
|
gotTTL := r.client.TTL(skey).Val()
|
||||||
timeCmpOpt := cmpopts.EquateApproxTime(time.Second)
|
if !cmp.Equal(ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
if !cmp.Equal(ttl, gotTTL, timeCmpOpt) {
|
t.Errorf("TTL of %q was %v, want %v", skey, gotTTL, ttl)
|
||||||
t.Errorf("TTL of %q was %v, want %v", pkey, gotTTL, ttl)
|
|
||||||
}
|
}
|
||||||
// Check ProcessInfo key was added to the set correctly
|
// Check ServerInfo key was added to the set correctly
|
||||||
gotProcesses := r.client.ZRange(base.AllProcesses, 0, -1).Val()
|
gotProcesses := r.client.ZRange(base.AllServers, 0, -1).Val()
|
||||||
wantProcesses := []string{pkey}
|
wantProcesses := []string{skey}
|
||||||
if diff := cmp.Diff(wantProcesses, gotProcesses); diff != "" {
|
if diff := cmp.Diff(wantProcesses, gotProcesses); diff != "" {
|
||||||
t.Errorf("%q contained %v, want %v", base.AllProcesses, gotProcesses, wantProcesses)
|
t.Errorf("%q contained %v, want %v", base.AllServers, gotProcesses, wantProcesses)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check WorkersInfo was written correctly
|
// Check WorkersInfo was written correctly
|
||||||
wkey := base.WorkersKey(host, pid)
|
wkey := base.WorkersKey(info.Host, info.PID, info.ServerID)
|
||||||
workerExist := r.client.Exists(wkey).Val()
|
workerExist := r.client.Exists(wkey).Val()
|
||||||
if workerExist != 0 {
|
if workerExist != 0 {
|
||||||
t.Errorf("%q key exists", wkey)
|
t.Errorf("%q key exists", wkey)
|
||||||
@@ -809,9 +929,8 @@ func TestWriteProcessState(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteProcessStateWithWorkers(t *testing.T) {
|
func TestWriteServerStateWithWorkers(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
host, pid := "localhost", 98765
|
|
||||||
queues := map[string]int{"default": 2, "email": 5, "low": 1}
|
queues := map[string]int{"default": 2, "email": 5, "low": 1}
|
||||||
concurrency := 10
|
concurrency := 10
|
||||||
|
|
||||||
@@ -820,31 +939,33 @@ func TestWriteProcessStateWithWorkers(t *testing.T) {
|
|||||||
w2Started := time.Now().Add(-time.Second)
|
w2Started := time.Now().Add(-time.Second)
|
||||||
msg1 := h.NewTaskMessage("send_email", map[string]interface{}{"user_id": "123"})
|
msg1 := h.NewTaskMessage("send_email", map[string]interface{}{"user_id": "123"})
|
||||||
msg2 := h.NewTaskMessage("gen_thumbnail", map[string]interface{}{"path": "some/path/to/imgfile"})
|
msg2 := h.NewTaskMessage("gen_thumbnail", map[string]interface{}{"path": "some/path/to/imgfile"})
|
||||||
ps := base.NewProcessState(host, pid, concurrency, queues, false)
|
ss := base.NewServerState("127.0.01", 4242, concurrency, queues, false)
|
||||||
ps.SetStarted(started)
|
ss.SetStarted(started)
|
||||||
ps.SetStatus(base.StatusRunning)
|
ss.SetStatus(base.StatusRunning)
|
||||||
ps.AddWorkerStats(msg1, w1Started)
|
ss.AddWorkerStats(msg1, w1Started)
|
||||||
ps.AddWorkerStats(msg2, w2Started)
|
ss.AddWorkerStats(msg2, w2Started)
|
||||||
ttl := 5 * time.Second
|
ttl := 5 * time.Second
|
||||||
|
|
||||||
h.FlushDB(t, r.client)
|
h.FlushDB(t, r.client)
|
||||||
|
|
||||||
err := r.WriteProcessState(ps, ttl)
|
err := r.WriteServerState(ss, ttl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("r.WriteProcessState returned an error: %v", err)
|
t.Errorf("r.WriteServerState returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check ProcessInfo was written correctly
|
// Check ServerInfo was written correctly
|
||||||
pkey := base.ProcessInfoKey(host, pid)
|
info := ss.GetInfo()
|
||||||
data := r.client.Get(pkey).Val()
|
skey := base.ServerInfoKey(info.Host, info.PID, info.ServerID)
|
||||||
var got base.ProcessInfo
|
data := r.client.Get(skey).Val()
|
||||||
|
var got base.ServerInfo
|
||||||
err = json.Unmarshal([]byte(data), &got)
|
err = json.Unmarshal([]byte(data), &got)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not decode json: %v", err)
|
t.Fatalf("could not decode json: %v", err)
|
||||||
}
|
}
|
||||||
want := base.ProcessInfo{
|
want := base.ServerInfo{
|
||||||
Host: host,
|
Host: info.Host,
|
||||||
PID: pid,
|
PID: info.PID,
|
||||||
|
ServerID: info.ServerID,
|
||||||
Concurrency: concurrency,
|
Concurrency: concurrency,
|
||||||
Queues: queues,
|
Queues: queues,
|
||||||
StrictPriority: false,
|
StrictPriority: false,
|
||||||
@@ -853,24 +974,23 @@ func TestWriteProcessStateWithWorkers(t *testing.T) {
|
|||||||
ActiveWorkerCount: 2,
|
ActiveWorkerCount: 2,
|
||||||
}
|
}
|
||||||
if diff := cmp.Diff(want, got); diff != "" {
|
if diff := cmp.Diff(want, got); diff != "" {
|
||||||
t.Errorf("persisted ProcessInfo was %v, want %v; (-want,+got)\n%s",
|
t.Errorf("persisted ServerInfo was %v, want %v; (-want,+got)\n%s",
|
||||||
got, want, diff)
|
got, want, diff)
|
||||||
}
|
}
|
||||||
// Check ProcessInfo TTL was set correctly
|
// Check ServerInfo TTL was set correctly
|
||||||
gotTTL := r.client.TTL(pkey).Val()
|
gotTTL := r.client.TTL(skey).Val()
|
||||||
timeCmpOpt := cmpopts.EquateApproxTime(time.Second)
|
if !cmp.Equal(ttl.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
if !cmp.Equal(ttl, gotTTL, timeCmpOpt) {
|
t.Errorf("TTL of %q was %v, want %v", skey, gotTTL, ttl)
|
||||||
t.Errorf("TTL of %q was %v, want %v", pkey, gotTTL, ttl)
|
|
||||||
}
|
}
|
||||||
// Check ProcessInfo key was added to the set correctly
|
// Check ServerInfo key was added to the set correctly
|
||||||
gotProcesses := r.client.ZRange(base.AllProcesses, 0, -1).Val()
|
gotProcesses := r.client.ZRange(base.AllServers, 0, -1).Val()
|
||||||
wantProcesses := []string{pkey}
|
wantProcesses := []string{skey}
|
||||||
if diff := cmp.Diff(wantProcesses, gotProcesses); diff != "" {
|
if diff := cmp.Diff(wantProcesses, gotProcesses); diff != "" {
|
||||||
t.Errorf("%q contained %v, want %v", base.AllProcesses, gotProcesses, wantProcesses)
|
t.Errorf("%q contained %v, want %v", base.AllServers, gotProcesses, wantProcesses)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check WorkersInfo was written correctly
|
// Check WorkersInfo was written correctly
|
||||||
wkey := base.WorkersKey(host, pid)
|
wkey := base.WorkersKey(info.Host, info.PID, info.ServerID)
|
||||||
wdata := r.client.HGetAll(wkey).Val()
|
wdata := r.client.HGetAll(wkey).Val()
|
||||||
if len(wdata) != 2 {
|
if len(wdata) != 2 {
|
||||||
t.Fatalf("HGETALL %q returned a hash of size %d, want 2", wkey, len(wdata))
|
t.Fatalf("HGETALL %q returned a hash of size %d, want 2", wkey, len(wdata))
|
||||||
@@ -884,18 +1004,18 @@ func TestWriteProcessStateWithWorkers(t *testing.T) {
|
|||||||
gotWorkers[key] = &w
|
gotWorkers[key] = &w
|
||||||
}
|
}
|
||||||
wantWorkers := map[string]*base.WorkerInfo{
|
wantWorkers := map[string]*base.WorkerInfo{
|
||||||
msg1.ID.String(): &base.WorkerInfo{
|
msg1.ID.String(): {
|
||||||
Host: host,
|
Host: info.Host,
|
||||||
PID: pid,
|
PID: info.PID,
|
||||||
ID: msg1.ID,
|
ID: msg1.ID,
|
||||||
Type: msg1.Type,
|
Type: msg1.Type,
|
||||||
Queue: msg1.Queue,
|
Queue: msg1.Queue,
|
||||||
Payload: msg1.Payload,
|
Payload: msg1.Payload,
|
||||||
Started: w1Started,
|
Started: w1Started,
|
||||||
},
|
},
|
||||||
msg2.ID.String(): &base.WorkerInfo{
|
msg2.ID.String(): {
|
||||||
Host: host,
|
Host: info.Host,
|
||||||
PID: pid,
|
PID: info.PID,
|
||||||
ID: msg2.ID,
|
ID: msg2.ID,
|
||||||
Type: msg2.Type,
|
Type: msg2.Type,
|
||||||
Queue: msg2.Queue,
|
Queue: msg2.Queue,
|
||||||
@@ -921,27 +1041,28 @@ func TestWriteProcessStateWithWorkers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClearProcessState(t *testing.T) {
|
func TestClearServerState(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
host, pid := "127.0.0.1", 1234
|
ss := base.NewServerState("127.0.01", 4242, 10, map[string]int{"default": 1}, false)
|
||||||
|
info := ss.GetInfo()
|
||||||
|
|
||||||
h.FlushDB(t, r.client)
|
h.FlushDB(t, r.client)
|
||||||
|
|
||||||
pkey := base.ProcessInfoKey(host, pid)
|
skey := base.ServerInfoKey(info.Host, info.PID, info.ServerID)
|
||||||
wkey := base.WorkersKey(host, pid)
|
wkey := base.WorkersKey(info.Host, info.PID, info.ServerID)
|
||||||
otherPKey := base.ProcessInfoKey("otherhost", 12345)
|
otherSKey := base.ServerInfoKey("otherhost", 12345, "server98")
|
||||||
otherWKey := base.WorkersKey("otherhost", 12345)
|
otherWKey := base.WorkersKey("otherhost", 12345, "server98")
|
||||||
// Populate the keys.
|
// Populate the keys.
|
||||||
if err := r.client.Set(pkey, "process-info", 0).Err(); err != nil {
|
if err := r.client.Set(skey, "process-info", 0).Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.client.HSet(wkey, "worker-key", "worker-info").Err(); err != nil {
|
if err := r.client.HSet(wkey, "worker-key", "worker-info").Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.client.ZAdd(base.AllProcesses, &redis.Z{Member: pkey}).Err(); err != nil {
|
if err := r.client.ZAdd(base.AllServers, &redis.Z{Member: skey}).Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.client.ZAdd(base.AllProcesses, &redis.Z{Member: otherPKey}).Err(); err != nil {
|
if err := r.client.ZAdd(base.AllServers, &redis.Z{Member: otherSKey}).Err(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.client.ZAdd(base.AllWorkers, &redis.Z{Member: wkey}).Err(); err != nil {
|
if err := r.client.ZAdd(base.AllWorkers, &redis.Z{Member: wkey}).Err(); err != nil {
|
||||||
@@ -951,24 +1072,22 @@ func TestClearProcessState(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ps := base.NewProcessState(host, pid, 10, map[string]int{"default": 1}, false)
|
err := r.ClearServerState(ss)
|
||||||
|
|
||||||
err := r.ClearProcessState(ps)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("(*RDB).ClearProcessState failed: %v", err)
|
t.Fatalf("(*RDB).ClearServerState failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check all keys are cleared
|
// Check all keys are cleared
|
||||||
if r.client.Exists(pkey).Val() != 0 {
|
if r.client.Exists(skey).Val() != 0 {
|
||||||
t.Errorf("Redis key %q exists", pkey)
|
t.Errorf("Redis key %q exists", skey)
|
||||||
}
|
}
|
||||||
if r.client.Exists(wkey).Val() != 0 {
|
if r.client.Exists(wkey).Val() != 0 {
|
||||||
t.Errorf("Redis key %q exists", wkey)
|
t.Errorf("Redis key %q exists", wkey)
|
||||||
}
|
}
|
||||||
gotProcessKeys := r.client.ZRange(base.AllProcesses, 0, -1).Val()
|
gotProcessKeys := r.client.ZRange(base.AllServers, 0, -1).Val()
|
||||||
wantProcessKeys := []string{otherPKey}
|
wantProcessKeys := []string{otherSKey}
|
||||||
if diff := cmp.Diff(wantProcessKeys, gotProcessKeys); diff != "" {
|
if diff := cmp.Diff(wantProcessKeys, gotProcessKeys); diff != "" {
|
||||||
t.Errorf("%q contained %v, want %v", base.AllProcesses, gotProcessKeys, wantProcessKeys)
|
t.Errorf("%q contained %v, want %v", base.AllServers, gotProcessKeys, wantProcessKeys)
|
||||||
}
|
}
|
||||||
gotWorkerKeys := r.client.ZRange(base.AllWorkers, 0, -1).Val()
|
gotWorkerKeys := r.client.ZRange(base.AllWorkers, 0, -1).Val()
|
||||||
wantWorkerKeys := []string{otherWKey}
|
wantWorkerKeys := []string{otherWKey}
|
||||||
|
187
internal/testbroker/testbroker.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT license
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package testbroker exports a broker implementation that should be used in package testing.
|
||||||
|
package testbroker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v7"
|
||||||
|
"github.com/hibiken/asynq/internal/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
var errRedisDown = errors.New("asynqtest: redis is down")
|
||||||
|
|
||||||
|
// TestBroker is a broker implementation which enables
|
||||||
|
// to simulate Redis failure in tests.
|
||||||
|
type TestBroker struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
sleeping bool
|
||||||
|
|
||||||
|
// real broker
|
||||||
|
real base.Broker
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTestBroker(b base.Broker) *TestBroker {
|
||||||
|
return &TestBroker{real: b}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Sleep() {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
tb.sleeping = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Wakeup() {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
tb.sleeping = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Enqueue(msg *base.TaskMessage) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Enqueue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) EnqueueUnique(msg *base.TaskMessage, ttl time.Duration) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.EnqueueUnique(msg, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Dequeue(qnames ...string) (*base.TaskMessage, error) {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return nil, errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Dequeue(qnames...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Done(msg *base.TaskMessage) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Done(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Requeue(msg *base.TaskMessage) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Requeue(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Schedule(msg *base.TaskMessage, processAt time.Time) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Schedule(msg, processAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) ScheduleUnique(msg *base.TaskMessage, processAt time.Time, ttl time.Duration) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.ScheduleUnique(msg, processAt, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Retry(msg *base.TaskMessage, processAt time.Time, errMsg string) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Retry(msg, processAt, errMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Kill(msg *base.TaskMessage, errMsg string) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Kill(msg, errMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) RequeueAll() (int64, error) {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return 0, errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.RequeueAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) CheckAndEnqueue(qnames ...string) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.CheckAndEnqueue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) WriteServerState(ss *base.ServerState, ttl time.Duration) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.WriteServerState(ss, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) ClearServerState(ss *base.ServerState) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.ClearServerState(ss)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) CancelationPubSub() (*redis.PubSub, error) {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return nil, errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.CancelationPubSub()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) PublishCancelation(id string) error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.PublishCancelation(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tb *TestBroker) Close() error {
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
if tb.sleeping {
|
||||||
|
return errRedisDown
|
||||||
|
}
|
||||||
|
return tb.real.Close()
|
||||||
|
}
|
883
payload_test.go
@@ -14,333 +14,626 @@ import (
|
|||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPayloadGet(t *testing.T) {
|
type payloadTest struct {
|
||||||
names := []string{"luke", "anakin", "rey"}
|
data map[string]interface{}
|
||||||
primes := []int{2, 3, 5, 7, 11, 13, 17}
|
key string
|
||||||
user := map[string]interface{}{"name": "Ken", "score": 3.14}
|
nonkey string
|
||||||
location := map[string]string{"address": "123 Main St.", "state": "NY", "zipcode": "10002"}
|
}
|
||||||
favs := map[string][]string{
|
|
||||||
"movies": []string{"forrest gump", "star wars"},
|
func TestPayloadString(t *testing.T) {
|
||||||
"tv_shows": []string{"game of thrones", "HIMYM", "breaking bad"},
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"name": "gopher"},
|
||||||
|
key: "name",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetString(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("Payload.GetString(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetString(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetString(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetString(tc.nonkey)
|
||||||
|
if err == nil || got != "" {
|
||||||
|
t.Errorf("Payload.GetString(%q) = %v, %v; want '', error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadInt(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"user_id": 42},
|
||||||
|
key: "user_id",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetInt(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("Payload.GetInt(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetInt(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetInt(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetInt(tc.nonkey)
|
||||||
|
if err == nil || got != 0 {
|
||||||
|
t.Errorf("Payload.GetInt(%q) = %v, %v; want 0, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadFloat64(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"pi": 3.14},
|
||||||
|
key: "pi",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetFloat64(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("Payload.GetFloat64(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetFloat64(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetFloat64(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetFloat64(tc.nonkey)
|
||||||
|
if err == nil || got != 0 {
|
||||||
|
t.Errorf("Payload.GetFloat64(%q) = %v, %v; want 0, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadBool(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"enabled": true},
|
||||||
|
key: "enabled",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetBool(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("Payload.GetBool(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetBool(tc.key)
|
||||||
|
if err != nil || got != tc.data[tc.key] {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetBool(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetBool(tc.nonkey)
|
||||||
|
if err == nil || got != false {
|
||||||
|
t.Errorf("Payload.GetBool(%q) = %v, %v; want false, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringSlice(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"names": []string{"luke", "rey", "anakin"}},
|
||||||
|
key: "names",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetStringSlice(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetStringSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringSlice(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetStringSlice(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetStringSlice(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadIntSlice(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"nums": []int{9, 8, 7}},
|
||||||
|
key: "nums",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetIntSlice(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetIntSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetIntSlice(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetIntSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetIntSlice(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetIntSlice(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringMap(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"user": map[string]interface{}{"name": "Jon Doe", "score": 2.2}},
|
||||||
|
key: "user",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetStringMap(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetStringMap(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringMap(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringMap(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetStringMap(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetStringMap(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringMapString(t *testing.T) {
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"address": map[string]string{"line": "123 Main St", "city": "San Francisco", "state": "CA"}},
|
||||||
|
key: "address",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetStringMapString(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetStringMapString(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringMapString(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringMapString(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetStringMapString(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetStringMapString(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringMapStringSlice(t *testing.T) {
|
||||||
|
favs := map[string][]string{
|
||||||
|
"movies": {"forrest gump", "star wars"},
|
||||||
|
"tv_shows": {"game of thrones", "HIMYM", "breaking bad"},
|
||||||
|
}
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"favorites": favs},
|
||||||
|
key: "favorites",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetStringMapStringSlice(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetStringMapStringSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringMapStringSlice(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringMapStringSlice(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetStringMapStringSlice(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetStringMapStringSlice(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringMapInt(t *testing.T) {
|
||||||
counter := map[string]int{
|
counter := map[string]int{
|
||||||
"a": 1,
|
"a": 1,
|
||||||
"b": 101,
|
"b": 101,
|
||||||
"c": 42,
|
"c": 42,
|
||||||
}
|
}
|
||||||
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
|
data: map[string]interface{}{"counts": counter},
|
||||||
|
key: "counts",
|
||||||
|
nonkey: "unknown",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
payload := Payload{tc.data}
|
||||||
|
|
||||||
|
got, err := payload.GetStringMapInt(tc.key)
|
||||||
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("Payload.GetStringMapInt(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode and then decode task messsage.
|
||||||
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
|
b, err := json.Marshal(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringMapInt(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringMapInt(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
|
// access non-existent key.
|
||||||
|
got, err = payload.GetStringMapInt(tc.nonkey)
|
||||||
|
if err == nil || got != nil {
|
||||||
|
t.Errorf("Payload.GetStringMapInt(%q) = %v, %v; want nil, error",
|
||||||
|
tc.key, got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPayloadStringMapBool(t *testing.T) {
|
||||||
features := map[string]bool{
|
features := map[string]bool{
|
||||||
"A": false,
|
"A": false,
|
||||||
"B": true,
|
"B": true,
|
||||||
"C": true,
|
"C": true,
|
||||||
}
|
}
|
||||||
now := time.Now()
|
tests := []payloadTest{
|
||||||
duration := 15 * time.Minute
|
{
|
||||||
|
data: map[string]interface{}{"features": features},
|
||||||
data := map[string]interface{}{
|
key: "features",
|
||||||
"greeting": "Hello",
|
nonkey: "unknown",
|
||||||
"user_id": 9876,
|
},
|
||||||
"pi": 3.1415,
|
|
||||||
"enabled": false,
|
|
||||||
"names": names,
|
|
||||||
"primes": primes,
|
|
||||||
"user": user,
|
|
||||||
"location": location,
|
|
||||||
"favs": favs,
|
|
||||||
"counter": counter,
|
|
||||||
"features": features,
|
|
||||||
"timestamp": now,
|
|
||||||
"duration": duration,
|
|
||||||
}
|
|
||||||
payload := Payload{data}
|
|
||||||
|
|
||||||
gotStr, err := payload.GetString("greeting")
|
|
||||||
if gotStr != "Hello" || err != nil {
|
|
||||||
t.Errorf("Payload.GetString(%q) = %v, %v, want %v, nil",
|
|
||||||
"greeting", gotStr, err, "Hello")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gotInt, err := payload.GetInt("user_id")
|
for _, tc := range tests {
|
||||||
if gotInt != 9876 || err != nil {
|
payload := Payload{tc.data}
|
||||||
t.Errorf("Payload.GetInt(%q) = %v, %v, want, %v, nil",
|
|
||||||
"user_id", gotInt, err, 9876)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotFloat, err := payload.GetFloat64("pi")
|
got, err := payload.GetStringMapBool(tc.key)
|
||||||
if gotFloat != 3.1415 || err != nil {
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
t.Errorf("Payload.GetFloat64(%q) = %v, %v, want, %v, nil",
|
if err != nil || diff != "" {
|
||||||
"pi", gotFloat, err, 3.141592)
|
t.Errorf("Payload.GetStringMapBool(%q) = %v, %v, want %v, nil",
|
||||||
}
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotBool, err := payload.GetBool("enabled")
|
// encode and then decode task messsage.
|
||||||
if gotBool != false || err != nil {
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
t.Errorf("Payload.GetBool(%q) = %v, %v, want, %v, nil",
|
b, err := json.Marshal(in)
|
||||||
"enabled", gotBool, err, false)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetStringMapBool(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetStringMapBool(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotStrSlice, err := payload.GetStringSlice("names")
|
// access non-existent key.
|
||||||
if diff := cmp.Diff(gotStrSlice, names); diff != "" {
|
got, err = payload.GetStringMapBool(tc.nonkey)
|
||||||
t.Errorf("Payload.GetStringSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
if err == nil || got != nil {
|
||||||
"names", gotStrSlice, err, names, diff)
|
t.Errorf("Payload.GetStringMapBool(%q) = %v, %v; want nil, error",
|
||||||
}
|
tc.key, got, err)
|
||||||
|
}
|
||||||
gotIntSlice, err := payload.GetIntSlice("primes")
|
|
||||||
if diff := cmp.Diff(gotIntSlice, primes); diff != "" {
|
|
||||||
t.Errorf("Payload.GetIntSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"primes", gotIntSlice, err, primes, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMap, err := payload.GetStringMap("user")
|
|
||||||
if diff := cmp.Diff(gotStrMap, user); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMap(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"user", gotStrMap, err, user, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStr, err := payload.GetStringMapString("location")
|
|
||||||
if diff := cmp.Diff(gotStrMapStr, location); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapString(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"location", gotStrMapStr, err, location, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStrSlice, err := payload.GetStringMapStringSlice("favs")
|
|
||||||
if diff := cmp.Diff(gotStrMapStrSlice, favs); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapStringSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"favs", gotStrMapStrSlice, err, favs, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapInt, err := payload.GetStringMapInt("counter")
|
|
||||||
if diff := cmp.Diff(gotStrMapInt, counter); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapInt(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"counter", gotStrMapInt, err, counter, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapBool, err := payload.GetStringMapBool("features")
|
|
||||||
if diff := cmp.Diff(gotStrMapBool, features); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapBool(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"features", gotStrMapBool, err, features, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotTime, err := payload.GetTime("timestamp")
|
|
||||||
if !gotTime.Equal(now) {
|
|
||||||
t.Errorf("Payload.GetTime(%q) = %v, %v, want %v, nil",
|
|
||||||
"timestamp", gotTime, err, now)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotDuration, err := payload.GetDuration("duration")
|
|
||||||
if gotDuration != duration {
|
|
||||||
t.Errorf("Payload.GetDuration(%q) = %v, %v, want %v, nil",
|
|
||||||
"duration", gotDuration, err, duration)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPayloadGetWithMarshaling(t *testing.T) {
|
func TestPayloadTime(t *testing.T) {
|
||||||
names := []string{"luke", "anakin", "rey"}
|
tests := []payloadTest{
|
||||||
primes := []int{2, 3, 5, 7, 11, 13, 17}
|
{
|
||||||
user := map[string]interface{}{"name": "Ken", "score": 3.14}
|
data: map[string]interface{}{"current": time.Now()},
|
||||||
location := map[string]string{"address": "123 Main St.", "state": "NY", "zipcode": "10002"}
|
key: "current",
|
||||||
favs := map[string][]string{
|
nonkey: "unknown",
|
||||||
"movies": []string{"forrest gump", "star wars"},
|
},
|
||||||
"tv_shows": []string{"game of throwns", "HIMYM", "breaking bad"},
|
|
||||||
}
|
|
||||||
counter := map[string]int{
|
|
||||||
"a": 1,
|
|
||||||
"b": 101,
|
|
||||||
"c": 42,
|
|
||||||
}
|
|
||||||
features := map[string]bool{
|
|
||||||
"A": false,
|
|
||||||
"B": true,
|
|
||||||
"C": true,
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
duration := 15 * time.Minute
|
|
||||||
|
|
||||||
in := Payload{map[string]interface{}{
|
|
||||||
"subject": "Hello",
|
|
||||||
"recipient_id": 9876,
|
|
||||||
"pi": 3.14,
|
|
||||||
"enabled": true,
|
|
||||||
"names": names,
|
|
||||||
"primes": primes,
|
|
||||||
"user": user,
|
|
||||||
"location": location,
|
|
||||||
"favs": favs,
|
|
||||||
"counter": counter,
|
|
||||||
"features": features,
|
|
||||||
"timestamp": now,
|
|
||||||
"duration": duration,
|
|
||||||
}}
|
|
||||||
// encode and then decode task messsage
|
|
||||||
inMsg := h.NewTaskMessage("testing", in.data)
|
|
||||||
data, err := json.Marshal(inMsg)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var outMsg base.TaskMessage
|
|
||||||
err = json.Unmarshal(data, &outMsg)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
out := Payload{outMsg.Payload}
|
|
||||||
|
|
||||||
gotStr, err := out.GetString("subject")
|
|
||||||
if gotStr != "Hello" || err != nil {
|
|
||||||
t.Errorf("Payload.GetString(%q) = %v, %v; want %q, nil",
|
|
||||||
"subject", gotStr, err, "Hello")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gotInt, err := out.GetInt("recipient_id")
|
for _, tc := range tests {
|
||||||
if gotInt != 9876 || err != nil {
|
payload := Payload{tc.data}
|
||||||
t.Errorf("Payload.GetInt(%q) = %v, %v; want %v, nil",
|
|
||||||
"recipient_id", gotInt, err, 9876)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotFloat, err := out.GetFloat64("pi")
|
got, err := payload.GetTime(tc.key)
|
||||||
if gotFloat != 3.14 || err != nil {
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
t.Errorf("Payload.GetFloat64(%q) = %v, %v; want %v, nil",
|
if err != nil || diff != "" {
|
||||||
"pi", gotFloat, err, 3.14)
|
t.Errorf("Payload.GetTime(%q) = %v, %v, want %v, nil",
|
||||||
}
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotBool, err := out.GetBool("enabled")
|
// encode and then decode task messsage.
|
||||||
if gotBool != true || err != nil {
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
t.Errorf("Payload.GetBool(%q) = %v, %v; want %v, nil",
|
b, err := json.Marshal(in)
|
||||||
"enabled", gotBool, err, true)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetTime(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetTime(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotStrSlice, err := out.GetStringSlice("names")
|
// access non-existent key.
|
||||||
if diff := cmp.Diff(gotStrSlice, names); diff != "" {
|
got, err = payload.GetTime(tc.nonkey)
|
||||||
t.Errorf("Payload.GetStringSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
if err == nil || !got.IsZero() {
|
||||||
"names", gotStrSlice, err, names, diff)
|
t.Errorf("Payload.GetTime(%q) = %v, %v; want %v, error",
|
||||||
}
|
tc.key, got, err, time.Time{})
|
||||||
|
}
|
||||||
gotIntSlice, err := out.GetIntSlice("primes")
|
|
||||||
if diff := cmp.Diff(gotIntSlice, primes); diff != "" {
|
|
||||||
t.Errorf("Payload.GetIntSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"primes", gotIntSlice, err, primes, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMap, err := out.GetStringMap("user")
|
|
||||||
if diff := cmp.Diff(gotStrMap, user); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMap(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"user", gotStrMap, err, user, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStr, err := out.GetStringMapString("location")
|
|
||||||
if diff := cmp.Diff(gotStrMapStr, location); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapString(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"location", gotStrMapStr, err, location, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStrSlice, err := out.GetStringMapStringSlice("favs")
|
|
||||||
if diff := cmp.Diff(gotStrMapStrSlice, favs); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapStringSlice(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"favs", gotStrMapStrSlice, err, favs, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapInt, err := out.GetStringMapInt("counter")
|
|
||||||
if diff := cmp.Diff(gotStrMapInt, counter); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapInt(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"counter", gotStrMapInt, err, counter, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapBool, err := out.GetStringMapBool("features")
|
|
||||||
if diff := cmp.Diff(gotStrMapBool, features); diff != "" {
|
|
||||||
t.Errorf("Payload.GetStringMapBool(%q) = %v, %v, want %v, nil;\n(-want,+got)\n%s",
|
|
||||||
"features", gotStrMapBool, err, features, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotTime, err := out.GetTime("timestamp")
|
|
||||||
if !gotTime.Equal(now) {
|
|
||||||
t.Errorf("Payload.GetTime(%q) = %v, %v, want %v, nil",
|
|
||||||
"timestamp", gotTime, err, now)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotDuration, err := out.GetDuration("duration")
|
|
||||||
if gotDuration != duration {
|
|
||||||
t.Errorf("Payload.GetDuration(%q) = %v, %v, want %v, nil",
|
|
||||||
"duration", gotDuration, err, duration)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPayloadKeyNotFound(t *testing.T) {
|
func TestPayloadDuration(t *testing.T) {
|
||||||
payload := Payload{nil}
|
tests := []payloadTest{
|
||||||
|
{
|
||||||
key := "something"
|
data: map[string]interface{}{"duration": 15 * time.Minute},
|
||||||
gotStr, err := payload.GetString(key)
|
key: "duration",
|
||||||
if err == nil || gotStr != "" {
|
nonkey: "unknown",
|
||||||
t.Errorf("Payload.GetString(%q) = %v, %v; want '', error",
|
},
|
||||||
key, gotStr, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gotInt, err := payload.GetInt(key)
|
for _, tc := range tests {
|
||||||
if err == nil || gotInt != 0 {
|
payload := Payload{tc.data}
|
||||||
t.Errorf("Payload.GetInt(%q) = %v, %v; want 0, error",
|
|
||||||
key, gotInt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotFloat, err := payload.GetFloat64(key)
|
got, err := payload.GetDuration(tc.key)
|
||||||
if err == nil || gotFloat != 0 {
|
diff := cmp.Diff(got, tc.data[tc.key])
|
||||||
t.Errorf("Payload.GetFloat64(%q = %v, %v; want 0, error",
|
if err != nil || diff != "" {
|
||||||
key, gotFloat, err)
|
t.Errorf("Payload.GetDuration(%q) = %v, %v, want %v, nil",
|
||||||
}
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotBool, err := payload.GetBool(key)
|
// encode and then decode task messsage.
|
||||||
if err == nil || gotBool != false {
|
in := h.NewTaskMessage("testing", tc.data)
|
||||||
t.Errorf("Payload.GetBool(%q) = %v, %v; want false, error",
|
b, err := json.Marshal(in)
|
||||||
key, gotBool, err)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out base.TaskMessage
|
||||||
|
err = json.Unmarshal(b, &out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
payload = Payload{out.Payload}
|
||||||
|
got, err = payload.GetDuration(tc.key)
|
||||||
|
diff = cmp.Diff(got, tc.data[tc.key])
|
||||||
|
if err != nil || diff != "" {
|
||||||
|
t.Errorf("With Marshaling: Payload.GetDuration(%q) = %v, %v, want %v, nil",
|
||||||
|
tc.key, got, err, tc.data[tc.key])
|
||||||
|
}
|
||||||
|
|
||||||
gotStrSlice, err := payload.GetStringSlice(key)
|
// access non-existent key.
|
||||||
if err == nil || gotStrSlice != nil {
|
got, err = payload.GetDuration(tc.nonkey)
|
||||||
t.Errorf("Payload.GetStringSlice(%q) = %v, %v; want nil, error",
|
if err == nil || got != 0 {
|
||||||
key, gotStrSlice, err)
|
t.Errorf("Payload.GetDuration(%q) = %v, %v; want %v, error",
|
||||||
}
|
tc.key, got, err, time.Duration(0))
|
||||||
|
}
|
||||||
gotIntSlice, err := payload.GetIntSlice(key)
|
|
||||||
if err == nil || gotIntSlice != nil {
|
|
||||||
t.Errorf("Payload.GetIntSlice(%q) = %v, %v; want nil, error",
|
|
||||||
key, gotIntSlice, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMap, err := payload.GetStringMap(key)
|
|
||||||
if err == nil || gotStrMap != nil {
|
|
||||||
t.Errorf("Payload.GetStringMap(%q) = %v, %v; want nil, error",
|
|
||||||
key, gotStrMap, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStr, err := payload.GetStringMapString(key)
|
|
||||||
if err == nil || gotStrMapStr != nil {
|
|
||||||
t.Errorf("Payload.GetStringMapString(%q) = %v, %v; want nil, error",
|
|
||||||
key, gotStrMapStr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapStrSlice, err := payload.GetStringMapStringSlice(key)
|
|
||||||
if err == nil || gotStrMapStrSlice != nil {
|
|
||||||
t.Errorf("Payload.GetStringMapStringSlice(%q) = %v, %v; want nil, error",
|
|
||||||
key, gotStrMapStrSlice, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapInt, err := payload.GetStringMapInt(key)
|
|
||||||
if err == nil || gotStrMapInt != nil {
|
|
||||||
t.Errorf("Payload.GetStringMapInt(%q) = %v, %v, want nil, error",
|
|
||||||
key, gotStrMapInt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotStrMapBool, err := payload.GetStringMapBool(key)
|
|
||||||
if err == nil || gotStrMapBool != nil {
|
|
||||||
t.Errorf("Payload.GetStringMapBool(%q) = %v, %v, want nil, error",
|
|
||||||
key, gotStrMapBool, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gotTime, err := payload.GetTime(key)
|
|
||||||
if err == nil || !gotTime.IsZero() {
|
|
||||||
t.Errorf("Payload.GetTime(%q) = %v, %v, want %v, error",
|
|
||||||
key, gotTime, err, time.Time{})
|
|
||||||
}
|
|
||||||
|
|
||||||
gotDuration, err := payload.GetDuration(key)
|
|
||||||
if err == nil || gotDuration != 0 {
|
|
||||||
t.Errorf("Payload.GetDuration(%q) = %v, %v, want 0, error",
|
|
||||||
key, gotDuration, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
112
processor.go
@@ -13,15 +13,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
|
"github.com/hibiken/asynq/internal/log"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
logger Logger
|
logger *log.Logger
|
||||||
rdb *rdb.RDB
|
broker base.Broker
|
||||||
|
|
||||||
ps *base.ProcessState
|
ss *base.ServerState
|
||||||
|
|
||||||
handler Handler
|
handler Handler
|
||||||
|
|
||||||
@@ -34,6 +35,8 @@ type processor struct {
|
|||||||
|
|
||||||
errHandler ErrorHandler
|
errHandler ErrorHandler
|
||||||
|
|
||||||
|
shutdownTimeout time.Duration
|
||||||
|
|
||||||
// channel via which to send sync requests to syncer.
|
// channel via which to send sync requests to syncer.
|
||||||
syncRequestCh chan<- *syncRequest
|
syncRequestCh chan<- *syncRequest
|
||||||
|
|
||||||
@@ -61,30 +64,40 @@ type processor struct {
|
|||||||
|
|
||||||
type retryDelayFunc func(n int, err error, task *Task) time.Duration
|
type retryDelayFunc func(n int, err error, task *Task) time.Duration
|
||||||
|
|
||||||
|
type newProcessorParams struct {
|
||||||
|
logger *log.Logger
|
||||||
|
broker base.Broker
|
||||||
|
ss *base.ServerState
|
||||||
|
retryDelayFunc retryDelayFunc
|
||||||
|
syncCh chan<- *syncRequest
|
||||||
|
cancelations *base.Cancelations
|
||||||
|
errHandler ErrorHandler
|
||||||
|
shutdownTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
// newProcessor constructs a new processor.
|
// newProcessor constructs a new processor.
|
||||||
func newProcessor(l Logger, r *rdb.RDB, ps *base.ProcessState, fn retryDelayFunc,
|
func newProcessor(params newProcessorParams) *processor {
|
||||||
syncCh chan<- *syncRequest, c *base.Cancelations, errHandler ErrorHandler) *processor {
|
info := params.ss.GetInfo()
|
||||||
info := ps.Get()
|
|
||||||
qcfg := normalizeQueueCfg(info.Queues)
|
qcfg := normalizeQueueCfg(info.Queues)
|
||||||
orderedQueues := []string(nil)
|
orderedQueues := []string(nil)
|
||||||
if info.StrictPriority {
|
if info.StrictPriority {
|
||||||
orderedQueues = sortByPriority(qcfg)
|
orderedQueues = sortByPriority(qcfg)
|
||||||
}
|
}
|
||||||
return &processor{
|
return &processor{
|
||||||
logger: l,
|
logger: params.logger,
|
||||||
rdb: r,
|
broker: params.broker,
|
||||||
ps: ps,
|
ss: params.ss,
|
||||||
queueConfig: qcfg,
|
queueConfig: qcfg,
|
||||||
orderedQueues: orderedQueues,
|
orderedQueues: orderedQueues,
|
||||||
retryDelayFunc: fn,
|
retryDelayFunc: params.retryDelayFunc,
|
||||||
syncRequestCh: syncCh,
|
syncRequestCh: params.syncCh,
|
||||||
cancelations: c,
|
cancelations: params.cancelations,
|
||||||
errLogLimiter: rate.NewLimiter(rate.Every(3*time.Second), 1),
|
errLogLimiter: rate.NewLimiter(rate.Every(3*time.Second), 1),
|
||||||
sema: make(chan struct{}, info.Concurrency),
|
sema: make(chan struct{}, info.Concurrency),
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
abort: make(chan struct{}),
|
abort: make(chan struct{}),
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
errHandler: errHandler,
|
errHandler: params.errHandler,
|
||||||
handler: HandlerFunc(func(ctx context.Context, t *Task) error { return fmt.Errorf("handler not set") }),
|
handler: HandlerFunc(func(ctx context.Context, t *Task) error { return fmt.Errorf("handler not set") }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,7 +106,7 @@ func newProcessor(l Logger, r *rdb.RDB, ps *base.ProcessState, fn retryDelayFunc
|
|||||||
// It's safe to call this method multiple times.
|
// It's safe to call this method multiple times.
|
||||||
func (p *processor) stop() {
|
func (p *processor) stop() {
|
||||||
p.once.Do(func() {
|
p.once.Do(func() {
|
||||||
p.logger.Info("Processor shutting down...")
|
p.logger.Debug("Processor shutting down...")
|
||||||
// Unblock if processor is waiting for sema token.
|
// Unblock if processor is waiting for sema token.
|
||||||
close(p.abort)
|
close(p.abort)
|
||||||
// Signal the processor goroutine to stop processing tasks
|
// Signal the processor goroutine to stop processing tasks
|
||||||
@@ -106,9 +119,7 @@ func (p *processor) stop() {
|
|||||||
func (p *processor) terminate() {
|
func (p *processor) terminate() {
|
||||||
p.stop()
|
p.stop()
|
||||||
|
|
||||||
// IDEA: Allow user to customize this timeout value.
|
time.AfterFunc(p.shutdownTimeout, func() { close(p.quit) })
|
||||||
const timeout = 8 * time.Second
|
|
||||||
time.AfterFunc(timeout, func() { close(p.quit) })
|
|
||||||
p.logger.Info("Waiting for all workers to finish...")
|
p.logger.Info("Waiting for all workers to finish...")
|
||||||
|
|
||||||
// send cancellation signal to all in-progress task handlers
|
// send cancellation signal to all in-progress task handlers
|
||||||
@@ -134,7 +145,7 @@ func (p *processor) start(wg *sync.WaitGroup) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-p.done:
|
case <-p.done:
|
||||||
p.logger.Info("Processor done")
|
p.logger.Debug("Processor done")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
p.exec()
|
p.exec()
|
||||||
@@ -147,20 +158,21 @@ func (p *processor) start(wg *sync.WaitGroup) {
|
|||||||
// process the task.
|
// process the task.
|
||||||
func (p *processor) exec() {
|
func (p *processor) exec() {
|
||||||
qnames := p.queues()
|
qnames := p.queues()
|
||||||
msg, err := p.rdb.Dequeue(qnames...)
|
msg, err := p.broker.Dequeue(qnames...)
|
||||||
if err == rdb.ErrNoProcessableTask {
|
switch {
|
||||||
|
case err == rdb.ErrNoProcessableTask:
|
||||||
// queues are empty, this is a normal behavior.
|
// queues are empty, this is a normal behavior.
|
||||||
if len(p.queueConfig) > 1 {
|
if len(qnames) > 1 {
|
||||||
// sleep to avoid slamming redis and let scheduler move tasks into queues.
|
// sleep to avoid slamming redis and let scheduler move tasks into queues.
|
||||||
// Note: With multiple queues, we are not using blocking pop operation and
|
// Note: With multiple queues, we are not using blocking pop operation and
|
||||||
// polling queues instead. This adds significant load to redis.
|
// polling queues instead. This adds significant load to redis.
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
|
p.logger.Debug("All queues are empty")
|
||||||
return
|
return
|
||||||
}
|
case err != nil:
|
||||||
if err != nil {
|
|
||||||
if p.errLogLimiter.Allow() {
|
if p.errLogLimiter.Allow() {
|
||||||
p.logger.Error("Dequeue error: %v", err)
|
p.logger.Errorf("Dequeue error: %v", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -171,26 +183,28 @@ func (p *processor) exec() {
|
|||||||
p.requeue(msg)
|
p.requeue(msg)
|
||||||
return
|
return
|
||||||
case p.sema <- struct{}{}: // acquire token
|
case p.sema <- struct{}{}: // acquire token
|
||||||
p.ps.AddWorkerStats(msg, time.Now())
|
p.ss.AddWorkerStats(msg, time.Now())
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
p.ps.DeleteWorkerStats(msg)
|
p.ss.DeleteWorkerStats(msg)
|
||||||
<-p.sema /* release token */
|
<-p.sema // release token
|
||||||
|
}()
|
||||||
|
|
||||||
|
ctx, cancel := createContext(msg)
|
||||||
|
p.cancelations.Add(msg.ID.String(), cancel)
|
||||||
|
defer func() {
|
||||||
|
cancel()
|
||||||
|
p.cancelations.Delete(msg.ID.String())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
resCh := make(chan error, 1)
|
resCh := make(chan error, 1)
|
||||||
task := NewTask(msg.Type, msg.Payload)
|
task := NewTask(msg.Type, msg.Payload)
|
||||||
ctx, cancel := createContext(msg)
|
go func() { resCh <- perform(ctx, task, p.handler) }()
|
||||||
p.cancelations.Add(msg.ID.String(), cancel)
|
|
||||||
go func() {
|
|
||||||
resCh <- perform(ctx, task, p.handler)
|
|
||||||
p.cancelations.Delete(msg.ID.String())
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-p.quit:
|
case <-p.quit:
|
||||||
// time is up, quit this worker goroutine.
|
// time is up, quit this worker goroutine.
|
||||||
p.logger.Warn("Quitting worker. task id=%s", msg.ID)
|
p.logger.Warnf("Quitting worker. task id=%s", msg.ID)
|
||||||
return
|
return
|
||||||
case resErr := <-resCh:
|
case resErr := <-resCh:
|
||||||
// Note: One of three things should happen.
|
// Note: One of three things should happen.
|
||||||
@@ -217,30 +231,30 @@ func (p *processor) exec() {
|
|||||||
// restore moves all tasks from "in-progress" back to queue
|
// restore moves all tasks from "in-progress" back to queue
|
||||||
// to restore all unfinished tasks.
|
// to restore all unfinished tasks.
|
||||||
func (p *processor) restore() {
|
func (p *processor) restore() {
|
||||||
n, err := p.rdb.RequeueAll()
|
n, err := p.broker.RequeueAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Could not restore unfinished tasks: %v", err)
|
p.logger.Errorf("Could not restore unfinished tasks: %v", err)
|
||||||
}
|
}
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
p.logger.Info("Restored %d unfinished tasks back to queue", n)
|
p.logger.Infof("Restored %d unfinished tasks back to queue", n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) requeue(msg *base.TaskMessage) {
|
func (p *processor) requeue(msg *base.TaskMessage) {
|
||||||
err := p.rdb.Requeue(msg)
|
err := p.broker.Requeue(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Could not push task id=%s back to queue: %v", msg.ID, err)
|
p.logger.Errorf("Could not push task id=%s back to queue: %v", msg.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) markAsDone(msg *base.TaskMessage) {
|
func (p *processor) markAsDone(msg *base.TaskMessage) {
|
||||||
err := p.rdb.Done(msg)
|
err := p.broker.Done(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := fmt.Sprintf("Could not remove task id=%s from %q", msg.ID, base.InProgressQueue)
|
errMsg := fmt.Sprintf("Could not remove task id=%s from %q", msg.ID, base.InProgressQueue)
|
||||||
p.logger.Warn("%s; Will retry syncing", errMsg)
|
p.logger.Warnf("%s; Will retry syncing", errMsg)
|
||||||
p.syncRequestCh <- &syncRequest{
|
p.syncRequestCh <- &syncRequest{
|
||||||
fn: func() error {
|
fn: func() error {
|
||||||
return p.rdb.Done(msg)
|
return p.broker.Done(msg)
|
||||||
},
|
},
|
||||||
errMsg: errMsg,
|
errMsg: errMsg,
|
||||||
}
|
}
|
||||||
@@ -250,13 +264,13 @@ func (p *processor) markAsDone(msg *base.TaskMessage) {
|
|||||||
func (p *processor) retry(msg *base.TaskMessage, e error) {
|
func (p *processor) retry(msg *base.TaskMessage, e error) {
|
||||||
d := p.retryDelayFunc(msg.Retried, e, NewTask(msg.Type, msg.Payload))
|
d := p.retryDelayFunc(msg.Retried, e, NewTask(msg.Type, msg.Payload))
|
||||||
retryAt := time.Now().Add(d)
|
retryAt := time.Now().Add(d)
|
||||||
err := p.rdb.Retry(msg, retryAt, e.Error())
|
err := p.broker.Retry(msg, retryAt, e.Error())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := fmt.Sprintf("Could not move task id=%s from %q to %q", msg.ID, base.InProgressQueue, base.RetryQueue)
|
errMsg := fmt.Sprintf("Could not move task id=%s from %q to %q", msg.ID, base.InProgressQueue, base.RetryQueue)
|
||||||
p.logger.Warn("%s; Will retry syncing", errMsg)
|
p.logger.Warnf("%s; Will retry syncing", errMsg)
|
||||||
p.syncRequestCh <- &syncRequest{
|
p.syncRequestCh <- &syncRequest{
|
||||||
fn: func() error {
|
fn: func() error {
|
||||||
return p.rdb.Retry(msg, retryAt, e.Error())
|
return p.broker.Retry(msg, retryAt, e.Error())
|
||||||
},
|
},
|
||||||
errMsg: errMsg,
|
errMsg: errMsg,
|
||||||
}
|
}
|
||||||
@@ -264,14 +278,14 @@ func (p *processor) retry(msg *base.TaskMessage, e error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) kill(msg *base.TaskMessage, e error) {
|
func (p *processor) kill(msg *base.TaskMessage, e error) {
|
||||||
p.logger.Warn("Retry exhausted for task id=%s", msg.ID)
|
p.logger.Warnf("Retry exhausted for task id=%s", msg.ID)
|
||||||
err := p.rdb.Kill(msg, e.Error())
|
err := p.broker.Kill(msg, e.Error())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := fmt.Sprintf("Could not move task id=%s from %q to %q", msg.ID, base.InProgressQueue, base.DeadQueue)
|
errMsg := fmt.Sprintf("Could not move task id=%s from %q to %q", msg.ID, base.InProgressQueue, base.DeadQueue)
|
||||||
p.logger.Warn("%s; Will retry syncing", errMsg)
|
p.logger.Warnf("%s; Will retry syncing", errMsg)
|
||||||
p.syncRequestCh <- &syncRequest{
|
p.syncRequestCh <- &syncRequest{
|
||||||
fn: func() error {
|
fn: func() error {
|
||||||
return p.rdb.Kill(msg, e.Error())
|
return p.broker.Kill(msg, e.Error())
|
||||||
},
|
},
|
||||||
errMsg: errMsg,
|
errMsg: errMsg,
|
||||||
}
|
}
|
||||||
|
@@ -37,19 +37,16 @@ func TestProcessorSuccess(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
enqueued []*base.TaskMessage // initial default queue state
|
enqueued []*base.TaskMessage // initial default queue state
|
||||||
incoming []*base.TaskMessage // tasks to be enqueued during run
|
incoming []*base.TaskMessage // tasks to be enqueued during run
|
||||||
wait time.Duration // wait duration between starting and stopping processor for this test case
|
|
||||||
wantProcessed []*Task // tasks to be processed at the end
|
wantProcessed []*Task // tasks to be processed at the end
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
enqueued: []*base.TaskMessage{m1},
|
enqueued: []*base.TaskMessage{m1},
|
||||||
incoming: []*base.TaskMessage{m2, m3, m4},
|
incoming: []*base.TaskMessage{m2, m3, m4},
|
||||||
wait: time.Second,
|
|
||||||
wantProcessed: []*Task{t1, t2, t3, t4},
|
wantProcessed: []*Task{t1, t2, t3, t4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
enqueued: []*base.TaskMessage{},
|
enqueued: []*base.TaskMessage{},
|
||||||
incoming: []*base.TaskMessage{m1},
|
incoming: []*base.TaskMessage{m1},
|
||||||
wait: time.Second,
|
|
||||||
wantProcessed: []*Task{t1},
|
wantProcessed: []*Task{t1},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -67,13 +64,20 @@ func TestProcessorSuccess(t *testing.T) {
|
|||||||
processed = append(processed, task)
|
processed = append(processed, task)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ps := base.NewProcessState("localhost", 1234, 10, defaultQueueConfig, false)
|
ss := base.NewServerState("localhost", 1234, 10, defaultQueueConfig, false)
|
||||||
cancelations := base.NewCancelations()
|
p := newProcessor(newProcessorParams{
|
||||||
p := newProcessor(testLogger, rdbClient, ps, defaultDelayFunc, nil, cancelations, nil)
|
logger: testLogger,
|
||||||
|
broker: rdbClient,
|
||||||
|
ss: ss,
|
||||||
|
retryDelayFunc: defaultDelayFunc,
|
||||||
|
syncCh: nil,
|
||||||
|
cancelations: base.NewCancelations(),
|
||||||
|
errHandler: nil,
|
||||||
|
shutdownTimeout: defaultShutdownTimeout,
|
||||||
|
})
|
||||||
p.handler = HandlerFunc(handler)
|
p.handler = HandlerFunc(handler)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
p.start(&sync.WaitGroup{})
|
||||||
p.start(&wg)
|
|
||||||
for _, msg := range tc.incoming {
|
for _, msg := range tc.incoming {
|
||||||
err := rdbClient.Enqueue(msg)
|
err := rdbClient.Enqueue(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -81,7 +85,7 @@ func TestProcessorSuccess(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(tc.wait)
|
time.Sleep(time.Second) // wait for one second to allow all enqueued tasks to be processed.
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
|
||||||
if diff := cmp.Diff(tc.wantProcessed, processed, sortTaskOpt, cmp.AllowUnexported(Payload{})); diff != "" {
|
if diff := cmp.Diff(tc.wantProcessed, processed, sortTaskOpt, cmp.AllowUnexported(Payload{})); diff != "" {
|
||||||
@@ -165,13 +169,20 @@ func TestProcessorRetry(t *testing.T) {
|
|||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
ps := base.NewProcessState("localhost", 1234, 10, defaultQueueConfig, false)
|
ss := base.NewServerState("localhost", 1234, 10, defaultQueueConfig, false)
|
||||||
cancelations := base.NewCancelations()
|
p := newProcessor(newProcessorParams{
|
||||||
p := newProcessor(testLogger, rdbClient, ps, delayFunc, nil, cancelations, ErrorHandlerFunc(errHandler))
|
logger: testLogger,
|
||||||
|
broker: rdbClient,
|
||||||
|
ss: ss,
|
||||||
|
retryDelayFunc: delayFunc,
|
||||||
|
syncCh: nil,
|
||||||
|
cancelations: base.NewCancelations(),
|
||||||
|
errHandler: ErrorHandlerFunc(errHandler),
|
||||||
|
shutdownTimeout: defaultShutdownTimeout,
|
||||||
|
})
|
||||||
p.handler = tc.handler
|
p.handler = tc.handler
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
p.start(&sync.WaitGroup{})
|
||||||
p.start(&wg)
|
|
||||||
for _, msg := range tc.incoming {
|
for _, msg := range tc.incoming {
|
||||||
err := rdbClient.Enqueue(msg)
|
err := rdbClient.Enqueue(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -182,7 +193,7 @@ func TestProcessorRetry(t *testing.T) {
|
|||||||
time.Sleep(tc.wait)
|
time.Sleep(tc.wait)
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
|
||||||
cmpOpt := cmpopts.EquateApprox(0, float64(time.Second)) // allow up to second difference in zset score
|
cmpOpt := cmpopts.EquateApprox(0, float64(time.Second)) // allow up to a second difference in zset score
|
||||||
gotRetry := h.GetRetryEntries(t, r)
|
gotRetry := h.GetRetryEntries(t, r)
|
||||||
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt, cmpOpt); diff != "" {
|
if diff := cmp.Diff(tc.wantRetry, gotRetry, h.SortZSetEntryOpt, cmpOpt); diff != "" {
|
||||||
t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.RetryQueue, diff)
|
t.Errorf("mismatch found in %q after running processor; (-want, +got)\n%s", base.RetryQueue, diff)
|
||||||
@@ -231,9 +242,17 @@ func TestProcessorQueues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
cancelations := base.NewCancelations()
|
ss := base.NewServerState("localhost", 1234, 10, tc.queueCfg, false)
|
||||||
ps := base.NewProcessState("localhost", 1234, 10, tc.queueCfg, false)
|
p := newProcessor(newProcessorParams{
|
||||||
p := newProcessor(testLogger, nil, ps, defaultDelayFunc, nil, cancelations, nil)
|
logger: testLogger,
|
||||||
|
broker: nil,
|
||||||
|
ss: ss,
|
||||||
|
retryDelayFunc: defaultDelayFunc,
|
||||||
|
syncCh: nil,
|
||||||
|
cancelations: base.NewCancelations(),
|
||||||
|
errHandler: nil,
|
||||||
|
shutdownTimeout: defaultShutdownTimeout,
|
||||||
|
})
|
||||||
got := p.queues()
|
got := p.queues()
|
||||||
if diff := cmp.Diff(tc.want, got, sortOpt); diff != "" {
|
if diff := cmp.Diff(tc.want, got, sortOpt); diff != "" {
|
||||||
t.Errorf("with queue config: %v\n(*processor).queues() = %v, want %v\n(-want,+got):\n%s",
|
t.Errorf("with queue config: %v\n(*processor).queues() = %v, want %v\n(-want,+got):\n%s",
|
||||||
@@ -299,13 +318,20 @@ func TestProcessorWithStrictPriority(t *testing.T) {
|
|||||||
"low": 1,
|
"low": 1,
|
||||||
}
|
}
|
||||||
// Note: Set concurrency to 1 to make sure tasks are processed one at a time.
|
// Note: Set concurrency to 1 to make sure tasks are processed one at a time.
|
||||||
cancelations := base.NewCancelations()
|
ss := base.NewServerState("localhost", 1234, 1 /* concurrency */, queueCfg, true /*strict*/)
|
||||||
ps := base.NewProcessState("localhost", 1234, 1 /* concurrency */, queueCfg, true /*strict*/)
|
p := newProcessor(newProcessorParams{
|
||||||
p := newProcessor(testLogger, rdbClient, ps, defaultDelayFunc, nil, cancelations, nil)
|
logger: testLogger,
|
||||||
|
broker: rdbClient,
|
||||||
|
ss: ss,
|
||||||
|
retryDelayFunc: defaultDelayFunc,
|
||||||
|
syncCh: nil,
|
||||||
|
cancelations: base.NewCancelations(),
|
||||||
|
errHandler: nil,
|
||||||
|
shutdownTimeout: defaultShutdownTimeout,
|
||||||
|
})
|
||||||
p.handler = HandlerFunc(handler)
|
p.handler = HandlerFunc(handler)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
p.start(&sync.WaitGroup{})
|
||||||
p.start(&wg)
|
|
||||||
time.Sleep(tc.wait)
|
time.Sleep(tc.wait)
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
|
||||||
@@ -446,3 +472,83 @@ func TestCreateContextWithoutTimeRestrictions(t *testing.T) {
|
|||||||
t.Error("ctx.Done() blocked, want it to be non-blocking")
|
t.Error("ctx.Done() blocked, want it to be non-blocking")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGCD(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input []int
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{[]int{6, 2, 12}, 2},
|
||||||
|
{[]int{3, 3, 3}, 3},
|
||||||
|
{[]int{6, 3, 1}, 1},
|
||||||
|
{[]int{1}, 1},
|
||||||
|
{[]int{1, 0, 2}, 1},
|
||||||
|
{[]int{8, 0, 4}, 4},
|
||||||
|
{[]int{9, 12, 18, 30}, 3},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got := gcd(tc.input...)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("gcd(%v) = %d, want %d", tc.input, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeQueueCfg(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input map[string]int
|
||||||
|
want map[string]int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: map[string]int{
|
||||||
|
"high": 100,
|
||||||
|
"default": 20,
|
||||||
|
"low": 5,
|
||||||
|
},
|
||||||
|
want: map[string]int{
|
||||||
|
"high": 20,
|
||||||
|
"default": 4,
|
||||||
|
"low": 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: map[string]int{
|
||||||
|
"default": 10,
|
||||||
|
},
|
||||||
|
want: map[string]int{
|
||||||
|
"default": 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: map[string]int{
|
||||||
|
"critical": 5,
|
||||||
|
"default": 1,
|
||||||
|
},
|
||||||
|
want: map[string]int{
|
||||||
|
"critical": 5,
|
||||||
|
"default": 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: map[string]int{
|
||||||
|
"critical": 6,
|
||||||
|
"default": 3,
|
||||||
|
"low": 0,
|
||||||
|
},
|
||||||
|
want: map[string]int{
|
||||||
|
"critical": 2,
|
||||||
|
"default": 1,
|
||||||
|
"low": 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got := normalizeQueueCfg(tc.input)
|
||||||
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||||
|
t.Errorf("normalizeQueueCfg(%v) = %v, want %v; (-want, +got):\n%s",
|
||||||
|
tc.input, got, tc.want, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
19
scheduler.go
@@ -8,12 +8,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
|
"github.com/hibiken/asynq/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type scheduler struct {
|
type scheduler struct {
|
||||||
logger Logger
|
logger *log.Logger
|
||||||
rdb *rdb.RDB
|
broker base.Broker
|
||||||
|
|
||||||
// channel to communicate back to the long running "scheduler" goroutine.
|
// channel to communicate back to the long running "scheduler" goroutine.
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
@@ -25,14 +26,14 @@ type scheduler struct {
|
|||||||
qnames []string
|
qnames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newScheduler(l Logger, r *rdb.RDB, avgInterval time.Duration, qcfg map[string]int) *scheduler {
|
func newScheduler(l *log.Logger, b base.Broker, avgInterval time.Duration, qcfg map[string]int) *scheduler {
|
||||||
var qnames []string
|
var qnames []string
|
||||||
for q := range qcfg {
|
for q := range qcfg {
|
||||||
qnames = append(qnames, q)
|
qnames = append(qnames, q)
|
||||||
}
|
}
|
||||||
return &scheduler{
|
return &scheduler{
|
||||||
logger: l,
|
logger: l,
|
||||||
rdb: r,
|
broker: b,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
avgInterval: avgInterval,
|
avgInterval: avgInterval,
|
||||||
qnames: qnames,
|
qnames: qnames,
|
||||||
@@ -40,7 +41,7 @@ func newScheduler(l Logger, r *rdb.RDB, avgInterval time.Duration, qcfg map[stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *scheduler) terminate() {
|
func (s *scheduler) terminate() {
|
||||||
s.logger.Info("Scheduler shutting down...")
|
s.logger.Debug("Scheduler shutting down...")
|
||||||
// Signal the scheduler goroutine to stop polling.
|
// Signal the scheduler goroutine to stop polling.
|
||||||
s.done <- struct{}{}
|
s.done <- struct{}{}
|
||||||
}
|
}
|
||||||
@@ -53,7 +54,7 @@ func (s *scheduler) start(wg *sync.WaitGroup) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.done:
|
case <-s.done:
|
||||||
s.logger.Info("Scheduler done")
|
s.logger.Debug("Scheduler done")
|
||||||
return
|
return
|
||||||
case <-time.After(s.avgInterval):
|
case <-time.After(s.avgInterval):
|
||||||
s.exec()
|
s.exec()
|
||||||
@@ -63,7 +64,7 @@ func (s *scheduler) start(wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *scheduler) exec() {
|
func (s *scheduler) exec() {
|
||||||
if err := s.rdb.CheckAndEnqueue(s.qnames...); err != nil {
|
if err := s.broker.CheckAndEnqueue(s.qnames...); err != nil {
|
||||||
s.logger.Error("Could not enqueue scheduled tasks: %v", err)
|
s.logger.Errorf("Could not enqueue scheduled tasks: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
servemux.go
@@ -23,9 +23,10 @@ import (
|
|||||||
// "images:thumbnails" and the former will receive tasks with type name beginning
|
// "images:thumbnails" and the former will receive tasks with type name beginning
|
||||||
// with "images".
|
// with "images".
|
||||||
type ServeMux struct {
|
type ServeMux struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
m map[string]muxEntry
|
m map[string]muxEntry
|
||||||
es []muxEntry // slice of entries sorted from longest to shortest.
|
es []muxEntry // slice of entries sorted from longest to shortest.
|
||||||
|
mws []MiddlewareFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
type muxEntry struct {
|
type muxEntry struct {
|
||||||
@@ -33,6 +34,11 @@ type muxEntry struct {
|
|||||||
pattern string
|
pattern string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MiddlewareFunc is a function which receives an asynq.Handler and returns another asynq.Handler.
|
||||||
|
// Typically, the returned handler is a closure which does something with the context and task passed
|
||||||
|
// to it, and then calls the handler passed as parameter to the MiddlewareFunc.
|
||||||
|
type MiddlewareFunc func(Handler) Handler
|
||||||
|
|
||||||
// NewServeMux allocates and returns a new ServeMux.
|
// NewServeMux allocates and returns a new ServeMux.
|
||||||
func NewServeMux() *ServeMux {
|
func NewServeMux() *ServeMux {
|
||||||
return new(ServeMux)
|
return new(ServeMux)
|
||||||
@@ -60,6 +66,9 @@ func (mux *ServeMux) Handler(t *Task) (h Handler, pattern string) {
|
|||||||
if h == nil {
|
if h == nil {
|
||||||
h, pattern = NotFoundHandler(), ""
|
h, pattern = NotFoundHandler(), ""
|
||||||
}
|
}
|
||||||
|
for i := len(mux.mws) - 1; i >= 0; i-- {
|
||||||
|
h = mux.mws[i](h)
|
||||||
|
}
|
||||||
return h, pattern
|
return h, pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +139,16 @@ func (mux *ServeMux) HandleFunc(pattern string, handler func(context.Context, *T
|
|||||||
mux.Handle(pattern, HandlerFunc(handler))
|
mux.Handle(pattern, HandlerFunc(handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use appends a MiddlewareFunc to the chain.
|
||||||
|
// Middlewares are executed in the order that they are applied to the ServeMux.
|
||||||
|
func (mux *ServeMux) Use(mws ...MiddlewareFunc) {
|
||||||
|
mux.mu.Lock()
|
||||||
|
defer mux.mu.Unlock()
|
||||||
|
for _, fn := range mws {
|
||||||
|
mux.mws = append(mux.mws, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NotFound returns an error indicating that the handler was not found for the given task.
|
// NotFound returns an error indicating that the handler was not found for the given task.
|
||||||
func NotFound(ctx context.Context, task *Task) error {
|
func NotFound(ctx context.Context, task *Task) error {
|
||||||
return fmt.Errorf("handler not found for task %q", task.Type)
|
return fmt.Errorf("handler not found for task %q", task.Type)
|
||||||
|
@@ -7,9 +7,12 @@ package asynq
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var called string
|
var called string // identity of the handler that was called.
|
||||||
|
var invoked []string // list of middlewares in the order they were invoked.
|
||||||
|
|
||||||
// makeFakeHandler returns a handler that updates the global called variable
|
// makeFakeHandler returns a handler that updates the global called variable
|
||||||
// to the given identity.
|
// to the given identity.
|
||||||
@@ -20,6 +23,17 @@ func makeFakeHandler(identity string) Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// makeFakeMiddleware returns a middleware function that appends the given identity
|
||||||
|
//to the global invoked slice.
|
||||||
|
func makeFakeMiddleware(identity string) MiddlewareFunc {
|
||||||
|
return func(next Handler) Handler {
|
||||||
|
return HandlerFunc(func(ctx context.Context, t *Task) error {
|
||||||
|
invoked = append(invoked, identity)
|
||||||
|
return next.ProcessTask(ctx, t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A list of pattern, handler pair that is registered with mux.
|
// A list of pattern, handler pair that is registered with mux.
|
||||||
var serveMuxRegister = []struct {
|
var serveMuxRegister = []struct {
|
||||||
pattern string
|
pattern string
|
||||||
@@ -114,3 +128,43 @@ func TestServeMuxNotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var middlewareTests = []struct {
|
||||||
|
typename string // task's type name
|
||||||
|
middlewares []string // middlewares to use. They should be called in this order.
|
||||||
|
want string // identifier of the handler that should be called
|
||||||
|
}{
|
||||||
|
{"email:signup", []string{"logging", "expiration"}, "signup email handler"},
|
||||||
|
{"csv:export", []string{}, "csv export handler"},
|
||||||
|
{"email:daily", []string{"expiration", "logging"}, "default email handler"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServeMuxMiddlewares(t *testing.T) {
|
||||||
|
for _, tc := range middlewareTests {
|
||||||
|
mux := NewServeMux()
|
||||||
|
for _, e := range serveMuxRegister {
|
||||||
|
mux.Handle(e.pattern, e.h)
|
||||||
|
}
|
||||||
|
var mws []MiddlewareFunc
|
||||||
|
for _, s := range tc.middlewares {
|
||||||
|
mws = append(mws, makeFakeMiddleware(s))
|
||||||
|
}
|
||||||
|
mux.Use(mws...)
|
||||||
|
|
||||||
|
invoked = []string{} // reset to empty slice
|
||||||
|
called = "" // reset to zero value
|
||||||
|
|
||||||
|
task := NewTask(tc.typename, nil)
|
||||||
|
if err := mux.ProcessTask(context.Background(), task); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(invoked, tc.middlewares); diff != "" {
|
||||||
|
t.Errorf("invoked middlewares were %v, want %v", invoked, tc.middlewares)
|
||||||
|
}
|
||||||
|
|
||||||
|
if called != tc.want {
|
||||||
|
t.Errorf("%q handler was called for task %q, want %q to be called", called, task.Type, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
429
server.go
Normal file
@@ -0,0 +1,429 @@
|
|||||||
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT license
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package asynq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq/internal/base"
|
||||||
|
"github.com/hibiken/asynq/internal/log"
|
||||||
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Server is responsible for managing the background-task processing.
|
||||||
|
//
|
||||||
|
// Server pulls tasks off queues and processes them.
|
||||||
|
// If the processing of a task is unsuccessful, server will
|
||||||
|
// schedule it for a retry.
|
||||||
|
// A task will be retried until either the task gets processed successfully
|
||||||
|
// or until it reaches its max retry count.
|
||||||
|
//
|
||||||
|
// If a task exhausts its retries, it will be moved to the "dead" queue and
|
||||||
|
// will be kept in the queue for some time until a certain condition is met
|
||||||
|
// (e.g., queue size reaches a certain limit, or the task has been in the
|
||||||
|
// queue for a certain amount of time).
|
||||||
|
type Server struct {
|
||||||
|
ss *base.ServerState
|
||||||
|
|
||||||
|
logger *log.Logger
|
||||||
|
|
||||||
|
broker base.Broker
|
||||||
|
|
||||||
|
// wait group to wait for all goroutines to finish.
|
||||||
|
wg sync.WaitGroup
|
||||||
|
scheduler *scheduler
|
||||||
|
processor *processor
|
||||||
|
syncer *syncer
|
||||||
|
heartbeater *heartbeater
|
||||||
|
subscriber *subscriber
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config specifies the server's background-task processing behavior.
|
||||||
|
type Config struct {
|
||||||
|
// Maximum number of concurrent processing of tasks.
|
||||||
|
//
|
||||||
|
// If set to a zero or negative value, NewServer will overwrite the value
|
||||||
|
// to the number of CPUs usable by the currennt process.
|
||||||
|
Concurrency int
|
||||||
|
|
||||||
|
// Function to calculate retry delay for a failed task.
|
||||||
|
//
|
||||||
|
// By default, it uses exponential backoff algorithm to calculate the delay.
|
||||||
|
//
|
||||||
|
// n is the number of times the task has been retried.
|
||||||
|
// e is the error returned by the task handler.
|
||||||
|
// t is the task in question.
|
||||||
|
RetryDelayFunc func(n int, e error, t *Task) time.Duration
|
||||||
|
|
||||||
|
// List of queues to process with given priority value. Keys are the names of the
|
||||||
|
// queues and values are associated priority value.
|
||||||
|
//
|
||||||
|
// If set to nil or not specified, the server will process only the "default" queue.
|
||||||
|
//
|
||||||
|
// Priority is treated as follows to avoid starving low priority queues.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
// Queues: map[string]int{
|
||||||
|
// "critical": 6,
|
||||||
|
// "default": 3,
|
||||||
|
// "low": 1,
|
||||||
|
// }
|
||||||
|
// With the above config and given that all queues are not empty, the tasks
|
||||||
|
// in "critical", "default", "low" should be processed 60%, 30%, 10% of
|
||||||
|
// the time respectively.
|
||||||
|
//
|
||||||
|
// If a queue has a zero or negative priority value, the queue will be ignored.
|
||||||
|
Queues map[string]int
|
||||||
|
|
||||||
|
// StrictPriority indicates whether the queue priority should be treated strictly.
|
||||||
|
//
|
||||||
|
// If set to true, tasks in the queue with the highest priority is processed first.
|
||||||
|
// The tasks in lower priority queues are processed only when those queues with
|
||||||
|
// higher priorities are empty.
|
||||||
|
StrictPriority bool
|
||||||
|
|
||||||
|
// ErrorHandler handles errors returned by the task handler.
|
||||||
|
//
|
||||||
|
// HandleError is invoked only if the task handler returns a non-nil error.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
// func reportError(task *asynq.Task, err error, retried, maxRetry int) {
|
||||||
|
// if retried >= maxRetry {
|
||||||
|
// err = fmt.Errorf("retry exhausted for task %s: %w", task.Type, err)
|
||||||
|
// }
|
||||||
|
// errorReportingService.Notify(err)
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// ErrorHandler: asynq.ErrorHandlerFunc(reportError)
|
||||||
|
ErrorHandler ErrorHandler
|
||||||
|
|
||||||
|
// Logger specifies the logger used by the server instance.
|
||||||
|
//
|
||||||
|
// If unset, default logger is used.
|
||||||
|
Logger Logger
|
||||||
|
|
||||||
|
// LogLevel specifies the minimum log level to enable.
|
||||||
|
//
|
||||||
|
// If unset, InfoLevel is used by default.
|
||||||
|
LogLevel LogLevel
|
||||||
|
|
||||||
|
// ShutdownTimeout specifies the duration to wait to let workers finish their tasks
|
||||||
|
// before forcing them to abort when stopping the server.
|
||||||
|
//
|
||||||
|
// If unset or zero, default timeout of 8 seconds is used.
|
||||||
|
ShutdownTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// An ErrorHandler handles errors returned by the task handler.
|
||||||
|
type ErrorHandler interface {
|
||||||
|
HandleError(task *Task, err error, retried, maxRetry int)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The ErrorHandlerFunc type is an adapter to allow the use of ordinary functions as a ErrorHandler.
|
||||||
|
// If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.
|
||||||
|
type ErrorHandlerFunc func(task *Task, err error, retried, maxRetry int)
|
||||||
|
|
||||||
|
// HandleError calls fn(task, err, retried, maxRetry)
|
||||||
|
func (fn ErrorHandlerFunc) HandleError(task *Task, err error, retried, maxRetry int) {
|
||||||
|
fn(task, err, retried, maxRetry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger supports logging at various log levels.
|
||||||
|
type Logger interface {
|
||||||
|
// Debug logs a message at Debug level.
|
||||||
|
Debug(args ...interface{})
|
||||||
|
|
||||||
|
// Info logs a message at Info level.
|
||||||
|
Info(args ...interface{})
|
||||||
|
|
||||||
|
// Warn logs a message at Warning level.
|
||||||
|
Warn(args ...interface{})
|
||||||
|
|
||||||
|
// Error logs a message at Error level.
|
||||||
|
Error(args ...interface{})
|
||||||
|
|
||||||
|
// Fatal logs a message at Fatal level
|
||||||
|
// and process will exit with status set to 1.
|
||||||
|
Fatal(args ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogLevel represents logging level.
|
||||||
|
//
|
||||||
|
// It satisfies flag.Value interface.
|
||||||
|
type LogLevel int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Note: reserving value zero to differentiate unspecified case.
|
||||||
|
level_unspecified LogLevel = iota
|
||||||
|
|
||||||
|
// DebugLevel is the lowest level of logging.
|
||||||
|
// Debug logs are intended for debugging and development purposes.
|
||||||
|
DebugLevel
|
||||||
|
|
||||||
|
// InfoLevel is used for general informational log messages.
|
||||||
|
InfoLevel
|
||||||
|
|
||||||
|
// WarnLevel is used for undesired but relatively expected events,
|
||||||
|
// which may indicate a problem.
|
||||||
|
WarnLevel
|
||||||
|
|
||||||
|
// ErrorLevel is used for undesired and unexpected events that
|
||||||
|
// the program can recover from.
|
||||||
|
ErrorLevel
|
||||||
|
|
||||||
|
// FatalLevel is used for undesired and unexpected events that
|
||||||
|
// the program cannot recover from.
|
||||||
|
FatalLevel
|
||||||
|
)
|
||||||
|
|
||||||
|
// String is part of the flag.Value interface.
|
||||||
|
func (l *LogLevel) String() string {
|
||||||
|
switch *l {
|
||||||
|
case DebugLevel:
|
||||||
|
return "debug"
|
||||||
|
case InfoLevel:
|
||||||
|
return "info"
|
||||||
|
case WarnLevel:
|
||||||
|
return "warn"
|
||||||
|
case ErrorLevel:
|
||||||
|
return "error"
|
||||||
|
case FatalLevel:
|
||||||
|
return "fatal"
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("asynq: unexpected log level: %v", *l))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set is part of the flag.Value interface.
|
||||||
|
func (l *LogLevel) Set(val string) error {
|
||||||
|
switch strings.ToLower(val) {
|
||||||
|
case "debug":
|
||||||
|
*l = DebugLevel
|
||||||
|
case "info":
|
||||||
|
*l = InfoLevel
|
||||||
|
case "warn", "warning":
|
||||||
|
*l = WarnLevel
|
||||||
|
case "error":
|
||||||
|
*l = ErrorLevel
|
||||||
|
case "fatal":
|
||||||
|
*l = FatalLevel
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("asynq: unsupported log level %q", val)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toInternalLogLevel(l LogLevel) log.Level {
|
||||||
|
switch l {
|
||||||
|
case DebugLevel:
|
||||||
|
return log.DebugLevel
|
||||||
|
case InfoLevel:
|
||||||
|
return log.InfoLevel
|
||||||
|
case WarnLevel:
|
||||||
|
return log.WarnLevel
|
||||||
|
case ErrorLevel:
|
||||||
|
return log.ErrorLevel
|
||||||
|
case FatalLevel:
|
||||||
|
return log.FatalLevel
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("asynq: unexpected log level: %v", l))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formula taken from https://github.com/mperham/sidekiq.
|
||||||
|
func defaultDelayFunc(n int, e error, t *Task) time.Duration {
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
s := int(math.Pow(float64(n), 4)) + 15 + (r.Intn(30) * (n + 1))
|
||||||
|
return time.Duration(s) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultQueueConfig = map[string]int{
|
||||||
|
base.DefaultQueueName: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultShutdownTimeout = 8 * time.Second
|
||||||
|
|
||||||
|
// NewServer returns a new Server given a redis connection option
|
||||||
|
// and background processing configuration.
|
||||||
|
func NewServer(r RedisConnOpt, cfg Config) *Server {
|
||||||
|
n := cfg.Concurrency
|
||||||
|
if n < 1 {
|
||||||
|
n = runtime.NumCPU()
|
||||||
|
}
|
||||||
|
delayFunc := cfg.RetryDelayFunc
|
||||||
|
if delayFunc == nil {
|
||||||
|
delayFunc = defaultDelayFunc
|
||||||
|
}
|
||||||
|
queues := make(map[string]int)
|
||||||
|
for qname, p := range cfg.Queues {
|
||||||
|
if p > 0 {
|
||||||
|
queues[qname] = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(queues) == 0 {
|
||||||
|
queues = defaultQueueConfig
|
||||||
|
}
|
||||||
|
shutdownTimeout := cfg.ShutdownTimeout
|
||||||
|
if shutdownTimeout == 0 {
|
||||||
|
shutdownTimeout = defaultShutdownTimeout
|
||||||
|
}
|
||||||
|
logger := log.NewLogger(cfg.Logger)
|
||||||
|
loglevel := cfg.LogLevel
|
||||||
|
if loglevel == level_unspecified {
|
||||||
|
loglevel = InfoLevel
|
||||||
|
}
|
||||||
|
logger.SetLevel(toInternalLogLevel(loglevel))
|
||||||
|
|
||||||
|
host, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
host = "unknown-host"
|
||||||
|
}
|
||||||
|
pid := os.Getpid()
|
||||||
|
|
||||||
|
rdb := rdb.NewRDB(createRedisClient(r))
|
||||||
|
ss := base.NewServerState(host, pid, n, queues, cfg.StrictPriority)
|
||||||
|
syncCh := make(chan *syncRequest)
|
||||||
|
cancels := base.NewCancelations()
|
||||||
|
syncer := newSyncer(logger, syncCh, 5*time.Second)
|
||||||
|
heartbeater := newHeartbeater(logger, rdb, ss, 5*time.Second)
|
||||||
|
scheduler := newScheduler(logger, rdb, 5*time.Second, queues)
|
||||||
|
subscriber := newSubscriber(logger, rdb, cancels)
|
||||||
|
processor := newProcessor(newProcessorParams{
|
||||||
|
logger: logger,
|
||||||
|
broker: rdb,
|
||||||
|
ss: ss,
|
||||||
|
retryDelayFunc: delayFunc,
|
||||||
|
syncCh: syncCh,
|
||||||
|
cancelations: cancels,
|
||||||
|
errHandler: cfg.ErrorHandler,
|
||||||
|
shutdownTimeout: shutdownTimeout,
|
||||||
|
})
|
||||||
|
return &Server{
|
||||||
|
ss: ss,
|
||||||
|
logger: logger,
|
||||||
|
broker: rdb,
|
||||||
|
scheduler: scheduler,
|
||||||
|
processor: processor,
|
||||||
|
syncer: syncer,
|
||||||
|
heartbeater: heartbeater,
|
||||||
|
subscriber: subscriber,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Handler processes tasks.
|
||||||
|
//
|
||||||
|
// ProcessTask should return nil if the processing of a task
|
||||||
|
// is successful.
|
||||||
|
//
|
||||||
|
// If ProcessTask return a non-nil error or panics, the task
|
||||||
|
// will be retried after delay.
|
||||||
|
type Handler interface {
|
||||||
|
ProcessTask(context.Context, *Task) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// The HandlerFunc type is an adapter to allow the use of
|
||||||
|
// ordinary functions as a Handler. If f is a function
|
||||||
|
// with the appropriate signature, HandlerFunc(f) is a
|
||||||
|
// Handler that calls f.
|
||||||
|
type HandlerFunc func(context.Context, *Task) error
|
||||||
|
|
||||||
|
// ProcessTask calls fn(ctx, task)
|
||||||
|
func (fn HandlerFunc) ProcessTask(ctx context.Context, task *Task) error {
|
||||||
|
return fn(ctx, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrServerStopped indicates that the operation is now illegal because of the server being stopped.
|
||||||
|
var ErrServerStopped = errors.New("asynq: the server has been stopped")
|
||||||
|
|
||||||
|
// Run starts the background-task processing and blocks until
|
||||||
|
// an os signal to exit the program is received. Once it receives
|
||||||
|
// a signal, it gracefully shuts down all active workers and other
|
||||||
|
// goroutines to process the tasks.
|
||||||
|
//
|
||||||
|
// Run returns any error encountered during server startup time.
|
||||||
|
// If the server has already been stopped, ErrServerStopped is returned.
|
||||||
|
func (srv *Server) Run(handler Handler) error {
|
||||||
|
if err := srv.Start(handler); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
srv.waitForSignals()
|
||||||
|
srv.Stop()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start starts the worker server. Once the server has started,
|
||||||
|
// it pulls tasks off queues and starts a worker goroutine for each task.
|
||||||
|
// Tasks are processed concurrently by the workers up to the number of
|
||||||
|
// concurrency specified at the initialization time.
|
||||||
|
//
|
||||||
|
// Start returns any error encountered during server startup time.
|
||||||
|
// If the server has already been stopped, ErrServerStopped is returned.
|
||||||
|
func (srv *Server) Start(handler Handler) error {
|
||||||
|
if handler == nil {
|
||||||
|
return fmt.Errorf("asynq: server cannot run with nil handler")
|
||||||
|
}
|
||||||
|
switch srv.ss.Status() {
|
||||||
|
case base.StatusRunning:
|
||||||
|
return fmt.Errorf("asynq: the server is already running")
|
||||||
|
case base.StatusStopped:
|
||||||
|
return ErrServerStopped
|
||||||
|
}
|
||||||
|
srv.ss.SetStatus(base.StatusRunning)
|
||||||
|
srv.processor.handler = handler
|
||||||
|
|
||||||
|
srv.logger.Info("Starting processing")
|
||||||
|
|
||||||
|
srv.heartbeater.start(&srv.wg)
|
||||||
|
srv.subscriber.start(&srv.wg)
|
||||||
|
srv.syncer.start(&srv.wg)
|
||||||
|
srv.scheduler.start(&srv.wg)
|
||||||
|
srv.processor.start(&srv.wg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop stops the worker server.
|
||||||
|
// It gracefully closes all active workers. The server will wait for
|
||||||
|
// active workers to finish processing tasks for duration specified in Config.ShutdownTimeout.
|
||||||
|
// If worker didn't finish processing a task during the timeout, the task will be pushed back to Redis.
|
||||||
|
func (srv *Server) Stop() {
|
||||||
|
switch srv.ss.Status() {
|
||||||
|
case base.StatusIdle, base.StatusStopped:
|
||||||
|
// server is not running, do nothing and return.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.logger.Info("Starting graceful shutdown")
|
||||||
|
// Note: The order of termination is important.
|
||||||
|
// Sender goroutines should be terminated before the receiver goroutines.
|
||||||
|
// processor -> syncer (via syncCh)
|
||||||
|
srv.scheduler.terminate()
|
||||||
|
srv.processor.terminate()
|
||||||
|
srv.syncer.terminate()
|
||||||
|
srv.subscriber.terminate()
|
||||||
|
srv.heartbeater.terminate()
|
||||||
|
|
||||||
|
srv.wg.Wait()
|
||||||
|
|
||||||
|
srv.broker.Close()
|
||||||
|
srv.ss.SetStatus(base.StatusStopped)
|
||||||
|
|
||||||
|
srv.logger.Info("Exiting")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quiet signals the server to stop pulling new tasks off queues.
|
||||||
|
// Quiet should be used before stopping the server.
|
||||||
|
func (srv *Server) Quiet() {
|
||||||
|
srv.logger.Info("Stopping processor")
|
||||||
|
srv.processor.stop()
|
||||||
|
srv.ss.SetStatus(base.StatusQuiet)
|
||||||
|
srv.logger.Info("Processor stopped")
|
||||||
|
}
|
240
server_test.go
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT license
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package asynq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
|
"github.com/hibiken/asynq/internal/testbroker"
|
||||||
|
"go.uber.org/goleak"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServer(t *testing.T) {
|
||||||
|
// https://github.com/go-redis/redis/issues/1029
|
||||||
|
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
|
||||||
|
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
||||||
|
|
||||||
|
r := &RedisClientOpt{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
DB: 15,
|
||||||
|
}
|
||||||
|
c := NewClient(r)
|
||||||
|
srv := NewServer(r, Config{
|
||||||
|
Concurrency: 10,
|
||||||
|
LogLevel: testLogLevel,
|
||||||
|
})
|
||||||
|
|
||||||
|
// no-op handler
|
||||||
|
h := func(ctx context.Context, task *Task) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := srv.Start(HandlerFunc(h))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.Enqueue(NewTask("send_email", map[string]interface{}{"recipient_id": 123}))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not enqueue a task: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.EnqueueAt(time.Now().Add(time.Hour), NewTask("send_email", map[string]interface{}{"recipient_id": 456}))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not enqueue a task: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerRun(t *testing.T) {
|
||||||
|
// https://github.com/go-redis/redis/issues/1029
|
||||||
|
ignoreOpt := goleak.IgnoreTopFunction("github.com/go-redis/redis/v7/internal/pool.(*ConnPool).reaper")
|
||||||
|
defer goleak.VerifyNoLeaks(t, ignoreOpt)
|
||||||
|
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
// Make sure server exits when receiving TERM signal.
|
||||||
|
go func() {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
|
||||||
|
done <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-time.After(10 * time.Second):
|
||||||
|
t.Fatal("server did not stop after receiving TERM signal")
|
||||||
|
case <-done:
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
mux := NewServeMux()
|
||||||
|
if err := srv.Run(mux); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerErrServerStopped(t *testing.T) {
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
|
handler := NewServeMux()
|
||||||
|
if err := srv.Start(handler); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
srv.Stop()
|
||||||
|
err := srv.Start(handler)
|
||||||
|
if err != ErrServerStopped {
|
||||||
|
t.Errorf("Restarting server: (*Server).Start(handler) = %v, want ErrServerStopped error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerErrNilHandler(t *testing.T) {
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
|
err := srv.Start(nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error")
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerErrServerRunning(t *testing.T) {
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
|
handler := NewServeMux()
|
||||||
|
if err := srv.Start(handler); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err := srv.Start(handler)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Calling (*Server).Start(handler) on already running server did not return error")
|
||||||
|
}
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerWithRedisDown(t *testing.T) {
|
||||||
|
// Make sure that server does not panic and exit if redis is down.
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Errorf("panic occurred: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
r := rdb.NewRDB(setup(t))
|
||||||
|
testBroker := testbroker.NewTestBroker(r)
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||||
|
srv.broker = testBroker
|
||||||
|
srv.scheduler.broker = testBroker
|
||||||
|
srv.heartbeater.broker = testBroker
|
||||||
|
srv.processor.broker = testBroker
|
||||||
|
srv.subscriber.broker = testBroker
|
||||||
|
testBroker.Sleep()
|
||||||
|
|
||||||
|
// no-op handler
|
||||||
|
h := func(ctx context.Context, task *Task) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := srv.Start(HandlerFunc(h))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerWithFlakyBroker(t *testing.T) {
|
||||||
|
// Make sure that server does not panic and exit if redis is down.
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Errorf("panic occurred: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
r := rdb.NewRDB(setup(t))
|
||||||
|
testBroker := testbroker.NewTestBroker(r)
|
||||||
|
srv := NewServer(RedisClientOpt{Addr: redisAddr, DB: redisDB}, Config{LogLevel: testLogLevel})
|
||||||
|
srv.broker = testBroker
|
||||||
|
srv.scheduler.broker = testBroker
|
||||||
|
srv.heartbeater.broker = testBroker
|
||||||
|
srv.processor.broker = testBroker
|
||||||
|
srv.subscriber.broker = testBroker
|
||||||
|
|
||||||
|
c := NewClient(RedisClientOpt{Addr: redisAddr, DB: redisDB})
|
||||||
|
|
||||||
|
h := func(ctx context.Context, task *Task) error {
|
||||||
|
// force task retry.
|
||||||
|
if task.Type == "bad_task" {
|
||||||
|
return fmt.Errorf("could not process %q", task.Type)
|
||||||
|
}
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := srv.Start(HandlerFunc(h))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
err := c.Enqueue(NewTask("enqueued", nil), MaxRetry(i))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = c.Enqueue(NewTask("bad_task", nil))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = c.EnqueueIn(time.Duration(i)*time.Second, NewTask("scheduled", nil))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// simulate redis going down.
|
||||||
|
testBroker.Sleep()
|
||||||
|
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
|
// simulate redis comes back online.
|
||||||
|
testBroker.Wakeup()
|
||||||
|
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
|
srv.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLogLevel(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
flagVal string
|
||||||
|
want LogLevel
|
||||||
|
wantStr string
|
||||||
|
}{
|
||||||
|
{"debug", DebugLevel, "debug"},
|
||||||
|
{"Info", InfoLevel, "info"},
|
||||||
|
{"WARN", WarnLevel, "warn"},
|
||||||
|
{"warning", WarnLevel, "warn"},
|
||||||
|
{"Error", ErrorLevel, "error"},
|
||||||
|
{"fatal", FatalLevel, "fatal"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
level := new(LogLevel)
|
||||||
|
if err := level.Set(tc.flagVal); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if *level != tc.want {
|
||||||
|
t.Errorf("Set(%q): got %v, want %v", tc.flagVal, level, &tc.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got := level.String(); got != tc.wantStr {
|
||||||
|
t.Errorf("String() returned %q, want %q", got, tc.wantStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
signals_unix.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// +build linux bsd darwin
|
||||||
|
|
||||||
|
package asynq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// waitForSignals waits for signals and handles them.
|
||||||
|
// It handles SIGTERM, SIGINT, and SIGTSTP.
|
||||||
|
// SIGTERM and SIGINT will signal the process to exit.
|
||||||
|
// SIGTSTP will signal the process to stop processing new tasks.
|
||||||
|
func (srv *Server) waitForSignals() {
|
||||||
|
srv.logger.Info("Send signal TSTP to stop processing new tasks")
|
||||||
|
srv.logger.Info("Send signal TERM or INT to terminate the process")
|
||||||
|
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
|
||||||
|
for {
|
||||||
|
sig := <-sigs
|
||||||
|
if sig == unix.SIGTSTP {
|
||||||
|
srv.Quiet()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
22
signals_windows.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package asynq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
// waitForSignals waits for signals and handles them.
|
||||||
|
// It handles SIGTERM and SIGINT.
|
||||||
|
// SIGTERM and SIGINT will signal the process to exit.
|
||||||
|
//
|
||||||
|
// Note: Currently SIGTSTP is not supported for windows build.
|
||||||
|
func (srv *Server) waitForSignals() {
|
||||||
|
srv.logger.Info("Send signal TERM or INT to terminate the process")
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, windows.SIGTERM, windows.SIGINT)
|
||||||
|
<-sigs
|
||||||
|
}
|
@@ -6,52 +6,72 @@ package asynq
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v7"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type subscriber struct {
|
type subscriber struct {
|
||||||
logger Logger
|
logger *log.Logger
|
||||||
rdb *rdb.RDB
|
broker base.Broker
|
||||||
|
|
||||||
// channel to communicate back to the long running "subscriber" goroutine.
|
// channel to communicate back to the long running "subscriber" goroutine.
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
|
||||||
// cancelations hold cancel functions for all in-progress tasks.
|
// cancelations hold cancel functions for all in-progress tasks.
|
||||||
cancelations *base.Cancelations
|
cancelations *base.Cancelations
|
||||||
|
|
||||||
|
// time to wait before retrying to connect to redis.
|
||||||
|
retryTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSubscriber(l Logger, rdb *rdb.RDB, cancelations *base.Cancelations) *subscriber {
|
func newSubscriber(l *log.Logger, b base.Broker, cancelations *base.Cancelations) *subscriber {
|
||||||
return &subscriber{
|
return &subscriber{
|
||||||
logger: l,
|
logger: l,
|
||||||
rdb: rdb,
|
broker: b,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
cancelations: cancelations,
|
cancelations: cancelations,
|
||||||
|
retryTimeout: 5 * time.Second,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *subscriber) terminate() {
|
func (s *subscriber) terminate() {
|
||||||
s.logger.Info("Subscriber shutting down...")
|
s.logger.Debug("Subscriber shutting down...")
|
||||||
// Signal the subscriber goroutine to stop.
|
// Signal the subscriber goroutine to stop.
|
||||||
s.done <- struct{}{}
|
s.done <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *subscriber) start(wg *sync.WaitGroup) {
|
func (s *subscriber) start(wg *sync.WaitGroup) {
|
||||||
pubsub, err := s.rdb.CancelationPubSub()
|
|
||||||
cancelCh := pubsub.Channel()
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("cannot subscribe to cancelation channel: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
var (
|
||||||
|
pubsub *redis.PubSub
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
// Try until successfully connect to Redis.
|
||||||
|
for {
|
||||||
|
pubsub, err = s.broker.CancelationPubSub()
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Errorf("cannot subscribe to cancelation channel: %v", err)
|
||||||
|
select {
|
||||||
|
case <-time.After(s.retryTimeout):
|
||||||
|
continue
|
||||||
|
case <-s.done:
|
||||||
|
s.logger.Debug("Subscriber done")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
cancelCh := pubsub.Channel()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.done:
|
case <-s.done:
|
||||||
pubsub.Close()
|
pubsub.Close()
|
||||||
s.logger.Info("Subscriber done")
|
s.logger.Debug("Subscriber done")
|
||||||
return
|
return
|
||||||
case msg := <-cancelCh:
|
case msg := <-cancelCh:
|
||||||
cancel, ok := s.cancelations.Get(msg.Payload)
|
cancel, ok := s.cancelations.Get(msg.Payload)
|
||||||
|
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
|
"github.com/hibiken/asynq/internal/testbroker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSubscriber(t *testing.T) {
|
func TestSubscriber(t *testing.T) {
|
||||||
@@ -40,13 +41,16 @@ func TestSubscriber(t *testing.T) {
|
|||||||
subscriber := newSubscriber(testLogger, rdbClient, cancelations)
|
subscriber := newSubscriber(testLogger, rdbClient, cancelations)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
subscriber.start(&wg)
|
subscriber.start(&wg)
|
||||||
|
defer subscriber.terminate()
|
||||||
|
|
||||||
|
// wait for subscriber to establish connection to pubsub channel
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
if err := rdbClient.PublishCancelation(tc.publishID); err != nil {
|
if err := rdbClient.PublishCancelation(tc.publishID); err != nil {
|
||||||
subscriber.terminate()
|
|
||||||
t.Fatalf("could not publish cancelation message: %v", err)
|
t.Fatalf("could not publish cancelation message: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// allow for redis to publish message
|
// wait for redis to publish message
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
@@ -58,7 +62,53 @@ func TestSubscriber(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
subscriber.terminate()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSubscriberWithRedisDown(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
t.Errorf("panic occurred: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
r := rdb.NewRDB(setup(t))
|
||||||
|
testBroker := testbroker.NewTestBroker(r)
|
||||||
|
|
||||||
|
cancelations := base.NewCancelations()
|
||||||
|
subscriber := newSubscriber(testLogger, testBroker, cancelations)
|
||||||
|
subscriber.retryTimeout = 1 * time.Second // set shorter retry timeout for testing purpose.
|
||||||
|
|
||||||
|
testBroker.Sleep() // simulate a situation where subscriber cannot connect to redis.
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
subscriber.start(&wg)
|
||||||
|
defer subscriber.terminate()
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second) // subscriber should wait and retry connecting to redis.
|
||||||
|
|
||||||
|
testBroker.Wakeup() // simulate a situation where redis server is back online.
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second) // allow subscriber to establish pubsub channel.
|
||||||
|
|
||||||
|
const id = "test"
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
called bool
|
||||||
|
)
|
||||||
|
cancelations.Add(id, func() {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
called = true
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := r.PublishCancelation(id); err != nil {
|
||||||
|
t.Fatalf("could not publish cancelation message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second) // wait for redis to publish message.
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
if !called {
|
||||||
|
t.Errorf("cancel function was not called")
|
||||||
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
10
syncer.go
@@ -7,12 +7,14 @@ package asynq
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// syncer is responsible for queuing up failed requests to redis and retry
|
// syncer is responsible for queuing up failed requests to redis and retry
|
||||||
// those requests to sync state between the background process and redis.
|
// those requests to sync state between the background process and redis.
|
||||||
type syncer struct {
|
type syncer struct {
|
||||||
logger Logger
|
logger *log.Logger
|
||||||
|
|
||||||
requestsCh <-chan *syncRequest
|
requestsCh <-chan *syncRequest
|
||||||
|
|
||||||
@@ -28,7 +30,7 @@ type syncRequest struct {
|
|||||||
errMsg string // error message
|
errMsg string // error message
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSyncer(l Logger, requestsCh <-chan *syncRequest, interval time.Duration) *syncer {
|
func newSyncer(l *log.Logger, requestsCh <-chan *syncRequest, interval time.Duration) *syncer {
|
||||||
return &syncer{
|
return &syncer{
|
||||||
logger: l,
|
logger: l,
|
||||||
requestsCh: requestsCh,
|
requestsCh: requestsCh,
|
||||||
@@ -38,7 +40,7 @@ func newSyncer(l Logger, requestsCh <-chan *syncRequest, interval time.Duration)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *syncer) terminate() {
|
func (s *syncer) terminate() {
|
||||||
s.logger.Info("Syncer shutting down...")
|
s.logger.Debug("Syncer shutting down...")
|
||||||
// Signal the syncer goroutine to stop.
|
// Signal the syncer goroutine to stop.
|
||||||
s.done <- struct{}{}
|
s.done <- struct{}{}
|
||||||
}
|
}
|
||||||
@@ -57,7 +59,7 @@ func (s *syncer) start(wg *sync.WaitGroup) {
|
|||||||
s.logger.Error(req.errMsg)
|
s.logger.Error(req.errMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.logger.Info("Syncer done")
|
s.logger.Debug("Syncer done")
|
||||||
return
|
return
|
||||||
case req := <-s.requestsCh:
|
case req := <-s.requestsCh:
|
||||||
requests = append(requests, req)
|
requests = append(requests, req)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
# Asynqmon
|
# Asynq CLI
|
||||||
|
|
||||||
Asynqmon is a command line tool to monitor the tasks managed by `asynq` package.
|
Asynq CLI is a command line tool to monitor the tasks managed by `asynq` package.
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ Asynqmon is a command line tool to monitor the tasks managed by `asynq` package.
|
|||||||
- [Quick Start](#quick-start)
|
- [Quick Start](#quick-start)
|
||||||
- [Stats](#stats)
|
- [Stats](#stats)
|
||||||
- [History](#history)
|
- [History](#history)
|
||||||
- [Process Status](#process-status)
|
- [Servers](#servers)
|
||||||
- [List](#list)
|
- [List](#list)
|
||||||
- [Enqueue](#enqueue)
|
- [Enqueue](#enqueue)
|
||||||
- [Delete](#delete)
|
- [Delete](#delete)
|
||||||
@@ -20,19 +20,19 @@ Asynqmon is a command line tool to monitor the tasks managed by `asynq` package.
|
|||||||
|
|
||||||
In order to use the tool, compile it using the following command:
|
In order to use the tool, compile it using the following command:
|
||||||
|
|
||||||
go get github.com/hibiken/asynq/tools/asynqmon
|
go get github.com/hibiken/asynq/tools/asynq
|
||||||
|
|
||||||
This will create the asynqmon executable under your `$GOPATH/bin` directory.
|
This will create the asynq executable under your `$GOPATH/bin` directory.
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
The tool has a few commands to inspect the state of tasks and queues.
|
The tool has a few commands to inspect the state of tasks and queues.
|
||||||
|
|
||||||
Run `asynqmon help` to see all the available commands.
|
Run `asynq help` to see all the available commands.
|
||||||
|
|
||||||
Asynqmon needs to connect to a redis-server to inspect the state of queues and tasks. Use flags to specify the options to connect to the redis-server used by your application.
|
Asynq CLI needs to connect to a redis-server to inspect the state of queues and tasks. Use flags to specify the options to connect to the redis-server used by your application.
|
||||||
|
|
||||||
By default, Asynqmon will try to connect to a redis server running at `localhost:6379`.
|
By default, CLI will try to connect to a redis server running at `localhost:6379`.
|
||||||
|
|
||||||
### Stats
|
### Stats
|
||||||
|
|
||||||
@@ -40,11 +40,11 @@ Stats command gives the overview of the current state of tasks and queues. You c
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
watch -n 3 asynqmon stats
|
watch -n 3 asynq stats
|
||||||
|
|
||||||
This will run `asynqmon stats` command every 3 seconds.
|
This will run `asynq stats` command every 3 seconds.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
### History
|
### History
|
||||||
|
|
||||||
@@ -54,19 +54,17 @@ By default, it shows the stats from the last 10 days. Use `--days` to specify th
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon history --days=30
|
asynq history --days=30
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
### Process Status
|
### Servers
|
||||||
|
|
||||||
PS (ProcessStatus) command shows the list of running worker processes.
|
Servers command shows the list of running worker servers pulling tasks from the given redis instance.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon ps
|
asynq servers
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
### List
|
### List
|
||||||
|
|
||||||
@@ -74,11 +72,11 @@ List command shows all tasks in the specified state in a table format
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon ls retry
|
asynq ls retry
|
||||||
asynqmon ls scheduled
|
asynq ls scheduled
|
||||||
asynqmon ls dead
|
asynq ls dead
|
||||||
asynqmon ls enqueued:default
|
asynq ls enqueued:default
|
||||||
asynqmon ls inprogress
|
asynq ls inprogress
|
||||||
|
|
||||||
### Enqueue
|
### Enqueue
|
||||||
|
|
||||||
@@ -88,13 +86,13 @@ Command `enq` takes a task ID and moves the task to **Enqueued** state. You can
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon enq d:1575732274:bnogo8gt6toe23vhef0g
|
asynq enq d:1575732274:bnogo8gt6toe23vhef0g
|
||||||
|
|
||||||
Command `enqall` moves all tasks to **Enqueued** state from the specified state.
|
Command `enqall` moves all tasks to **Enqueued** state from the specified state.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon enqall retry
|
asynq enqall retry
|
||||||
|
|
||||||
Running the above command will move all **Retry** tasks to **Enqueued** state.
|
Running the above command will move all **Retry** tasks to **Enqueued** state.
|
||||||
|
|
||||||
@@ -106,13 +104,13 @@ Command `del` takes a task ID and deletes the task. You can obtain the task ID b
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon del r:1575732274:bnogo8gt6toe23vhef0g
|
asynq del r:1575732274:bnogo8gt6toe23vhef0g
|
||||||
|
|
||||||
Command `delall` deletes all tasks which are in the specified state.
|
Command `delall` deletes all tasks which are in the specified state.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon delall retry
|
asynq delall retry
|
||||||
|
|
||||||
Running the above command will delete all **Retry** tasks.
|
Running the above command will delete all **Retry** tasks.
|
||||||
|
|
||||||
@@ -124,13 +122,13 @@ Command `kill` takes a task ID and kills the task. You can obtain the task ID by
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon kill r:1575732274:bnogo8gt6toe23vhef0g
|
asynq kill r:1575732274:bnogo8gt6toe23vhef0g
|
||||||
|
|
||||||
Command `killall` kills all tasks which are in the specified state.
|
Command `killall` kills all tasks which are in the specified state.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon killall retry
|
asynq killall retry
|
||||||
|
|
||||||
Running the above command will move all **Retry** tasks to **Dead** state.
|
Running the above command will move all **Retry** tasks to **Dead** state.
|
||||||
|
|
||||||
@@ -144,15 +142,15 @@ Handler implementation needs to be context aware in order to actually stop proce
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
asynqmon cancel bnogo8gt6toe23vhef0g
|
asynq cancel bnogo8gt6toe23vhef0g
|
||||||
|
|
||||||
## Config File
|
## Config File
|
||||||
|
|
||||||
You can use a config file to set default values for the flags.
|
You can use a config file to set default values for the flags.
|
||||||
This is useful, for example when you have to connect to a remote redis server.
|
This is useful, for example when you have to connect to a remote redis server.
|
||||||
|
|
||||||
By default, `asynqmon` will try to read config file located in
|
By default, `asynq` will try to read config file located in
|
||||||
`$HOME/.asynqmon.(yaml|json)`. You can specify the file location via `--config` flag.
|
`$HOME/.asynq.(yaml|json)`. You can specify the file location via `--config` flag.
|
||||||
|
|
||||||
Config file example:
|
Config file example:
|
||||||
|
|
@@ -18,17 +18,17 @@ import (
|
|||||||
var cancelCmd = &cobra.Command{
|
var cancelCmd = &cobra.Command{
|
||||||
Use: "cancel [task id]",
|
Use: "cancel [task id]",
|
||||||
Short: "Sends a cancelation signal to the goroutine processing the specified task",
|
Short: "Sends a cancelation signal to the goroutine processing the specified task",
|
||||||
Long: `Cancel (asynqmon cancel) will send a cancelation signal to the goroutine processing
|
Long: `Cancel (asynq cancel) will send a cancelation signal to the goroutine processing
|
||||||
the specified task.
|
the specified task.
|
||||||
|
|
||||||
The command takes one argument which specifies the task to cancel.
|
The command takes one argument which specifies the task to cancel.
|
||||||
The task should be in in-progress state.
|
The task should be in in-progress state.
|
||||||
Identifier for a task should be obtained by running "asynqmon ls" command.
|
Identifier for a task should be obtained by running "asynq ls" command.
|
||||||
|
|
||||||
Handler implementation needs to be context aware for cancelation signal to
|
Handler implementation needs to be context aware for cancelation signal to
|
||||||
actually cancel the processing.
|
actually cancel the processing.
|
||||||
|
|
||||||
Example: asynqmon cancel bnogo8gt6toe23vhef0g`,
|
Example: asynq cancel bnogo8gt6toe23vhef0g`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: cancel,
|
Run: cancel,
|
||||||
}
|
}
|
@@ -18,13 +18,13 @@ import (
|
|||||||
var delCmd = &cobra.Command{
|
var delCmd = &cobra.Command{
|
||||||
Use: "del [task id]",
|
Use: "del [task id]",
|
||||||
Short: "Deletes a task given an identifier",
|
Short: "Deletes a task given an identifier",
|
||||||
Long: `Del (asynqmon del) will delete a task given an identifier.
|
Long: `Del (asynq del) will delete a task given an identifier.
|
||||||
|
|
||||||
The command takes one argument which specifies the task to delete.
|
The command takes one argument which specifies the task to delete.
|
||||||
The task should be in either scheduled, retry or dead state.
|
The task should be in either scheduled, retry or dead state.
|
||||||
Identifier for a task should be obtained by running "asynqmon ls" command.
|
Identifier for a task should be obtained by running "asynq ls" command.
|
||||||
|
|
||||||
Example: asynqmon enq d:1575732274:bnogo8gt6toe23vhef0g`,
|
Example: asynq enq d:1575732274:bnogo8gt6toe23vhef0g`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: del,
|
Run: del,
|
||||||
}
|
}
|
@@ -20,11 +20,11 @@ var delallValidArgs = []string{"scheduled", "retry", "dead"}
|
|||||||
var delallCmd = &cobra.Command{
|
var delallCmd = &cobra.Command{
|
||||||
Use: "delall [state]",
|
Use: "delall [state]",
|
||||||
Short: "Deletes all tasks in the specified state",
|
Short: "Deletes all tasks in the specified state",
|
||||||
Long: `Delall (asynqmon delall) will delete all tasks in the specified state.
|
Long: `Delall (asynq delall) will delete all tasks in the specified state.
|
||||||
|
|
||||||
The argument should be one of "scheduled", "retry", or "dead".
|
The argument should be one of "scheduled", "retry", or "dead".
|
||||||
|
|
||||||
Example: asynqmon delall dead -> Deletes all dead tasks`,
|
Example: asynq delall dead -> Deletes all dead tasks`,
|
||||||
ValidArgs: delallValidArgs,
|
ValidArgs: delallValidArgs,
|
||||||
Args: cobra.ExactValidArgs(1),
|
Args: cobra.ExactValidArgs(1),
|
||||||
Run: delall,
|
Run: delall,
|
||||||
@@ -60,7 +60,7 @@ func delall(cmd *cobra.Command, args []string) {
|
|||||||
case "dead":
|
case "dead":
|
||||||
err = r.DeleteAllDeadTasks()
|
err = r.DeleteAllDeadTasks()
|
||||||
default:
|
default:
|
||||||
fmt.Printf("error: `asynqmon delall [state]` only accepts %v as the argument.\n", delallValidArgs)
|
fmt.Printf("error: `asynq delall [state]` only accepts %v as the argument.\n", delallValidArgs)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
@@ -18,16 +18,16 @@ import (
|
|||||||
var enqCmd = &cobra.Command{
|
var enqCmd = &cobra.Command{
|
||||||
Use: "enq [task id]",
|
Use: "enq [task id]",
|
||||||
Short: "Enqueues a task given an identifier",
|
Short: "Enqueues a task given an identifier",
|
||||||
Long: `Enq (asynqmon enq) will enqueue a task given an identifier.
|
Long: `Enq (asynq enq) will enqueue a task given an identifier.
|
||||||
|
|
||||||
The command takes one argument which specifies the task to enqueue.
|
The command takes one argument which specifies the task to enqueue.
|
||||||
The task should be in either scheduled, retry or dead state.
|
The task should be in either scheduled, retry or dead state.
|
||||||
Identifier for a task should be obtained by running "asynqmon ls" command.
|
Identifier for a task should be obtained by running "asynq ls" command.
|
||||||
|
|
||||||
The task enqueued by this command will be processed as soon as the task
|
The task enqueued by this command will be processed as soon as the task
|
||||||
gets dequeued by a processor.
|
gets dequeued by a processor.
|
||||||
|
|
||||||
Example: asynqmon enq d:1575732274:bnogo8gt6toe23vhef0g`,
|
Example: asynq enq d:1575732274:bnogo8gt6toe23vhef0g`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: enq,
|
Run: enq,
|
||||||
}
|
}
|
@@ -20,14 +20,14 @@ var enqallValidArgs = []string{"scheduled", "retry", "dead"}
|
|||||||
var enqallCmd = &cobra.Command{
|
var enqallCmd = &cobra.Command{
|
||||||
Use: "enqall [state]",
|
Use: "enqall [state]",
|
||||||
Short: "Enqueues all tasks in the specified state",
|
Short: "Enqueues all tasks in the specified state",
|
||||||
Long: `Enqall (asynqmon enqall) will enqueue all tasks in the specified state.
|
Long: `Enqall (asynq enqall) will enqueue all tasks in the specified state.
|
||||||
|
|
||||||
The argument should be one of "scheduled", "retry", or "dead".
|
The argument should be one of "scheduled", "retry", or "dead".
|
||||||
|
|
||||||
The tasks enqueued by this command will be processed as soon as it
|
The tasks enqueued by this command will be processed as soon as it
|
||||||
gets dequeued by a processor.
|
gets dequeued by a processor.
|
||||||
|
|
||||||
Example: asynqmon enqall dead -> Enqueues all dead tasks`,
|
Example: asynq enqall dead -> Enqueues all dead tasks`,
|
||||||
ValidArgs: enqallValidArgs,
|
ValidArgs: enqallValidArgs,
|
||||||
Args: cobra.ExactValidArgs(1),
|
Args: cobra.ExactValidArgs(1),
|
||||||
Run: enqall,
|
Run: enqall,
|
||||||
@@ -64,7 +64,7 @@ func enqall(cmd *cobra.Command, args []string) {
|
|||||||
case "dead":
|
case "dead":
|
||||||
n, err = r.EnqueueAllDeadTasks()
|
n, err = r.EnqueueAllDeadTasks()
|
||||||
default:
|
default:
|
||||||
fmt.Printf("error: `asynqmon enqall [state]` only accepts %v as the argument.\n", enqallValidArgs)
|
fmt.Printf("error: `asynq enqall [state]` only accepts %v as the argument.\n", enqallValidArgs)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
@@ -22,12 +22,12 @@ var days int
|
|||||||
var historyCmd = &cobra.Command{
|
var historyCmd = &cobra.Command{
|
||||||
Use: "history",
|
Use: "history",
|
||||||
Short: "Shows historical aggregate data",
|
Short: "Shows historical aggregate data",
|
||||||
Long: `History (asynqmon history) will show the number of processed and failed tasks
|
Long: `History (asynq history) will show the number of processed and failed tasks
|
||||||
from the last x days.
|
from the last x days.
|
||||||
|
|
||||||
By default, it will show the data from the last 10 days.
|
By default, it will show the data from the last 10 days.
|
||||||
|
|
||||||
Example: asynqmon history -x=30 -> Shows stats from the last 30 days`,
|
Example: asynq history -x=30 -> Shows stats from the last 30 days`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: history,
|
Run: history,
|
||||||
}
|
}
|
@@ -18,13 +18,13 @@ import (
|
|||||||
var killCmd = &cobra.Command{
|
var killCmd = &cobra.Command{
|
||||||
Use: "kill [task id]",
|
Use: "kill [task id]",
|
||||||
Short: "Kills a task given an identifier",
|
Short: "Kills a task given an identifier",
|
||||||
Long: `Kill (asynqmon kill) will put a task in dead state given an identifier.
|
Long: `Kill (asynq kill) will put a task in dead state given an identifier.
|
||||||
|
|
||||||
The command takes one argument which specifies the task to kill.
|
The command takes one argument which specifies the task to kill.
|
||||||
The task should be in either scheduled or retry state.
|
The task should be in either scheduled or retry state.
|
||||||
Identifier for a task should be obtained by running "asynqmon ls" command.
|
Identifier for a task should be obtained by running "asynq ls" command.
|
||||||
|
|
||||||
Example: asynqmon kill r:1575732274:bnogo8gt6toe23vhef0g`,
|
Example: asynq kill r:1575732274:bnogo8gt6toe23vhef0g`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: kill,
|
Run: kill,
|
||||||
}
|
}
|
@@ -20,11 +20,11 @@ var killallValidArgs = []string{"scheduled", "retry"}
|
|||||||
var killallCmd = &cobra.Command{
|
var killallCmd = &cobra.Command{
|
||||||
Use: "killall [state]",
|
Use: "killall [state]",
|
||||||
Short: "Kills all tasks in the specified state",
|
Short: "Kills all tasks in the specified state",
|
||||||
Long: `Killall (asynqmon killall) will update all tasks from the specified state to dead state.
|
Long: `Killall (asynq killall) will update all tasks from the specified state to dead state.
|
||||||
|
|
||||||
The argument should be either "scheduled" or "retry".
|
The argument should be either "scheduled" or "retry".
|
||||||
|
|
||||||
Example: asynqmon killall retry -> Update all retry tasks to dead tasks`,
|
Example: asynq killall retry -> Update all retry tasks to dead tasks`,
|
||||||
ValidArgs: killallValidArgs,
|
ValidArgs: killallValidArgs,
|
||||||
Args: cobra.ExactValidArgs(1),
|
Args: cobra.ExactValidArgs(1),
|
||||||
Run: killall,
|
Run: killall,
|
||||||
@@ -59,7 +59,7 @@ func killall(cmd *cobra.Command, args []string) {
|
|||||||
case "retry":
|
case "retry":
|
||||||
n, err = r.KillAllRetryTasks()
|
n, err = r.KillAllRetryTasks()
|
||||||
default:
|
default:
|
||||||
fmt.Printf("error: `asynqmon killall [state]` only accepts %v as the argument.\n", killallValidArgs)
|
fmt.Printf("error: `asynq killall [state]` only accepts %v as the argument.\n", killallValidArgs)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
@@ -25,19 +25,19 @@ var lsValidArgs = []string{"enqueued", "inprogress", "scheduled", "retry", "dead
|
|||||||
var lsCmd = &cobra.Command{
|
var lsCmd = &cobra.Command{
|
||||||
Use: "ls [state]",
|
Use: "ls [state]",
|
||||||
Short: "Lists tasks in the specified state",
|
Short: "Lists tasks in the specified state",
|
||||||
Long: `Ls (asynqmon ls) will list all tasks in the specified state in a table format.
|
Long: `Ls (asynq ls) will list all tasks in the specified state in a table format.
|
||||||
|
|
||||||
The command takes one argument which specifies the state of tasks.
|
The command takes one argument which specifies the state of tasks.
|
||||||
The argument value should be one of "enqueued", "inprogress", "scheduled",
|
The argument value should be one of "enqueued", "inprogress", "scheduled",
|
||||||
"retry", or "dead".
|
"retry", or "dead".
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
asynqmon ls dead -> Lists all tasks in dead state
|
asynq ls dead -> Lists all tasks in dead state
|
||||||
|
|
||||||
Enqueued tasks requires a queue name after ":"
|
Enqueued tasks requires a queue name after ":"
|
||||||
Example:
|
Example:
|
||||||
asynqmon ls enqueued:default -> List tasks from default queue
|
asynq ls enqueued:default -> List tasks from default queue
|
||||||
asynqmon ls enqueued:critical -> List tasks from critical queue
|
asynq ls enqueued:critical -> List tasks from critical queue
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactValidArgs(1),
|
Args: cobra.ExactValidArgs(1),
|
||||||
Run: ls,
|
Run: ls,
|
||||||
@@ -72,7 +72,7 @@ func ls(cmd *cobra.Command, args []string) {
|
|||||||
switch parts[0] {
|
switch parts[0] {
|
||||||
case "enqueued":
|
case "enqueued":
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
fmt.Printf("error: Missing queue name\n`asynqmon ls enqueued:[queue name]`\n")
|
fmt.Printf("error: Missing queue name\n`asynq ls enqueued:[queue name]`\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
listEnqueued(r, parts[1])
|
listEnqueued(r, parts[1])
|
||||||
@@ -85,7 +85,7 @@ func ls(cmd *cobra.Command, args []string) {
|
|||||||
case "dead":
|
case "dead":
|
||||||
listDead(r)
|
listDead(r)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("error: `asynqmon ls [state]`\nonly accepts %v as the argument.\n", lsValidArgs)
|
fmt.Printf("error: `asynq ls [state]`\nonly accepts %v as the argument.\n", lsValidArgs)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -18,11 +18,11 @@ import (
|
|||||||
var rmqCmd = &cobra.Command{
|
var rmqCmd = &cobra.Command{
|
||||||
Use: "rmq [queue name]",
|
Use: "rmq [queue name]",
|
||||||
Short: "Removes the specified queue",
|
Short: "Removes the specified queue",
|
||||||
Long: `Rmq (asynqmon rmq) will remove the specified queue.
|
Long: `Rmq (asynq rmq) will remove the specified queue.
|
||||||
By default, it will remove the queue only if it's empty.
|
By default, it will remove the queue only if it's empty.
|
||||||
Use --force option to override this behavior.
|
Use --force option to override this behavior.
|
||||||
|
|
||||||
Example: asynqmon rmq low -> Removes "low" queue`,
|
Example: asynq rmq low -> Removes "low" queue`,
|
||||||
Args: cobra.ExactValidArgs(1),
|
Args: cobra.ExactValidArgs(1),
|
||||||
Run: rmq,
|
Run: rmq,
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ func rmq(cmd *cobra.Command, args []string) {
|
|||||||
err := r.RemoveQueue(args[0], rmqForce)
|
err := r.RemoveQueue(args[0], rmqForce)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(*rdb.ErrQueueNotEmpty); ok {
|
if _, ok := err.(*rdb.ErrQueueNotEmpty); ok {
|
||||||
fmt.Printf("error: %v\nIf you are sure you want to delete it, run 'asynqmon rmq --force %s'\n", err, args[0])
|
fmt.Printf("error: %v\nIf you are sure you want to delete it, run 'asynq rmq --force %s'\n", err, args[0])
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("error: %v", err)
|
fmt.Printf("error: %v", err)
|
@@ -26,9 +26,9 @@ var password string
|
|||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "asynqmon",
|
Use: "asynq",
|
||||||
Short: "A monitoring tool for asynq queues",
|
Short: "A monitoring tool for asynq queues",
|
||||||
Long: `Asynqmon is a montoring CLI to inspect tasks and queues managed by asynq.`,
|
Long: `Asynq is a montoring CLI to inspect tasks and queues managed by asynq.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
@@ -43,7 +43,7 @@ func Execute() {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file to set flag defaut values (default is $HOME/.asynqmon.yaml)")
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file to set flag defaut values (default is $HOME/.asynq.yaml)")
|
||||||
rootCmd.PersistentFlags().StringVarP(&uri, "uri", "u", "127.0.0.1:6379", "redis server URI")
|
rootCmd.PersistentFlags().StringVarP(&uri, "uri", "u", "127.0.0.1:6379", "redis server URI")
|
||||||
rootCmd.PersistentFlags().IntVarP(&db, "db", "n", 0, "redis database number (default is 0)")
|
rootCmd.PersistentFlags().IntVarP(&db, "db", "n", 0, "redis database number (default is 0)")
|
||||||
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password to use when connecting to redis server")
|
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password to use when connecting to redis server")
|
||||||
@@ -65,9 +65,9 @@ func initConfig() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search config in home directory with name ".asynqmon" (without extension).
|
// Search config in home directory with name ".asynq" (without extension).
|
||||||
viper.AddConfigPath(home)
|
viper.AddConfigPath(home)
|
||||||
viper.SetConfigName(".asynqmon")
|
viper.SetConfigName(".asynq")
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
@@ -18,64 +18,64 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// psCmd represents the ps command
|
// serversCmd represents the servers command
|
||||||
var psCmd = &cobra.Command{
|
var serversCmd = &cobra.Command{
|
||||||
Use: "ps",
|
Use: "servers",
|
||||||
Short: "Shows all background worker processes",
|
Short: "Shows all running worker servers",
|
||||||
Long: `Ps (asynqmon ps) will show all background worker processes
|
Long: `Servers (asynq servers) will show all running worker servers
|
||||||
backed by the specified redis instance.
|
pulling tasks from the specified redis instance.
|
||||||
|
|
||||||
The command shows the following for each process:
|
The command shows the following for each server:
|
||||||
* Host and PID of the process
|
* Host and PID of the process in which the server is running
|
||||||
* Number of active workers out of worker pool
|
* Number of active workers out of worker pool
|
||||||
* Queue configuration
|
* Queue configuration
|
||||||
* State of the worker process ("running" | "stopped")
|
* State of the worker server ("running" | "quiet")
|
||||||
* Time the process was started
|
* Time the server was started
|
||||||
|
|
||||||
A "running" process is processing tasks in queues.
|
A "running" server is pulling tasks from queues and processing them.
|
||||||
A "stopped" process is no longer processing new tasks.`,
|
A "quiet" server is no longer pulling new tasks from queues`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: ps,
|
Run: servers,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(psCmd)
|
rootCmd.AddCommand(serversCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ps(cmd *cobra.Command, args []string) {
|
func servers(cmd *cobra.Command, args []string) {
|
||||||
r := rdb.NewRDB(redis.NewClient(&redis.Options{
|
r := rdb.NewRDB(redis.NewClient(&redis.Options{
|
||||||
Addr: viper.GetString("uri"),
|
Addr: viper.GetString("uri"),
|
||||||
DB: viper.GetInt("db"),
|
DB: viper.GetInt("db"),
|
||||||
Password: viper.GetString("password"),
|
Password: viper.GetString("password"),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
processes, err := r.ListProcesses()
|
servers, err := r.ListServers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if len(processes) == 0 {
|
if len(servers) == 0 {
|
||||||
fmt.Println("No processes")
|
fmt.Println("No running servers")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort by hostname and pid
|
// sort by hostname and pid
|
||||||
sort.Slice(processes, func(i, j int) bool {
|
sort.Slice(servers, func(i, j int) bool {
|
||||||
x, y := processes[i], processes[j]
|
x, y := servers[i], servers[j]
|
||||||
if x.Host != y.Host {
|
if x.Host != y.Host {
|
||||||
return x.Host < y.Host
|
return x.Host < y.Host
|
||||||
}
|
}
|
||||||
return x.PID < y.PID
|
return x.PID < y.PID
|
||||||
})
|
})
|
||||||
|
|
||||||
// print processes
|
// print server info
|
||||||
cols := []string{"Host", "PID", "State", "Active Workers", "Queues", "Started"}
|
cols := []string{"Host", "PID", "State", "Active Workers", "Queues", "Started"}
|
||||||
printRows := func(w io.Writer, tmpl string) {
|
printRows := func(w io.Writer, tmpl string) {
|
||||||
for _, ps := range processes {
|
for _, info := range servers {
|
||||||
fmt.Fprintf(w, tmpl,
|
fmt.Fprintf(w, tmpl,
|
||||||
ps.Host, ps.PID, ps.Status,
|
info.Host, info.PID, info.Status,
|
||||||
fmt.Sprintf("%d/%d", ps.ActiveWorkerCount, ps.Concurrency),
|
fmt.Sprintf("%d/%d", info.ActiveWorkerCount, info.Concurrency),
|
||||||
formatQueues(ps.Queues), timeAgo(ps.Started))
|
formatQueues(info.Queues), timeAgo(info.Started))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printTable(cols, printRows)
|
printTable(cols, printRows)
|
@@ -33,7 +33,7 @@ Specifically, the command shows the following:
|
|||||||
To monitor the tasks continuously, it's recommended that you run this
|
To monitor the tasks continuously, it's recommended that you run this
|
||||||
command in conjunction with the watch command.
|
command in conjunction with the watch command.
|
||||||
|
|
||||||
Example: watch -n 3 asynqmon stats -> Shows current state of tasks every three seconds`,
|
Example: watch -n 3 asynq stats -> Shows current state of tasks every three seconds`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: stats,
|
Run: stats,
|
||||||
}
|
}
|
@@ -20,9 +20,9 @@ import (
|
|||||||
var workersCmd = &cobra.Command{
|
var workersCmd = &cobra.Command{
|
||||||
Use: "workers",
|
Use: "workers",
|
||||||
Short: "Shows all running workers information",
|
Short: "Shows all running workers information",
|
||||||
Long: `Workers (asynqmon workers) will show all running workers information.
|
Long: `Workers (asynq workers) will show all running workers information.
|
||||||
|
|
||||||
The command shows the follwoing for each worker:
|
The command shows the following for each worker:
|
||||||
* Process in which the worker is running
|
* Process in which the worker is running
|
||||||
* ID of the task worker is processing
|
* ID of the task worker is processing
|
||||||
* Type of the task worker is processing
|
* Type of the task worker is processing
|
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/hibiken/asynq/tools/asynqmon/cmd"
|
import "github.com/hibiken/asynq/tools/asynq/cmd"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|