Skip to content

Commit 8de801d

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 1eb6291 + f952d2b commit 8de801d

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

docs/recipes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Here you can find the most **delicious** recipes to cook delicious meals using o
1515
<!-- AUTO-GENERATED-CONTENT:START -->
1616
- [404 Handler](./404-handler/README.md) - Custom 404 error page handling.
1717
- [Air Live Reloading](./air/README.md) - Live reloading for Go applications.
18+
- [Asynq](./asynq/README.md) - Enqueue background jobs from Fiber and process them with an Asynq worker.
1819
- [Auth + Docker + Postgres + JWT](./auth-docker-postgres-jwt/README.md) - Authentication with Docker, Postgres, and JWT.
1920
- [Auth + JWT](./auth-jwt/README.md) - Simple JWT authentication.
2021
- [Autocert](./autocert/README.md) - Automatic TLS certificate management.

docs/recipes/asynq/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Asynq
3+
keywords: [asynq, redis, queue, worker, background jobs, tasks]
4+
description: Enqueue background jobs from Fiber and process them with an Asynq worker.
5+
---
6+
7+
# Fiber and Asynq example
8+
9+
[![Github](https://img.shields.io/static/v1?label=&message=Github&color=2ea44f&style=for-the-badge&logo=github)](https://github.com/gofiber/recipes/tree/master/asynq) [![StackBlitz](https://img.shields.io/static/v1?label=&message=StackBlitz&color=2ea44f&style=for-the-badge&logo=StackBlitz)](https://stackblitz.com/github/gofiber/recipes/tree/master/asynq)
10+
11+
## Description
12+
13+
This example shows how to run background jobs with [Asynq](https://github.com/hibiken/asynq) (a Redis-backed task queue) from a [Fiber](https://github.com/gofiber/fiber) HTTP server. The API enqueues a job and returns immediately; a separate worker process consumes the queue and does the slow work, with retries and priorities handled by Asynq.
14+
15+
## How it works
16+
17+
- The Fiber API exposes `POST /enqueue` with a JSON body: `{"user_id": "...", "email": "..."}`.
18+
- Each request enqueues a `email:welcome` task onto Redis and returns the task id — it never does the work inline.
19+
- A separate **worker** process pulls tasks from Redis and runs them, retrying on failure with backoff.
20+
- The task type and payload live in a shared `task` package, so the API and the worker can't drift apart.
21+
22+
The worker uses weighted queues (`critical` drained ~6x as often as `low`), which is the usual way to keep a noisy low-priority job from starving important ones.
23+
24+
## Requirements
25+
26+
- [Go](https://golang.org/dl/) 1.25 or higher
27+
- A running Redis instance (or use the provided `docker-compose.yml`)
28+
29+
## Running the example
30+
31+
### With Docker Compose
32+
33+
```bash
34+
docker compose up --build
35+
```
36+
37+
This starts Redis, the API, and the worker together.
38+
39+
### Manually
40+
41+
Start Redis, then in two terminals:
42+
43+
```bash
44+
# terminal 1 — API
45+
make run-api
46+
47+
# terminal 2 — worker
48+
make run-worker
49+
```
50+
51+
Set `REDIS_ADDR` if Redis isn't on `localhost:6379`.
52+
53+
## Trying it out
54+
55+
```bash
56+
curl -X POST http://localhost:3000/enqueue \
57+
-H "Content-Type: application/json" \
58+
-d '{"user_id":"42","email":"jane@example.com"}'
59+
# {"enqueued":true,"task_id":"...","queue":"default"}
60+
```
61+
62+
The worker terminal logs:
63+
64+
```text
65+
sending welcome email for user 42
66+
```
67+
68+
A request with a missing or invalid body returns `400`.
69+
70+
## Notes
71+
72+
- The handler returns an error to signal a retry; returning `asynq.SkipRetry` (as it does for a malformed payload) tells Asynq not to bother retrying something that will never succeed.
73+
- Enqueue options like `asynq.MaxRetry` and `asynq.Queue` are set per call, so different endpoints can enqueue onto different queues with different retry policies.

0 commit comments

Comments
 (0)