2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 23:32:17 +08:00

Update readme

This commit is contained in:
Ken Hibino 2020-05-16 10:44:39 -07:00
parent aef0775c05
commit 4e3e053989

View File

@ -68,9 +68,10 @@ const (
ImageProcessing = "image:process" ImageProcessing = "image:process"
) )
//-------------------------------------------- //----------------------------------------------
// Write function NewXXXTask to create a task. // 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 { func NewEmailDeliveryTask(userID int, tmplID string) *asynq.Task {
payload := map[string]interface{}{"user_id": userID, "template_id": tmplID} payload := map[string]interface{}{"user_id": userID, "template_id": tmplID}
@ -82,13 +83,13 @@ func NewImageProcessingTask(src, dst string) *asynq.Task {
return asynq.NewTask(ImageProcessing, payload) return asynq.NewTask(ImageProcessing, payload)
} }
//------------------------------------------------------------- //---------------------------------------------------------------
// Write function HandleXXXTask to handle the given task. // Write a function HandleXXXTask to handle the input task.
// NOTE: It satisfies the asynq.HandlerFunc interface. // Note that it satisfies the asynq.HandlerFunc interface.
// //
// Handler doesn't need to be a function. You can define a type // Handler doesn't need to be a function. You can define a type
// that satisfies asynq.Handler interface. See example below. // that satisfies asynq.Handler interface. See examples below.
//------------------------------------------------------------- //---------------------------------------------------------------
func HandleEmailDeliveryTask(ctx context.Context, t *asynq.Task) error { func HandleEmailDeliveryTask(ctx context.Context, t *asynq.Task) error {
userID, err := t.Payload.GetInt("user_id") userID, err := t.Payload.GetInt("user_id")
@ -104,11 +105,11 @@ func HandleEmailDeliveryTask(ctx context.Context, t *asynq.Task) error {
return nil return nil
} }
// ImageProcessor implements asynq.Handler interface.
type ImageProcesser struct { type ImageProcesser struct {
// ... fields for struct // ... fields for struct
} }
// ImageProcessor implements asynq.Handler.
func (p *ImageProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error { func (p *ImageProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error {
src, err := t.Payload.GetString("src") src, err := t.Payload.GetString("src")
if err != nil { if err != nil {
@ -149,10 +150,10 @@ func main() {
c := asynq.NewClient(r) c := asynq.NewClient(r)
defer c.Close() defer c.Close()
// ---------------------------------------------------- // ------------------------------------------------------
// Example 1: Enqueue task to be processed immediately. // Example 1: Enqueue task to be processed immediately.
// Use (*Client).Enqueue method. // Use (*Client).Enqueue method.
// ---------------------------------------------------- // ------------------------------------------------------
t := tasks.NewEmailDeliveryTask(42, "some:template:id") t := tasks.NewEmailDeliveryTask(42, "some:template:id")
err := c.Enqueue(t) err := c.Enqueue(t)
@ -161,10 +162,10 @@ func main() {
} }
// ---------------------------------------------------------- // ------------------------------------------------------------
// Example 2: Schedule task to be processed in the future. // Example 2: Schedule task to be processed in the future.
// Use (*Client).EnqueueIn or (*Client).EnqueueAt. // Use (*Client).EnqueueIn or (*Client).EnqueueAt.
// ---------------------------------------------------------- // ------------------------------------------------------------
t = tasks.NewEmailDeliveryTask(42, "other:template:id") t = tasks.NewEmailDeliveryTask(42, "other:template:id")
err = c.EnqueueIn(24*time.Hour, t) err = c.EnqueueIn(24*time.Hour, t)
@ -173,10 +174,10 @@ func main() {
} }
// -------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Example 3: Set options to tune task processing behavior. // Example 3: Set options to tune task processing behavior.
// Options include MaxRetry, Queue, Timeout, Deadline, Unique etc. // Options include MaxRetry, Queue, Timeout, Deadline, Unique etc.
// -------------------------------------------------------------------------- // ----------------------------------------------------------------------------
c.SetDefaultOptions(tasks.ImageProcessing, asynq.MaxRetry(10), asynq.Timeout(time.Minute)) c.SetDefaultOptions(tasks.ImageProcessing, asynq.MaxRetry(10), asynq.Timeout(time.Minute))
@ -186,10 +187,10 @@ func main() {
log.Fatal("could not enqueue task: %v", err) log.Fatal("could not enqueue task: %v", err)
} }
// -------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Example 4: Pass options to tune task processing behavior at enqueue time. // Example 4: Pass options to tune task processing behavior at enqueue time.
// Options passed at enqueue time override default ones, if any. // Options passed at enqueue time override default ones, if any.
// -------------------------------------------------------------------------- // ---------------------------------------------------------------------------
t = tasks.NewImageProcessingTask("some/blobstore/url", "other/blobstore/url") t = tasks.NewImageProcessingTask("some/blobstore/url", "other/blobstore/url")
err = c.Enqueue(t, asynq.Queue("critical"), asynq.Timeout(30*time.Second)) err = c.Enqueue(t, asynq.Queue("critical"), asynq.Timeout(30*time.Second))