AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(web): queue plugin schedule updates for the same task
tianma-ifde77005f1cfb
diff --git a/apps/web/src/lib/plugins/plugin-schedule-adapter.ts b/apps/web/src/lib/plugins/plugin-schedule-adapter.tsindex 591d8a00..34a33e0c 100644--- a/apps/web/src/lib/plugins/plugin-schedule-adapter.ts+++ b/apps/web/src/lib/plugins/plugin-schedule-adapter.ts@@ -2,6 +2,7 @@ import type { PluginSchedule, PluginScheduleInput } from "@edgeever/plugin-api";import type { ScheduledTask } from "@edgeever/shared";import { api } from "@/lib/api";import type { PluginScheduleAdapter } from "@/lib/plugins/plugin-host";+import { createPluginScheduleQueue } from "@/lib/plugins/plugin-schedule-queue";const toPluginSchedule = (task: ScheduledTask, deviceId: string): PluginSchedule => ({key: task.pluginScheduleKey ?? "",@@ -24,28 +25,36 @@ const toPluginSchedule = (task: ScheduledTask, deviceId: string): PluginScheduleexport const createPluginScheduleAdapter = (deviceId: string,onChanged: () => void | Promise<void>,-): PluginScheduleAdapter => ({- async upsert(pluginId: string, input: PluginScheduleInput) {- const { task } = await api.upsertPluginScheduledTask({- pluginId,- scheduleKey: input.key,- name: input.name,- commandId: input.commandId,- cronExpression: input.cronExpression,- timezone: input.timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone ?? "UTC",- executorDeviceId: deviceId,- missedRunPolicy: input.missedRunPolicy ?? "run-once",- isEnabled: input.isEnabled,- });- await onChanged();- return toPluginSchedule(task, deviceId);- },- async list(pluginId: string) {- const { tasks } = await api.listPluginScheduledTasks(pluginId);- return tasks.map((task) => toPluginSchedule(task, deviceId));- },- async remove(pluginId: string, key: string) {- await api.deletePluginScheduledTask(pluginId, key);- await onChanged();- },-});+): PluginScheduleAdapter => {+ const queue = createPluginScheduleQueue();+ const keyFor = (pluginId: string, scheduleKey: string) => `${pluginId}\0${scheduleKey}`;+ return {+ upsert(pluginId: string, input: PluginScheduleInput) {+ return queue.enqueue(keyFor(pluginId, input.key), async () => {+ const { task } = await api.upsertPluginScheduledTask({+ pluginId,+ scheduleKey: input.key,+ name: input.name,+ commandId: input.commandId,+ cronExpression: input.cronExpression,+ timezone: input.timezone?.trim() || Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",+ executorDeviceId: deviceId,+ missedRunPolicy: input.missedRunPolicy ?? "run-once",+ isEnabled: input.isEnabled,+ });+ await onChanged();+ return toPluginSchedule(task, deviceId);+ });+ },+ async list(pluginId: string) {+ const { tasks } = await api.listPluginScheduledTasks(pluginId);+ return tasks.map((task) => toPluginSchedule(task, deviceId));+ },+ remove(pluginId: string, key: string) {+ return queue.enqueue(keyFor(pluginId, key), async () => {+ await api.deletePluginScheduledTask(pluginId, key);+ await onChanged();+ });+ },+ };+};diff --git a/apps/web/src/lib/plugins/plugin-schedule-queue.test.mjs b/apps/web/src/lib/plugins/plugin-schedule-queue.test.mjsnew file mode 100644index 00000000..703309d8--- /dev/null+++ b/apps/web/src/lib/plugins/plugin-schedule-queue.test.mjs@@ -0,0 +1,56 @@+import { describe, expect, test } from "bun:test";+import { createPluginScheduleQueue } from "./plugin-schedule-queue.ts";++describe("plugin schedule queue", () => {+ test("runs a second upsert only after the first one finishes", async () => {+ const queue = createPluginScheduleQueue();+ const order = [];+ let releaseFirst = () => {};+ const first = queue.enqueue("org.plugin:daily", () => new Promise((resolve) => {+ releaseFirst = () => {+ order.push("first");+ resolve("first");+ };+ }));+ const second = queue.enqueue("org.plugin:daily", async () => {+ order.push("second");+ return "second";+ });++ expect(order).toEqual([]);+ await Promise.resolve();+ await Promise.resolve();+ releaseFirst();+ await expect(first).resolves.toBe("first");+ await expect(second).resolves.toBe("second");+ expect(order).toEqual(["first", "second"]);+ });++ test("a failed upsert does not block the next one for the same schedule", async () => {+ const queue = createPluginScheduleQueue();+ await expect(queue.enqueue("org.plugin:daily", async () => {+ throw new Error("database is locked");+ })).rejects.toThrow("database is locked");+ await expect(queue.enqueue("org.plugin:daily", async () => "enabled")).resolves.toBe("enabled");+ });++ test("different schedules do not wait for each other", async () => {+ const queue = createPluginScheduleQueue();+ const order = [];+ let releaseA = () => {};+ const a = queue.enqueue("a", () => new Promise((resolve) => {+ releaseA = () => {+ order.push("a");+ resolve("a");+ };+ }));+ const b = queue.enqueue("b", async () => {+ order.push("b");+ return "b";+ });+ await expect(b).resolves.toBe("b");+ expect(order).toEqual(["b"]);+ releaseA();+ await expect(a).resolves.toBe("a");+ });+});diff --git a/apps/web/src/lib/plugins/plugin-schedule-queue.ts b/apps/web/src/lib/plugins/plugin-schedule-queue.tsnew file mode 100644index 00000000..6bb4fde4--- /dev/null+++ b/apps/web/src/lib/plugins/plugin-schedule-queue.ts@@ -0,0 +1,17 @@+/** Runs async work for one key in order. A newer call waits until the previous one settles. */+export const createPluginScheduleQueue = () => {+ const tails = new Map<string, Promise<void>>();++ return {+ enqueue<T>(key: string, run: () => Promise<T>): Promise<T> {+ const previous = tails.get(key) ?? Promise.resolve();+ const result = previous.catch(() => undefined).then(run);+ const tail = result.then(() => undefined, () => undefined);+ tails.set(key, tail);+ void tail.finally(() => {+ if (tails.get(key) === tail) tails.delete(key);+ });+ return result;+ },+ };+};