@teamscala/service-boot 0.23.306 → 0.23.308

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.
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "name": "@teamscala/service-boot",
8
8
  "type": "module",
9
- "version": "0.23.306",
9
+ "version": "0.23.308",
10
10
  "sideEffects": false,
11
11
  "files": [
12
12
  "src"
@@ -18,7 +18,7 @@ export type PgBossJobHandler = (ctx: {
18
18
  config: ScheduledJobConfig;
19
19
  /** Per-job data payload from a one-shot `boss.send()` — absent for scheduled jobs. */
20
20
  jobData?: unknown;
21
- }) => Promise<void> | void;
21
+ }) => Promise<unknown> | unknown;
22
22
  export declare const pgBossHandlerRegistry: import("@teamscala/generic-registry/create-registry").Registry<PgBossJobHandler>;
23
23
  /**
24
24
  * Register a handler for a `scheduled_job.handler_slug`. Idempotent on re-import
@@ -18,7 +18,9 @@ import { createRegistry } from "@teamscala/generic-registry/create-registry";
18
18
 
19
19
  export type PgBossJobHandler = (ctx: {
20
20
  config: ScheduledJobConfig;
21
- }) => Promise<void> | void;
21
+ /** Per-job data payload from a one-shot `boss.send()` — absent for scheduled jobs. */
22
+ jobData?: unknown;
23
+ }) => Promise<unknown> | unknown;
22
24
 
23
25
  export const pgBossHandlerRegistry = createRegistry<PgBossJobHandler>(
24
26
  "pg-boss-handlers",
@@ -35,6 +35,20 @@ import "./handlers/singleton-tools-currency-watch.ts";
35
35
  import "./handlers/registry-stale-inactive-sweep.ts";
36
36
  import "./handlers/pane-outbox-drain.ts";
37
37
 
38
+ /**
39
+ * Module-level reference to the mounted PgBoss instance — set by mountPgBoss on
40
+ * successful start. Exposed via getPgBoss() so ORPC procedures (and other
41
+ * in-process callers) can submit one-shot jobs via boss.send() without a second
42
+ * PgBoss connection. Null before mount or when mountPgBoss found no scheduled
43
+ * jobs for this service (boss never created).
44
+ */
45
+ let pgBossInstance: PgBoss | null = null;
46
+
47
+ /** Returns the mounted PgBoss instance, or null if pg-boss is not running on this service. */
48
+ export function getPgBoss(): PgBoss | null {
49
+ return pgBossInstance;
50
+ }
51
+
38
52
  export interface PgBossMountContext {
39
53
  databaseUrl: string;
40
54
  serviceSlug: string;
@@ -86,7 +100,8 @@ export function intervalToCron(intervalSeconds: number): string {
86
100
  async function resolveHandler(
87
101
  handlerSlug: string,
88
102
  config: ScheduledJobConfig,
89
- ): Promise<void> {
103
+ jobData?: unknown,
104
+ ): Promise<unknown> {
90
105
  const handler = pgBossHandlerRegistry.get(handlerSlug);
91
106
  if (!handler) {
92
107
  throw new Error(
@@ -94,7 +109,7 @@ async function resolveHandler(
94
109
  `register it via pgBossHandlerRegistry.register(slug, fn) before the job fires`,
95
110
  );
96
111
  }
97
- await handler({ config });
112
+ return await handler({ config, jobData });
98
113
  }
99
114
 
100
115
  export async function mountPgBoss(
@@ -149,6 +164,7 @@ export async function mountPgBoss(
149
164
  // calling schedule() before start() crashes the timekeeper with
150
165
  // "Database not opened. Call open() before executing SQL." (pg-boss v12)
151
166
  await boss.start();
167
+ pgBossInstance = boss;
152
168
 
153
169
  for (const entry of scheduledEntries) {
154
170
  // crash-isolation: a malformed scheduled_job row degrades to "this job
@@ -188,8 +204,8 @@ export async function mountPgBoss(
188
204
  // underlying SQL is ON CONFLICT DO NOTHING, so calling it on every
189
205
  // boot is a safe idempotent assert, never a duplicate-creation error.
190
206
  await boss.createQueue(queueName);
191
- await boss.work(queueName, async () => {
192
- await resolveHandler(config.handler_slug, config);
207
+ await boss.work(queueName, async (job) => {
208
+ return await resolveHandler(config.handler_slug, config, job?.data);
193
209
  });
194
210
 
195
211
  if (config.cron) {