mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 21:34:48 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - The server signals queue work with database work signals, and it
sends a PostgreSQL notification for each signal so that another server
can wake during a deploy overlap
> - Each signal sends its own `select pg_notify($1, $2)`, inside the
open transaction, and waits for it
> - On staging this is the most-called statement in the database: 31.8
per second, 15.1 million calls in total against 7.5 million `begin`,
about 3.7 awaits per transaction
> - Every await inside an open transaction holds its locks and its
connection for one more turn of a saturated event loop
> - This pull request collects a transaction's notifications and sends
them in one statement just before commit
> - The benefit is at most one notification round trip per transaction,
with the same payloads delivered at the same commit
## Linked Issues or Issue Description
**What existing behavior does this improve?**
Database work signals send one `pg_notify` statement per signal, inside
the transaction.
**Subsystem affected**
`packages/db` work signals, used by every delivery queue in the server.
**Current behavior**
`signalDatabaseWork` and `notifyDatabaseWorkOnCommit` each run `select
pg_notify(channel, payload)` on the transaction and wait for it.
`notifyIssueRecovery` alone signals 7 queues per call, and a transaction
often calls it several times. A local legacy run (wake, start, 20 output
lines, finish) sends 424 notification statements. Staging measures 31.8
per second, 8.5 `begin` per second.
**Proposed behavior**
A signal inside a transaction records its payload on that transaction.
Before the outermost commit, the transaction sends every distinct
payload in one statement. Payloads and the channel are byte-identical to
today's, and PostgreSQL still delivers them only on commit.
**Reason and benefit**
At most one notification statement per transaction instead of one per
signal. Fewer awaits while a transaction holds its locks and its
connection.
**Breaking changes**
None. The listener protocol and the payload format do not change.
## What Changed
- `packages/db/src/work-signals.ts`: a signal records `(channel,
payload)` on the current transaction level. A released savepoint merges
its records into its parent; a rolled-back savepoint drops them. The
outermost transaction sends all distinct payloads in one statement
(`select pg_notify(channel, payload) from
jsonb_array_elements_text($2::jsonb)`) after its callback returns and
before commit. A transaction that signals nothing sends no extra
statement.
- The record lives on the transaction object only. A retried callback
gets a new transaction object and starts empty.
- `publishDatabaseWorkHint` (outside a transaction) is unchanged and
still sends at once.
- `packages/db/src/work-signals.test.ts`: tests on real PostgreSQL with
a LISTEN client.
## Verification
- `pnpm --filter @paperclipai/db exec vitest run
src/work-signals.test.ts`: 22 pass. 6 of the new tests fail on master.
- New tests (real PostgreSQL, LISTEN client, asserting what is delivered
as well as what is sent):
- Distinct topics in one transaction: one statement at commit. The
payloads delivered are identical to the format on master (`{version,
origin, topic}`), in first-signal order.
- A transaction that signals nothing: no notification statement.
- Savepoints: a released savepoint two levels deep keeps its topic. A
rolled-back savepoint drops its topic. A topic from a rolled-back
savepoint that the outer level signals again is still delivered.
- The callback throws: nothing is sent or delivered.
- The commit-time send fails: the transaction fails and rolls back its
write, and nothing is delivered.
- 500 distinct topics: one statement, all 500 delivered.
- Sequential, concurrent and retried transactions each start clean.
- Server suites that use notifications pass: `deployment-notifications`,
`run-start-handover`, `heartbeat-run-start-handover`,
`agent-runtime-hooks`, `legacy-controller-lease`,
`heartbeat-run-dispatch-lifecycle` (99 tests). Two of them hit statement
timeouts once on a loaded machine and then passed 41/41 when run alone.
- Before and after, one local legacy run (embedded PostgreSQL, process
adapter, 20 output lines 50 ms apart, uninstrumented pass, alternating
arms):
`TSX_DISABLE_CACHE=1 UNITS=legacy TASKS=50 AGENTS=3
NODE_OPTIONS=--expose-gc pnpm --filter @paperclipai/server exec tsx
scripts/query-cost/drive.ts` (harness from #21413)
| Per legacy run | Before (2 runs) | After (2 runs) |
| --- | --- | --- |
| Statements | 883, 883 | 474, 491 |
| `pg_notify` statements | 424 | 21 (one per signalling transaction) |
| Notification round trips per signalling transaction | about 20 | 1 |
| Transactions (BEGIN plus COMMIT) | 72 | 72 |
| Allocated | 81.1, 79.3 MB | 76.3, 74.3 MB |
| Wall time | 5.0, 5.2 s | 4.1, 7.7 s (noisy: loaded machine) |
Statement counts are exact and stable. Allocation fell by about 5 MB per
run (about 6%). Wall time varies too much on this machine to compare.
- Staging, measured by the coordinator (Neon statistics, 64 seconds):
31.8 `pg_notify` per second, 8.5 `begin` per second. At about 44 KB of
allocation per statement, notifications cost about 1.4 MB/s, about 2.4%
of the 57.6 MB/s. The allocation saving is part of that, about 2%, and
it is not the reason for this change. The reason is round trips inside
open transactions: at most one notification statement per transaction
instead of about 3.7. The per-second effect on staging is an estimate
until it is measured after deploy.
## Risks
- A failure of the notification send now surfaces at commit time, not at
the signal call. No caller catches errors around a signal call and
continues: the only `.catch` near a signal (`notifyBudgetStopsSettled`)
wraps a root-level call that is not in a transaction and already fails
before this change.
- Order within a transaction: notifications are sent in first-signal
order. A savepoint's records join its parent when the savepoint is
released, which matches the order of sequential code. Listeners do not
depend on order: each payload is an independent hint, and PostgreSQL
already removes duplicate payloads within a transaction.
- The notification is sent after the transaction's last statement
instead of in the middle. Delivery still happens only at commit, as
before.
Remaining work, not in this pull request: sending stops being useful
when no other server listens. The listener is open only during a startup
handoff, while this process owns a native execution, or during a
run-start handover, and a server drops its own notifications. A later
change could send only while an overlap is possible. It would have to
prove this interleaving: a new server announces itself, the old server
is still outside its own window, and the old server must start sending
before the new server relies on a hint. Otherwise the new server waits
for its next reconciliation pass.
## Model Used
- Claude Opus 5.5 (claude-opus-5-5), Anthropic, with tool use and code
execution in Claude Code.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes (no
documentation describes the send timing)
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
359 lines
19 KiB
TypeScript
359 lines
19 KiB
TypeScript
import { sql } from "drizzle-orm";
|
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import { createDb, withDedicatedDbConnection } from "./client.js";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./test-embedded-postgres.js";
|
|
import { DATABASE_WORK_CHANNEL, databaseWorkScopeKey, databaseWorkPending, enableDatabaseWorkNotifications, installDatabaseWorkSignals, notifyDatabaseWorkOnCommit, reconcileDatabaseWork, signalDatabaseWork, subscribeDatabaseWork } from "./work-signals.js";
|
|
|
|
const support = await getEmbeddedPostgresTestSupport();
|
|
(support.supported ? describe : describe.skip)("database work signals", () => {
|
|
let db: ReturnType<typeof createDb>;
|
|
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
beforeAll(async () => {
|
|
temporary = await startEmbeddedPostgresTestDatabase("work-signals-");
|
|
db = createDb(temporary.connectionString);
|
|
await db.execute(sql`create table work_signal_test (id text primary key)`);
|
|
}, 90_000);
|
|
afterAll(async () => { await temporary?.cleanup(); });
|
|
|
|
it("defers nested savepoint notifications until the outer commit and preserves return values", async () => {
|
|
const events: string[] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, "nested", event => events.push(event));
|
|
try {
|
|
const result = await db.transaction(async tx => {
|
|
await tx.transaction(async nested => {
|
|
await signalDatabaseWork(nested, "nested");
|
|
await nested.execute(sql`insert into work_signal_test values ('nested')`);
|
|
});
|
|
expect(events).toEqual(["intent"]);
|
|
expect(databaseWorkPending(db, "nested")).toBe(true);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = 'nested'`)).toHaveLength(0);
|
|
return 123;
|
|
});
|
|
expect(result).toBe(123);
|
|
expect(events).toEqual(["intent", "settled"]);
|
|
expect(databaseWorkPending(db, "nested")).toBe(false);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = 'nested'`)).toHaveLength(1);
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("sends an untracked commit-bound hint that leaves nothing to reconcile", async () => {
|
|
const events: string[] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, "untracked", event => events.push(event));
|
|
try {
|
|
await expect(db.transaction(async tx => {
|
|
await notifyDatabaseWorkOnCommit(tx, "untracked");
|
|
throw new Error("abort");
|
|
})).rejects.toThrow("abort");
|
|
await db.transaction(async tx => { await notifyDatabaseWorkOnCommit(tx, "untracked"); });
|
|
expect(databaseWorkPending(db, "untracked")).toBe(false);
|
|
expect(events).toEqual([]);
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("reconciles a rollback without leaving a permanent pending flag", async () => {
|
|
await expect(db.transaction(async tx => {
|
|
await signalDatabaseWork(tx, "rollback");
|
|
await tx.execute(sql`insert into work_signal_test values ('rollback')`);
|
|
throw new Error("abort");
|
|
})).rejects.toThrow("abort");
|
|
await reconcileDatabaseWork(db, "rollback");
|
|
expect(databaseWorkPending(db, "rollback")).toBe(false);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = 'rollback'`)).toHaveLength(0);
|
|
});
|
|
|
|
it("coalesces scoped hints and publishes them only when the outer transaction settles", async () => {
|
|
const settled: string[][] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, "scoped", (event, hints) => {
|
|
if (event === "settled") settled.push([...(hints ?? [])]);
|
|
});
|
|
try {
|
|
await db.transaction(async tx => {
|
|
await signalDatabaseWork(tx, "scoped", "connection-a");
|
|
await tx.transaction(async nested => {
|
|
await signalDatabaseWork(nested, "scoped", "connection-b");
|
|
await signalDatabaseWork(nested, "scoped", "connection-a");
|
|
});
|
|
expect(settled).toEqual([]);
|
|
});
|
|
expect(settled).toEqual([["connection-a", "connection-b"]]);
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("keeps outer ownership after a caught savepoint rollback", async () => {
|
|
const events: string[] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, "savepoint", event => events.push(event));
|
|
try {
|
|
await db.transaction(async tx => {
|
|
await expect(tx.transaction(async nested => {
|
|
await signalDatabaseWork(nested, "savepoint");
|
|
await nested.execute(sql`insert into work_signal_test values ('savepoint')`);
|
|
throw new Error("rollback savepoint");
|
|
})).rejects.toThrow("rollback savepoint");
|
|
expect(databaseWorkPending(db, "savepoint")).toBe(true);
|
|
expect(events).toEqual(["intent"]);
|
|
await signalDatabaseWork(tx, "savepoint");
|
|
await tx.execute(sql`insert into work_signal_test values ('savepoint-retry')`);
|
|
});
|
|
expect(events).toEqual(["intent", "intent", "settled"]);
|
|
expect(databaseWorkPending(db, "savepoint")).toBe(false);
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it.each(["commit", "abort"])("does not clear a late %s until PostgreSQL reports a settled transaction", async outcome => {
|
|
// Inject a lost response at the client boundary while a REAL transaction
|
|
// remains open on PostgreSQL. The status probe and row visibility are real.
|
|
const raw = drizzle(db.$client);
|
|
const transaction = raw.transaction.bind(raw);
|
|
let release!: () => void;
|
|
const gate = new Promise<void>(resolve => { release = resolve; });
|
|
let wrote!: () => void;
|
|
const written = new Promise<void>(resolve => { wrote = resolve; });
|
|
let serverDone!: Promise<unknown>;
|
|
raw.transaction = ((callback: any) => {
|
|
serverDone = transaction(async tx => {
|
|
const result = await callback(tx);
|
|
wrote();
|
|
await gate;
|
|
if (outcome === "abort") throw new Error("server abort");
|
|
return result;
|
|
});
|
|
// Observe rejection immediately; tests await the same result below.
|
|
void serverDone.catch(() => {});
|
|
return written.then(() => { throw new Error("lost transaction response"); });
|
|
}) as typeof raw.transaction;
|
|
const owner = installDatabaseWorkSignals(raw);
|
|
const topic = `late-${outcome}`;
|
|
const settled: string[][] = [];
|
|
const unsubscribe = subscribeDatabaseWork(owner, topic, (event, hints) => {
|
|
if (event === "settled") settled.push([...(hints ?? [])]);
|
|
});
|
|
try {
|
|
await expect(owner.transaction(async tx => {
|
|
await signalDatabaseWork(tx, topic, "connection-late");
|
|
await tx.execute(sql`insert into work_signal_test values (${topic})`);
|
|
})).rejects.toThrow("lost transaction response");
|
|
await reconcileDatabaseWork(owner, topic);
|
|
expect(databaseWorkPending(owner, topic)).toBe(true);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = ${topic}`)).toHaveLength(0);
|
|
settled.length = 0;
|
|
release(); await serverDone.catch(() => {});
|
|
await reconcileDatabaseWork(owner, topic);
|
|
expect(settled).toEqual([["connection-late"]]);
|
|
expect(databaseWorkPending(owner, topic)).toBe(false);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = ${topic}`)).toHaveLength(outcome === "commit" ? 1 : 0);
|
|
} finally { release(); await serverDone?.catch(() => {}); unsubscribe(); }
|
|
});
|
|
|
|
it("recovers after a real backend disconnect without replaying the queue write", async () => {
|
|
await expect(db.transaction(async tx => {
|
|
await signalDatabaseWork(tx, "disconnect");
|
|
await tx.execute(sql`insert into work_signal_test values ('disconnect')`);
|
|
const [{ pid }] = await tx.execute(sql`select pg_backend_pid() as pid`);
|
|
await db.execute(sql`select pg_terminate_backend(${pid as number})`);
|
|
await tx.execute(sql`select 1`);
|
|
})).rejects.toThrow();
|
|
await reconcileDatabaseWork(db, "disconnect");
|
|
expect(databaseWorkPending(db, "disconnect")).toBe(false);
|
|
expect(await db.execute(sql`select * from work_signal_test where id = 'disconnect'`)).toHaveLength(0);
|
|
});
|
|
|
|
it("shares notifications with dedicated child pools but isolates unrelated roots and topics", async () => {
|
|
const listener = vi.fn();
|
|
const other = createDb(temporary.connectionString);
|
|
const unsubscribe = subscribeDatabaseWork(db, "dedicated", listener);
|
|
try {
|
|
await other.transaction(tx => signalDatabaseWork(tx, "dedicated"));
|
|
await db.transaction(tx => signalDatabaseWork(tx, "other"));
|
|
expect(listener).not.toHaveBeenCalled();
|
|
await withDedicatedDbConnection(db, dedicated => dedicated.transaction(tx => signalDatabaseWork(tx, "dedicated")));
|
|
expect(listener.mock.calls.map(([event]) => event)).toEqual(["intent", "settled"]);
|
|
} finally { unsubscribe(); await other.$client.end(); }
|
|
});
|
|
|
|
it("never changes transaction success because a listener throws", async () => {
|
|
const warn = vi.spyOn(process, "emitWarning").mockImplementation(() => {});
|
|
const unsubscribe = subscribeDatabaseWork(db, "listener", () => { throw new Error("listener"); });
|
|
try {
|
|
await expect(db.transaction(async tx => { await signalDatabaseWork(tx, "listener"); return 7; })).resolves.toBe(7);
|
|
expect(warn).toHaveBeenCalledTimes(2);
|
|
} finally { unsubscribe(); warn.mockRestore(); }
|
|
});
|
|
});
|
|
|
|
describe("work signal query boundaries", () => {
|
|
it("adds no SQL to transactions which do not register queue work", async () => {
|
|
const execute = vi.fn();
|
|
const connection = installDatabaseWorkSignals({ execute, transaction: async (callback: any) => callback({ execute, transaction: async () => {} }) });
|
|
await expect(connection.transaction(async () => 19)).resolves.toBe(19);
|
|
expect(execute).not.toHaveBeenCalled();
|
|
});
|
|
it("fails before the write if XID acquisition fails, without retaining uncertainty", async () => {
|
|
const execute = vi.fn().mockRejectedValue(new Error("disconnected"));
|
|
const connection = installDatabaseWorkSignals({ execute, transaction: async (callback: any) => callback({ execute, transaction: async () => {} }) });
|
|
const write = vi.fn();
|
|
await expect(connection.transaction(async (tx: any) => { await signalDatabaseWork(tx, "failed"); write(); })).rejects.toThrow("disconnected");
|
|
expect(write).not.toHaveBeenCalled();
|
|
expect(databaseWorkPending(connection, "failed")).toBe(false);
|
|
});
|
|
it("rejects registration on an autocommit root", async () => {
|
|
const connection = installDatabaseWorkSignals({ execute: vi.fn(), transaction: vi.fn() });
|
|
await expect(signalDatabaseWork(connection, "bad")).rejects.toThrow("inside a transaction");
|
|
});
|
|
});
|
|
|
|
|
|
describe("database work scope identity", () => {
|
|
type TestConnection = { execute: () => Promise<unknown[]>; transaction: (callback: (tx: TestConnection) => unknown) => Promise<unknown> };
|
|
function connection(): TestConnection {
|
|
return { execute: vi.fn(async () => []), transaction: async callback => callback(connection()) };
|
|
}
|
|
it("shares owner identity through child pools and nested transactions, but isolates independent owners", async () => {
|
|
const first = installDatabaseWorkSignals(connection());
|
|
const second = installDatabaseWorkSignals(connection());
|
|
const child = installDatabaseWorkSignals(connection(), first);
|
|
const key = databaseWorkScopeKey(first);
|
|
expect(databaseWorkScopeKey(second)).not.toBe(key);
|
|
expect(databaseWorkScopeKey(child)).toBe(key);
|
|
await child.transaction(async tx => {
|
|
expect(databaseWorkScopeKey(tx)).toBe(key);
|
|
await tx.transaction(async nested => { expect(databaseWorkScopeKey(nested)).toBe(key); });
|
|
});
|
|
});
|
|
});
|
|
|
|
(support.supported ? describe : describe.skip)("database work notifications flushed once per transaction", () => {
|
|
let db: ReturnType<typeof createDb>;
|
|
let listener: ReturnType<typeof createDb>;
|
|
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
const delivered: string[] = [];
|
|
let notifyStatements = 0;
|
|
let sentPayloads: string[] = [];
|
|
beforeAll(async () => {
|
|
temporary = await startEmbeddedPostgresTestDatabase("work-signals-notify-");
|
|
db = createDb(temporary.connectionString);
|
|
enableDatabaseWorkNotifications(db);
|
|
listener = createDb(temporary.connectionString, { maxConnections: 1 });
|
|
await listener.$client.listen(DATABASE_WORK_CHANNEL, payload => { delivered.push(payload); });
|
|
db.$client.options.debug = (_connection: number, query: string, parameters: unknown[]) => {
|
|
if (!/pg_notify/.test(query) || parameters.some(value => String(value).includes("fence"))) return;
|
|
notifyStatements += 1;
|
|
sentPayloads.push(...JSON.parse(String(parameters[parameters.length - 1])) as string[]);
|
|
};
|
|
}, 90_000);
|
|
afterAll(async () => {
|
|
await listener?.$client.end({ timeout: 1 });
|
|
await db?.$client.end({ timeout: 1 });
|
|
await temporary?.cleanup();
|
|
});
|
|
const topic = (payload: string) => (JSON.parse(payload) as { topic: string }).topic;
|
|
async function run(action: (root: typeof db) => Promise<unknown>) {
|
|
notifyStatements = 0;
|
|
sentPayloads = [];
|
|
delivered.length = 0;
|
|
let failure: unknown = null;
|
|
await action(db).catch(error => { failure = error; });
|
|
await db.execute(sql`select pg_notify(${DATABASE_WORK_CHANNEL}, ${JSON.stringify({ topic: "fence" })})`);
|
|
await vi.waitFor(() => expect(delivered.some(payload => topic(payload) === "fence")).toBe(true));
|
|
const received = delivered.filter(payload => topic(payload) !== "fence");
|
|
return { statements: notifyStatements, sent: sentPayloads.map(topic), delivered: received.map(topic), payloads: received,
|
|
failure: failure instanceof Error ? failure.message : failure };
|
|
}
|
|
const rollback = () => { throw new Error("rollback"); };
|
|
|
|
it("sends every distinct topic of a transaction in one statement at commit, in first-signal order", async () => {
|
|
const result = await run(root => root.transaction(async tx => {
|
|
await signalDatabaseWork(tx, "a", "one");
|
|
await signalDatabaseWork(tx, "b");
|
|
await signalDatabaseWork(tx, "a", "two");
|
|
await notifyDatabaseWorkOnCommit(tx, "c");
|
|
expect(notifyStatements).toBe(0);
|
|
}));
|
|
expect(result).toMatchObject({ statements: 1, sent: ["a", "b", "c"], delivered: ["a", "b", "c"], failure: null });
|
|
expect(result.payloads.map(payload => JSON.parse(payload))).toEqual(["a", "b", "c"].map(name =>
|
|
({ version: 1, origin: expect.any(String), topic: name })));
|
|
});
|
|
|
|
it("sends nothing for a transaction that signals nothing", async () => {
|
|
expect(await run(root => root.transaction(async tx => { await tx.execute(sql`select 1`); })))
|
|
.toMatchObject({ statements: 0, delivered: [] });
|
|
});
|
|
|
|
it("keeps a released savepoint's topics, two levels deep, and drops a rolled-back savepoint's topics", async () => {
|
|
expect(await run(root => root.transaction(async tx => {
|
|
await tx.transaction(async first => {
|
|
await first.transaction(async second => { await signalDatabaseWork(second, "kept"); });
|
|
await first.transaction(async second => { await signalDatabaseWork(second, "dropped"); rollback(); }).catch(() => {});
|
|
});
|
|
await tx.transaction(async nested => { await signalDatabaseWork(nested, "dropped-too"); rollback(); }).catch(() => {});
|
|
}))).toMatchObject({ statements: 1, sent: ["kept"], delivered: ["kept"], failure: null });
|
|
});
|
|
|
|
it("still sends a topic signalled in a rolled-back savepoint when the outer level signals it again", async () => {
|
|
expect(await run(root => root.transaction(async tx => {
|
|
await tx.transaction(async nested => { await signalDatabaseWork(nested, "a"); rollback(); }).catch(() => {});
|
|
await signalDatabaseWork(tx, "a");
|
|
}))).toMatchObject({ statements: 1, delivered: ["a"] });
|
|
expect(await run(root => root.transaction(async tx => {
|
|
await signalDatabaseWork(tx, "a");
|
|
await tx.transaction(async nested => { await signalDatabaseWork(nested, "a"); rollback(); }).catch(() => {});
|
|
}))).toMatchObject({ statements: 1, delivered: ["a"] });
|
|
});
|
|
|
|
it("sends nothing when the callback throws", async () => {
|
|
expect(await run(root => root.transaction(async tx => { await signalDatabaseWork(tx, "a"); rollback(); })))
|
|
.toMatchObject({ statements: 0, delivered: [], failure: "rollback" });
|
|
});
|
|
|
|
it("fails and rolls back the transaction when the commit-time send fails", async () => {
|
|
await db.execute(sql`create table if not exists work_signal_flush_test (id text primary key)`);
|
|
const result = await run(root => root.transaction(async tx => {
|
|
await tx.execute(sql`insert into work_signal_flush_test values ('flush')`);
|
|
await signalDatabaseWork(tx, "a");
|
|
await tx.execute(sql`select 1 / 0`).catch(() => {});
|
|
}));
|
|
expect(result.delivered).toEqual([]);
|
|
expect(result.statements).toBe(1);
|
|
expect(result.failure).toMatch(/pg_notify/);
|
|
expect(await db.execute(sql`select id from work_signal_flush_test where id = 'flush'`)).toHaveLength(0);
|
|
});
|
|
|
|
it("sends a large number of distinct topics in one bounded statement", async () => {
|
|
const names = Array.from({ length: 500 }, (_, index) => `topic-${index}`);
|
|
const result = await run(root => root.transaction(async tx => { for (const name of names) await signalDatabaseWork(tx, name); }));
|
|
expect(result.statements).toBe(1);
|
|
expect(result.delivered).toEqual(names);
|
|
});
|
|
|
|
it("rejects a signal recorded after its transaction or savepoint ended instead of losing it", async () => {
|
|
let outer: Parameters<Parameters<typeof db.transaction>[0]>[0] | undefined;
|
|
let savepoint: typeof outer;
|
|
const result = await run(root => root.transaction(async tx => {
|
|
outer = tx;
|
|
await signalDatabaseWork(tx, "a");
|
|
await tx.transaction(async nested => { savepoint = nested; await signalDatabaseWork(nested, "b"); });
|
|
await expect(signalDatabaseWork(savepoint!, "late-savepoint")).rejects.toThrow("after its transaction or savepoint ended");
|
|
await expect(notifyDatabaseWorkOnCommit(savepoint!, "late-savepoint")).rejects.toThrow("after its transaction or savepoint ended");
|
|
}));
|
|
expect(result).toMatchObject({ statements: 1, delivered: ["a", "b"], failure: null });
|
|
await expect(signalDatabaseWork(outer!, "late-outer")).rejects.toThrow("after its transaction or savepoint ended");
|
|
await expect(notifyDatabaseWorkOnCommit(outer!, "late-outer")).rejects.toThrow("after its transaction or savepoint ended");
|
|
});
|
|
|
|
it("starts every transaction clean, including concurrent transactions and a retried callback", async () => {
|
|
expect(await run(async root => {
|
|
await root.transaction(async tx => { await signalDatabaseWork(tx, "a"); });
|
|
await root.transaction(async tx => { await signalDatabaseWork(tx, "a"); });
|
|
await Promise.all([1, 2].map(() => root.transaction(async tx => { await signalDatabaseWork(tx, "b"); })));
|
|
let attempts = 0;
|
|
const callback = async (tx: Parameters<Parameters<typeof root.transaction>[0]>[0]) => {
|
|
attempts += 1;
|
|
await signalDatabaseWork(tx, "c");
|
|
if (attempts === 1) rollback();
|
|
};
|
|
await root.transaction(callback).catch(() => {});
|
|
await root.transaction(callback);
|
|
})).toMatchObject({ statements: 5, delivered: ["a", "a", "b", "b", "c"] });
|
|
});
|
|
});
|