Crons Convex 组件
Convex 组件提供了注册和管理克龙的功能 运行时的工作。 Convex有内在的支持,但必须 部署时采用静态定义。 此库允许动态 在运行时登记工作.
// Register a cron to run once per day.
const daily = await crons.register(
ctx,
{ kind: "cron", cronspec: "0 0 * * *" },
internal.example.logStuff,
{ message: "daily cron" },
);
// Register a cron to run every hour.
const hourly = await crons.register(
ctx,
{ kind: "interval", ms: 3600000 },
internal.example.logStuff,
{ message: "hourly cron" },
);
它支持间隔( 毫秒) 以及相同的克龙时间表
格式为 unix cron 命令 :
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └── day of week (0 - 7, 1L - 7L) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └──────── day of month (1 - 31, L)
│ │ └─────────── hour (0 - 23)
│ └────────────── minute (0 - 59)
└───────────────── second (0 - 59, optional)
设计
这个组件的设计是基于Cronvex的演示应用 描述 内 此堆栈帖。 。 。 .
预先要求: Convex
你需要一个现有的 Convex 项目来使用组件。 Convex 是一个主机 后端平台,包括数据库、无服务器功能和多一个吨 可以学习 这里。 。 。 .
运行 npm create convex 或遵循其中任何一条
快速启动 来设置一个.
安装
安装组件包 :
npm install @convex-dev/crons
创建一个 convex.config.ts 应用程序中的文件 convex/ 文件夹并安装
通过调用 use数字 :
// convex/convex.config.ts
import { defineApp } from "convex/server";
import crons from "@convex-dev/crons/convex.config.js";
const app = defineApp();
app.use(crons);
export default app;
使用量
A 类 Crons 包装器可以在您的 Convex 代码中被即时化为:
import { components } from "./_generated/api";
import { Crons } from "@convex-dev/crons";
const crons = new Crons(components.crons);
这个 Crons 包装类提供下列方法:
register(ctx, schedule, fn, args, name?): 注册新的 crun 工作.get(ctx, { name | id }):通过姓名或身份证获得工作.list(ctx):列出所有 cron 任务.delete(ctx, { name | id }):按名称或身份删除 cron 任务.
示例用法:
import { v } from "convex/values";
import { internalMutation } from "./_generated/server";
import { internal } from "./_generated/api";
// Dummy function that we're going to schedule.
export const logStuff = internalMutation({
args: {
message: v.string(),
},
handler: async (_ctx, args) => {
console.log(args.message);
},
});
// Run a bunch of cron operations as a test. Note that this function runs as a
// transaction and cleans up after itself so you won't actually see these crons
// showing up in the database while it's in progress.
export const doSomeStuff = internalMutation({
handler: async (ctx) => {
// Register some crons.
const namedCronId = await crons.register(
ctx,
{ kind: "interval", ms: 3600000 },
internal.example.logStuff,
{ message: "Hourly cron test" },
"hourly-test",
);
console.log("Registered new cron job with ID:", namedCronId);
const unnamedCronId = await crons.register(
ctx,
{ kind: "cron", cronspec: "0 * * * *" },
internal.example.logStuff,
{ message: "Minutely cron test" },
);
console.log("Registered new cron job with ID:", unnamedCronId);
// Get the cron job by name.
const cronByName = await crons.get(ctx, { name: "hourly-test" });
console.log("Retrieved cron job by name:", cronByName);
// Get the cron job by ID.
const cronById = await crons.get(ctx, { id: unnamedCronId });
console.log("Retrieved cron job by ID:", cronById);
// List all cron jobs.
const allCrons = await crons.list(ctx);
console.log("All cron jobs:", allCrons);
// Delete the cron jobs.
await crons.delete(ctx, { name: "hourly-test" });
console.log("Deleted cron job by name:", "hourly-test");
await crons.delete(ctx, { id: unnamedCronId });
console.log("Deleted cron job by ID:", unnamedCronId);
// Verify deletion.
const deletedCronByName = await crons.get(ctx, { name: "hourly-test" });
console.log("Deleted cron job (should be null):", deletedCronByName);
const deletedCronById = await crons.get(ctx, { id: unnamedCronId });
console.log("Deleted cron job (should be null):", deletedCronById);
},
});
如果你想静静地定义 衣着工作像内置 crons.ts
Convex 特性,您可以通过 imit 脚本来进行
冠以给定的名字。 例如,在 init.ts 每次部署都会运行的文件
通过 convex dev --run init。 。 。 .
// Register a daily cron job. This could be called from an init script to make
// sure it's always registered, like the built-in crons in Convex.
export const registerDailyCron = internalMutation({
handler: async (ctx) => {
if ((await crons.get(ctx, { name: "daily" })) === null) {
await crons.register(
ctx,
{ kind: "cron", cronspec: "0 0 * * *" },
internal.example.logStuff,
{
message: "daily cron",
},
"daily",
);
}
},
});
Crons 是以交易方式创建的,并保证在 产生它们的突变已经运行。 这样可以写工作流程 然后在运行时立即删除, 没有其它关于 cron 不存在的错误处理.
// This will schedule a cron job to run every 10 seconds but then delete itself
// the first time it runs.
export const selfDeletingCron = internalMutation({
handler: async (ctx) => {
const cronId = await crons.register(
ctx,
{ kind: "interval", ms: 10000 },
internal.example.deleteSelf,
{ name: "self-deleting-cron" },
"self-deleting-cron",
);
console.log("Registered self-deleting cron job with ID:", cronId);
},
});
// Worker function that deletes a cron job.
export const deleteSelf = internalMutation({
args: { name: v.string() },
handler: async (ctx, { name }) => {
console.log("Self-deleting cron job running. Name:", name);
await crons.delete(ctx, { name });
console.log("Self-deleting cron job has been deleted. Name:", name);
},
});