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-07-28 22:46:41 -07:00
.github/ISSUE_TEMPLATE Update issue templates 2019-12-27 10:45:45 -08:00
.travis Limit the number of tasks moved by CheckAndEnqueue to prevent a long 2020-06-08 06:06:18 -07:00
docs/assets Update all reference to asynqmon to Asynq CLI 2020-04-19 08:51:17 -07:00
internal Add healthchecker to check broker connection 2020-07-28 22:45:57 -07:00
tools Fix history command 2020-07-28 22:45:57 -07:00
.gitignore Update all reference to asynqmon to Asynq CLI 2020-04-19 08:51:17 -07:00
.travis.yml Limit the number of tasks moved by CheckAndEnqueue to prevent a long 2020-06-08 06:06:18 -07:00
asynq_test.go Clean up log messages 2020-05-16 08:00:35 -07:00
asynq.go Add ParseRedisURI helper function 2020-04-25 13:06:20 -07:00
benchmark_test.go Return Result struct to caller of Enqueue 2020-07-06 05:48:31 -07:00
CHANGELOG.md v0.11.0 2020-07-28 22:46:41 -07:00
client_test.go Add Inspector type 2020-07-28 22:45:57 -07:00
client.go Return Result struct to caller of Enqueue 2020-07-06 05:48:31 -07:00
context_test.go Replace github.com/rs/xid with github.com/google/uuid 2020-07-06 05:48:31 -07:00
context.go Minor code cleanup 2020-07-06 05:48:31 -07:00
CONTRIBUTING.md [ci skip] Add contribution doc 2020-02-08 09:48:04 -08:00
doc.go Return Result struct to caller of Enqueue 2020-07-06 05:48:31 -07:00
example_test.go Add ParseRedisURI helper function 2020-04-25 13:06:20 -07:00
go.mod Replace github.com/rs/xid with github.com/google/uuid 2020-07-06 05:48:31 -07:00
go.sum Replace github.com/rs/xid with github.com/google/uuid 2020-07-06 05:48:31 -07:00
healthcheck_test.go Add healthchecker to check broker connection 2020-07-28 22:45:57 -07:00
healthcheck.go Add healthchecker to check broker connection 2020-07-28 22:45:57 -07:00
heartbeat_test.go Refactor server state management 2020-05-31 06:41:19 -07:00
heartbeat.go Replace github.com/rs/xid with github.com/google/uuid 2020-07-06 05:48:31 -07:00
inspector_test.go Fix flaky test 2020-07-28 22:45:57 -07:00
inspector.go Add Pause and Unpause queue methods to Inspector 2020-07-28 22:45:57 -07:00
LICENSE Add MIT License 2019-11-30 10:21:25 -08:00
payload_test.go Fix JSON number ovewflow issue 2020-06-12 06:29:36 -07:00
payload.go Fix JSON number ovewflow issue 2020-06-12 06:29:36 -07:00
processor_test.go Add Inspector type 2020-07-28 22:45:57 -07:00
processor.go Do not start worker goroutine for task already exceeded its deadline 2020-07-06 05:48:31 -07:00
README.md Do not start worker goroutine for task already exceeded its deadline 2020-07-06 05:48:31 -07:00
recoverer_test.go Add Inspector type 2020-07-28 22:45:57 -07:00
recoverer.go Add recoverer 2020-07-06 05:48:31 -07:00
scheduler_test.go Add Inspector type 2020-07-28 22:45:57 -07:00
scheduler.go Limit the number of tasks moved by CheckAndEnqueue to prevent a long 2020-06-08 06:06:18 -07:00
servemux_test.go Add Use method to better support middlewares with ServeMux 2020-03-13 14:13:17 -07:00
servemux.go remove typo and redundant code 2020-05-22 05:11:54 -07:00
server_test.go Return Result struct to caller of Enqueue 2020-07-06 05:48:31 -07:00
server.go Add healthchecker to check broker connection 2020-07-28 22:45:57 -07:00
signals_unix.go Export Start, Stop and Quiet method on Server type 2020-04-19 08:51:17 -07:00
signals_windows.go Rename Background to Server 2020-04-19 08:51:17 -07:00
subscriber_test.go Change internal constructor signatures. 2020-05-17 13:25:24 -07:00
subscriber.go Change internal constructor signatures. 2020-05-17 13:25:24 -07:00
syncer_test.go Add deadline to syncRequest 2020-07-06 05:48:31 -07:00
syncer.go Add deadline to syncRequest 2020-07-06 05:48:31 -07:00

Asynq

Build Status License: MIT Go Report Card GoDoc Gitter chat codecov

Overview

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.

Task Queue Diagram

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

Quickstart

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

$ redis-server

Next, write a package that encapsulates task creation and task handling.

package tasks

import (
    "fmt"

    "github.com/hibiken/asynq"
)

// A list of task types.
const (
    EmailDelivery   = "email:deliver"
    ImageProcessing = "image:process"
)

//----------------------------------------------
// Write a function NewXXXTask to create a task.
// A task consists of a type and a payload.
//----------------------------------------------

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 a function HandleXXXTask to handle the input task.
// Note that 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 examples 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
}

// ImageProcessor implements asynq.Handler interface.
type ImageProcesser struct {
    // ... fields for struct
}

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
}

func NewImageProcessor() *ImageProcessor {
    // ... return an instance
}

In your web application code, import the above package and use Client to put tasks on the queue.
A task will be processed asynchronously 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.

package main

import (
    "time"

    "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")
    res, err := c.Enqueue(t)
    if err != nil {
        log.Fatal("could not enqueue task: %v", err)
    }
    fmt.Printf("Enqueued Result: %+v\n", res)


    // ------------------------------------------------------------
    // Example 2: Schedule task to be processed in the future.
    //            Use (*Client).EnqueueIn or (*Client).EnqueueAt.
    // ------------------------------------------------------------

    t = tasks.NewEmailDeliveryTask(42, "other:template:id")
    res, err = c.EnqueueIn(24*time.Hour, t)
    if err != nil {
        log.Fatal("could not schedule task: %v", err)
    }
    fmt.Printf("Enqueued Result: %+v\n", res)


    // ----------------------------------------------------------------------------
    // 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")
    res, err = c.Enqueue(t)
    if err != nil {
        log.Fatal("could not enqueue task: %v", err)
    }
    fmt.Printf("Enqueued Result: %+v\n", res)

    // ---------------------------------------------------------------------------
    // 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")
    res, err = c.Enqueue(t, asynq.Queue("critical"), asynq.Timeout(30*time.Second))
    if err != nil {
        log.Fatal("could not enqueue task: %v", err)
    }
    fmt.Printf("Enqueued Result: %+v\n", res)
}

Next, create a worker server to process these tasks in the background.
To start the background workers, use Server and provide your Handler to process the tasks.

You can optionally use ServeMux to create a handler, just as you would with "net/http" Handler.

package main

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
        Concurrency: 10,
        // Optionally specify multiple queues with different priority.
        Queues: map[string]int{
            "critical": 6,
            "default":  3,
            "low":      1,
        },
        // See the godoc for other configuration options
    })

    // mux maps a type to a handler
    mux := asynq.NewServeMux()
    mux.HandleFunc(tasks.EmailDelivery, tasks.HandleEmailDeliveryTask)
    mux.Handle(tasks.ImageProcessing, tasks.NewImageProcessor())
    // ...register other handlers...

    if err := srv.Run(mux); err != nil {
        log.Fatalf("could not run server: %v", err)
    }
}

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 and godoc.

Command Line Tool

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

Here's an example of running the stats command.

Gif

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

Installation

To install asynq library, run the following command:

go get -u github.com/hibiken/asynq

To install the CLI tool, run the following command:

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

Requirements

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

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
  • RQ : Client APIs are inspired by rq library.
  • Cobra : Asynq CLI is built with cobra

License

Asynq is released under the MIT license. See LICENSE.