@studious-lms/server 1.4.9 → 1.5.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.
Files changed (160) hide show
  1. package/dist/lib/config/cors.d.ts.map +1 -1
  2. package/dist/lib/config/cors.js +2 -4
  3. package/dist/lib/config/cors.js.map +1 -1
  4. package/dist/lib/fileUpload.d.ts.map +1 -1
  5. package/dist/lib/fileUpload.js +4 -5
  6. package/dist/lib/fileUpload.js.map +1 -1
  7. package/dist/models/auth.d.ts +1 -0
  8. package/dist/models/auth.d.ts.map +1 -1
  9. package/dist/models/auth.js +3 -2
  10. package/dist/models/auth.js.map +1 -1
  11. package/dist/models/comment.d.ts +0 -5
  12. package/dist/models/comment.d.ts.map +1 -1
  13. package/dist/models/conversation.d.ts +2 -1
  14. package/dist/models/conversation.d.ts.map +1 -1
  15. package/dist/models/labChat.d.ts +2 -1
  16. package/dist/models/labChat.d.ts.map +1 -1
  17. package/dist/models/message.d.ts +6 -3
  18. package/dist/models/message.d.ts.map +1 -1
  19. package/dist/models/school.d.ts +0 -22
  20. package/dist/models/school.d.ts.map +1 -1
  21. package/dist/models/school.js +2 -14
  22. package/dist/models/school.js.map +1 -1
  23. package/dist/models/worksheet.d.ts +76 -19
  24. package/dist/models/worksheet.d.ts.map +1 -1
  25. package/dist/models/worksheet.js +2 -2
  26. package/dist/models/worksheet.js.map +1 -1
  27. package/dist/pipelines/aiLabChat.js +6 -6
  28. package/dist/pipelines/aiLabChat.js.map +1 -1
  29. package/dist/pipelines/gradeWorksheet.d.ts +5 -3
  30. package/dist/pipelines/gradeWorksheet.d.ts.map +1 -1
  31. package/dist/pipelines/gradeWorksheet.js +60 -24
  32. package/dist/pipelines/gradeWorksheet.js.map +1 -1
  33. package/dist/pipelines/pdfToWorksheet.d.ts +3 -0
  34. package/dist/pipelines/pdfToWorksheet.d.ts.map +1 -1
  35. package/dist/pipelines/pdfToWorksheet.js +65 -24
  36. package/dist/pipelines/pdfToWorksheet.js.map +1 -1
  37. package/dist/routers/_app.d.ts +92 -48
  38. package/dist/routers/_app.d.ts.map +1 -1
  39. package/dist/routers/announcement.d.ts +0 -4
  40. package/dist/routers/announcement.d.ts.map +1 -1
  41. package/dist/routers/assignment.d.ts +0 -8
  42. package/dist/routers/assignment.d.ts.map +1 -1
  43. package/dist/routers/auth.d.ts +1 -0
  44. package/dist/routers/auth.d.ts.map +1 -1
  45. package/dist/routers/comment.d.ts +0 -3
  46. package/dist/routers/comment.d.ts.map +1 -1
  47. package/dist/routers/conversation.d.ts +2 -1
  48. package/dist/routers/conversation.d.ts.map +1 -1
  49. package/dist/routers/labChat.d.ts +2 -1
  50. package/dist/routers/labChat.d.ts.map +1 -1
  51. package/dist/routers/school.d.ts +6 -0
  52. package/dist/routers/school.d.ts.map +1 -1
  53. package/dist/routers/school.js +7 -2
  54. package/dist/routers/school.js.map +1 -1
  55. package/dist/routers/worksheet.d.ts +35 -7
  56. package/dist/routers/worksheet.d.ts.map +1 -1
  57. package/dist/services/auth.d.ts +1 -0
  58. package/dist/services/auth.d.ts.map +1 -1
  59. package/dist/services/comment.d.ts +0 -3
  60. package/dist/services/comment.d.ts.map +1 -1
  61. package/dist/services/conversation.d.ts +2 -1
  62. package/dist/services/conversation.d.ts.map +1 -1
  63. package/dist/services/labChat.d.ts +2 -1
  64. package/dist/services/labChat.d.ts.map +1 -1
  65. package/dist/services/labChat.js +6 -6
  66. package/dist/services/labChat.js.map +1 -1
  67. package/dist/services/message.d.ts.map +1 -1
  68. package/dist/services/message.js +14 -2
  69. package/dist/services/message.js.map +1 -1
  70. package/dist/services/school.d.ts +6 -0
  71. package/dist/services/school.d.ts.map +1 -1
  72. package/dist/services/school.js +71 -6
  73. package/dist/services/school.js.map +1 -1
  74. package/dist/services/worksheet.d.ts +35 -7
  75. package/dist/services/worksheet.d.ts.map +1 -1
  76. package/dist/services/worksheet.js +17 -19
  77. package/dist/services/worksheet.js.map +1 -1
  78. package/dist/utils/generateUsername.d.ts +8 -0
  79. package/dist/utils/generateUsername.d.ts.map +1 -0
  80. package/dist/utils/generateUsername.js +103 -0
  81. package/dist/utils/generateUsername.js.map +1 -0
  82. package/dist/workers/connection.d.ts +3 -0
  83. package/dist/workers/connection.d.ts.map +1 -0
  84. package/dist/workers/connection.js +33 -0
  85. package/dist/workers/connection.js.map +1 -0
  86. package/dist/workers/index.d.ts +10 -0
  87. package/dist/workers/index.d.ts.map +1 -0
  88. package/dist/workers/index.js +53 -0
  89. package/dist/workers/index.js.map +1 -0
  90. package/dist/workers/processors/pdfToWorksheet.d.ts +9 -0
  91. package/dist/workers/processors/pdfToWorksheet.d.ts.map +1 -0
  92. package/dist/workers/processors/pdfToWorksheet.js +100 -0
  93. package/dist/workers/processors/pdfToWorksheet.js.map +1 -0
  94. package/dist/workers/queues.d.ts +69 -0
  95. package/dist/workers/queues.d.ts.map +1 -0
  96. package/dist/workers/queues.js +119 -0
  97. package/dist/workers/queues.js.map +1 -0
  98. package/package.json +4 -1
  99. package/prisma/migrations/20260506162500_consolidate_run_job_and_unify_job_status/migration.sql +166 -0
  100. package/prisma/schema.prisma +465 -387
  101. package/src/lib/config/cors.ts +0 -2
  102. package/src/lib/fileUpload.ts +2 -4
  103. package/src/models/auth.ts +1 -0
  104. package/src/models/school.ts +0 -14
  105. package/src/models/worksheet.ts +2 -2
  106. package/src/pipelines/aiLabChat.ts +4 -4
  107. package/src/pipelines/gradeWorksheet.ts +63 -25
  108. package/src/pipelines/pdfToWorksheet.ts +68 -22
  109. package/src/routers/school.ts +5 -0
  110. package/src/services/labChat.ts +4 -4
  111. package/src/services/message.ts +13 -0
  112. package/src/services/school.ts +89 -6
  113. package/src/services/worksheet.ts +16 -18
  114. package/src/utils/generateUsername.ts +108 -0
  115. package/src/workers/connection.ts +37 -0
  116. package/src/workers/index.ts +61 -0
  117. package/src/workers/processors/pdfToWorksheet.ts +119 -0
  118. package/src/workers/queues.ts +159 -0
  119. package/tests/{globalSetup.ts → integration/globalSetup.ts} +4 -4
  120. package/tests/{helpers.ts → integration/helpers.ts} +2 -2
  121. package/tests/{pipelines → integration/pipelines}/pdfToWorksheet.test.ts +55 -43
  122. package/tests/{routers → integration/routers}/attendance.test.ts +1 -1
  123. package/tests/{routers → integration/routers}/auth.test.ts +3 -3
  124. package/tests/{routers → integration/routers}/class.test.ts +3 -17
  125. package/tests/{routers → integration/routers}/conversation.test.ts +1 -1
  126. package/tests/{routers → integration/routers}/event.test.ts +1 -1
  127. package/tests/{routers → integration/routers}/message.test.ts +1 -1
  128. package/tests/{routers → integration/routers}/newtonChat.test.ts +1 -1
  129. package/tests/{routers → integration/routers}/studentProgress.test.ts +1 -1
  130. package/tests/{user.test.ts → integration/routers/user.test.ts} +2 -2
  131. package/tests/integration/routers/worksheet.test.ts +448 -0
  132. package/tests/{setup.ts → integration/setup.ts} +4 -4
  133. package/tests/{lib → unit/lib}/cors.test.ts +1 -1
  134. package/tests/{middleware → unit/middleware}/security.test.ts +1 -1
  135. package/tests/{pipelines → unit/pipelines}/aiLabChat.test.ts +1 -1
  136. package/tests/{lib → unit/pipelines}/aiLabChatContract.test.ts +1 -1
  137. package/tests/{utils/aiLabChatPrompt.test.ts → unit/pipelines/labChatPrompt.test.ts} +2 -2
  138. package/tests/{utils → unit/services}/studentProgress.test.ts +5 -5
  139. package/tests/{utils → unit/utils}/aiUser.test.ts +1 -1
  140. package/tests/{utils → unit/utils}/generateInviteCode.test.ts +1 -1
  141. package/tests/{utils → unit/utils}/inference.test.ts +6 -6
  142. package/tests/{utils → unit/utils}/logger.test.ts +1 -1
  143. package/tests/{utils → unit/utils}/prismaErrorHandler.test.ts +1 -1
  144. package/tests/{utils → unit/utils}/prismaWrapper.test.ts +1 -1
  145. package/vitest.config.ts +11 -3
  146. package/vitest.unit.config.ts +10 -10
  147. package/tests/routers/worksheet.test.ts +0 -194
  148. package/tests/startup.test.ts +0 -5
  149. package/tests/worksheet.test.ts +0 -181
  150. /package/tests/{routers → integration/routers}/agenda.test.ts +0 -0
  151. /package/tests/{routers → integration/routers}/announcement.test.ts +0 -0
  152. /package/tests/{routers → integration/routers}/assignment.test.ts +0 -0
  153. /package/tests/{routers → integration/routers}/comment.test.ts +0 -0
  154. /package/tests/{routers → integration/routers}/folder.test.ts +0 -0
  155. /package/tests/{routers → integration/routers}/labChat.test.ts +0 -0
  156. /package/tests/{routers → integration/routers}/marketing.test.ts +0 -0
  157. /package/tests/{routers → integration/routers}/notification.test.ts +0 -0
  158. /package/tests/{routers → integration/routers}/search.test.ts +0 -0
  159. /package/tests/{routers → integration/routers}/section.test.ts +0 -0
  160. /package/tests/{server → unit/middleware}/rateLimit.test.ts +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"/","sources":["workers/connection.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAoC9B,eAAO,MAAM,cAAc,SAAwB,CAAC"}
@@ -0,0 +1,33 @@
1
+
2
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="afa7018f-10b5-59d0-b3b7-2c3033bd6329")}catch(e){}}();
3
+ import IORedis from 'ioredis';
4
+ import { env } from '../lib/config/env.js';
5
+ import { logger } from '../utils/logger.js';
6
+ /**
7
+ * BullMQ-specific Redis connection.
8
+ *
9
+ * Kept separate from `src/lib/redis.ts` (which is used for caching and
10
+ * rate-limiting) because BullMQ requires `maxRetriesPerRequest: null` and
11
+ * `enableReadyCheck: false` on its connection. Mixing those settings with
12
+ * general Redis usage causes unwanted behaviour on transient errors.
13
+ */
14
+ function buildBullConnection() {
15
+ if (!env.REDIS_URL) {
16
+ throw new Error('REDIS_URL is required for BullMQ. Set it in .env / .env.production / your hosting env.');
17
+ }
18
+ const connection = new IORedis(env.REDIS_URL, {
19
+ maxRetriesPerRequest: null,
20
+ enableReadyCheck: false,
21
+ lazyConnect: false,
22
+ });
23
+ connection.on('error', (err) => {
24
+ logger.error('BullMQ Redis connection error', { error: err.message });
25
+ });
26
+ connection.on('connect', () => {
27
+ logger.info('BullMQ Redis connected');
28
+ });
29
+ return connection;
30
+ }
31
+ export const bullConnection = buildBullConnection();
32
+ //# sourceMappingURL=connection.js.map
33
+ //# debugId=afa7018f-10b5-59d0-b3b7-2c3033bd6329
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.js","sources":["workers/connection.ts"],"sourceRoot":"/","sourcesContent":["import IORedis from 'ioredis';\nimport { env } from '../lib/config/env.js';\nimport { logger } from '../utils/logger.js';\n\n/**\n * BullMQ-specific Redis connection.\n *\n * Kept separate from `src/lib/redis.ts` (which is used for caching and\n * rate-limiting) because BullMQ requires `maxRetriesPerRequest: null` and\n * `enableReadyCheck: false` on its connection. Mixing those settings with\n * general Redis usage causes unwanted behaviour on transient errors.\n */\nfunction buildBullConnection(): IORedis {\n if (!env.REDIS_URL) {\n throw new Error(\n 'REDIS_URL is required for BullMQ. Set it in .env / .env.production / your hosting env.',\n );\n }\n\n const connection = new IORedis(env.REDIS_URL, {\n maxRetriesPerRequest: null,\n enableReadyCheck: false,\n lazyConnect: false,\n });\n\n connection.on('error', (err) => {\n logger.error('BullMQ Redis connection error', { error: err.message });\n });\n\n connection.on('connect', () => {\n logger.info('BullMQ Redis connected');\n });\n\n return connection;\n}\n\nexport const bullConnection = buildBullConnection();\n"],"names":[],"mappings":";;AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;;;;;GAOG;AACH,SAAS,mBAAmB;IAC1B,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;IACJ,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE;QAC5C,oBAAoB,EAAE,IAAI;QAC1B,gBAAgB,EAAE,KAAK;QACvB,WAAW,EAAE,KAAK;KACnB,CAAC,CAAC;IAEH,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;QAC7B,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;QAC5B,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,EAAE,CAAC","debug_id":"afa7018f-10b5-59d0-b3b7-2c3033bd6329"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Worker process entrypoint.
3
+ *
4
+ * Run separately from the HTTP server (e.g. `node dist/workers/index.js` on
5
+ * Render/Railway/Fly). Importing this file boots every BullMQ Worker
6
+ * declared under `src/workers/processors/*`. Do NOT import this from the
7
+ * HTTP server — that would spin up workers in your serverless functions.
8
+ */
9
+ import '../instrument.js';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["workers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Worker process entrypoint.
3
+ *
4
+ * Run separately from the HTTP server (e.g. `node dist/workers/index.js` on
5
+ * Render/Railway/Fly). Importing this file boots every BullMQ Worker
6
+ * declared under `src/workers/processors/*`. Do NOT import this from the
7
+ * HTTP server — that would spin up workers in your serverless functions.
8
+ */
9
+
10
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="1675d7e1-4547-5721-83c1-42be379eca86")}catch(e){}}();
11
+ import '../instrument.js';
12
+ import { prisma } from '../lib/prisma.js';
13
+ import { logger } from '../utils/logger.js';
14
+ import { bullConnection } from './connection.js';
15
+ import { pdfToWorksheetWorker } from './processors/pdfToWorksheet.js';
16
+ const workers = [pdfToWorksheetWorker];
17
+ logger.info('Workers ready', {
18
+ pid: process.pid,
19
+ queues: workers.map((w) => w.name),
20
+ });
21
+ let shuttingDown = false;
22
+ async function shutdown(signal) {
23
+ if (shuttingDown)
24
+ return;
25
+ shuttingDown = true;
26
+ logger.info(`Received ${signal}, draining workers...`);
27
+ // `worker.close()` waits for in-flight jobs to finish (up to the worker's
28
+ // settings) before resolving. Run them in parallel.
29
+ await Promise.allSettled(workers.map((w) => w.close()));
30
+ await Promise.allSettled([
31
+ bullConnection.quit(),
32
+ prisma.$disconnect(),
33
+ ]);
34
+ logger.info('Workers shut down cleanly');
35
+ process.exit(0);
36
+ }
37
+ process.on('SIGTERM', () => void shutdown('SIGTERM'));
38
+ process.on('SIGINT', () => void shutdown('SIGINT'));
39
+ process.on('uncaughtException', (error) => {
40
+ logger.error('Uncaught exception in worker process', {
41
+ error: error.message,
42
+ stack: error.stack,
43
+ });
44
+ process.exit(1);
45
+ });
46
+ process.on('unhandledRejection', (reason) => {
47
+ logger.error('Unhandled rejection in worker process', {
48
+ reason: reason instanceof Error ? reason.message : String(reason),
49
+ });
50
+ process.exit(1);
51
+ });
52
+ //# sourceMappingURL=index.js.map
53
+ //# debugId=1675d7e1-4547-5721-83c1-42be379eca86
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["workers/index.ts"],"sourceRoot":"/","sourcesContent":["/**\n * Worker process entrypoint.\n *\n * Run separately from the HTTP server (e.g. `node dist/workers/index.js` on\n * Render/Railway/Fly). Importing this file boots every BullMQ Worker\n * declared under `src/workers/processors/*`. Do NOT import this from the\n * HTTP server — that would spin up workers in your serverless functions.\n */\nimport '../instrument.js';\n\nimport { Worker } from 'bullmq';\nimport { prisma } from '../lib/prisma.js';\nimport { logger } from '../utils/logger.js';\nimport { bullConnection } from './connection.js';\nimport { pdfToWorksheetWorker } from './processors/pdfToWorksheet.js';\n\nconst workers: Worker<any>[] = [pdfToWorksheetWorker];\n\nlogger.info('Workers ready', {\n pid: process.pid,\n queues: workers.map((w) => w.name),\n});\n\nlet shuttingDown = false;\n\nasync function shutdown(signal: string): Promise<void> {\n if (shuttingDown) return;\n shuttingDown = true;\n\n logger.info(`Received ${signal}, draining workers...`);\n\n // `worker.close()` waits for in-flight jobs to finish (up to the worker's\n // settings) before resolving. Run them in parallel.\n await Promise.allSettled(workers.map((w) => w.close()));\n\n await Promise.allSettled([\n bullConnection.quit(),\n prisma.$disconnect(),\n ]);\n\n logger.info('Workers shut down cleanly');\n process.exit(0);\n}\n\nprocess.on('SIGTERM', () => void shutdown('SIGTERM'));\nprocess.on('SIGINT', () => void shutdown('SIGINT'));\n\nprocess.on('uncaughtException', (error) => {\n logger.error('Uncaught exception in worker process', {\n error: error.message,\n stack: error.stack,\n });\n process.exit(1);\n});\n\nprocess.on('unhandledRejection', (reason) => {\n logger.error('Unhandled rejection in worker process', {\n reason: reason instanceof Error ? reason.message : String(reason),\n });\n process.exit(1);\n});\n"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;;AACH,OAAO,kBAAkB,CAAC;AAG1B,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAEtE,MAAM,OAAO,GAAkB,CAAC,oBAAoB,CAAC,CAAC;AAEt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AAC;AAClB,CAAC,CAAC,CAAC;AAEH,OAAO,CAAC,EAAE,CAAC,oBAAoB,EAAE,CAAC,MAAM,EAAE,EAAE;IAC1C,MAAM,CAAC,KAAK,CAAC,uCAAuC,EAAE;QACpD,MAAM,EAAE,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;KAClE,CAAC,CAAC;IACH,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC","debug_id":"1675d7e1-4547-5721-83c1-42be379eca86"}
@@ -0,0 +1,9 @@
1
+ import { Worker } from 'bullmq';
2
+ interface PdfToWorksheetJobData {
3
+ runJobId: string;
4
+ gcsUrl: string;
5
+ classId: string;
6
+ }
7
+ export declare const pdfToWorksheetWorker: Worker<PdfToWorksheetJobData, any, string>;
8
+ export {};
9
+ //# sourceMappingURL=pdfToWorksheet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdfToWorksheet.d.ts","sourceRoot":"/","sources":["workers/processors/pdfToWorksheet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAO1C,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AA8CD,eAAO,MAAM,oBAAoB,4CAYhC,CAAC"}
@@ -0,0 +1,100 @@
1
+
2
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="1b13378b-3952-5f91-b138-a69c3693a56d")}catch(e){}}();
3
+ import { Worker } from 'bullmq';
4
+ import { Prisma } from '@prisma/client';
5
+ import { bullConnection } from '../connection.js';
6
+ import { prisma } from '../../lib/prisma.js';
7
+ import { logger } from '../../utils/logger.js';
8
+ import { pdfToWorksheet } from '../../pipelines/pdfToWorksheet.js';
9
+ async function process(job) {
10
+ const { runJobId, gcsUrl, classId } = job.data;
11
+ // If a previous attempt already succeeded for this RunJob (rare race after a
12
+ // crash that lost the BullMQ ack), return the prior result instead of
13
+ // re-running the AI call and creating a duplicate worksheet.
14
+ const existing = await prisma.runJob.findUnique({
15
+ where: { id: runJobId },
16
+ select: { status: true, result: true },
17
+ });
18
+ if (existing?.status === 'SUCCEEDED') {
19
+ logger.info('pdfToWorksheet RunJob already succeeded; skipping re-run', {
20
+ runJobId,
21
+ bullJobId: job.id,
22
+ });
23
+ return existing.result;
24
+ }
25
+ await prisma.runJob.update({
26
+ where: { id: runJobId },
27
+ data: {
28
+ status: 'RUNNING',
29
+ startedAt: existing?.status === 'RUNNING' ? undefined : new Date(),
30
+ attempts: { increment: 1 },
31
+ },
32
+ });
33
+ const worksheet = await pdfToWorksheet(gcsUrl, classId);
34
+ const result = worksheet ? { worksheetId: worksheet.id, name: worksheet.name } : null;
35
+ await prisma.runJob.update({
36
+ where: { id: runJobId },
37
+ data: {
38
+ status: 'SUCCEEDED',
39
+ finishedAt: new Date(),
40
+ error: null,
41
+ result: result === null ? Prisma.JsonNull : result,
42
+ },
43
+ });
44
+ return result;
45
+ }
46
+ export const pdfToWorksheetWorker = new Worker('pdfToWorksheet', process, {
47
+ connection: bullConnection,
48
+ // Conservative defaults — tune once we have real-world numbers.
49
+ concurrency: 2,
50
+ limiter: {
51
+ max: 5,
52
+ duration: 60000, // 5 jobs / minute, e.g. to stay under OpenAI rate limits
53
+ },
54
+ });
55
+ pdfToWorksheetWorker.on('error', (err) => {
56
+ logger.error('pdfToWorksheet worker error', {
57
+ error: err.message,
58
+ stack: err.stack,
59
+ });
60
+ });
61
+ pdfToWorksheetWorker.on('failed', async (job, err) => {
62
+ if (!job)
63
+ return;
64
+ const runJobId = job.data?.runJobId;
65
+ const isFinalAttempt = job.attemptsMade >= (job.opts.attempts ?? 1);
66
+ logger.error('pdfToWorksheet job failed', {
67
+ bullJobId: job.id,
68
+ runJobId,
69
+ attempt: job.attemptsMade,
70
+ finalAttempt: isFinalAttempt,
71
+ error: err.message,
72
+ });
73
+ if (!runJobId)
74
+ return;
75
+ // Always record the latest error message on the RunJob row.
76
+ // Only flip to FAILED on the final attempt — earlier failures are still
77
+ // "RUNNING" because BullMQ is going to retry.
78
+ await prisma.runJob.update({
79
+ where: { id: runJobId },
80
+ data: {
81
+ error: err.message,
82
+ ...(isFinalAttempt
83
+ ? { status: 'FAILED', finishedAt: new Date() }
84
+ : {}),
85
+ },
86
+ }).catch((updateError) => {
87
+ logger.error('Failed to record pdfToWorksheet failure on RunJob', {
88
+ runJobId,
89
+ error: updateError instanceof Error ? updateError.message : String(updateError),
90
+ });
91
+ });
92
+ });
93
+ pdfToWorksheetWorker.on('completed', (job) => {
94
+ logger.info('pdfToWorksheet job completed', {
95
+ bullJobId: job.id,
96
+ runJobId: job.data?.runJobId,
97
+ });
98
+ });
99
+ //# sourceMappingURL=pdfToWorksheet.js.map
100
+ //# debugId=1b13378b-3952-5f91-b138-a69c3693a56d
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdfToWorksheet.js","sources":["workers/processors/pdfToWorksheet.ts"],"sourceRoot":"/","sourcesContent":["import { Worker, type Job } from 'bullmq';\nimport { Prisma } from '@prisma/client';\nimport { bullConnection } from '../connection.js';\nimport { prisma } from '../../lib/prisma.js';\nimport { logger } from '../../utils/logger.js';\nimport { pdfToWorksheet } from '../../pipelines/pdfToWorksheet.js';\n\ninterface PdfToWorksheetJobData {\n runJobId: string;\n gcsUrl: string;\n classId: string;\n}\n\nasync function process(job: Job<PdfToWorksheetJobData>) {\n const { runJobId, gcsUrl, classId } = job.data;\n\n // If a previous attempt already succeeded for this RunJob (rare race after a\n // crash that lost the BullMQ ack), return the prior result instead of\n // re-running the AI call and creating a duplicate worksheet.\n const existing = await prisma.runJob.findUnique({\n where: { id: runJobId },\n select: { status: true, result: true },\n });\n if (existing?.status === 'SUCCEEDED') {\n logger.info('pdfToWorksheet RunJob already succeeded; skipping re-run', {\n runJobId,\n bullJobId: job.id,\n });\n return existing.result;\n }\n\n await prisma.runJob.update({\n where: { id: runJobId },\n data: {\n status: 'RUNNING',\n startedAt: existing?.status === 'RUNNING' ? undefined : new Date(),\n attempts: { increment: 1 },\n },\n });\n\n const worksheet = await pdfToWorksheet(gcsUrl, classId);\n\n const result = worksheet ? { worksheetId: worksheet.id, name: worksheet.name } : null;\n\n await prisma.runJob.update({\n where: { id: runJobId },\n data: {\n status: 'SUCCEEDED',\n finishedAt: new Date(),\n error: null,\n result: result === null ? Prisma.JsonNull : (result as Prisma.InputJsonValue),\n },\n });\n\n return result;\n}\n\nexport const pdfToWorksheetWorker = new Worker<PdfToWorksheetJobData>(\n 'pdfToWorksheet',\n process,\n {\n connection: bullConnection,\n // Conservative defaults — tune once we have real-world numbers.\n concurrency: 2,\n limiter: {\n max: 5,\n duration: 60_000, // 5 jobs / minute, e.g. to stay under OpenAI rate limits\n },\n },\n);\n\npdfToWorksheetWorker.on('error', (err) => {\n logger.error('pdfToWorksheet worker error', {\n error: err.message,\n stack: err.stack,\n });\n});\n\npdfToWorksheetWorker.on('failed', async (job, err) => {\n if (!job) return;\n\n const runJobId = job.data?.runJobId;\n const isFinalAttempt = job.attemptsMade >= (job.opts.attempts ?? 1);\n\n logger.error('pdfToWorksheet job failed', {\n bullJobId: job.id,\n runJobId,\n attempt: job.attemptsMade,\n finalAttempt: isFinalAttempt,\n error: err.message,\n });\n\n if (!runJobId) return;\n\n // Always record the latest error message on the RunJob row.\n // Only flip to FAILED on the final attempt — earlier failures are still\n // \"RUNNING\" because BullMQ is going to retry.\n await prisma.runJob.update({\n where: { id: runJobId },\n data: {\n error: err.message,\n ...(isFinalAttempt\n ? { status: 'FAILED', finishedAt: new Date() }\n : {}),\n },\n }).catch((updateError) => {\n logger.error('Failed to record pdfToWorksheet failure on RunJob', {\n runJobId,\n error: updateError instanceof Error ? updateError.message : String(updateError),\n });\n });\n});\n\npdfToWorksheetWorker.on('completed', (job) => {\n logger.info('pdfToWorksheet job completed', {\n bullJobId: job.id,\n runJobId: job.data?.runJobId,\n });\n});\n"],"names":[],"mappings":";;AAAA,OAAO,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AAQnE,KAAK,UAAU,OAAO,CAAC,GAA+B;IACpD,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC;IAE/C,6EAA6E;IAC7E,sEAAsE;IACtE,6DAA6D;IAC7D,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC;QAC9C,KAAK,EAAE,EAAE,EAAE,EAAE,QAAQ,EAAE;QACvB,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;KACvC,CAAC,CAAC;IACH,IAAI,QAAQ,EAAE,MAAM,KAAK,WAAW,EAAE,CAAC;QACrC,MAAM,CAAC,IAAI,CAAC,0DAA0D,EAAE;YACtE,QAAQ;YACR,SAAS,EAAE,GAAG,CAAC,EAAE;SAClB,CAAC,CAAC;QACH,OAAO,QAAQ,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;QACzB,KAAK,EAAE,EAAE,EAAE,EAAE,QAAQ,EAAE;QACvB,IAAI,EAAE;YACJ,MAAM,EAAE,SAAS;YACjB,SAAS,EAAE,QAAQ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;IAC3C,MAAM,CAAC,IAAI,CAAC,8BAA8B,EAAE;QAC1C,SAAS,EAAE,GAAG,CAAC,EAAE;QACjB,QAAQ,EAAE,GAAG,CAAC,IAAI,EAAE,QAAQ;KAC7B,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","debug_id":"1b13378b-3952-5f91-b138-a69c3693a56d"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * BullMQ queue producers and the `enqueueRunJob` helper.
3
+ *
4
+ * Producer side only — safe to import from HTTP routes / services running on
5
+ * Vercel. Importing this file does NOT start any workers; workers live in
6
+ * `src/workers/processors/*` and are wired up by `src/workers/index.ts`,
7
+ * which is the entrypoint of the separate long-running worker process.
8
+ */
9
+ import { type JobsOptions } from 'bullmq';
10
+ import { type RunJobType } from '@prisma/client';
11
+ /**
12
+ * Map RunJobType → (queue name, input payload shape).
13
+ *
14
+ * Add a new entry here whenever you introduce a new background job type.
15
+ * The queue name string must match what the corresponding processor passes
16
+ * to `new Worker(<name>, ...)`.
17
+ */
18
+ type JobInputs = {
19
+ PDF_TO_WORKSHEET: {
20
+ gcsUrl: string;
21
+ classId: string;
22
+ };
23
+ GRADE_WORKSHEET: {
24
+ worksheetResponseId: string;
25
+ };
26
+ AI_LAB_CHAT: {
27
+ conversationId: string;
28
+ messageId: string;
29
+ };
30
+ AI_NEWTON_CHAT: {
31
+ conversationId: string;
32
+ messageId: string;
33
+ };
34
+ STUDENT_PROGRESS_CHAT: {
35
+ classId: string;
36
+ studentId: string;
37
+ message: string;
38
+ };
39
+ };
40
+ export interface EnqueueOptions {
41
+ classId?: string | null;
42
+ userId?: string | null;
43
+ /** Override BullMQ job options (attempts, delay, priority, etc.). */
44
+ jobOptions?: JobsOptions;
45
+ }
46
+ export interface EnqueueResult {
47
+ runJobId: string;
48
+ bullJobId: string | null;
49
+ }
50
+ /**
51
+ * Create a `RunJob` audit row, enqueue the BullMQ job, and link the two.
52
+ *
53
+ * Returns immediately — the actual work happens on the worker process. The
54
+ * caller can return `runJobId` to the client and have them poll `getRunJob`
55
+ * (or subscribe over Pusher) to track progress.
56
+ *
57
+ * IMPORTANT: do not call this inside a Prisma `$transaction`. If the outer
58
+ * transaction rolls back after the BullMQ job is added, the worker will pick
59
+ * up an orphaned job pointing at a non-existent RunJob row. Enqueue *after*
60
+ * the transaction commits.
61
+ */
62
+ export declare function enqueueRunJob<T extends RunJobType>(type: T, input: JobInputs[T], options?: EnqueueOptions): Promise<EnqueueResult>;
63
+ /**
64
+ * Close every cached queue. Call from the HTTP server's shutdown handler so
65
+ * the producer side disconnects from Redis cleanly.
66
+ */
67
+ export declare function closeAllQueues(): Promise<void>;
68
+ export {};
69
+ //# sourceMappingURL=queues.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queues.d.ts","sourceRoot":"/","sources":["workers/queues.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAS,KAAK,WAAW,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,EAAU,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAYzD;;;;;;GAMG;AACH,KAAK,SAAS,GAAG;IACf,gBAAgB,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD,eAAe,EAAE;QAAE,mBAAmB,EAAE,MAAM,CAAA;KAAE,CAAC;IACjD,WAAW,EAAE;QAAE,cAAc,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3D,cAAc,EAAE;QAAE,cAAc,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9D,qBAAqB,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAChF,CAAC;AA4BF,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,qEAAqE;IACrE,UAAU,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,aAAa,CAAC,CAAC,SAAS,UAAU,EACtD,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC,CA2DxB;AAED;;;GAGG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAGpD"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * BullMQ queue producers and the `enqueueRunJob` helper.
3
+ *
4
+ * Producer side only — safe to import from HTTP routes / services running on
5
+ * Vercel. Importing this file does NOT start any workers; workers live in
6
+ * `src/workers/processors/*` and are wired up by `src/workers/index.ts`,
7
+ * which is the entrypoint of the separate long-running worker process.
8
+ */
9
+
10
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="f8634186-ae9e-551f-96ec-058802ec2c99")}catch(e){}}();
11
+ import { Queue } from 'bullmq';
12
+ import { bullConnection } from './connection.js';
13
+ import { prisma } from '../lib/prisma.js';
14
+ import { logger } from '../utils/logger.js';
15
+ const DEFAULT_JOB_OPTIONS = {
16
+ attempts: 3,
17
+ backoff: { type: 'exponential', delay: 5000 },
18
+ removeOnComplete: { age: 60 * 60 }, // keep completed jobs in Redis for 1h, then drop
19
+ removeOnFail: { age: 24 * 60 * 60 }, // keep failed jobs for 24h for inspection
20
+ };
21
+ const QUEUE_NAMES = {
22
+ PDF_TO_WORKSHEET: 'pdfToWorksheet',
23
+ GRADE_WORKSHEET: 'gradeWorksheet',
24
+ AI_LAB_CHAT: 'aiLabChat',
25
+ AI_NEWTON_CHAT: 'aiNewtonChat',
26
+ STUDENT_PROGRESS_CHAT: 'studentProgressChat',
27
+ };
28
+ /**
29
+ * One Queue instance per type. Created lazily so that importing this module
30
+ * does not eagerly open queues you may never use in a given runtime.
31
+ */
32
+ const queueCache = new Map();
33
+ function getQueue(type) {
34
+ let queue = queueCache.get(type);
35
+ if (!queue) {
36
+ queue = new Queue(QUEUE_NAMES[type], {
37
+ connection: bullConnection,
38
+ defaultJobOptions: DEFAULT_JOB_OPTIONS,
39
+ });
40
+ queueCache.set(type, queue);
41
+ }
42
+ return queue;
43
+ }
44
+ /**
45
+ * Create a `RunJob` audit row, enqueue the BullMQ job, and link the two.
46
+ *
47
+ * Returns immediately — the actual work happens on the worker process. The
48
+ * caller can return `runJobId` to the client and have them poll `getRunJob`
49
+ * (or subscribe over Pusher) to track progress.
50
+ *
51
+ * IMPORTANT: do not call this inside a Prisma `$transaction`. If the outer
52
+ * transaction rolls back after the BullMQ job is added, the worker will pick
53
+ * up an orphaned job pointing at a non-existent RunJob row. Enqueue *after*
54
+ * the transaction commits.
55
+ */
56
+ export async function enqueueRunJob(type, input, options = {}) {
57
+ const runJob = await prisma.runJob.create({
58
+ data: {
59
+ type,
60
+ status: 'PENDING',
61
+ classId: options.classId ?? null,
62
+ userId: options.userId ?? null,
63
+ input: input,
64
+ },
65
+ });
66
+ try {
67
+ const queue = getQueue(type);
68
+ const job = await queue.add(QUEUE_NAMES[type], { runJobId: runJob.id, ...input }, options.jobOptions);
69
+ try {
70
+ await prisma.runJob.update({
71
+ where: { id: runJob.id },
72
+ data: { bullJobId: job.id ?? null },
73
+ });
74
+ }
75
+ catch (linkError) {
76
+ logger.error('RunJob enqueued but bullJobId linkage failed', {
77
+ runJobId: runJob.id,
78
+ bullJobId: job.id ?? null,
79
+ type,
80
+ error: linkError instanceof Error ? linkError.message : String(linkError),
81
+ });
82
+ }
83
+ return { runJobId: runJob.id, bullJobId: job.id ?? null };
84
+ }
85
+ catch (error) {
86
+ logger.error('Failed to enqueue RunJob; marking FAILED', {
87
+ runJobId: runJob.id,
88
+ type,
89
+ error: error instanceof Error ? error.message : String(error),
90
+ });
91
+ try {
92
+ await prisma.runJob.update({
93
+ where: { id: runJob.id },
94
+ data: {
95
+ status: 'FAILED',
96
+ error: error instanceof Error ? error.message : String(error),
97
+ finishedAt: new Date(),
98
+ },
99
+ });
100
+ }
101
+ catch (updateError) {
102
+ logger.error('Failed to mark RunJob as FAILED after enqueue error', {
103
+ runJobId: runJob.id,
104
+ error: updateError instanceof Error ? updateError.message : String(updateError),
105
+ });
106
+ }
107
+ throw error;
108
+ }
109
+ }
110
+ /**
111
+ * Close every cached queue. Call from the HTTP server's shutdown handler so
112
+ * the producer side disconnects from Redis cleanly.
113
+ */
114
+ export async function closeAllQueues() {
115
+ await Promise.all(Array.from(queueCache.values()).map((q) => q.close()));
116
+ queueCache.clear();
117
+ }
118
+ //# sourceMappingURL=queues.js.map
119
+ //# debugId=f8634186-ae9e-551f-96ec-058802ec2c99
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queues.js","sources":["workers/queues.ts"],"sourceRoot":"/","sourcesContent":["/**\n * BullMQ queue producers and the `enqueueRunJob` helper.\n *\n * Producer side only — safe to import from HTTP routes / services running on\n * Vercel. Importing this file does NOT start any workers; workers live in\n * `src/workers/processors/*` and are wired up by `src/workers/index.ts`,\n * which is the entrypoint of the separate long-running worker process.\n */\nimport { Queue, type JobsOptions } from 'bullmq';\nimport { Prisma, type RunJobType } from '@prisma/client';\nimport { bullConnection } from './connection.js';\nimport { prisma } from '../lib/prisma.js';\nimport { logger } from '../utils/logger.js';\n\nconst DEFAULT_JOB_OPTIONS: JobsOptions = {\n attempts: 3,\n backoff: { type: 'exponential', delay: 5_000 },\n removeOnComplete: { age: 60 * 60 }, // keep completed jobs in Redis for 1h, then drop\n removeOnFail: { age: 24 * 60 * 60 }, // keep failed jobs for 24h for inspection\n};\n\n/**\n * Map RunJobType → (queue name, input payload shape).\n *\n * Add a new entry here whenever you introduce a new background job type.\n * The queue name string must match what the corresponding processor passes\n * to `new Worker(<name>, ...)`.\n */\ntype JobInputs = {\n PDF_TO_WORKSHEET: { gcsUrl: string; classId: string };\n GRADE_WORKSHEET: { worksheetResponseId: string };\n AI_LAB_CHAT: { conversationId: string; messageId: string };\n AI_NEWTON_CHAT: { conversationId: string; messageId: string };\n STUDENT_PROGRESS_CHAT: { classId: string; studentId: string; message: string };\n};\n\nconst QUEUE_NAMES: Record<RunJobType, string> = {\n PDF_TO_WORKSHEET: 'pdfToWorksheet',\n GRADE_WORKSHEET: 'gradeWorksheet',\n AI_LAB_CHAT: 'aiLabChat',\n AI_NEWTON_CHAT: 'aiNewtonChat',\n STUDENT_PROGRESS_CHAT: 'studentProgressChat',\n};\n\n/**\n * One Queue instance per type. Created lazily so that importing this module\n * does not eagerly open queues you may never use in a given runtime.\n */\nconst queueCache = new Map<RunJobType, Queue>();\n\nfunction getQueue(type: RunJobType): Queue {\n let queue = queueCache.get(type);\n if (!queue) {\n queue = new Queue(QUEUE_NAMES[type], {\n connection: bullConnection,\n defaultJobOptions: DEFAULT_JOB_OPTIONS,\n });\n queueCache.set(type, queue);\n }\n return queue;\n}\n\nexport interface EnqueueOptions {\n classId?: string | null;\n userId?: string | null;\n /** Override BullMQ job options (attempts, delay, priority, etc.). */\n jobOptions?: JobsOptions;\n}\n\nexport interface EnqueueResult {\n runJobId: string;\n bullJobId: string | null;\n}\n\n/**\n * Create a `RunJob` audit row, enqueue the BullMQ job, and link the two.\n *\n * Returns immediately — the actual work happens on the worker process. The\n * caller can return `runJobId` to the client and have them poll `getRunJob`\n * (or subscribe over Pusher) to track progress.\n *\n * IMPORTANT: do not call this inside a Prisma `$transaction`. If the outer\n * transaction rolls back after the BullMQ job is added, the worker will pick\n * up an orphaned job pointing at a non-existent RunJob row. Enqueue *after*\n * the transaction commits.\n */\nexport async function enqueueRunJob<T extends RunJobType>(\n type: T,\n input: JobInputs[T],\n options: EnqueueOptions = {},\n): Promise<EnqueueResult> {\n const runJob = await prisma.runJob.create({\n data: {\n type,\n status: 'PENDING',\n classId: options.classId ?? null,\n userId: options.userId ?? null,\n input: input as unknown as Prisma.InputJsonValue,\n },\n });\n\n try {\n const queue = getQueue(type);\n const job = await queue.add(\n QUEUE_NAMES[type],\n { runJobId: runJob.id, ...input },\n options.jobOptions,\n );\n\n try {\n await prisma.runJob.update({\n where: { id: runJob.id },\n data: { bullJobId: job.id ?? null },\n });\n } catch (linkError) {\n logger.error('RunJob enqueued but bullJobId linkage failed', {\n runJobId: runJob.id,\n bullJobId: job.id ?? null,\n type,\n error: linkError instanceof Error ? linkError.message : String(linkError),\n });\n }\n\n return { runJobId: runJob.id, bullJobId: job.id ?? null };\n } catch (error) {\n logger.error('Failed to enqueue RunJob; marking FAILED', {\n runJobId: runJob.id,\n type,\n error: error instanceof Error ? error.message : String(error),\n });\n\n try {\n await prisma.runJob.update({\n where: { id: runJob.id },\n data: {\n status: 'FAILED',\n error: error instanceof Error ? error.message : String(error),\n finishedAt: new Date(),\n },\n });\n } catch (updateError) {\n logger.error('Failed to mark RunJob as FAILED after enqueue error', {\n runJobId: runJob.id,\n error: updateError instanceof Error ? updateError.message : String(updateError),\n });\n }\n\n throw error;\n }\n}\n\n/**\n * Close every cached queue. Call from the HTTP server's shutdown handler so\n * the producer side disconnects from Redis cleanly.\n */\nexport async function closeAllQueues(): Promise<void> {\n await Promise.all(Array.from(queueCache.values()).map((q) => q.close()));\n queueCache.clear();\n}\n"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;;AACH,OAAO,EAAE,KAAK,EAAoB,MAAM,QAAQ,CAAC;AAEjD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,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K,CAAC;SAC9D,CAAC,CAAC;QAEH,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;gBACzB,KAAK,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE;gBACxB,IAAI,EAAE;oBACJ,MAAM,EAAE,QAAQ;oBAChB,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;oBAC7D,UAAU,EAAE,IAAI,IAAI,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,WAAW,EAAE,CAAC;YACrB,MAAM,CAAC,KAAK,CAAC,qDAAqD,EAAE;gBAClE,QAAQ,EAAE,MAAM,CAAC,EAAE;gBACnB,KAAK,EAAE,WAAW,YAAY,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC;aAChF,CAAC,CAAC;QACL,CAAC;QAED,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc;IAClC,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACzE,UAAU,CAAC,KAAK,EAAE,CAAC;AACrB,CAAC","debug_id":"f8634186-ae9e-551f-96ec-058802ec2c99"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studious-lms/server",
3
- "version": "1.4.9",
3
+ "version": "1.5.0",
4
4
  "description": "Backend server for Studious application",
5
5
  "main": "dist/exportType.js",
6
6
  "types": "dist/exportType.d.ts",
@@ -13,8 +13,10 @@
13
13
  },
14
14
  "scripts": {
15
15
  "dev": "tsx watch src/index.ts",
16
+ "dev:worker": "tsx watch src/workers/index.ts",
16
17
  "build": "tsc && npm run sentry:sourcemaps",
17
18
  "start": "node dist/index.js",
19
+ "start:worker": "node dist/workers/index.js",
18
20
  "generate": "npx prisma generate",
19
21
  "prepublishOnly": "npm run generate && npm run build",
20
22
  "test:pre": "NODE_ENV=test tsx scripts/test-pre-push.ts",
@@ -35,6 +37,7 @@
35
37
  "@trpc/server": "^11.4.3",
36
38
  "@unkey/ratelimit": "^2.1.3",
37
39
  "bcryptjs": "^3.0.2",
40
+ "bullmq": "^5.76.6",
38
41
  "compression": "^1.8.1",
39
42
  "cors": "^2.8.5",
40
43
  "date-fns": "^4.1.0",