2020-01-05 09:06:23 -08:00
# Asynq
2020-01-20 15:17:41 -08:00
[](https://travis-ci.com/hibiken/asynq)
[](https://opensource.org/licenses/MIT)
[](https://goreportcard.com/report/github.com/hibiken/asynq)
[](https://godoc.org/github.com/hibiken/asynq)
[](https://gitter.im/go-asynq/community)
2019-11-30 09:38:46 -08:00
2020-01-26 19:58:48 -08:00
Asynq is a simple 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.
2020-01-14 21:19:06 -08:00
2020-01-23 06:33:34 -08:00
**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.
2019-12-27 16:26:11 -08:00
2020-01-29 21:23:40 -08:00

2020-01-18 20:31:22 -08:00
2019-11-30 09:38:46 -08:00
## Installation
2020-01-26 19:58:48 -08:00
To install `asynq` library, run the following command:
2020-01-20 15:17:41 -08:00
2020-01-26 19:58:48 -08:00
```sh
2020-01-14 21:19:06 -08:00
go get -u github.com/hibiken/asynq
2019-11-30 09:38:46 -08:00
```
2020-02-05 21:58:05 -08:00
## Quickstart
2020-01-20 15:17:41 -08:00
2020-01-26 19:58:48 -08:00
First, make sure you are running a Redis server locally.
2020-01-23 06:33:34 -08:00
```sh
2020-02-07 06:45:36 -08:00
$ redis-server
2020-01-23 06:33:34 -08:00
```
2020-01-26 19:58:48 -08:00
To create and schedule tasks, use `Client` and provide a task and when to process the task.
2020-01-14 21:19:06 -08:00
```go
2020-01-26 19:58:48 -08:00
func main() {
r := &asynq.RedisClientOpt{
Addr: "localhost:6379",
}
2020-01-14 21:19:06 -08:00
2020-01-26 19:58:48 -08:00
client := asynq.NewClient(r)
2019-11-30 09:38:46 -08:00
2020-01-26 19:58:48 -08:00
// Create a task with task type and payload
t1 := asynq.NewTask("send_welcome_email", map[string]interface{}{"user_id": 42})
2019-11-30 09:38:46 -08:00
2020-01-26 19:58:48 -08:00
t2 := asynq.NewTask("send_reminder_email", map[string]interface{}{"user_id": 42})
2020-01-23 06:33:34 -08:00
2020-01-26 19:58:48 -08:00
// Process immediately
err := client.Schedule(t1, time.Now())
2020-01-23 06:33:34 -08:00
2020-01-26 19:58:48 -08:00
// Process 24 hrs later
err = client.Schedule(t2, time.Now().Add(24 * time.Hour))
// If processing fails, retry up to 10 times (Default is 25)
err = client.Schedule(t1, time.Now(), asynq.Retry(10))
2020-01-23 06:33:34 -08:00
2020-01-26 19:58:48 -08:00
// Use custom queue called "critical"
err = client.Schedule(t1, time.Now(), asynq.Queue("critical"))
2020-01-23 06:33:34 -08:00
}
```
2020-01-26 19:58:48 -08:00
To start the background workers, use `Background` and provide your `Handler` to process the tasks.
2019-11-30 09:38:46 -08:00
```go
func main() {
2020-01-26 19:58:48 -08:00
r := &asynq.RedisClientOpt{
Addr: "localhost:6379",
2020-01-20 15:17:41 -08:00
}
2019-11-30 09:38:46 -08:00
2020-01-26 19:58:48 -08:00
bg := asynq.NewBackground(r, &asynq.Config{
// Specify how many concurrent workers to use
2020-01-14 21:19:06 -08:00
Concurrency: 10,
2020-01-26 19:58:48 -08:00
// You can optionally create multiple queues
// with different priority level
Queues: map[string]uint{
"critical": 6,
"default": 3,
"low": 1,
},
// See the godoc for other configuration options
2019-12-29 16:55:51 -08:00
})
2019-11-30 09:38:46 -08:00
2019-12-27 16:26:11 -08:00
bg.Run(handler)
2019-11-30 09:38:46 -08:00
}
2019-12-27 16:26:11 -08:00
```
2020-01-26 19:58:48 -08:00
`Handler` is an interface with one method `ProcessTask` with the following signature.
2019-11-30 09:38:46 -08:00
2019-12-27 16:26:11 -08:00
```go
// ProcessTask should return nil if the processing of a task
// is successful.
//
// If ProcessTask return a non-nil error or panics, the task
2020-01-26 19:58:48 -08:00
// will be retried after delay.
2019-12-27 16:26:11 -08:00
type Handler interface {
ProcessTask(*Task) error
}
```
2020-01-26 19:58:48 -08:00
For a more detailed walk-through of the library, see our [Getting Started Guide ](https://github.com/hibiken/asynq/wiki/Getting-Started ).
2020-01-23 06:33:34 -08:00
2020-01-26 19:58:48 -08:00
To Learn more about `asynq` features and APIs, see our [Wiki pages ](https://github.com/hibiken/asynq/wiki ) and [godoc ](https://godoc.org/github.com/hibiken/asynq ).
2020-01-20 15:17:41 -08:00
2020-01-26 19:58:48 -08:00
## Requirements
2020-01-20 15:17:41 -08:00
2020-01-26 19:58:48 -08:00
| Dependency | Version |
| -------------------------- | ------- |
| [Redis ](https://redis.io/ ) | v2.8+ |
| [Go ](https://golang.org/ ) | v1.12+ |
2020-01-20 15:17:41 -08:00
2020-01-23 06:33:34 -08:00
## Command Line Tool
2020-01-14 21:19:06 -08:00
2020-01-23 06:33:34 -08:00
Asynq ships with a command line tool to inspect the state of queues and tasks.
2020-01-18 20:31:22 -08:00
2020-01-23 06:33:34 -08:00
To install, run the following command:
2020-01-18 20:31:22 -08:00
2020-02-07 06:45:36 -08:00
```sh
go get -u github.com/hibiken/asynq/tools/asynqmon
```
2020-01-18 20:31:22 -08:00
2020-01-23 06:33:34 -08:00
For details on how to use the tool, refer to the tool's [README ](/tools/asynqmon/README.md ).
2020-01-14 21:19:06 -08:00
2020-01-05 09:06:23 -08:00
## Acknowledgements
- [Sidekiq ](https://github.com/mperham/sidekiq ) : Many of the design ideas are taken from sidekiq and its Web UI
- [Cobra ](https://github.com/spf13/cobra ) : Asynqmon CLI is built with cobra
2019-12-27 16:26:11 -08:00
## License
Asynq is released under the MIT license. See [LICENSE ](https://github.com/hibiken/asynq/blob/master/LICENSE ).