All skills
upstash avatar

/upstash-workflow-js

@36daab8
by upstashupstash/skills27 stars
7

Work with the @upstash/workflow TypeScript/JavaScript SDK for durable, long-running workflows in serverless functions, multi-step processes that survive timeouts, retries, and restarts (built on QStash). Use when defining a workflow endpoint with serve(), running steps with context.run, sleeping for minutes to days without holding a function open, calling external APIs with context.call, waiting for an external event or webhook, invoking other workflows, configuring retries, failure callbacks, and a DLQ, controlling concurrency, rate, and parallelism, triggering, cancelling, or inspecting runs with the Workflow client, building AI agents and orchestrators, human-in-the-loop approvals, realtime updates, local development with the QStash dev server, adding middleware, or migrating workflows safely. Also use when the user asks for durable execution, step functions, saga or orchestration patterns, background jobs with checkpoints, or long-running tasks on Vercel, Next.js, Cloudflare Workers, or other serverless platforms.

Use this Skill: https://skilld.dev/gh/upstash/skills/upstash-workflow-js

This session only. Nothing lands on disk.

featureswait-for-event.md

≈716 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Wait for Event

Use this feature to pause workflow execution until an external event arrives. It allows building asynchronous, event‑driven workflows without holding compute resources.


How It Works

context.waitForEvent() suspends the workflow and registers a waiter under an event ID. When a matching notify call is sent, the workflow resumes with the provided event data.

• Each waiter has a timeout. If the event does not arrive in time, the workflow continues with { timeout: true }. • Maximum timeout depends on your pricing tier. • Multiple workflow runs may wait on the same event ID; a notify call resumes all of them.


Key Concepts & Pitfalls

Event IDs

Use unique event IDs to avoid heavy fan‑out notifications.

Example patterns: • order-123-processing-complete • user-42-email-verified

Race Conditions

A notify call sent before the workflow begins waiting is lost.

To avoid this: • Always inspect the notify response. • Retry if no waiters were found.


Combined Example

Below is a single TypeScript example showing waiting for an event, handling timeouts, and safely notifying:

import { serve } from "@upstash/workflow/nextjs";
import { Client } from "@upstash/workflow";

// Workflow that waits for an event
export const { POST } = serve(async (context) => {
  const { orderId } = context.requestPayload;
  const eventId = `order-${orderId}-processed`;

  // Wait for the event with timeout
  const { eventData, timeout } = await context.waitForEvent(
    "wait-for-order-processing", // step name
    eventId, // event id
    {
      timeout: "1d", // optional timeout
    }
  );

  if (timeout) {
    // handle timeout here
    await context.run("timeout-handler", async () => {
      console.log("Order processing timed out");
    });
    return;
  }

  // Continue workflow using eventData
  await context.run("process-completed-order", async () => {
    console.log("Order processed:", eventData);
  });
});

// External notifier (safe retry pattern)
const client = new Client({ token: "<WORKFLOW_TOKEN>" });

async function notifyProcessingComplete(orderId: string, payload: any) {
  const eventId = `order-${orderId}-processed`;

  // First attempt - returns array of NotifyResponse
  const waiters = await client.notify({ eventId, eventData: payload });

  if (waiters > 0) return waiters;

  // Retry if no workflows were waiting
  await new Promise((r) => setTimeout(r, 3000));
  return await client.notify({ eventId, eventData: payload });
}

Best Practices

• Generate event IDs that uniquely identify a specific workflow instance. • Always handle the timeout case explicitly. • On notification, check the returned array length to detect if any workflows were waiting. • Prefer one event ID per workflow run to avoid notifying large groups.

Source: SKILL.md on GitHub

1 warning17d3 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill is a comprehensive documentation and implementation guide for the Upstash Workflow SDK. It covers durable serverless workflows, agent orchestration, and reliability features. All identified external resources and tool downloads are official components of the Upstash platform or well-known development services. No security issues or malicious patterns were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: MEDIUM · 1 issue

Signed by skilld at 36daab8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 days ago.

Activeupdated last month
metadata
{
  "author": "Upstash",
  "homepage": "https://upstash.com"
}

README badge

README badge for upstash/skills/upstash-workflow-js