Communitygithub.com

curiositech/windags-skills

精通使用 Bull/BullMQ (Redis)、Celery 和云队列进行后台任务处理。实现重试、调度、优先级队列和工作进程管理。适用于异步任务处理、邮件营销、报告生成、批量操作。激活关键词:“后台任务”、“异步任务”、“队列”、“工作进程”、“BullMQ”、“Celery”。不适用于实时 WebSocket 通信、同步 API 调用或简单的 setTimeout 操作。

windags-skills 是什么?

此技能旨在指导工程师设计和实现生产级的后台任务编排系统,处理那些会长时间运行、无法阻塞 API 响应的任务。它涵盖了技术选型(如 Node.js 下的 BullMQ、Python 下的 Celery 以及云原生的 AWS SQS)、常见的反模式(如缺少死信队列、同步处理、缺乏幂等性和速率限制)以及对应的正确实现方案。该技能提供了从邮件营销、定时报表到视频转码的多种实现模式,并强调了监控、可观测性(如 Bull Board UI)和生产级清单(如重试策略、工作进程并发调优、优雅关闭)。通过决策树和场景对比表,帮助用户快速判断何种任务适合转为后台作业。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/curiositech/windags-skills/tree/HEAD/skills/background-job-orchestrator

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

后台任务编排器

精通设计和实现生产级后台作业系统,能处理长时间运行的任务而不阻塞 API 响应。

使用时机

✅ 适用于:

  • 长时间运行的任务(邮件发送、报告生成、图片处理)
  • 批量操作(批量导入、导出、数据迁移)
  • 定时任务(每日摘要、清理作业、定期报告)
  • 需要重试逻辑的任务(外部 API 调用、不稳定的操作)
  • 基于优先级的处理(高级用户优先、关键告警)
  • 限制速率的操作(API 配额、第三方服务限制)

❌ 不适用于:

  • 实时双向通信(使用 WebSockets)
  • 亚秒级延迟要求(使用内存缓存)
  • 简单的延时(小于 5 秒可使用 setTimeout)
  • 同步 API 响应(将逻辑保留在请求处理器中)

快速决策树

此任务:
├── 耗时超过 5 秒?→ 后台作业
├── 失败时需要重试?→ 后台作业
├── 需要按计划执行?→ 后台作业(cron 模式)
├── 会阻塞用户交互?→ 后台作业
├── 需要批量处理?→ 后台作业
└── 需要立即返回?→ 保持同步

技术选型

Node.js:BullMQ(2024 年及以后推荐)

使用场景:

  • TypeScript 项目
  • 已在技术栈中使用 Redis
  • 需要高级功能(速率限制、优先级、可重复作业)

为什么选择 BullMQ 而非 Bull:

  • Bull (v3) → BullMQ (v4+):完全用 TypeScript 重写
  • 更好的 Redis 连接处理
  • 改进的并发性和性能
  • 持续维护(Bull 已进入维护模式)

Python:Celery

使用场景:

  • Python/Django 项目
  • 需要分布式任务执行
  • 复杂工作流(链、组、和弦)

替代方案:

  • RQ(Redis Queue):更简单,功能更少
  • Dramatiq:现代,生态系统较小
  • Huey:轻量级,适合小型项目

云原生:AWS SQS、Google Cloud Tasks

使用场景:

  • 无服务器架构
  • 不想管理 Redis/RabbitMQ
  • 需要确保送达和死信队列

常见反模式

反模式 1:没有死信队列

新手思维:“重试 3 次,然后静默失败”

问题:失败的作业消失了,没有可见性或恢复路径。

正确做法:

// BullMQ 带死信队列
const queue = new Queue('email-queue', {
  connection: redis,
  defaultJobOptions: {
    attempts: 3,
    backoff: {
      type: 'exponential',
      delay: 2000
    },
    removeOnComplete: 100, // 保留最近 100 个成功的
    removeOnFail: false     // 保留所有失败的以供检查
  }
});

// 监控失败的作业
const failedJobs = await queue.getFailed();

时间线:

  • 2020 年前:重试并遗忘
  • 2020 年起:死信队列成为标准
  • 2024 年起:作业失败的可观测性成为必需

反模式 2:同步处理作业

症状:API 端点等待作业完成

问题:

// ❌ 错误 - 阻塞 API 响应
app.post('/send-email', async (req, res) => {
  await sendEmail(req.body.to, req.body.subject);
  res.json({ success: true });
});

为什么错误:超时、用户体验差、浪费服务器资源

正确做法:

// ✅ 正确 - 入队后立即返回
app.post('/send-email', async (req, res) => {
  const job = await emailQueue.add('send', {
    to: req.body.to,
    subject: req.body.subject
  });

  res.json({
    success: true,
    jobId: job.id,
    status: 'queued'
  });
});

// 独立的工作进程处理作业
worker.process('send', async (job) => {
  await sendEmail(job.data.to, job.data.subject);
});

反模式 3:没有幂等性

问题:作业运行两次 → 重复扣费、重复发送邮件

原因:

  • Redis 连接在处理过程中断开
  • 工作进程在作业完成前崩溃
  • 作业超时触发重试,但原作业仍在运行

正确做法:

// ✅ 带去重键的幂等作业
await queue.add('charge-payment', {
  userId: 123,
  amount: 50.00
}, {
  jobId: `payment-${orderId}`, // 防止重复
  attempts: 3
});

// 在工作进程中:检查是否已处理
worker.process('charge-payment', async (job) => {
  const { userId, amount } = job.data;

  // 检查幂等性
  const existing = await db.payments.findOne({
    jobId: job.id
  });
  if (existing) {
    return existing; // 已处理
  }

  // 处理支付
  const result = await stripe.charges.create({...});

  // 存储幂等记录
  await db.payments.create({
    jobId: job.id,
    result
  });

  return result;
});

反模式 4:没有速率限制

问题:压垮第三方 API 或耗尽配额

症状:来自 Sendgrid、Stripe 等的“速率限制超出”错误

正确做法:

// BullMQ 速率限制
const queue = new Queue('api-calls', {
  limiter: {
    max: 100,        // 最多 100 个作业
    duration: 60000  // 每 60 秒
  }
});

// 或者:基于优先级的速率限制
await queue.add('send-email', data, {
  priority: user.isPremium ? 1 : 10,
  rateLimiter: {
    max: user.isPremium ? 1000 : 100,
    duration: 3600000 // 每小时
  }
});

反模式 5:忘记工作进程扩展

问题:单个工作进程无法跟上队列深度

症状:队列积压,作业延迟数小时/天

正确做法:

// 水平扩展,多个工作进程
const worker = new Worker('email-queue', async (job) => {
  await processEmail(job.data);
}, {
  connection: redis,
  concurrency: 5  // 每个工作进程并发处理 5 个作业
});

// 运行多个工作进程(PM2、Kubernetes 等)
// 每个工作进程处理 concurrency * num_workers 个作业

监控:

// 设置队列深度告警
setInterval(async () => {
  const waiting = await queue.getWaitingCount();
  if (waiting > 1000) {
    alert('队列深度超过 1000,请扩展工作进程!');
  }
}, 60000);

实现模式

模式 1:邮件营销

// 队列设置
const emailQueue = new Queue('email-campaign', { connection: redis });

// 批量入队
async function sendCampaign(userIds: number[], template: string) {
  const jobs = userIds.map(userId => ({
    name: 'send',
    data: { userId, template },
    opts: {
      attempts: 3,
      backoff: { type: 'exponential', delay: 5000 }
    }
  }));

  await emailQueue.addBulk(jobs);
}

// 带重试逻辑的工作进程
const worker = new Worker('email-campaign', async (job) => {
  const { userId, template } = job.data;

  const user = await db.users.findById(userId);
  const email = renderTemplate(template, user);

  try {
    await sendgrid.send({
      to: user.email,
      subject: email.subject,
      html: email.body
    });
  } catch (error) {
    if (error.code === 'ECONNREFUSED') {
      throw error; // 重试
    }
    // 无效邮箱,不重试
    console.error(`用户 ${userId} 的邮箱无效`);
  }
}, {
  connection: redis,
  concurrency: 10
});

模式 2:定时报表

// 每天上午 9 点的日报
await queue.add('daily-report', {
  type: 'sales',
  recipients: ['[email protected]']
}, {
  repeat: {
    pattern: '0 9 * * *', // Cron 语法
    tz: 'America/New_York'
  }
});

// 工作进程生成并通过邮件发送报表
worker.process('daily-report', async (job) => {
  const { type, recipients } = job.data;

  const data = await generateReport(type);
  const pdf = await createPDF(data);

  await emailQueue.add('send', {
    to: recipients,
    subject: `每日 ${type} 报表`,
    attachments: [{ filename: 'report.pdf', content: pdf }]
  });
});

模式 3:视频转码流水线

// 多阶段作业,带进度跟踪
await videoQueue.add('transcode', {
  videoId: 123,
  formats: ['720p', '1080p', '4k']
}, {
  attempts: 2,
  timeout: 3600000 // 1 小时超时
});

worker.process('transcode', async (job) => {
  const { videoId, formats } = job.data;

  for (let i = 0; i < formats.length; i++) {
    const format = formats[i];

    // 更新进度
    await job.updateProgress((i / formats.length) * 100);

    // 转码
    await ffmpeg.transcode(videoId, format);
  }

  await job.updateProgress(100);
});

// 客户端轮询进度
app.get('/videos/:id/status', async (req, res) => {
  const job = await queue.getJob(req.params.jobId);
  res.json({
    state: await job.getState(),
    progress: job.progress
  });
});

监控与可观测性

关键指标

// 队列健康仪表盘
async function getQueueMetrics() {
  const [waiting, active, completed, failed, delayed] = await Promise.all([
    queue.getWaitingCount(),
    queue.getActiveCount(),
    queue.getCompletedCount(),
    queue.getFailedCount(),
    queue.getDelayedCount()
  ]);

  return {
    waiting,    // 等待处理的作业
    active,     // 正在处理的作业
    completed,  // 成功完成的
    failed,     // 重试后失败的
    delayed,    // 计划在将来执行的
    health: waiting < 1000 && failed < 100 ? 'healthy' : 'degraded'
  };
}

BullMQ Board (UI)

// 开发环境:可视化监控作业
import { createBullBoard } from '@bull-board/api';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import { ExpressAdapter } from '@bull-board/express';

const serverAdapter = new ExpressAdapter();

createBullBoard({
  queues: [
    new BullMQAdapter(emailQueue),
    new BullMQAdapter(videoQueue)
  ],
  serverAdapter
});

app.use('/admin/queues', serverAdapter.getRouter());
// 访问 http://localhost:3000/admin/queues

生产清单

□ 已配置死信队列
□ 带指数退避的重试策略
□ 设置作业超时限制
□ 第三方 API 的速率限制
□ 关键操作的幂等键
□ 工作进程并发数已调优(CPU 核心数 * 2)
□ 已配置水平扩展(多个工作进程)
□ 带告警的队列深度监控
□ 已失败的作业检查工作流
□ 作业数据不包含日志中的 PII
□ Redis 持久化已启用(AOF 或 RDB)
□ 优雅关闭处理(SIGTERM)

使用 vs 避免 场景

场景使用后台作业?
注册时发送欢迎邮件✅ 是 - 可能需要 2-5 秒
信用卡扣款⚠️ 可能 - 取决于支付提供商延迟
生成 PDF 报告(30 秒)✅ 是 - 肯定是后台
从数据库获取用户资料❌ 否 - 毫秒级,保持同步
处理视频上传(5 分钟)✅ 是 - 始终后台
验证表单输入❌ 否 - 同步验证
每日定时任务✅ 是 - 使用可重复作业
实时聊天消息❌ 否 - 使用 WebSockets

技术对比

特性BullMQCeleryAWS SQS
语言Node.jsPython任意(HTTP API)
后端RedisRedis/RabbitMQ/SQS托管
优先级✅✅✅
速率限制✅❌✅(通过属性)
重复/Cron✅✅ (celery-beat)❌(使用 EventBridge)
UI 仪表盘Bull BoardFlowerCloudWatch
工作流❌✅(链、组)❌
学习曲线中等中等低
成本Redis 托管费用Redis 托管费用$0.40/百万请求

参考资料

  • /references/bullmq-patterns.md - 高级 BullMQ 模式与示例
  • /references/celery-workflows.md - Celery 链、组与和弦
  • /references/job-observability.md - 监控、告警与调试

脚本

  • scripts/setup_bullmq.sh - 使用 Redis 初始化 BullMQ
  • scripts/queue_health_check.ts - 队列指标仪表盘
  • scripts/retry_failed_jobs.ts - 批量重试失败的作业

本技能指导:后台作业实现 | 队列架构 | 重试策略 | 工作进程扩展 | 作业可观测性

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

curiositech/windags-skills

Expert in 2000s-era music visualization (Milkdrop, AVS, Geiss) and modern WebGL implementations. Specializes in Butterchurn integration, Web Audio API AnalyserNode FFT data, GLSL shaders for audio-reactive visuals, and psychedelic generative art. Activate on "Milkdrop", "music visualization", "WebGL visualizer", "Butterchurn", "audio reactive", "FFT visualization", "spectrum analyzer". NOT for simple bar charts/waveforms (use basic canvas), video editing, or non-audio visuals.

curiositech/windags-skills

Expert legal research agent for finding and scraping expungement data state by state. Knows authoritative sources, URL patterns, Firecrawl configuration, and 2026 legal landscape.

curiositech/windags-skills

Expert in 3D computer vision labeling tools, workflows, and AI-assisted annotation for LiDAR, point clouds, and sensor fusion. Covers SAM4D/Point-SAM, human-in-the-loop architectures, and vertical-specific training strategies. Activate on '3D labeling', 'point cloud annotation', 'LiDAR labeling', 'SAM 3D', 'SAM4D', 'sensor fusion annotation', '3D bounding box', 'semantic segmentation point cloud'. NOT for 2D image labeling (use clip-aware-embeddings), general ML training (use ml-engineer), video annotation without 3D (use computer-vision-pipeline), or VLM prompt engineering (use prompt-engineer).

curiositech/windags-skills

Implement WCAG 2.2 AA/AAA compliance with automated testing, keyboard navigation, screen reader support, and focus management. Activate on: accessibility audit, WCAG compliance, keyboard navigation, screen reader, aria attributes, axe-core, focus trap. NOT for: design-level accessibility review (use design-accessibility-auditor), color contrast only (use css-in-js-architect).

curiositech/windags-skills

Time-blind friendly planning, executive function support, and daily structure for ADHD brains. Specializes in realistic time estimation, dopamine-aware task design, and building systems that actually work for neurodivergent minds.

curiositech/windags-skills

Designs digital experiences for ADHD brains using neuroscience research and UX principles. Expert in reducing cognitive load, time blindness solutions, dopamine-driven engagement, and compassionate design patterns. Activate on 'ADHD design', 'cognitive load', 'accessibility', 'neurodivergent UX', 'time blindness', 'dopamine-driven', 'executive function'. NOT for general accessibility (WCAG only), neurotypical UX design, or simple UI styling without ADHD context.

curiositech/windags-skills

>- Apply crisis decision-making research to agent routing, uncertainty triage, and coordination failure analysis in time-pressured systems. Use when diagnosing handoff failures, analytical paralysis, or expert judgment under incomplete information. NOT for routine coding, simple CRUD design, or static single-agent tasks with complete information.

curiositech/windags-skills

Extend and modify the admin dashboard, developer portal, and operations console. Use when adding new admin tabs, metrics, monitoring features, or internal tools. Activates for dashboard development, analytics, user management, and internal tooling.

curiositech/windags-skills

Conversation patterns and interaction protocols for multi-agent systems. Covers request/response, pub/sub, blackboard, delegation chains, debate, critique, consensus, fan-out/fan-in, supervisor-worker, and peer negotiation. Deep analysis of AutoGen conversation patterns, CrewAI delegation, LangGraph state passing, and FIPA-ACL performatives. Teaches how to design what agents say to each other and in what order. Activate on: "agent conversation", "agent protocol", "multi-agent debate", "agent delegation", "supervisor worker pattern", "agent voting", "consensus protocol", "fan-out fan-in", "agent negotiation", "blackboard pattern", "agent dialogue", "conversation topology", "agent handoff". NOT for: wire format or serialization (use agent-interchange-formats), orchestration infrastructure (use agentic-infrastructure-2026), single agent behavior (use agentic-patterns).

curiositech/windags-skills

Meta-agent for creating new custom agents, skills, and MCP integrations. Expert in agent design, MCP development, skill architecture, and rapid prototyping. Activate on 'create agent', 'new skill', 'MCP server', 'custom tool', 'agent design'. NOT for using existing agents (invoke them directly), general coding (use language-specific skills), or infrastructure setup (use deployment-engineer).

curiositech/windags-skills

AI-powered calendar management and agent-based scheduling coordination. Covers calendar APIs (Google Calendar, CalDAV/iCal), AI scheduling assistants (Reclaim, Clockwise, Motion, Cal.com), building custom calendar agents with MCP, multi-calendar merging, timezone management, focus block protection, meeting fatigue detection, and agent-to-agent meeting negotiation protocols. Activate on: "calendar agent", "AI scheduling", "calendar coordination", "meeting scheduling", "calendar API", "focus time protection", "calendar optimization", "Google Calendar MCP", "Reclaim", "Clockwise", "Motion", "Cal.com", "smart scheduling", "calendar-aware agent", "timezone scheduling", "agent negotiation meetings". NOT for: manual calendar UI component design (use form-validation-architect), project management scheduling or Gantt charts (use project-management-guru-adhd), general time-tracking or pomodoro apps (use adhd-daily-planner for time-awareness), building the agent itself from scratch (use agent-creator).

curiositech/windags-skills

Build and adopt production AI agent infrastructure in 2026. Covers framework selection (LangGraph, CrewAI, AutoGen, MCP), orchestration patterns, evaluation, observability, memory systems, and tool use. Also covers the SOCIAL dimension: how to sell agent infrastructure internally, change management, measuring ROI, building trust in autonomous systems, and scaling adoption across teams. Activate on: "agent infrastructure", "agent framework comparison", "which agent framework", "sell AI tools internally", "agent adoption", "agent observability", "agent evaluation", "MCP architecture", "agentic mesh", "enterprise AI agents", "AI change management", "agent ROI". NOT for: building specific agents (use ai-engineer), designing agent behavior patterns (use agentic-patterns), prompt tuning (use prompt-engineer).

curiositech/windags-skills

Fundamental patterns for effective agentic behavior. Teaches decomposition, tool orchestration, error recovery, context management, quality self-assessment, and knowing when to stop. Model-agnostic principles that make any agent more effective regardless of domain. Activate on: "how should I structure this agent", "agentic workflow", "agent patterns", "multi-step task", "tool orchestration", "/agentic-patterns", "decompose this", "agent best practices", "chain of actions", "when should the agent stop", "agent loop design". NOT for: creating agent infrastructure (use agent-creator), building DAGs (use windags-architect), specific tool implementation.

curiositech/windags-skills

Automated discovery and matching of agent skills for dynamic task routing and capability assessment

curiositech/windags-skills

Cryptographic security for agentic systems — zero-trust agent networking, signed message envelopes (JWS/JWE), capability-based security (ocaps), Merkle tree audit trails, WASM sandboxing, and formal verification. Covers CLI dev tool security, mTLS between agents, permission boundaries (least privilege for AI agents), and supply chain security for skills/plugins. Activate on: "agent security", "zero trust agents", "secure agent communication", "capability-based security", "ocap", "signed messages between agents", "agent audit trail", "sandbox agent execution", "agent permissions", "mTLS agents", "cryptographic verification", "agent supply chain", "OWASP agentic", "prove agent did X", "tamper-proof agent logs". NOT for: application-level SAST scanning (use security-auditor), network firewall rules (use infrastructure), SOC2/HIPAA compliance (organizational), or prompt injection defense (use prompt-engineer).

curiositech/windags-skills

Data structures and serialization formats for agent-to-agent communication. Covers message envelopes, structured output schemas, capability declarations, task handoff payloads, error/retry signaling, and context windows as data structures. Deep comparison of A2A protocol, MCP, OpenAI function calling, and LangChain message types. Teaches when to use rigid schemas vs free-form with validation, typed vs untyped, streaming vs batch. Activate on: "agent message format", "agent communication schema", "agent-to-agent protocol", "A2A protocol", "MCP message format", "structured output for agents", "agent interop", "interchange format", "agent serialization", "task handoff format", "capability declaration". NOT for: what agents say to each other (use agent-conversation-protocols), orchestration topology (use multi-agent-coordination), building agent infrastructure (use agentic-infrastructure-2026).

curiositech/windags-skills

Logic-based agent programming language implementing BDI architecture for practical autonomous agent development

curiositech/windags-skills

>- Design AgentSpeak(L)-style BDI agents with context-guarded plans, selection functions, and intention stacks. Use for interruptible autonomy, agent policy, and multi-agent orchestration in dynamic environments. NOT for simple rule engines, static planners, or centralized workflows.

curiositech/windags-skills

Foundational concurrent computation model where actors communicate exclusively through asynchronous message passing

curiositech/windags-skills

license: Apache-2.0 NOT for unrelated tasks outside this domain.

相关技能