@skaile/forge-layer-jobs 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,19 @@
1
+ import { appRole, servesApp } from '../jobs/role'
2
+
3
+ const HEALTH = '/api/health'
4
+
5
+ /**
6
+ * `APP_ROLE=worker` runs handlers only. Its HTTP server stays up for the
7
+ * health check (Coolify, compose) and refuses everything else, so a worker
8
+ * container never serves pages, actions or MCP by accident.
9
+ */
10
+ export default defineEventHandler(event => {
11
+ if (servesApp(appRole())) return
12
+ const base = useRuntimeConfig(event).app.baseURL.replace(/\/$/, '')
13
+ const path = (base && event.path.startsWith(base) ? event.path.slice(base.length) : event.path).split('?')[0]
14
+ if (path === HEALTH) return
15
+ throw createError({
16
+ statusCode: 503,
17
+ statusMessage: `This process is a jobs worker (APP_ROLE=worker): it answers ${HEALTH} only`,
18
+ })
19
+ })
@@ -0,0 +1,48 @@
1
+ import { registerActions } from '~~/server/utils/actions/registry'
2
+ import { dbReady, onBeforeDatabaseClose } from '~~/server/utils/db'
3
+ import { jobActions } from '../jobs/actions'
4
+ import { startJobs, stopJobs } from '../jobs/boss'
5
+ import { allJobs } from '../jobs/define'
6
+ import { appRole, assertRoleFitsDatabase, runsHandlers } from '../jobs/role'
7
+
8
+ /**
9
+ * Registers the job actions, then starts pg-boss on the app's database once it
10
+ * is open and migrated (`APP_ROLE` decides whether this process also runs
11
+ * handlers and cron). The app registers its jobs from its own Nitro plugin
12
+ * (`registerJobs([...])`); the boss reads the registry once the database is up.
13
+ */
14
+ export default defineNitroPlugin(() => {
15
+ const role = appRole()
16
+ try {
17
+ assertRoleFitsDatabase(role)
18
+ } catch (error) {
19
+ console.error(`[jobs] ${error instanceof Error ? error.message : String(error)}`)
20
+ throw error
21
+ }
22
+ registerActions(jobActions)
23
+ startJobs({ role, target: dbReady().then(handle => handle.target) })
24
+ .then(() =>
25
+ console.info(
26
+ `[jobs] pg-boss started, role ${role}: ${
27
+ runsHandlers(role)
28
+ ? `working ${
29
+ allJobs()
30
+ .map(job => job.name)
31
+ .join(', ') || 'no jobs'
32
+ }`
33
+ : 'enqueue only'
34
+ }`,
35
+ ),
36
+ )
37
+ .catch((error: unknown) =>
38
+ console.error(`[jobs] start failed: ${error instanceof Error ? error.message : String(error)}`),
39
+ )
40
+
41
+ // pg-boss must stop (finish running handlers, release its connection) before
42
+ // the database closes.
43
+ onBeforeDatabaseClose(() =>
44
+ stopJobs().catch((error: unknown) =>
45
+ console.error(`[jobs] stop failed: ${error instanceof Error ? error.message : String(error)}`),
46
+ ),
47
+ )
48
+ })
package/shared/jobs.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Shared by the jobs layer's server (actions) and its UI (Jobs page). No
3
+ * server imports here: this file ends up in the browser bundle.
4
+ */
5
+
6
+ /** pg-boss job states, in lifecycle order. */
7
+ export const JOB_STATES = ['created', 'retry', 'active', 'completed', 'cancelled', 'failed'] as const
8
+ export type JobState = (typeof JOB_STATES)[number]