2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00
golang基于redis的异步队列
Go to file
2020-02-16 21:25:10 -08:00
.github/ISSUE_TEMPLATE Update issue templates 2019-12-27 10:45:45 -08:00
.travis Run and compare benchmarks after successful ci-build 2020-01-30 21:38:16 -08:00
docs/assets [ci skip] Update readme 2020-02-05 22:12:26 -08:00
internal Make heartbeater goroutine a monitor goroutine for process info. 2020-02-16 18:22:59 -08:00
tools/asynqmon Use int as priority value type. 2020-02-13 06:23:51 -08:00
.gitignore [ci skip] Allow config file to set default values for flags 2020-01-19 09:10:48 -08:00
.travis.yml Run and compare benchmarks after successful ci-build 2020-01-30 21:38:16 -08:00
asynq_test.go Fix benchmark tests 2020-01-18 15:07:15 -08:00
asynq.go [ci skip] Fix comments 2020-02-06 20:46:59 -08:00
background_test.go Use int as priority value type. 2020-02-13 06:23:51 -08:00
background.go Fix data race in subscriber test 2020-02-16 18:22:59 -08:00
benchmark_test.go Use int as priority value type. 2020-02-13 06:23:51 -08:00
CHANGELOG.md v0.4.0 2020-02-13 06:25:27 -08:00
client_test.go Allow user to specify timeout per task 2020-02-13 06:23:51 -08:00
client.go Allow user to specify timeout per task 2020-02-13 06:23:51 -08:00
CONTRIBUTING.md [ci skip] Add contribution doc 2020-02-08 09:48:04 -08:00
doc.go Update docs for context and timeout 2020-02-13 06:23:51 -08:00
go.mod Rate limit error logs 2020-01-22 06:36:18 -08:00
go.sum Rate limit error logs 2020-01-22 06:36:18 -08:00
heartbeat_test.go Clean up heartbeater test 2020-02-16 18:22:59 -08:00
heartbeat.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
LICENSE Add MIT License 2019-11-30 10:21:25 -08:00
logger_test.go Add custom logger 2020-01-22 06:02:53 -08:00
logger.go Add custom logger 2020-01-22 06:02:53 -08:00
payload_test.go Add test for payload key not exist 2020-01-05 09:55:39 -08:00
payload.go [ci skip] Update docs 2020-01-16 21:04:46 -08:00
processor_test.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
processor.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
README.md Use int as priority value type. 2020-02-13 06:23:51 -08:00
scheduler_test.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
scheduler.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
subscriber_test.go Fix data race in subscriber test 2020-02-16 18:22:59 -08:00
subscriber.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00
syncer_test.go Fix data race in syncer test 2020-02-16 21:25:10 -08:00
syncer.go Use sync.WaitGroup for shutdown 2020-02-16 18:22:59 -08:00

Asynq

Build Status License: MIT Go Report Card GoDoc Gitter chat

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.

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.

Gif

Installation

To install asynq library, run the following command:

go get -u github.com/hibiken/asynq

Quickstart

First, make sure you are running a Redis server locally.

$ redis-server

To create and schedule tasks, use Client and provide a task and when to process the task.

func main() {
    r := &asynq.RedisClientOpt{
        Addr: "localhost:6379",
    }

    client := asynq.NewClient(r)

    // Create a task with task type and payload
    t1 := asynq.NewTask("send_welcome_email", map[string]interface{}{"user_id": 42})

    t2 := asynq.NewTask("send_reminder_email", map[string]interface{}{"user_id": 42})

    // Process immediately
    err := client.Schedule(t1, time.Now())

    // 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))

    // Use custom queue called "critical"
    err = client.Schedule(t1, time.Now(), asynq.Queue("critical"))

    // Use timeout to specify how long a task may run (Default is no limit)
    err = client.Schedule(t1, time.Now(), asynq.Timeout(30 * time.Second))
}

To start the background workers, use Background and provide your Handler to process the tasks.

func main() {
    r := &asynq.RedisClientOpt{
        Addr: "localhost:6379",
    }

    bg := asynq.NewBackground(r, &asynq.Config{
        // Specify how many concurrent workers to use
        Concurrency: 10,
        // You can optionally create multiple queues with different priority.
        Queues: map[string]int{
            "critical": 6,
            "default":  3,
            "low":      1,
        },
        // See the godoc for other configuration options
    })

    bg.Run(handler)
}

Handler is an interface with one method ProcessTask with the following signature.

// 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, *asynq.Task) error
}

For a more detailed walk-through of the library, see our Getting Started Guide.

To Learn more about asynq features and APIs, see our Wiki pages and godoc.

Requirements

Dependency Version
Redis v2.8+
Go v1.12+

Command Line Tool

Asynq ships with a command line tool to inspect the state of queues and tasks.

To install, run the following command:

go get -u github.com/hibiken/asynq/tools/asynqmon

For details on how to use the tool, refer to the tool's README.

Contributing

We are open to, and grateful for, any contributions (Github issues/pull-requests, feedback on Gitter channel, etc) made by the community. Please see the Contribution Guide before contributing.

Acknowledgements

  • Sidekiq : Many of the design ideas are taken from sidekiq and its Web UI
  • Cobra : Asynqmon CLI is built with cobra

License

Asynq is released under the MIT license. See LICENSE.