Manage queues

Manage queues

Create, read, update, list, purge, and delete a queue from Go, TypeScript, or HTTP.

You manage a queue at its address, the same bucket and key pair a stream uses. A create needs only the queue's kind, and every other setting has a default, which the queue API lists. Read a queue to see its settings, and update it to change them. Purge it to drop its messages, and delete it when you are done. The API has the queue routes. From a shell, nova queue lists, shows, purges, and replays queues, but cannot create, update, or delete them.

Create a queue

Create a queue before you send to it. This one holds each claim for a minute and gives each message three attempts.

emails := client.Address{Bucket: "demo", Key: "emails"}

info, err := c.CreateQueue(ctx, emails, client.QueueConfig{
	Kind:     client.QueueStandard,
	ClaimTTL: time.Minute,                       // a claim lasts a minute unless extended
	Retry:    client.QueueRetry{MaxAttempts: 3}, // dead-letter after the third attempt
})
if err != nil {
	return err
}

fmt.Println("created", info.Address.Key, "with", len(info.Ranges), "range")
Output
created emails with 1 range

A queue can only be created at an address that holds no stream. The kind, the claims mode, and the number of initial ranges are fixed at creation; ranges then split and merge as the queue's load changes. A create is safe to retry: the SDKs send the same creation token on each attempt of one call, and over HTTP you can send your own creation_token. Creating a queue needs the create permission.

Read a queue

info, err := c.GetQueue(ctx, emails)
if err != nil {
	return err
}

cfg := info.Config
fmt.Printf("%s queue, %s claims, claim TTL %v s, %d attempts\n",
	cfg.Kind, cfg.Claims, cfg.ClaimTTL.Seconds(), cfg.Retry.MaxAttempts)
Output
standard queue, memory claims, claim TTL 60 s, 3 attempts

emails is the address from the first example.

The result holds every setting with its default filled in, the queue's open ranges with the node that delivers each, and the id of its dead-letter stream.

Update a queue

An update replaces every setting at once. A setting you leave out goes back to its default, except the kind, the claims mode, and the dedup policy, which keep the queue's own. So read the queue, change what you want, and send it all back:

// An update replaces every setting: read them, change one, send all back.
info, err := c.GetQueue(ctx, emails)
if err != nil {
	return err
}

cfg := info.Config
cfg.Concurrency = 32

updated, err := c.UpdateQueue(ctx, emails, cfg)
if err != nil {
	return err
}

fmt.Printf("concurrency %d, claim TTL %v s\n", updated.Config.Concurrency, updated.Config.ClaimTTL.Seconds())
Output
concurrency 32, claim TTL 60 s

The nodes that deliver the queue apply an update within about 5 seconds, without restarting delivery. When a change applies says how each setting treats the claims already out. An update that names another kind or claims mode, or any initial ranges, is refused and changes nothing. Updating needs the create permission.

List queues

opts := client.QueueListOptions{KeyPrefix: "email", Limit: 100}

for {
	page, next, err := c.ListQueues(ctx, "demo", opts)
	if err != nil {
		return err
	}

	for _, q := range page {
		fmt.Println(q.Address.Key, q.Config.Kind)
	}

	if next == "" {
		break
	}

	opts.After = next
}
Output
emails standard

A listing returns one page of a bucket's queues in key order, at most 100, and the cursor of the next page, which is empty on the last. A key prefix narrows it. Listing needs the list permission.

Purge a queue

A purge removes every message the queue has not settled, including those waiting for a retry or a delay, and takes back the claims workers hold. The queue, its settings, and its dead letters stay.

removed, err := queue.Purge(ctx)

if partial, ok := errors.AsType[*client.PurgeError](err); ok {
	// A node was out of reach: the ranges it delivers still hold messages.
	return fmt.Errorf("purged %d, then: %w", partial.Purged, partial.Err)
}

if err != nil {
	return err
}

fmt.Println("purged", removed)
Output
purged 2

The count is the messages this call removed. A worker that then settles a purged message's claim is refused, as refusals shows. If a node that delivers one of the ranges is out of reach, the purge empties the ranges it can reach and fails with what it removed. That is a *client.PurgeError in Go, a PurgeError in TypeScript, or 503 with purged in the body over HTTP. Purge again once the node is back. Purging needs the trim permission.

A purge can need the admin permission

When a range's messages are stored on a different node from the one that delivers them, a purge fails with PermissionDenied, even with the trim permission. Purge with a token that holds nova:admin. nova queue purge has the same limit.

Delete a queue

if err := c.DeleteQueue(ctx, emails); err != nil {
	return err
}

fmt.Println("deleted")
Output
deleted

A delete removes the queue, its messages, its dead letters, and its delivery state. Deleting needs the delete permission.

When a call is refused

Error: Go, TypeScript, HTTPWhenWhat to do
ErrQueueExists, QueueExistsError, 409A queue or a stream already holds the addressUse the queue there, or another address
ErrInvalidQueueConfig, InvalidQueueConfigError, 400No kind, another kind or claims mode on an update, or a setting out of boundsFix the settings. Nothing changed
ErrStreamNotFound, StreamNotFoundError, 404No queue at the address, including on a deleteCheck the address
*PurgeError, PurgeError, 503 with purgedA purge could not reach every rangePurge again later, and add the counts
ErrUnavailable, UnavailableErrorNo node answered within the retry budget, 30 seconds by defaultTry again later. A create may have been made: creating it again reports that it exists

See also

On this page