Skip to content

Commit 2d8e0ef

Browse files
authored
remove runAtTime
1 parent f8e0963 commit 2d8e0ef

File tree

7 files changed

+15
-79
lines changed

7 files changed

+15
-79
lines changed

eslint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default [
3838
{
3939
argsIgnorePattern: "^_",
4040
varsIgnorePattern: "^_",
41-
"caughtErrorsIgnorePattern": "^_",
41+
caughtErrorsIgnorePattern: "^_",
4242
},
4343
],
4444
},

example/convex/_generated/api.d.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export declare const components: {
5656
fnArgs: any;
5757
fnHandle: string;
5858
fnName: string;
59-
fnType: "action" | "mutation" | "unknown";
59+
fnType: "action" | "mutation";
6060
options: {
6161
actionTimeoutMs?: number;
6262
debounceMs?: number;
@@ -66,9 +66,7 @@ export declare const components: {
6666
mutationTimeoutMs?: number;
6767
slowHeartbeatMs?: number;
6868
ttl?: number;
69-
unknownTimeoutMs?: number;
7069
};
71-
runAtTime: number;
7270
},
7371
string
7472
>;
@@ -104,7 +102,7 @@ export declare const components: {
104102
fnArgs: any;
105103
fnHandle: string;
106104
fnName: string;
107-
fnType: "action" | "mutation" | "unknown";
105+
fnType: "action" | "mutation";
108106
options: {
109107
actionTimeoutMs?: number;
110108
debounceMs?: number;
@@ -114,9 +112,7 @@ export declare const components: {
114112
mutationTimeoutMs?: number;
115113
slowHeartbeatMs?: number;
116114
ttl?: number;
117-
unknownTimeoutMs?: number;
118115
};
119-
runAtTime: number;
120116
},
121117
string
122118
>;

src/client/index.ts

-26
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export class WorkPool {
7979
fnName: getFunctionName(fn),
8080
fnArgs,
8181
fnType: "action",
82-
runAtTime: Date.now(),
8382
options: this.options,
8483
});
8584
return id as WorkId;
@@ -95,31 +94,6 @@ export class WorkPool {
9594
fnName: getFunctionName(fn),
9695
fnArgs,
9796
fnType: "mutation",
98-
runAtTime: Date.now(),
99-
options: this.options,
100-
});
101-
return id as WorkId;
102-
}
103-
// Unknown is if you don't know at runtime whether it's an action or mutation,
104-
// which can happen if it comes from `runAt` or `runAfter`.
105-
async enqueueUnknown<Args extends DefaultFunctionArgs>(
106-
ctx: RunMutationCtx,
107-
fn: FunctionReference<
108-
"action" | "mutation",
109-
FunctionVisibility,
110-
Args,
111-
null
112-
>,
113-
fnArgs: Args,
114-
runAtTime: number
115-
): Promise<WorkId> {
116-
const fnHandle = await createFunctionHandle(fn);
117-
const id = await ctx.runMutation(this.component.lib.enqueue, {
118-
fnHandle,
119-
fnName: getFunctionName(fn),
120-
fnArgs,
121-
fnType: "unknown",
122-
runAtTime,
12397
options: this.options,
12498
});
12599
return id as WorkId;

src/component/_generated/api.d.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type Mounts = {
4141
fnArgs: any;
4242
fnHandle: string;
4343
fnName: string;
44-
fnType: "action" | "mutation" | "unknown";
44+
fnType: "action" | "mutation";
4545
options: {
4646
actionTimeoutMs?: number;
4747
debounceMs?: number;
@@ -51,9 +51,7 @@ export type Mounts = {
5151
mutationTimeoutMs?: number;
5252
slowHeartbeatMs?: number;
5353
ttl?: number;
54-
unknownTimeoutMs?: number;
5554
};
56-
runAtTime: number;
5755
},
5856
string
5957
>;

src/component/lib.ts

+8-32
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ export const enqueue = mutation({
2525
fnHandle: v.string(),
2626
fnName: v.string(),
2727
fnArgs: v.any(),
28-
fnType: v.union(
29-
v.literal("action"),
30-
v.literal("mutation"),
31-
v.literal("unknown")
32-
),
33-
runAtTime: v.number(),
28+
fnType: v.union(v.literal("action"), v.literal("mutation")),
3429
options: v.object({
3530
maxParallelism: v.number(),
3631
actionTimeoutMs: v.optional(v.number()),
3732
mutationTimeoutMs: v.optional(v.number()),
38-
unknownTimeoutMs: v.optional(v.number()),
3933
debounceMs: v.optional(v.number()),
4034
fastHeartbeatMs: v.optional(v.number()),
4135
slowHeartbeatMs: v.optional(v.number()),
@@ -44,16 +38,12 @@ export const enqueue = mutation({
4438
}),
4539
},
4640
returns: v.id("pendingWork"),
47-
handler: async (
48-
ctx,
49-
{ fnHandle, fnName, options, fnArgs, fnType, runAtTime }
50-
) => {
41+
handler: async (ctx, { fnHandle, fnName, options, fnArgs, fnType }) => {
5142
const debounceMs = options.debounceMs ?? 50;
5243
await ensurePoolExists(ctx, {
5344
maxParallelism: options.maxParallelism,
5445
actionTimeoutMs: options.actionTimeoutMs ?? 15 * 60 * 1000,
5546
mutationTimeoutMs: options.mutationTimeoutMs ?? 30 * 1000,
56-
unknownTimeoutMs: options.unknownTimeoutMs ?? 15 * 60 * 1000,
5747
debounceMs,
5848
fastHeartbeatMs: options.fastHeartbeatMs ?? 10 * 1000,
5949
slowHeartbeatMs: options.slowHeartbeatMs ?? 2 * 60 * 60 * 1000,
@@ -65,10 +55,8 @@ export const enqueue = mutation({
6555
fnName,
6656
fnArgs,
6757
fnType,
68-
runAtTime,
6958
});
70-
const delay = Math.max(runAtTime - Date.now(), debounceMs);
71-
await kickMainLoop(ctx, delay, false);
59+
await kickMainLoop(ctx, debounceMs, false);
7260
return workId;
7361
},
7462
});
@@ -150,10 +138,7 @@ export const mainLoop = internalMutation({
150138
BATCH_SIZE
151139
);
152140
let didSomething = false;
153-
const pending = await ctx.db
154-
.query("pendingWork")
155-
.withIndex("runAtTime", (q) => q.lte("runAtTime", Date.now()))
156-
.take(toSchedule);
141+
const pending = await ctx.db.query("pendingWork").take(toSchedule);
157142
console_.debug(`scheduling ${pending.length} pending work`);
158143
await Promise.all(
159144
pending.map(async (work) => {
@@ -254,12 +239,9 @@ export const mainLoop = internalMutation({
254239
} else {
255240
// Decide when to wake up.
256241
const allInProgressWork = await ctx.db.query("inProgressWork").collect();
257-
const nextPending = await ctx.db
258-
.query("pendingWork")
259-
.withIndex("runAtTime")
260-
.first();
242+
const nextPending = await ctx.db.query("pendingWork").first();
261243
const nextPendingTime = nextPending
262-
? nextPending.runAtTime
244+
? nextPending._creationTime
263245
: slowHeartbeatMs + Date.now();
264246
const nextInProgress = allInProgressWork.length
265247
? Math.min(
@@ -285,8 +267,8 @@ async function beginWork(
285267
if (!options) {
286268
throw new Error("cannot begin work with no pool");
287269
}
288-
recordStarted(work._id, work.fnName, work._creationTime, work.runAtTime);
289-
const { mutationTimeoutMs, actionTimeoutMs, unknownTimeoutMs } = options;
270+
recordStarted(work._id, work.fnName, work._creationTime);
271+
const { mutationTimeoutMs, actionTimeoutMs } = options;
290272
if (work.fnType === "action") {
291273
return {
292274
scheduledId: await ctx.scheduler.runAfter(
@@ -313,12 +295,6 @@ async function beginWork(
313295
),
314296
timeoutMs: mutationTimeoutMs,
315297
};
316-
} else if (work.fnType === "unknown") {
317-
const fnHandle = work.fnHandle as FunctionHandle<"action" | "mutation">;
318-
return {
319-
scheduledId: await ctx.scheduler.runAfter(0, fnHandle, work.fnArgs),
320-
timeoutMs: unknownTimeoutMs,
321-
};
322298
} else {
323299
throw new Error(`Unexpected fnType ${work.fnType}`);
324300
}

src/component/schema.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default defineSchema({
4848
maxParallelism: v.number(),
4949
actionTimeoutMs: v.number(),
5050
mutationTimeoutMs: v.number(),
51-
unknownTimeoutMs: v.number(),
5251
debounceMs: v.number(),
5352
fastHeartbeatMs: v.number(),
5453
slowHeartbeatMs: v.number(),
@@ -69,16 +68,11 @@ export default defineSchema({
6968
}).index("runAtTime", ["runAtTime"]),
7069

7170
pendingWork: defineTable({
72-
fnType: v.union(
73-
v.literal("action"),
74-
v.literal("mutation"),
75-
v.literal("unknown")
76-
),
71+
fnType: v.union(v.literal("action"), v.literal("mutation")),
7772
fnHandle: v.string(),
7873
fnName: v.string(),
7974
fnArgs: v.any(),
80-
runAtTime: v.number(),
81-
}).index("runAtTime", ["runAtTime"]),
75+
}),
8276
pendingCompletion: defineTable({
8377
completionStatus,
8478
workId: v.id("pendingWork"),

src/component/stats.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ workpool
2222
export function recordStarted(
2323
workId: Id<"pendingWork">,
2424
fnName: string,
25-
enqueuedAt: number,
26-
runAtTime: number
25+
enqueuedAt: number
2726
) {
2827
console.log(
2928
JSON.stringify({
3029
workId,
3130
event: "started",
3231
fnName,
3332
enqueuedAt,
34-
runAtTime,
3533
startedAt: Date.now(),
3634
lagSinceEnqueued: Date.now() - enqueuedAt,
3735
})

0 commit comments

Comments
 (0)