Skip to content

How to call two (or many) workflows in Scheduled Handler #21

@tkowalczyk

Description

@tkowalczyk

I am trying to call two workflows inside scheduled handler inside worker:

const workflows = {
    onChainWorkflow,
    macroWorkflow,
} as const;

First approach (here only the first one is fired):

	async scheduled(event, env, ctx): Promise<void> {

		const taskId = `sentiment-${Date.now()}`;

		const id = env.WORKFLOWS.idFromName(taskId);
		const stub = env.WORKFLOWS.get(id);

		const onChainResult = await stub.run({
			workflow: "onChainWorkflow",
			input: `onchain-${taskId}`,
		});

		console.log(`trigger fired at ${event.cron}: ${onChainResult.id}`);

		const macroResult = await stub.run({
			workflow: "macroWorkflow",
			input: `macro-${taskId}`,
		});

		console.log(`trigger fired at ${event.cron}: ${macroResult.id}`);
}

Second approach (don't know how to call second one):

async scheduled(event, env, ctx): Promise<void> {

		const clientOnChain = WorkflowClient.fromBinding(env.WORKFLOWS);

		const taskId = `sentiment-${Date.now()}`;

		const { id } = await Effect.runPromise(
			clientOnChain.runAsync({
			  workflow: "onChainWorkflow",
			  input: `onchain-${taskId}`
			})
		  );


console.log(`trigger fired at ${event.cron}: ${id}`);
}

or maybe I should do this differently or such scenario is not supported? What I am going to achieve is to split workflows which contain many steps into smaller, call smaller in scheduled handler and allow to call each workflow on demand (probably as hono endpoint, like in the example).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions