Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Pages から Workflows を呼び出す

最終更新 Markdown で表示Agent セットアップ

Pages Functions から Workflows をバインドして起動できます。Workflow 定義を含む Workers プロジェクトをデプロイし、Service Bindings または通常の fetch() 呼び出しでその Worker を呼び出します。

Service Bindings を使う

Service Bindings を使うと、Worker を直接公開せずに、別の Worker または Pages Function から呼び出せます。

手順は次のとおりです。

  1. Worker 内に Workflow をデプロイします
  2. Pages プロジェクトで、その Worker への Service Binding を作成します
  3. バインディングを使って Worker をリモート呼び出しします

たとえば Worker 名が workflows-starter の場合、Pages プロジェクトに次の Service Binding を作成します。service 名は、Workflow を定義した Worker の名前と一致させてください。

{
	"services": [
		{
			"binding": "WORKFLOW_SERVICE",
			"service": "workflows-starter"
		}
	]
}
[[services]]
binding = "WORKFLOW_SERVICE"
service = "workflows-starter"

Worker では、Service Binding 経由でのみほかの Workers または Pages Functions が呼べるメソッドを公開できます。

次の例では、Payload を受け取り、Workflows API の InstanceStatus を返す createInstance メソッドを公開します。

index.jsjs
import { WorkerEntrypoint } from "cloudflare:workers";

export default class WorkflowsService extends WorkerEntrypoint {
	// Currently, entrypoints without a named handler are not supported
	async fetch() {
		return new Response(null, { status: 404 });
	}

	async createInstance(payload) {
		let instance = await this.env.MY_WORKFLOW.create({
			params: payload,
		});

		return Response.json({
			id: instance.id,
			details: await instance.status(),
		});
	}
}
index.tsts
import { WorkerEntrypoint } from "cloudflare:workers";

interface Env {
	MY_WORKFLOW: Workflow;
}

type Payload = {
	hello: string;
};

export default class WorkflowsService extends WorkerEntrypoint<Env> {
	// Currently, entrypoints without a named handler are not supported
	async fetch() {
		return new Response(null, { status: 404 });
	}

	async createInstance(payload: Payload) {
		let instance = await this.env.MY_WORKFLOW.create({
			params: payload,
		});

		return Response.json({
			id: instance.id,
			details: await instance.status(),
		});
	}
}

Pages Function は次のようになります。

functions/request.jsjs
export const onRequest = async (context) => {
	// This payload could be anything from within your app or from your frontend
	let payload = { hello: "world" };
	return context.env.WORKFLOWS_SERVICE.createInstance(payload);
};
functions/request.tsts
interface Env {
	WORKFLOW_SERVICE: Service;
}

export const onRequest: PagesFunction<Env> = async (context) => {
	// This payload could be anything from within your app or from your frontend
	let payload = { hello: "world" };
	return context.env.WORKFLOWS_SERVICE.createInstance(payload);
};

Pages Functions からリソースへバインドする方法(Cloudflare ダッシュボードでのバインドを含む)は、Pages Functions のバインディングドキュメント を参照してください。

fetch を使う

Service Binding を設定する代わりに、HTTP 経由で Worker を呼べます。Worker への受信 HTTP 呼び出しごとに、Workflows の Workers API で新しい Workflow インスタンスを create します。

index.jsjs
// This is in the same file as your Workflow definition
export default {
	async fetch(req, env) {
		let instance = await env.MY_WORKFLOW.create({
			params: payload,
		});
		return Response.json({
			id: instance.id,
			details: await instance.status(),
		});
	},
};
index.tsts
// This is in the same file as your Workflow definition
export default {
	async fetch(req: Request, env: Env): Promise<Response> {
		let instance = await env.MY_WORKFLOW.create({
			params: payload,
		});
		return Response.json({
			id: instance.id,
			details: await instance.status(),
		});
	},
};

Pages Function からは、Worker へ通常の fetch を送れます。

functions/request.jsjs
export const onRequest = async (context) => {
	// Other code
	let payload = { hello: "world" };
	const instanceStatus = await fetch("https://YOUR_WORKER.workers.dev/", {
		method: "POST",
		body: JSON.stringify(payload), // Send a payload for our Worker to pass to the Workflow
	});

	return Response.json(instanceStatus);
};
functions/request.tsts
export const onRequest: PagesFunction<Env> = async (context) => {
	// Other code
	let payload = { hello: "world" };
	const instanceStatus = await fetch("https://YOUR_WORKER.workers.dev/", {
		method: "POST",
		body: JSON.stringify(payload), // Send a payload for our Worker to pass to the Workflow
	});

	return Response.json(instanceStatus);
};

ヘッダーに共有シークレットを渡し、Worker 側で検証して、これらのリクエストを認証することもできます。

次のステップ

役に立ちましたか?