Help us shape the future of Go! Take the 2026 Go Developer Survey

taskqueue

package
v1.21.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package taskqueue is what the producers and the consumers of the asynq task queue share: the task type names, the payloads, the task IDs that deduplicate enqueues, and the client and worker middleware that carry the OpenTelemetry trace context across the queue. Producers depend on these definitions, never on the consumers in internal/transport/task.

asynq has no message headers, so the producer wraps the task payload in an envelope holding the W3C trace context, and the worker-side middleware unwraps it, resumes the producer's trace and opens a consumer span around the handler. Payloads without an envelope (older in-flight tasks, traceless producers, scheduler ticks) pass through untouched and get a root span, so every task execution logs a trace id either way.

Index

Constants

View Source
const (
	EmailTypeVerify        = "verify"
	EmailTypeMaintenance   = "maintenance"
	EmailTypeExpiration    = "expiration"
	EmailTypeTrafficExceed = "traffic_exceed"
	EmailTypeCustom        = "custom"
)

The email types of SendEmailPayload: each selects the configured subject and template, except EmailTypeCustom, whose payload carries the text.

View Source
const (
	DeferCloseOrder                 = "defer:order:close"
	ForthwithActivateOrder          = "forthwith:order:activate"
	SchedulerReconcilePaidOrders    = "scheduler:order:reconcile-paid"
	SchedulerReconcilePendingOrders = "scheduler:order:reconcile-pending"
	SchedulerPublishOrderEvents     = "scheduler:order:publish-events"
	SchedulerCleanupOrderEvents     = "scheduler:order:cleanup-events"
	// SchedulerDailyOrderReport pushes the previous day's settlement summary
	// to the administrators bound on Telegram.
	SchedulerDailyOrderReport = "scheduler:order:daily-report"
)

The order task types.

View Source
const (
	SchedulerCheckSubscription = "scheduler:check:subscription"
	// SchedulerRemindExpiringSubscriptions warns owners whose subscription
	// expires soon. Daily, at a civil hour.
	SchedulerRemindExpiringSubscriptions = "scheduler:subscription:remind-expiring"
	// SchedulerDispatchDomainEvents pumps the generic domain-event outbox
	// onto the asynq queue as events:deliver tasks.
	SchedulerDispatchDomainEvents = "scheduler:events:dispatch"
	SchedulerResetTraffic         = "scheduler:reset:traffic"
	SchedulerTrafficStat          = "scheduler:traffic:stat"
	SchedulerLogCleanup           = "scheduler:log:cleanup"
	SchedulerFlushTraffic         = "scheduler:flush:traffic"
)

The task types of the scheduled jobs.

View Source
const (
	// ScheduledBatchSendEmail runs a batch email task.
	ScheduledBatchSendEmail = "scheduled:email:batch"

	// ForthwithQuotaTask runs a quota task right away.
	ForthwithQuotaTask = "forthwith:quota:task"

	// SchedulerExchangeRate refreshes the currency exchange rates.
	SchedulerExchangeRate = "scheduler:exchange:rate"
)
View Source
const BatchEmailTaskTimeout = 6 * time.Hour

BatchEmailTaskTimeout bounds one run of a batch email task, as the asynq.Timeout of every batch email enqueue (the initial one and each continuation). asynq's default is 30 minutes, which a paced campaign to thousands of recipients overruns; the worker stops itself before the deadline and the campaign continues from a follow-up task, so the timeout only has to be long enough for a chunk worth running.

View Source
const (
	// EventDeliver carries one domain event from the outbox to its topic
	// subscribers; asynq is the broker (retry, backoff, dead-letter archive).
	EventDeliver = "events:deliver"
)
View Source
const (
	// ForthwithSendEmail sends one email (SendEmailPayload).
	ForthwithSendEmail = "forthwith:email:send"
)
View Source
const (
	// ForthwithSendSms sends one text message (SendSmsPayload).
	ForthwithSendSms = "forthwith:sms:send"
)

Variables

This section is empty.

Functions

func ActivationTaskID

func ActivationTaskID(orderNo string) string

ActivationTaskID is the task ID of an order's activation: every producer (payment callback, reconciliation) enqueues under the same ID, so a paid order has one activation task queued at a time, however often its payment is reported.

func EventTaskID

func EventTaskID(id int64) string

EventTaskID deduplicates enqueues of the same outbox event: the publish pump may replay an enqueue when marking the event published fails, and the task-id conflict turns that replay into a no-op.

func Middleware

func Middleware() asynq.MiddlewareFunc

Middleware resumes the producer's trace from the payload envelope (when present), hands the handler the unwrapped payload, and records the task execution as a consumer span.

func Wrap

func Wrap(ctx context.Context, task *asynq.Task) *asynq.Task

Wrap returns a task whose payload carries ctx's trace context; without an active span (or with a payload the envelope cannot hold) it returns the task unchanged.

Types

type Client

type Client struct {
	*asynq.Client
}

Client wraps *asynq.Client so EnqueueContext stamps the caller's trace context onto the task. Wrapping rebuilds the task, which drops options embedded at NewTask time — pass options to EnqueueContext instead. The plain Enqueue promotes from the inner client untraced (it has no context to read).

func NewClient

func NewClient(client *asynq.Client) *Client

NewClient wraps client.

func (*Client) EnqueueContext

func (c *Client) EnqueueContext(ctx context.Context, task *asynq.Task, opts ...asynq.Option) (*asynq.TaskInfo, error)

EnqueueContext enqueues task with ctx's trace context in its payload.

type DeferCloseOrderPayload

type DeferCloseOrderPayload struct {
	OrderNo string `json:"order_no"`
}

DeferCloseOrderPayload names the unpaid order DeferCloseOrder closes.

type EventDeliverPayload

type EventDeliverPayload struct {
	ID      int64  `json:"id"`
	Topic   string `json:"topic"`
	Key     string `json:"key"`
	Payload string `json:"payload"`
}

EventDeliverPayload is the broker representation of one outbox event.

type ForthwithActivateOrderPayload

type ForthwithActivateOrderPayload struct {
	OrderNo string `json:"order_no"`
}

ForthwithActivateOrderPayload names the paid order to activate.

type SendEmailPayload

type SendEmailPayload struct {
	requestmeta.Metadata
	Type    string         `json:"type"`
	Email   string         `json:"to"`
	Subject string         `json:"subject"`
	Content map[string]any `json:"content"`
}

SendEmailPayload is one email to send; Content fills the template of its Type.

type SendSmsPayload

type SendSmsPayload struct {
	requestmeta.Metadata
	Type          uint8  `json:"type"`
	Telephone     string `json:"telephone"`
	TelephoneArea string `json:"area"`
	Content       string `json:"content"`
}

SendSmsPayload is one text message to send: Content is the verification code the message carries.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL