# Generate a workflow
Adds a durable background workflow to src/lib/workflows, with a server test; --cron makes it recurring.
Tags: openworkflow, workflows, background jobs, cron, pocketbase

$ vela generate workflow send-welcome-email

## src/lib/workflows/send-welcome-email.ts

```ts
import { z } from "zod";
import { ow } from "$lib/server/workflows";

/**
 * Start a run from any server code with `sendWelcomeEmail.run(input)`. Runs
 * are listed under Workflows in the PocketBase dashboard.
 */
export const sendWelcomeEmail = ow.defineWorkflow(
  {
    name: "send-welcome-email",
    // What `run()` takes, checked before the run is queued.
    schema: z.object({}),
    retryPolicy: { maximumAttempts: 3 },
  },
  async ({ input, step }) => {
    // Each step's return value is saved, so a retry or a restart carries on
    // after the last step that finished. Keep every step safe to repeat.
    const result = await step.run({ name: "first-step" }, async () => {
      return input;
    });
    return result;
  },
);
```

## src/lib/workflows/send-welcome-email.server.test.ts

```ts
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { startWorker, stopWorker } from "$lib/server/workflows";
import { sendWelcomeEmail } from "./send-welcome-email";

describe("send-welcome-email", () => {
  // The test process runs a worker of its own, so a run completes here
  // without going through the dev server.
  beforeAll(() => startWorker());
  afterAll(() => stopWorker());

  it("completes", async () => {
    const handle = await sendWelcomeEmail.run({});
    await expect(handle.result({ timeoutMs: 15_000 })).resolves.toEqual({});
  });
});
```
