INTEGRITY Документация

Настройка Cron Triggers

export default {
	async scheduled(controller, env, ctx) {
		console.log("cron processed");
	},
};
interface Env {}
export default {
	async scheduled(
		controller: ScheduledController,
		env: Env,
		ctx: ExecutionContext,
	) {
		console.log("cron processed");
	},
};
from workers import WorkerEntrypoint, Response

class Default(WorkerEntrypoint):
    async def scheduled(self, controller, env, ctx):
  			print("cron processed")
import { Hono } from "hono";

interface Env {}

// Create Hono app
const app = new Hono<{ Bindings: Env }>();

// Regular routes for normal HTTP requests
app.get("/", (c) => c.text("Hello World!"));

// Export both the app and a scheduled function
export default {
	// The Hono app handles regular HTTP requests
	fetch: app.fetch,

	// The scheduled function handles Cron triggers
	async scheduled(
		controller: ScheduledController,
		env: Env,
		ctx: ExecutionContext,
	) {
		console.log("cron processed");

		// You could also perform actions like:
		// - Fetching data from external APIs
		// - Updating KV or Durable Object storage
		// - Running maintenance tasks
		// - Sending notifications
	},
};

Настройка Cron Triggers в Wrangler

См. Cron Triggers с дополнительной информацией о том, как добавить Cron Trigger.

Если вы развертываете проект с помощью Wrangler, задайте cron-синтаксис (раз в час, как показано ниже), добавив следующее в файл Wrangler:

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "worker",
	// ...
	"triggers": {
		"crons": [
			"0 * * * *"
		]
	}
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "worker"

[triggers]
crons = [ "0 * * * *" ]

Также можно задать отдельный Cron Trigger для каждого окружение в вашем конфигурационный файл Wrangler. Необходимо поместить [triggers] таблицу в выбранном окружении. Например:

{
	"env": {
		"dev": {
			"triggers": {
				"crons": [
					"0 * * * *"
				]
			}
		}
	}
}
[env.dev.triggers]
crons = [ "0 * * * *" ]

Тестирование Cron Triggers с помощью Wrangler

Тестировать Cron Triggers рекомендуется с помощью Wrangler.

Cron Triggers можно протестировать с помощью Wrangler, передав --test-scheduled флаг для wrangler dev. Это предоставит /__scheduled (или /cdn-cgi/handler/scheduled для Python Workers) маршрут, который можно использовать для тестирования с помощью HTTP-запроса. Чтобы смоделировать разные варианты cron, cron параметр запроса можно передать.

npx wrangler dev --test-scheduled

curl "http://localhost:8787/__scheduled?cron=0+*+*+*+*"

curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers