@convex-dev/crons 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +56 -0
  3. package/dist/commonjs/client/index.d.ts +50 -0
  4. package/dist/commonjs/client/index.d.ts.map +1 -0
  5. package/dist/commonjs/client/index.js +99 -0
  6. package/dist/commonjs/client/index.js.map +1 -0
  7. package/dist/commonjs/client/utils.d.ts +15 -0
  8. package/dist/commonjs/client/utils.d.ts.map +1 -0
  9. package/dist/commonjs/client/utils.js +3 -0
  10. package/dist/commonjs/client/utils.js.map +1 -0
  11. package/dist/commonjs/component/_generated/api.d.ts +14 -0
  12. package/dist/commonjs/component/_generated/api.d.ts.map +1 -0
  13. package/dist/commonjs/component/_generated/api.js +24 -0
  14. package/dist/commonjs/component/_generated/api.js.map +1 -0
  15. package/dist/commonjs/component/_generated/server.d.ts +64 -0
  16. package/dist/commonjs/component/_generated/server.d.ts.map +1 -0
  17. package/dist/commonjs/component/_generated/server.js +76 -0
  18. package/dist/commonjs/component/_generated/server.js.map +1 -0
  19. package/dist/commonjs/component/convex.config.d.ts +3 -0
  20. package/dist/commonjs/component/convex.config.d.ts.map +1 -0
  21. package/dist/commonjs/component/convex.config.js +3 -0
  22. package/dist/commonjs/component/convex.config.js.map +1 -0
  23. package/dist/commonjs/component/index.d.ts +135 -0
  24. package/dist/commonjs/component/index.d.ts.map +1 -0
  25. package/dist/commonjs/component/index.js +289 -0
  26. package/dist/commonjs/component/index.js.map +1 -0
  27. package/dist/commonjs/component/parseArgs.d.ts +11 -0
  28. package/dist/commonjs/component/parseArgs.d.ts.map +1 -0
  29. package/dist/commonjs/component/parseArgs.js +28 -0
  30. package/dist/commonjs/component/parseArgs.js.map +1 -0
  31. package/dist/commonjs/component/public.d.ts +98 -0
  32. package/dist/commonjs/component/public.d.ts.map +1 -0
  33. package/dist/commonjs/component/public.js +225 -0
  34. package/dist/commonjs/component/public.js.map +1 -0
  35. package/dist/commonjs/component/schema.d.ts +45 -0
  36. package/dist/commonjs/component/schema.d.ts.map +1 -0
  37. package/dist/commonjs/component/schema.js +20 -0
  38. package/dist/commonjs/component/schema.js.map +1 -0
  39. package/dist/commonjs/frontend/index.d.ts +2 -0
  40. package/dist/commonjs/frontend/index.d.ts.map +1 -0
  41. package/dist/commonjs/frontend/index.js +8 -0
  42. package/dist/commonjs/frontend/index.js.map +1 -0
  43. package/dist/esm/client/index.d.ts +50 -0
  44. package/dist/esm/client/index.d.ts.map +1 -0
  45. package/dist/esm/client/index.js +99 -0
  46. package/dist/esm/client/index.js.map +1 -0
  47. package/dist/esm/client/utils.d.ts +15 -0
  48. package/dist/esm/client/utils.d.ts.map +1 -0
  49. package/dist/esm/client/utils.js +3 -0
  50. package/dist/esm/client/utils.js.map +1 -0
  51. package/dist/esm/component/_generated/api.d.ts +14 -0
  52. package/dist/esm/component/_generated/api.d.ts.map +1 -0
  53. package/dist/esm/component/_generated/api.js +24 -0
  54. package/dist/esm/component/_generated/api.js.map +1 -0
  55. package/dist/esm/component/_generated/server.d.ts +64 -0
  56. package/dist/esm/component/_generated/server.d.ts.map +1 -0
  57. package/dist/esm/component/_generated/server.js +76 -0
  58. package/dist/esm/component/_generated/server.js.map +1 -0
  59. package/dist/esm/component/convex.config.d.ts +3 -0
  60. package/dist/esm/component/convex.config.d.ts.map +1 -0
  61. package/dist/esm/component/convex.config.js +3 -0
  62. package/dist/esm/component/convex.config.js.map +1 -0
  63. package/dist/esm/component/index.d.ts +135 -0
  64. package/dist/esm/component/index.d.ts.map +1 -0
  65. package/dist/esm/component/index.js +289 -0
  66. package/dist/esm/component/index.js.map +1 -0
  67. package/dist/esm/component/parseArgs.d.ts +11 -0
  68. package/dist/esm/component/parseArgs.d.ts.map +1 -0
  69. package/dist/esm/component/parseArgs.js +28 -0
  70. package/dist/esm/component/parseArgs.js.map +1 -0
  71. package/dist/esm/component/public.d.ts +98 -0
  72. package/dist/esm/component/public.d.ts.map +1 -0
  73. package/dist/esm/component/public.js +225 -0
  74. package/dist/esm/component/public.js.map +1 -0
  75. package/dist/esm/component/schema.d.ts +45 -0
  76. package/dist/esm/component/schema.d.ts.map +1 -0
  77. package/dist/esm/component/schema.js +20 -0
  78. package/dist/esm/component/schema.js.map +1 -0
  79. package/dist/esm/frontend/index.d.ts +2 -0
  80. package/dist/esm/frontend/index.d.ts.map +1 -0
  81. package/dist/esm/frontend/index.js +8 -0
  82. package/dist/esm/frontend/index.js.map +1 -0
  83. package/dist/esm/package.json +3 -0
  84. package/package.json +56 -0
  85. package/src/client/index.ts +128 -0
  86. package/src/client/utils.ts +44 -0
  87. package/src/component/_generated/api.d.ts +98 -0
  88. package/src/component/_generated/api.js +27 -0
  89. package/src/component/_generated/dataModel.d.ts +64 -0
  90. package/src/component/_generated/server.d.ts +153 -0
  91. package/src/component/_generated/server.js +94 -0
  92. package/src/component/convex.config.ts +3 -0
  93. package/src/component/public.ts +287 -0
  94. package/src/component/schema.ts +23 -0
@@ -0,0 +1,98 @@
1
+ import { FunctionHandle } from "convex/server";
2
+ export type Schedule = {
3
+ kind: "cron";
4
+ cronspec: string;
5
+ } | {
6
+ kind: "interval";
7
+ ms: number;
8
+ };
9
+ export type CronInfo = {
10
+ id: string;
11
+ name?: string;
12
+ functionHandle: FunctionHandle<"mutation" | "action">;
13
+ args: Record<string, unknown>;
14
+ schedule: Schedule;
15
+ };
16
+ /**
17
+ * Schedule a mutation or action to run on a cron schedule or interval.
18
+ *
19
+ * @param name - Optional unique name for the job. Will throw if a name is
20
+ * provided and a job with the same name already exists.
21
+ * @param schedule - Either a cron specification string or an interval in
22
+ * milliseconds. For intervals, ms must be >= 1000.
23
+ * @param functionHandle - A {@link FunctionHandle} string for the function to
24
+ * schedule.
25
+ * @param args - The arguments to the function.
26
+ * @returns The ID of the scheduled job.
27
+ */
28
+ export declare const register: import("convex/server").RegisteredMutation<"public", {
29
+ name?: string | undefined;
30
+ functionHandle: string;
31
+ args: Record<string, any>;
32
+ schedule: {
33
+ kind: "interval";
34
+ ms: number;
35
+ } | {
36
+ kind: "cron";
37
+ cronspec: string;
38
+ };
39
+ }, Promise<import("convex/values").GenericId<"crons">>>;
40
+ /**
41
+ * List all user space cron jobs.
42
+ *
43
+ * @returns List of `cron` table rows.
44
+ */
45
+ export declare const list: import("convex/server").RegisteredQuery<"public", {}, Promise<{
46
+ functionHandle: string;
47
+ args: Record<string, any>;
48
+ schedule: {
49
+ kind: "interval";
50
+ ms: number;
51
+ } | {
52
+ kind: "cron";
53
+ cronspec: string;
54
+ };
55
+ name?: string | undefined;
56
+ id: import("convex/values").GenericId<"crons">;
57
+ }[]>>;
58
+ /**
59
+ * Get an existing cron job by id or name.
60
+ *
61
+ * @param identifier - Either the ID or name of the cron job.
62
+ * @returns Cron job document or null if not found.
63
+ */
64
+ export declare const get: import("convex/server").RegisteredQuery<"public", {
65
+ identifier: {
66
+ id: import("convex/values").GenericId<"crons">;
67
+ } | {
68
+ name: string;
69
+ };
70
+ }, Promise<{
71
+ functionHandle: string;
72
+ args: Record<string, any>;
73
+ schedule: {
74
+ kind: "interval";
75
+ ms: number;
76
+ } | {
77
+ kind: "cron";
78
+ cronspec: string;
79
+ };
80
+ name?: string | undefined;
81
+ id: import("convex/values").GenericId<"crons">;
82
+ } | null>>;
83
+ /**
84
+ * Delete and deschedule a cron job by id or name.
85
+ *
86
+ * @param identifier - Either the ID or name of the cron job.
87
+ */
88
+ export declare const del: import("convex/server").RegisteredMutation<"public", {
89
+ identifier: {
90
+ id: import("convex/values").GenericId<"crons">;
91
+ } | {
92
+ name: string;
93
+ };
94
+ }, Promise<void>>;
95
+ export declare const rescheduler: import("convex/server").RegisteredMutation<"internal", {
96
+ id: import("convex/values").GenericId<"crons">;
97
+ }, Promise<void>>;
98
+ //# sourceMappingURL=public.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../../src/component/public.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAa/C,MAAM,MAAM,QAAQ,GAChB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAGrC,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,cAAc,CAAC,UAAU,GAAG,QAAQ,CAAC,CAAC;IACtD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,QAAQ,EAAE,QAAQ,CAAC;CACpB,CAAC;AASF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;;uDAiCnB,CAAC;AAyCH;;;;GAIG;AACH,eAAO,MAAM,IAAI;;;;;;;;;;;;KAaf,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;UAyBd,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,GAAG;;;;;;iBAoCd,CAAC;AASH,eAAO,MAAM,WAAW;;iBA4DtB,CAAC"}
@@ -0,0 +1,225 @@
1
+ // Implementation of crons in user space.
2
+ //
3
+ // See ../client/index.ts for the public API.
4
+ import { v } from "convex/values";
5
+ import { mutation, query, internalMutation, } from "./_generated/server.js";
6
+ import { internal } from "./_generated/api.js";
7
+ import parser from "cron-parser";
8
+ import schema from "./schema.js";
9
+ const scheduleValidator = schema.tables.crons.validator.fields.schedule;
10
+ const cronInfoValidator = v.object({
11
+ id: v.id("crons"),
12
+ name: v.optional(v.string()),
13
+ functionHandle: v.string(),
14
+ args: v.record(v.string(), v.any()),
15
+ schedule: scheduleValidator,
16
+ });
17
+ /**
18
+ * Schedule a mutation or action to run on a cron schedule or interval.
19
+ *
20
+ * @param name - Optional unique name for the job. Will throw if a name is
21
+ * provided and a job with the same name already exists.
22
+ * @param schedule - Either a cron specification string or an interval in
23
+ * milliseconds. For intervals, ms must be >= 1000.
24
+ * @param functionHandle - A {@link FunctionHandle} string for the function to
25
+ * schedule.
26
+ * @param args - The arguments to the function.
27
+ * @returns The ID of the scheduled job.
28
+ */
29
+ export const register = mutation({
30
+ args: {
31
+ name: v.optional(v.string()),
32
+ schedule: scheduleValidator,
33
+ functionHandle: v.string(),
34
+ args: v.record(v.string(), v.any()),
35
+ },
36
+ returns: v.id("crons"),
37
+ handler: async (ctx, { name, schedule, functionHandle, args }) => {
38
+ if (name &&
39
+ (await ctx.db
40
+ .query("crons")
41
+ .withIndex("name", (q) => q.eq("name", name))
42
+ .unique())) {
43
+ throw new Error(`Cron with name "${name}" already exists`);
44
+ }
45
+ validateSchedule(schedule);
46
+ const id = await ctx.db.insert("crons", {
47
+ functionHandle,
48
+ args,
49
+ name,
50
+ schedule,
51
+ });
52
+ console.log(`Scheduling cron "${name}" (${id}) on schedule ${JSON.stringify(schedule)}`);
53
+ await scheduleNextRun(ctx, id, new Date(), schedule);
54
+ return id;
55
+ },
56
+ });
57
+ function validateSchedule(schedule) {
58
+ if (schedule.kind === "interval" && schedule.ms < 1000) {
59
+ throw new Error("Interval must be >= 1000ms");
60
+ }
61
+ if (schedule.kind === "cron") {
62
+ try {
63
+ parser.parseExpression(schedule.cronspec);
64
+ }
65
+ catch {
66
+ throw new Error(`Invalid cronspec: "${schedule.cronspec}"`);
67
+ }
68
+ }
69
+ }
70
+ async function scheduleNextRun(ctx, id, lastScheduled, schedule) {
71
+ const nextRun = calculateNextRun(lastScheduled, schedule);
72
+ const schedulerJobId = await ctx.scheduler.runAt(nextRun, internal.public.rescheduler, { id });
73
+ await ctx.db.patch(id, { schedulerJobId });
74
+ }
75
+ function calculateNextRun(lastScheduled, schedule) {
76
+ if (schedule.kind === "interval") {
77
+ return new Date(lastScheduled.getTime() + schedule.ms);
78
+ }
79
+ else {
80
+ const cron = parser.parseExpression(schedule.cronspec, {
81
+ currentDate: lastScheduled,
82
+ });
83
+ return cron.next().toDate();
84
+ }
85
+ }
86
+ /**
87
+ * List all user space cron jobs.
88
+ *
89
+ * @returns List of `cron` table rows.
90
+ */
91
+ export const list = query({
92
+ args: {},
93
+ returns: v.array(cronInfoValidator),
94
+ handler: async (ctx) => {
95
+ const crons = await ctx.db.query("crons").collect();
96
+ return crons.map((cron) => ({
97
+ id: cron._id,
98
+ ...(cron.name && { name: cron.name }),
99
+ functionHandle: cron.functionHandle,
100
+ args: cron.args,
101
+ schedule: cron.schedule,
102
+ }));
103
+ },
104
+ });
105
+ /**
106
+ * Get an existing cron job by id or name.
107
+ *
108
+ * @param identifier - Either the ID or name of the cron job.
109
+ * @returns Cron job document or null if not found.
110
+ */
111
+ export const get = query({
112
+ args: {
113
+ identifier: v.union(v.object({ id: v.id("crons") }), v.object({ name: v.string() })),
114
+ },
115
+ returns: v.union(cronInfoValidator, v.null()),
116
+ handler: async (ctx, { identifier }) => {
117
+ const cron = "id" in identifier
118
+ ? await ctx.db.get(identifier.id)
119
+ : await ctx.db
120
+ .query("crons")
121
+ .withIndex("name", (q) => q.eq("name", identifier.name))
122
+ .unique();
123
+ if (!cron)
124
+ return null;
125
+ return {
126
+ id: cron._id,
127
+ ...(cron.name && { name: cron.name }),
128
+ functionHandle: cron.functionHandle,
129
+ args: cron.args,
130
+ schedule: cron.schedule,
131
+ };
132
+ },
133
+ });
134
+ /**
135
+ * Delete and deschedule a cron job by id or name.
136
+ *
137
+ * @param identifier - Either the ID or name of the cron job.
138
+ */
139
+ export const del = mutation({
140
+ args: {
141
+ identifier: v.union(v.object({ id: v.id("crons") }), v.object({ name: v.string() })),
142
+ },
143
+ returns: v.null(),
144
+ handler: async (ctx, { identifier }) => {
145
+ let cron;
146
+ if ("id" in identifier) {
147
+ cron = await ctx.db.get(identifier.id);
148
+ if (!cron) {
149
+ throw new Error(`Cron ${identifier.id} not found`);
150
+ }
151
+ }
152
+ else {
153
+ cron = await ctx.db
154
+ .query("crons")
155
+ .withIndex("name", (q) => q.eq("name", identifier.name))
156
+ .unique();
157
+ if (!cron) {
158
+ throw new Error(`Cron "${identifier.name}" not found`);
159
+ }
160
+ }
161
+ if (!cron.schedulerJobId) {
162
+ throw new Error(`Cron ${cron._id} not scheduled`);
163
+ }
164
+ console.log(`Canceling scheduler job ${cron.schedulerJobId}`);
165
+ await ctx.scheduler.cancel(cron.schedulerJobId);
166
+ if (cron.executionJobId) {
167
+ console.log(`Canceling execution job ${cron.executionJobId}`);
168
+ await ctx.scheduler.cancel(cron.executionJobId);
169
+ }
170
+ console.log(`Deleting cron ${cron._id}`);
171
+ await ctx.db.delete(cron._id);
172
+ },
173
+ });
174
+ // Continue rescheduling a cron job.
175
+ //
176
+ // This is the main worker function that does the scheduling but also schedules
177
+ // the target function so that it runs in a different context. As a result this
178
+ // function probably *shouldn't* fail since it isn't doing much, but under heavy
179
+ // OCC contention it's possible it may eventually fail. In this case the cron
180
+ // will be lost and we'll need a janitor job to recover it.
181
+ export const rescheduler = internalMutation({
182
+ args: {
183
+ id: v.id("crons"),
184
+ },
185
+ returns: v.null(),
186
+ handler: async (ctx, { id }) => {
187
+ // Cron job is the logical concept we're rescheduling repeatedly.
188
+ const cronJob = await ctx.db.get(id);
189
+ if (!cronJob) {
190
+ throw Error(`Cron ${id} not found`);
191
+ }
192
+ if (!cronJob.schedulerJobId) {
193
+ throw Error(`Cron ${id} not scheduled`);
194
+ }
195
+ // Scheduler job is the job that's running right now, that we use to trigger
196
+ // repeated executions.
197
+ const schedulerJob = await ctx.db.system.get(cronJob.schedulerJobId);
198
+ if (!schedulerJob) {
199
+ throw Error(`Scheduler job ${cronJob.schedulerJobId} not found`);
200
+ }
201
+ if (schedulerJob.state.kind !== "pending" &&
202
+ schedulerJob.state.kind !== "inProgress") {
203
+ throw Error(`We are running in job ${schedulerJob._id} but state is ${schedulerJob.state.kind}`);
204
+ }
205
+ // Execution job is the previous job used to actually do the work of the cron.
206
+ let stillRunning = false;
207
+ if (cronJob.executionJobId) {
208
+ const executionJob = await ctx.db.system.get(cronJob.executionJobId);
209
+ if (executionJob &&
210
+ (executionJob.state.kind === "pending" ||
211
+ executionJob.state.kind === "inProgress")) {
212
+ stillRunning = true;
213
+ }
214
+ }
215
+ if (stillRunning) {
216
+ console.log(`Cron ${cronJob._id} still running, skipping this run.`);
217
+ }
218
+ else {
219
+ console.log(`Running cron ${cronJob._id}.`);
220
+ await ctx.scheduler.runAfter(0, cronJob.functionHandle, cronJob.args);
221
+ }
222
+ await scheduleNextRun(ctx, id, new Date(schedulerJob.scheduledTime), cronJob.schedule);
223
+ },
224
+ });
225
+ //# sourceMappingURL=public.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.js","sourceRoot":"","sources":["../../../src/component/public.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,EAAE;AACF,6CAA6C;AAG7C,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAEL,QAAQ,EACR,KAAK,EACL,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,aAAa,CAAC;AAKjC,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC;AASxE,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC;IACjC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC;IACjB,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAC5B,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE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AY,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,CAAC,EAC3C;gBACA,YAAY,GAAG,IAAI,CAAC;aACrB;SACF;QACD,IAAI,YAAY,EAAE;YAChB,OAAO,CAAC,GAAG,CAAC,QAAQ,OAAO,CAAC,GAAG,oCAAoC,CAAC,CAAC;SACtE;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,gBAAgB,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC;YAC5C,MAAM,GAAG,CAAC,SAAS,CAAC,QAAQ,CAC1B,CAAC,EACD,OAAO,CAAC,cAAuD,EAC/D,OAAO,CAAC,IAAI,CACb,CAAC;SACH;QAED,MAAM,eAAe,CACnB,GAAG,EACH,EAAE,EACF,IAAI,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,EACpC,OAAO,CAAC,QAAQ,CACjB,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ declare const _default: import("convex/server").SchemaDefinition<{
2
+ crons: import("convex/server").TableDefinition<import("convex/values").VObject<{
3
+ name?: string | undefined;
4
+ schedulerJobId?: import("convex/values").GenericId<"_scheduled_functions"> | undefined;
5
+ executionJobId?: import("convex/values").GenericId<"_scheduled_functions"> | undefined;
6
+ functionHandle: string;
7
+ args: Record<string, any>;
8
+ schedule: {
9
+ kind: "interval";
10
+ ms: number;
11
+ } | {
12
+ kind: "cron";
13
+ cronspec: string;
14
+ };
15
+ }, {
16
+ name: import("convex/values").VString<string | undefined, "optional">;
17
+ functionHandle: import("convex/values").VString<string, "required">;
18
+ args: import("convex/values").VRecord<Record<string, any>, import("convex/values").VString<string, "required">, import("convex/values").VAny<any, "required", string>, "required", string>;
19
+ schedule: import("convex/values").VUnion<{
20
+ kind: "interval";
21
+ ms: number;
22
+ } | {
23
+ kind: "cron";
24
+ cronspec: string;
25
+ }, [import("convex/values").VObject<{
26
+ kind: "interval";
27
+ ms: number;
28
+ }, {
29
+ kind: import("convex/values").VLiteral<"interval", "required">;
30
+ ms: import("convex/values").VFloat64<number, "required">;
31
+ }, "required", "kind" | "ms">, import("convex/values").VObject<{
32
+ kind: "cron";
33
+ cronspec: string;
34
+ }, {
35
+ kind: import("convex/values").VLiteral<"cron", "required">;
36
+ cronspec: import("convex/values").VString<string, "required">;
37
+ }, "required", "kind" | "cronspec">], "required", "kind" | "ms" | "cronspec">;
38
+ schedulerJobId: import("convex/values").VId<import("convex/values").GenericId<"_scheduled_functions"> | undefined, "optional">;
39
+ executionJobId: import("convex/values").VId<import("convex/values").GenericId<"_scheduled_functions"> | undefined, "optional">;
40
+ }, "required", "name" | "functionHandle" | "args" | "schedule" | "schedulerJobId" | "executionJobId" | `args.${string}` | "schedule.kind" | "schedule.ms" | "schedule.cronspec">, {
41
+ name: ["name", "_creationTime"];
42
+ }, {}, {}>;
43
+ }, true>;
44
+ export default _default;
45
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/component/schema.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wBAmBG"}
@@ -0,0 +1,20 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ export default defineSchema({
4
+ // User space crons.
5
+ crons: defineTable({
6
+ name: v.optional(v.string()),
7
+ functionHandle: v.string(),
8
+ args: v.record(v.string(), v.any()),
9
+ schedule: v.union(v.object({
10
+ kind: v.literal("interval"),
11
+ ms: v.float64(), // milliseconds
12
+ }), v.object({
13
+ kind: v.literal("cron"),
14
+ cronspec: v.string(), // "* * * * *"
15
+ })),
16
+ schedulerJobId: v.optional(v.id("_scheduled_functions")),
17
+ executionJobId: v.optional(v.id("_scheduled_functions")), // async job to run the function
18
+ }).index("name", ["name"]),
19
+ });
20
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,eAAe,YAAY,CAAC;IAC1B,oBAAoB;IACpB,KAAK,EAAE,WAAW,CAAC;QACjB,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAC5B,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;QAC1B,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;QACnC,QAAQ,EAAE,CAAC,CAAC,KAAK,CACf,CAAC,CAAC,MAAM,CAAC;YACP,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC;YAC3B,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,eAAe;SACjC,CAAC,EACF,CAAC,CAAC,MAAM,CAAC;YACP,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;YACvB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,cAAc;SACrC,CAAC,CACH;QACD,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,sBAAsB,CAAC,CAAC;QACxD,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,sBAAsB,CAAC,CAAC,EAAE,gCAAgC;KAC3F,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;CAC3B,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function subtract(a: number, b: number): number;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/frontend/index.ts"],"names":[],"mappings":"AAKA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD"}
@@ -0,0 +1,8 @@
1
+ // This is where React components go.
2
+ if (typeof window === "undefined") {
3
+ throw new Error("this is frontend code, but it's running somewhere else!");
4
+ }
5
+ export function subtract(a, b) {
6
+ return a - b;
7
+ }
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/frontend/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;CAC5E;AAED,MAAM,UAAU,QAAQ,CAAC,CAAS,EAAE,CAAS;IAC3C,OAAO,CAAC,GAAG,CAAC,CAAC;AACf,CAAC"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@convex-dev/crons",
3
+ "version": "0.0.1",
4
+ "description": "Convex component for scheduling periodic jobs.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "scripts": {
8
+ "build": "npm run build:esm && npm run build:cjs",
9
+ "build:esm": "tsc --project ./esm.json && echo '{\\n \"type\": \"module\"\\n}' > dist/esm/package.json",
10
+ "build:cjs": "tsc --project ./commonjs.json && echo '{\\n \"type\": \"commonjs\"\\n}' > dist/esm/package.json",
11
+ "typecheck": "tsc --noEmit",
12
+ "prepare": "npm run build"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
18
+ "exports": {
19
+ "./package.json": "./package.json",
20
+ ".": {
21
+ "import": {
22
+ "@convex-dev/component-source": "./src/client/index.ts",
23
+ "types": "./dist/esm/client/index.d.ts",
24
+ "default": "./dist/esm/client/index.js"
25
+ },
26
+ "require": {
27
+ "@convex-dev/component-source": "./src/client/index.ts",
28
+ "types": "./dist/commonjs/client/index.d.ts",
29
+ "default": "./dist/commonjs/client/index.js"
30
+ }
31
+ },
32
+ "./convex.config.js": {
33
+ "import": {
34
+ "@convex-dev/component-source": "./src/component/convex.config.ts",
35
+ "types": "./dist/esm/component/convex.config.d.ts",
36
+ "default": "./dist/esm/component/convex.config.js"
37
+ }
38
+ }
39
+ },
40
+ "dependencies": {
41
+ "convex": "^1.16.1",
42
+ "cron-parser": "^4.9.0"
43
+ },
44
+ "devDependencies": {
45
+ "@eslint/js": "^9.9.1",
46
+ "@types/node": "^18.17.0",
47
+ "eslint": "^9.9.1",
48
+ "globals": "^15.9.0",
49
+ "prettier": "3.2.5",
50
+ "typescript": "~5.0.3",
51
+ "typescript-eslint": "^8.4.0"
52
+ },
53
+ "main": "./dist/commonjs/client/index.js",
54
+ "types": "./dist/commonjs/client/index.d.ts",
55
+ "module": "./dist/esm/client/index.js"
56
+ }
@@ -0,0 +1,128 @@
1
+ // Client side implementation of the Crons component.
2
+
3
+ import {
4
+ createFunctionHandle,
5
+ FunctionArgs,
6
+ FunctionHandle,
7
+ SchedulableFunctionReference,
8
+ } from "convex/server";
9
+ import { api } from "../component/_generated/api.js";
10
+ import { CronInfo, Schedule } from "../component/public.js";
11
+ import { RunMutationCtx, RunQueryCtx, UseApi } from "./utils.js";
12
+
13
+ export type { CronInfo };
14
+
15
+ // TODO (james): should we add helpers for minutely, hourly, etc schedules?
16
+
17
+ // Implementation of crons in user space.
18
+ //
19
+ // Supports intervals in ms as well as cron schedules with the same format as
20
+ // the unix `cron` command:
21
+ //
22
+ // * * * * * *
23
+ // ┬ ┬ ┬ ┬ ┬ ┬
24
+ // │ │ │ │ │ |
25
+ // │ │ │ │ │ └── day of week (0 - 7, 1L - 7L) (0 or 7 is Sun)
26
+ // │ │ │ │ └───── month (1 - 12)
27
+ // │ │ │ └──────── day of month (1 - 31, L)
28
+ // │ │ └─────────── hour (0 - 23)
29
+ // │ └────────────── minute (0 - 59)
30
+ // └───────────────── second (0 - 59, optional)
31
+ //
32
+ // Crons can be registered at runtime via the `register` function.
33
+ //
34
+ // If you'd like to statically define cronjobs like in the built-in `crons.ts`
35
+ // Convex feature you can do so via an init script that idempotently registers a
36
+ // cron with a given name. e.g., in an `init.ts` file that gets run on every
37
+ // deploy via `convex dev --run init`:
38
+ //
39
+ // const crons = new Crons(components.crons);
40
+ // ...
41
+ // if ((await crons.get(ctx, { name: "daily" })) === null) {
42
+ // await crons.register(
43
+ // ctx,
44
+ // { kind: "cron", cronspec: "0 0 * * *" },
45
+ // internal.example.logStuff,
46
+ // { message: "daily cron" },
47
+ // "daily"
48
+ // );
49
+ // }
50
+ export class Crons {
51
+ constructor(private component: UseApi<typeof api>) {}
52
+
53
+ /**
54
+ * Schedule a mutation or action to run on a cron schedule or interval.
55
+ *
56
+ * @param ctx - The mutation context from the calling Convex mutation.
57
+ * @param schedule - Either a cron specification string or an interval in
58
+ * milliseconds. For intervals, ms must be >= 1000.
59
+ * @param func - A function reference to the mutation or action to schedule.
60
+ * @param args - The arguments to the function.
61
+ * @param name - Optional unique name for the cron. Will throw if a name is
62
+ * provided and a cron with the same name already exists.
63
+ * @returns A string identifier for the cron job.
64
+ */
65
+ async register<F extends SchedulableFunctionReference>(
66
+ ctx: RunMutationCtx,
67
+ schedule: Schedule,
68
+ func: F,
69
+ args: FunctionArgs<F>,
70
+ name?: string
71
+ ): Promise<string> {
72
+ return ctx.runMutation(this.component.public.register, {
73
+ name,
74
+ schedule,
75
+ functionHandle: await createFunctionHandle(func),
76
+ args,
77
+ });
78
+ }
79
+
80
+ /**
81
+ * List all user space cron jobs.
82
+ *
83
+ * @returns List of `cron` table rows.
84
+ */
85
+ async list(ctx: RunQueryCtx): Promise<CronInfo[]> {
86
+ const crons = await ctx.runQuery(this.component.public.list, {});
87
+ return crons.map((cron) => ({
88
+ ...cron,
89
+ functionHandle: cron.functionHandle as FunctionHandle<
90
+ "mutation" | "action"
91
+ >,
92
+ }));
93
+ }
94
+
95
+ /**
96
+ * Get an existing cron job by id or name.
97
+ *
98
+ * @param identifier - Either the ID or name of the cron job.
99
+ * @returns Cron job document.
100
+ */
101
+ async get(
102
+ ctx: RunQueryCtx,
103
+ identifier: { id: string } | { name: string }
104
+ ): Promise<CronInfo | null> {
105
+ const cron = await ctx.runQuery(this.component.public.get, { identifier });
106
+ if (cron === null) {
107
+ return null;
108
+ }
109
+ return {
110
+ ...cron,
111
+ functionHandle: cron.functionHandle as FunctionHandle<
112
+ "mutation" | "action"
113
+ >,
114
+ };
115
+ }
116
+
117
+ /**
118
+ * Delete and deschedule a cron job by id or name.
119
+ *
120
+ * @param identifier - Either the ID or name of the cron job.
121
+ */
122
+ async del(
123
+ ctx: RunMutationCtx,
124
+ identifier: { id: string } | { name: string }
125
+ ): Promise<null> {
126
+ return ctx.runMutation(this.component.public.del, { identifier });
127
+ }
128
+ }
@@ -0,0 +1,44 @@
1
+ // Type utilities for component clients.
2
+
3
+ import {
4
+ Expand,
5
+ FunctionReference,
6
+ GenericDataModel,
7
+ GenericMutationCtx,
8
+ GenericQueryCtx,
9
+ } from "convex/server";
10
+ import { GenericId } from "convex/values";
11
+
12
+ export type RunQueryCtx = {
13
+ runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
14
+ };
15
+ export type RunMutationCtx = {
16
+ runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
17
+ };
18
+
19
+ export type OpaqueIds<T> =
20
+ T extends GenericId<infer _T>
21
+ ? string
22
+ : T extends (infer U)[]
23
+ ? OpaqueIds<U>[]
24
+ : T extends object
25
+ ? { [K in keyof T]: OpaqueIds<T[K]> }
26
+ : T;
27
+
28
+ export type UseApi<API> = Expand<{
29
+ [mod in keyof API]: API[mod] extends FunctionReference<
30
+ infer FType,
31
+ "public",
32
+ infer FArgs,
33
+ infer FReturnType,
34
+ infer FComponentPath
35
+ >
36
+ ? FunctionReference<
37
+ FType,
38
+ "internal",
39
+ OpaqueIds<FArgs>,
40
+ OpaqueIds<FReturnType>,
41
+ FComponentPath
42
+ >
43
+ : UseApi<API[mod]>;
44
+ }>;