Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions alchemy-web/docs/providers/cloudflare/workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ const workflow = await Workflow("my-workflow", {
});
```

## Use a Workflow Defined in Another Script

Reference a workflow implemented in a different worker script using `scriptName`.

```ts
import { Workflow } from "alchemy/cloudflare";

const workflow = await Workflow("shared-workflow", {
workflowName: "my-workflow",
className: "MyWorkflow",
scriptName: "shared-worker"
});
```

## Bind to a Worker

Bind a workflow to a Worker to use its functionality.
Expand Down
3 changes: 1 addition & 2 deletions alchemy/src/cloudflare/worker-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ export async function prepareWorkerMetadata<B extends Bindings>(
name: bindingName,
workflow_name: binding.workflowName,
class_name: binding.className,
// this should be set if the Workflow is in another script ...
// script_name: ??,
script_name: binding.scriptName,
});
// it's unclear whether this is needed, but it works both ways
// configureClassMigration(binding, binding.id, binding.className);
Expand Down
2 changes: 1 addition & 1 deletion alchemy/src/cloudflare/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ export const _Worker = Resource(
await upsertWorkflow(api, {
workflowName: workflow.workflowName,
className: workflow.className,
scriptName: workerName,
scriptName: workflow.scriptName ?? workerName,
});
}

Expand Down
9 changes: 9 additions & 0 deletions alchemy/src/cloudflare/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export interface WorkflowProps {
* @default - workflowName if provided, otherwise id
*/
className?: string;

/**
* Name of the script containing the workflow implementation
*
* @default - bound worker script
*/
scriptName?: string;
}

export function isWorkflow(binding: Binding): binding is Workflow {
Expand All @@ -36,13 +43,15 @@ export class Workflow<PARAMS = unknown> {

public readonly workflowName: string;
public readonly className: string;
public readonly scriptName?: string;

constructor(
public readonly id: string,
props: WorkflowProps = {},
) {
this.workflowName = props.workflowName ?? props.className ?? id;
this.className = props.className ?? this.workflowName;
this.scriptName = props.scriptName;
}
}

Expand Down
9 changes: 8 additions & 1 deletion alchemy/src/cloudflare/wrangler.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export interface WranglerJsonSpec {
name: string;
binding: string;
class_name: string;
script_name?: string;
}[];

/**
Expand Down Expand Up @@ -366,7 +367,12 @@ function processBindings(
const services: { binding: string; service: string; environment?: string }[] =
[];
const secrets: string[] = [];
const workflows: { name: string; binding: string; class_name: string }[] = [];
const workflows: {
name: string;
binding: string;
class_name: string;
script_name?: string;
}[] = [];
const d1Databases: {
binding: string;
database_id: string;
Expand Down Expand Up @@ -481,6 +487,7 @@ function processBindings(
name: binding.workflowName,
binding: bindingName,
class_name: binding.className,
script_name: binding.scriptName,
});
} else if (binding.type === "d1") {
d1Databases.push({
Expand Down
2 changes: 2 additions & 0 deletions alchemy/test/cloudflare/wrangler-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ describe("WranglerJson Resource", () => {
const workflow = new Workflow("test-workflow", {
className: "TestWorkflow",
workflowName: "test-workflow",
scriptName: "other-script",
});

const worker = await Worker(name, {
Expand All @@ -345,6 +346,7 @@ describe("WranglerJson Resource", () => {
expect(spec.workflows?.[0].name).toEqual("test-workflow");
expect(spec.workflows?.[0].binding).toEqual("WF");
expect(spec.workflows?.[0].class_name).toEqual("TestWorkflow");
expect(spec.workflows?.[0].script_name).toEqual("other-script");
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
await destroy(scope);
Expand Down
Loading