使用 Supabase 和工作进程构建任务队列
探索使用 Supabase 和外部工作进程服务实现后台任务处理与消息队列的策略
使用 Supabase 和工作进程构建任务队列 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
Tasks: Background vs. Foreground
Imagine your app needs to send a welcome email, process a large image, or generate a report. If your user has to wait for these long tasks to finish, their experience suffers.
These are background tasks: operations that don't need immediate user interaction and can run independently without blocking the user interface.
Introducing Task Queues
A task queue (or message queue) is a system that allows different parts of your application to communicate asynchronously. It acts like a temporary holding area for tasks.
- Producers: Add tasks to the queue.
- Consumers (Workers): Pick up tasks from the queue and process them.
This decouples the task creation from its execution.
Why Use a Task Queue?
Task queues bring several key benefits to your application architecture:
- Improved Responsiveness: Users don't wait for long operations.
- Scalability: You can add more workers to handle increased load.
- Reliability: Tasks can be retried if they fail.
- Decoupling: Separates task submission from task execution.
Supabase as a Simple Queue
While Supabase isn't a dedicated message queue, its powerful PostgreSQL database can serve as a simple task queue for many use cases. We can create a dedicated table to store tasks.
A typical tasks table might have columns like:
id(Primary Key)payload(JSONB, for task data)status(e.g., 'pending', 'processing', 'completed', 'failed')created_at(Timestamp)processed_at(Timestamp, nullable)
Enqueueing Tasks (Producer)
To enqueue a task, your client-side application or API endpoint simply inserts a new row into the tasks table with a 'pending' status. The payload column holds all the necessary data for the worker to process.
For example, to send a welcome email after user signup:
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'YOUR_SUPABASE_URL';
const supabaseKey = 'YOUR_SUPABASE_ANON_KEY';
const supabase = createClient(supabaseUrl, supabaseKey);
async function enqueueWelcomeEmail(userId, email) {
const { data, error } = await supabase
.from('tasks')
.insert({
type: 'send_welcome_email',
payload: { userId, email },
status: 'pending'
});
if (error) {
console.error('Error enqueueing task:', error.message);
} else {
console.log('Welcome email task enqueued!');
}
}
// Example usage (not runnable directly without Supabase setup)
// enqueueWelcomeEmail('user123', 'test@example.com');Introducing External Workers
An external worker is a separate application or service that continuously monitors the task queue for new jobs. When it finds a 'pending' task, it picks it up, processes it, and updates its status.
Workers can be written in any language (Node.js, Python, Go) and run on various platforms (servers, serverless functions, containers). They connect to your Supabase database to read and update task records.
Dequeueing Tasks (Consumer/Worker)
A worker needs to:
- Fetch 'pending' tasks.
- Mark a task as 'processing' to prevent other workers from picking it up.
- Execute the task logic using the
payload. - Update the task status to 'completed' or 'failed'.
Workers can either poll (periodically check) or use Supabase's Realtime feature to listen for new task insertions.
Code: Simple Worker Logic (Polling)
Here's a simplified Node.js worker logic that polls the Supabase tasks table every few seconds. Remember to replace placeholder values.
// worker.js
const { createClient } = require('@supabase/supabase-js');
const SUPABASE_URL = 'YOUR_SUPABASE_URL';
const SUPABASE_KEY = 'YOUR_SUPABASE_SERVICE_ROLE_KEY'; // Use service key for workers
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
async function processTask(task) {
console.log(`Processing task ${task.id}: ${task.type}`);
// Simulate work (e.g., sending email, processing image)
await new Promise(resolve => setTimeout(resolve, 2000));
console.log(`Task ${task.id} processed.`);
// In a real app, this would involve calling an email API etc.
}
async function runWorker() {
console.log('Worker started, looking for tasks...');
setInterval(async () => {
try {
// Fetch one pending task and lock it by updating status
const { data: tasks, error } = await supabase
.from('tasks')
.select('*')
.eq('status', 'pending')
.order('created_at', { ascending: true })
.limit(1);
if (error) throw error;
if (tasks.length > 0) {
const task = tasks[0];
// Atomically update status to 'processing'
const { error: updateError } = await supabase
.from('tasks')
.update({ status: 'processing', processed_at: new Date().toISOString() })
.eq('id', task.id)
.eq('status', 'pending'); // Ensure no other worker picked it up
if (updateError) {
if (updateError.code === '40600') { // Row already updated by another process
console.log(`Task ${task.id} already picked up.`);
return;
}
throw updateError;
}
await processTask(task);
// Update status to 'completed'
await supabase
.from('tasks')
.update({ status: 'completed' })
.eq('id', task.id);
} else {
// console.log('No pending tasks.');
}
} catch (err) {
console.error('Worker error:', err.message);
// Implement error handling, e.g., update task status to 'failed'
}
}, 5000); // Poll every 5 seconds
}
// To run this: node worker.js
// (Requires 'npm install @supabase/supabase-js' and a Supabase project)
runWorker();Task States & Reliability
Properly managing task states is crucial for reliability:
- Pending: Task is waiting to be processed.
- Processing: Worker has picked up the task.
- Completed: Task finished successfully.
- Failed: Task encountered an error.
For failed tasks, you might implement retries (e.g., after a delay) and ensure tasks are idempotent (running them multiple times has the same effect as running once) to prevent unintended side effects.
Queueing Question
You've learned about using Supabase and external workers for task queues. Consider a scenario where a user uploads a large video file that needs transcoding.
Recap: Task Queues & Workers
We've explored how task queues enable efficient background processing in your applications. By using Supabase as a simple queue and external workers, you can:
- Offload long-running operations.
- Improve user experience by keeping your app responsive.
- Build more scalable and robust systems.
This pattern is fundamental for building complex, high-performance web and mobile applications.
常见问题解答
「使用 Supabase 和工作进程构建任务队列」课时是免费的吗?
是的 — 「使用 Supabase 和工作进程构建任务队列」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 3 节课。
「使用 Supabase 和工作进程构建任务队列」这节课中我会学到什么?
探索使用 Supabase 和外部工作进程服务实现后台任务处理与消息队列的策略 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Supabase Backend as a Service 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「使用 Supabase 和工作进程构建任务队列」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?
能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 集成外部服务
- 使用 Supabase 和工作进程构建任务队列
- 使用 pg_cron 调度周期性任务