@studious-lms/server 1.4.9 → 1.6.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.
- package/dist/index.js +15 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/config/cors.d.ts.map +1 -1
- package/dist/lib/config/cors.js +2 -3
- package/dist/lib/config/cors.js.map +1 -1
- package/dist/lib/fileUpload.d.ts.map +1 -1
- package/dist/lib/fileUpload.js +4 -5
- package/dist/lib/fileUpload.js.map +1 -1
- package/dist/lib/pusher.d.ts +7 -1
- package/dist/lib/pusher.d.ts.map +1 -1
- package/dist/lib/pusher.js +9 -3
- package/dist/lib/pusher.js.map +1 -1
- package/dist/models/attendance.d.ts +16 -7
- package/dist/models/attendance.d.ts.map +1 -1
- package/dist/models/attendance.js +17 -2
- package/dist/models/attendance.js.map +1 -1
- package/dist/models/auth.d.ts +1 -0
- package/dist/models/auth.d.ts.map +1 -1
- package/dist/models/auth.js +3 -2
- package/dist/models/auth.js.map +1 -1
- package/dist/models/comment.d.ts +0 -5
- package/dist/models/comment.d.ts.map +1 -1
- package/dist/models/conversation.d.ts +17 -16
- package/dist/models/conversation.d.ts.map +1 -1
- package/dist/models/labChat.d.ts +2 -1
- package/dist/models/labChat.d.ts.map +1 -1
- package/dist/models/message.d.ts +6 -3
- package/dist/models/message.d.ts.map +1 -1
- package/dist/models/school.d.ts +0 -22
- package/dist/models/school.d.ts.map +1 -1
- package/dist/models/school.js +2 -14
- package/dist/models/school.js.map +1 -1
- package/dist/models/worksheet.d.ts +76 -19
- package/dist/models/worksheet.d.ts.map +1 -1
- package/dist/models/worksheet.js +2 -2
- package/dist/models/worksheet.js.map +1 -1
- package/dist/pipelines/aiLabChat.d.ts +20 -9
- package/dist/pipelines/aiLabChat.d.ts.map +1 -1
- package/dist/pipelines/aiLabChat.js +63 -75
- package/dist/pipelines/aiLabChat.js.map +1 -1
- package/dist/pipelines/aiNewtonChat.d.ts +13 -4
- package/dist/pipelines/aiNewtonChat.d.ts.map +1 -1
- package/dist/pipelines/aiNewtonChat.js +39 -27
- package/dist/pipelines/aiNewtonChat.js.map +1 -1
- package/dist/pipelines/gradeWorksheet.d.ts +5 -3
- package/dist/pipelines/gradeWorksheet.d.ts.map +1 -1
- package/dist/pipelines/gradeWorksheet.js +60 -24
- package/dist/pipelines/gradeWorksheet.js.map +1 -1
- package/dist/pipelines/pdfToWorksheet.d.ts +34 -2
- package/dist/pipelines/pdfToWorksheet.d.ts.map +1 -1
- package/dist/pipelines/pdfToWorksheet.js +205 -32
- package/dist/pipelines/pdfToWorksheet.js.map +1 -1
- package/dist/routers/_app.d.ts +106 -48
- package/dist/routers/_app.d.ts.map +1 -1
- package/dist/routers/announcement.d.ts +0 -4
- package/dist/routers/announcement.d.ts.map +1 -1
- package/dist/routers/assignment.d.ts +0 -8
- package/dist/routers/assignment.d.ts.map +1 -1
- package/dist/routers/assignment.js +64 -4
- package/dist/routers/assignment.js.map +1 -1
- package/dist/routers/auth.d.ts +1 -0
- package/dist/routers/auth.d.ts.map +1 -1
- package/dist/routers/comment.d.ts +0 -3
- package/dist/routers/comment.d.ts.map +1 -1
- package/dist/routers/conversation.d.ts +2 -1
- package/dist/routers/conversation.d.ts.map +1 -1
- package/dist/routers/labChat.d.ts +3 -1
- package/dist/routers/labChat.d.ts.map +1 -1
- package/dist/routers/message.d.ts +3 -0
- package/dist/routers/message.d.ts.map +1 -1
- package/dist/routers/newtonChat.d.ts +1 -0
- package/dist/routers/newtonChat.d.ts.map +1 -1
- package/dist/routers/school.d.ts +6 -0
- package/dist/routers/school.d.ts.map +1 -1
- package/dist/routers/school.js +7 -2
- package/dist/routers/school.js.map +1 -1
- package/dist/routers/worksheet.d.ts +37 -7
- package/dist/routers/worksheet.d.ts.map +1 -1
- package/dist/routers/worksheet.js +16 -6
- package/dist/routers/worksheet.js.map +1 -1
- package/dist/services/announcement.js +4 -4
- package/dist/services/announcement.js.map +1 -1
- package/dist/services/assignment.d.ts.map +1 -1
- package/dist/services/assignment.js +16 -6
- package/dist/services/assignment.js.map +1 -1
- package/dist/services/attendance.d.ts.map +1 -1
- package/dist/services/attendance.js +68 -4
- package/dist/services/attendance.js.map +1 -1
- package/dist/services/auth.d.ts +1 -0
- package/dist/services/auth.d.ts.map +1 -1
- package/dist/services/comment.d.ts +0 -3
- package/dist/services/comment.d.ts.map +1 -1
- package/dist/services/conversation.d.ts +2 -1
- package/dist/services/conversation.d.ts.map +1 -1
- package/dist/services/labChat.d.ts +10 -2
- package/dist/services/labChat.d.ts.map +1 -1
- package/dist/services/labChat.js +103 -44
- package/dist/services/labChat.js.map +1 -1
- package/dist/services/message.d.ts +3 -0
- package/dist/services/message.d.ts.map +1 -1
- package/dist/services/message.js +20 -2
- package/dist/services/message.js.map +1 -1
- package/dist/services/newtonChat.d.ts +1 -0
- package/dist/services/newtonChat.d.ts.map +1 -1
- package/dist/services/newtonChat.js +40 -17
- package/dist/services/newtonChat.js.map +1 -1
- package/dist/services/school.d.ts +6 -0
- package/dist/services/school.d.ts.map +1 -1
- package/dist/services/school.js +71 -6
- package/dist/services/school.js.map +1 -1
- package/dist/services/worksheet.d.ts +48 -11
- package/dist/services/worksheet.d.ts.map +1 -1
- package/dist/services/worksheet.js +73 -29
- package/dist/services/worksheet.js.map +1 -1
- package/dist/utils/generateUsername.d.ts +8 -0
- package/dist/utils/generateUsername.d.ts.map +1 -0
- package/dist/utils/generateUsername.js +103 -0
- package/dist/utils/generateUsername.js.map +1 -0
- package/dist/utils/inference.d.ts +50 -1
- package/dist/utils/inference.d.ts.map +1 -1
- package/dist/utils/inference.js +167 -2
- package/dist/utils/inference.js.map +1 -1
- package/dist/workers/connection.d.ts +3 -0
- package/dist/workers/connection.d.ts.map +1 -0
- package/dist/workers/connection.js +33 -0
- package/dist/workers/connection.js.map +1 -0
- package/dist/workers/index.d.ts +10 -0
- package/dist/workers/index.d.ts.map +1 -0
- package/dist/workers/index.js +63 -0
- package/dist/workers/index.js.map +1 -0
- package/dist/workers/processors/aiLabChat.d.ts +7 -0
- package/dist/workers/processors/aiLabChat.d.ts.map +1 -0
- package/dist/workers/processors/aiLabChat.js +52 -0
- package/dist/workers/processors/aiLabChat.js.map +1 -0
- package/dist/workers/processors/aiNewtonChat.d.ts +7 -0
- package/dist/workers/processors/aiNewtonChat.d.ts.map +1 -0
- package/dist/workers/processors/aiNewtonChat.js +57 -0
- package/dist/workers/processors/aiNewtonChat.js.map +1 -0
- package/dist/workers/processors/createRunJobWorker.d.ts +46 -0
- package/dist/workers/processors/createRunJobWorker.d.ts.map +1 -0
- package/dist/workers/processors/createRunJobWorker.js +118 -0
- package/dist/workers/processors/createRunJobWorker.js.map +1 -0
- package/dist/workers/processors/gradeWorksheet.d.ts +6 -0
- package/dist/workers/processors/gradeWorksheet.d.ts.map +1 -0
- package/dist/workers/processors/gradeWorksheet.js +19 -0
- package/dist/workers/processors/gradeWorksheet.js.map +1 -0
- package/dist/workers/processors/pdfToWorksheet.d.ts +8 -0
- package/dist/workers/processors/pdfToWorksheet.d.ts.map +1 -0
- package/dist/workers/processors/pdfToWorksheet.js +20 -0
- package/dist/workers/processors/pdfToWorksheet.js.map +1 -0
- package/dist/workers/processors/studentProgressChat.d.ts +8 -0
- package/dist/workers/processors/studentProgressChat.d.ts.map +1 -0
- package/dist/workers/processors/studentProgressChat.js +27 -0
- package/dist/workers/processors/studentProgressChat.js.map +1 -0
- package/dist/workers/queues.d.ts +85 -0
- package/dist/workers/queues.d.ts.map +1 -0
- package/dist/workers/queues.js +119 -0
- package/dist/workers/queues.js.map +1 -0
- package/package.json +6 -3
- package/prisma/migrations/20260506162500_consolidate_run_job_and_unify_job_status/migration.sql +166 -0
- package/prisma/schema.prisma +465 -387
- package/src/index.ts +12 -1
- package/src/lib/config/cors.ts +0 -1
- package/src/lib/fileUpload.ts +2 -4
- package/src/lib/pusher.ts +7 -1
- package/src/models/attendance.ts +15 -0
- package/src/models/auth.ts +1 -0
- package/src/models/school.ts +0 -14
- package/src/models/worksheet.ts +2 -2
- package/src/pipelines/aiLabChat.ts +71 -79
- package/src/pipelines/aiNewtonChat.ts +47 -31
- package/src/pipelines/gradeWorksheet.ts +63 -25
- package/src/pipelines/pdfToWorksheet.ts +272 -34
- package/src/routers/assignment.ts +74 -3
- package/src/routers/school.ts +5 -0
- package/src/routers/worksheet.ts +25 -4
- package/src/services/announcement.ts +2 -2
- package/src/services/assignment.ts +18 -4
- package/src/services/attendance.ts +87 -2
- package/src/services/labChat.ts +108 -48
- package/src/services/message.ts +19 -0
- package/src/services/newtonChat.ts +46 -25
- package/src/services/school.ts +89 -6
- package/src/services/worksheet.ts +87 -28
- package/src/utils/generateUsername.ts +108 -0
- package/src/utils/inference.ts +209 -1
- package/src/workers/connection.ts +37 -0
- package/src/workers/index.ts +71 -0
- package/src/workers/processors/aiLabChat.ts +65 -0
- package/src/workers/processors/aiNewtonChat.ts +75 -0
- package/src/workers/processors/createRunJobWorker.ts +164 -0
- package/src/workers/processors/gradeWorksheet.ts +16 -0
- package/src/workers/processors/pdfToWorksheet.ts +17 -0
- package/src/workers/processors/studentProgressChat.ts +28 -0
- package/src/workers/queues.ts +174 -0
- package/tests/{globalSetup.ts → integration/globalSetup.ts} +4 -4
- package/tests/{helpers.ts → integration/helpers.ts} +2 -2
- package/tests/{pipelines → integration/pipelines}/pdfToWorksheet.test.ts +55 -43
- package/tests/{routers → integration/routers}/attendance.test.ts +61 -2
- package/tests/{routers → integration/routers}/auth.test.ts +3 -3
- package/tests/{routers → integration/routers}/class.test.ts +3 -17
- package/tests/{routers → integration/routers}/conversation.test.ts +1 -1
- package/tests/{routers → integration/routers}/event.test.ts +1 -1
- package/tests/{routers → integration/routers}/message.test.ts +1 -1
- package/tests/{routers → integration/routers}/newtonChat.test.ts +1 -1
- package/tests/{routers → integration/routers}/studentProgress.test.ts +1 -1
- package/tests/{user.test.ts → integration/routers/user.test.ts} +2 -2
- package/tests/integration/routers/worksheet.test.ts +448 -0
- package/tests/{setup.ts → integration/setup.ts} +4 -4
- package/tests/{lib → unit/lib}/cors.test.ts +1 -1
- package/tests/{middleware → unit/middleware}/security.test.ts +1 -1
- package/tests/{pipelines → unit/pipelines}/aiLabChat.test.ts +1 -1
- package/tests/{lib → unit/pipelines}/aiLabChatContract.test.ts +1 -1
- package/tests/{utils/aiLabChatPrompt.test.ts → unit/pipelines/labChatPrompt.test.ts} +2 -2
- package/tests/{utils → unit/services}/studentProgress.test.ts +5 -5
- package/tests/{utils → unit/utils}/aiUser.test.ts +1 -1
- package/tests/{utils → unit/utils}/generateInviteCode.test.ts +1 -1
- package/tests/{utils → unit/utils}/inference.test.ts +6 -6
- package/tests/{utils → unit/utils}/logger.test.ts +1 -1
- package/tests/{utils → unit/utils}/prismaErrorHandler.test.ts +1 -1
- package/tests/{utils → unit/utils}/prismaWrapper.test.ts +1 -1
- package/vitest.config.ts +11 -3
- package/vitest.unit.config.ts +10 -10
- package/tests/routers/worksheet.test.ts +0 -194
- package/tests/startup.test.ts +0 -5
- package/tests/worksheet.test.ts +0 -181
- /package/tests/{routers → integration/routers}/agenda.test.ts +0 -0
- /package/tests/{routers → integration/routers}/announcement.test.ts +0 -0
- /package/tests/{routers → integration/routers}/assignment.test.ts +0 -0
- /package/tests/{routers → integration/routers}/comment.test.ts +0 -0
- /package/tests/{routers → integration/routers}/folder.test.ts +0 -0
- /package/tests/{routers → integration/routers}/labChat.test.ts +0 -0
- /package/tests/{routers → integration/routers}/marketing.test.ts +0 -0
- /package/tests/{routers → integration/routers}/notification.test.ts +0 -0
- /package/tests/{routers → integration/routers}/search.test.ts +0 -0
- /package/tests/{routers → integration/routers}/section.test.ts +0 -0
- /package/tests/{server → unit/middleware}/rateLimit.test.ts +0 -0
|
@@ -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]="feefd10a-9381-5c30-a525-0ffe36e2e323")}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
|
+
export 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=feefd10a-9381-5c30-a525-0ffe36e2e323
|
|
@@ -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 */\nexport type JobInputs = {\n /**\n * `worksheetId` is the placeholder Worksheet row created synchronously by\n * the producer (so the client has an id to navigate to). The pipeline fills\n * that row in asynchronously and broadcasts progress on the per-worksheet\n * job channel.\n */\n PDF_TO_WORKSHEET: { gcsUrl: string; classId: string; worksheetId: string };\n GRADE_WORKSHEET: { worksheetResponseId: string };\n /**\n * `aiMessageId` is the placeholder AI Message row created synchronously by\n * the producer before enqueue (so the client renders a PENDING bubble in\n * the timeline). The pipeline fills in the content and flips the row to\n * COMPLETED / FAILED. Used for both intros and replies — single shape.\n */\n AI_LAB_CHAT: { conversationId: string; aiMessageId: string };\n /**\n * `aiMessageId` placeholder pattern, same as AI_LAB_CHAT — see above.\n */\n AI_NEWTON_CHAT: { conversationId: string; aiMessageId: string };\n STUDENT_PROGRESS_CHAT: { classId: string; studentId: string; message: string };\n};\n\nexport const 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,MAAM,oBAAoB,CAAC;AAE5C,MAAM,mBAAmB,GAAgB;IACvC,QAAQ,EAAE,CAAC;IACX,OAAO,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,IAAK,EAAE;IAC9C,gBAAgB,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,iDAAiD;IACrF,YAAY,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,0CAA0C;CAChF,CAAC;AAgCF,MAAM,CAAC,MAAM,WAAW,GAA+B;IACrD,gBAAgB,EAAE,gBAAgB;IAClC,eAAe,EAAE,gBAAgB;IACjC,WAAW,EAAE,WAAW;IACxB,cAAc,EAAE,cAAc;IAC9B,qBAAqB,EAAE,qBAAqB;CAC7C,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,GAAG,IAAI,GAAG,EAAqB,CAAC;AAEhD,SAAS,QAAQ,CAAC,IAAgB;IAChC,IAAI,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACjC,IAAI,CAAC,KAAK,E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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":"feefd10a-9381-5c30-a525-0ffe36e2e323"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studious-lms/server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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",
|
|
@@ -27,7 +29,7 @@
|
|
|
27
29
|
"sentry:sourcemaps": "sentry-cli sourcemaps inject ./dist && sentry-cli sourcemaps upload --org \"${SENTRY_ORG:-studious-lms}\" --project \"${SENTRY_PROJECT:-server}\" ./dist"
|
|
28
30
|
},
|
|
29
31
|
"dependencies": {
|
|
30
|
-
"@google-cloud/storage": "^
|
|
32
|
+
"@google-cloud/storage": "^5.18.3",
|
|
31
33
|
"@pdf-lib/fontkit": "^1.1.1",
|
|
32
34
|
"@prisma/client": "^6.7.0",
|
|
33
35
|
"@sentry/cli": "^2.58.2",
|
|
@@ -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",
|
|
@@ -43,7 +46,7 @@
|
|
|
43
46
|
"express-rate-limit": "^8.2.1",
|
|
44
47
|
"helmet": "^8.1.0",
|
|
45
48
|
"ioredis": "^5.10.0",
|
|
46
|
-
"nodemailer": "^
|
|
49
|
+
"nodemailer": "^8.0.7",
|
|
47
50
|
"openai": "^5.23.0",
|
|
48
51
|
"pdf-lib": "^1.17.1",
|
|
49
52
|
"prisma": "^6.7.0",
|
package/prisma/migrations/20260506162500_consolidate_run_job_and_unify_job_status/migration.sql
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
-- Consolidate background-job tracking onto RunJob and unify domain status.
|
|
2
|
+
--
|
|
3
|
+
-- 1. Replace the bare-bones RunJob table with a fully-typed audit row that
|
|
4
|
+
-- carries job type, status, input/result, error, attempts, timestamps.
|
|
5
|
+
-- 2. Replace the catch-all GenerationStatus enum with a single shared
|
|
6
|
+
-- JobStatus enum used by every domain row that has a user-observable
|
|
7
|
+
-- async lifecycle (Message, StudentQuestionProgress).
|
|
8
|
+
-- 3. Add a nullable runJobId FK on Message and StudentQuestionProgress so we
|
|
9
|
+
-- can trace a domain row back to the job that produced it. Operational
|
|
10
|
+
-- state (retries, errors, attempts) lives on RunJob, not on the row.
|
|
11
|
+
-- 4. Drop the unused Comment.status column.
|
|
12
|
+
|
|
13
|
+
-- ---------------------------------------------------------------------------
|
|
14
|
+
-- 1. New enum types
|
|
15
|
+
-- ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
CREATE TYPE "RunJobType" AS ENUM (
|
|
18
|
+
'PDF_TO_WORKSHEET',
|
|
19
|
+
'GRADE_WORKSHEET',
|
|
20
|
+
'AI_LAB_CHAT',
|
|
21
|
+
'AI_NEWTON_CHAT',
|
|
22
|
+
'STUDENT_PROGRESS_CHAT'
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
-- Queue-internal lifecycle. Distinct from JobStatus because PENDING (queued
|
|
26
|
+
-- but not picked up) vs RUNNING (worker actively executing) is a real
|
|
27
|
+
-- operational distinction we want when debugging.
|
|
28
|
+
CREATE TYPE "RunJobStatus" AS ENUM (
|
|
29
|
+
'PENDING',
|
|
30
|
+
'RUNNING',
|
|
31
|
+
'SUCCEEDED',
|
|
32
|
+
'FAILED',
|
|
33
|
+
'CANCELLED'
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
-- Shared UI-shaped status for domain rows. Not every row uses every value
|
|
37
|
+
-- (e.g. Message never gets NOT_STARTED) but we keep one enum to avoid an
|
|
38
|
+
-- explosion of near-duplicate types.
|
|
39
|
+
CREATE TYPE "JobStatus" AS ENUM (
|
|
40
|
+
'NOT_STARTED',
|
|
41
|
+
'PENDING',
|
|
42
|
+
'COMPLETED',
|
|
43
|
+
'FAILED',
|
|
44
|
+
'CANCELLED'
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
-- ---------------------------------------------------------------------------
|
|
48
|
+
-- 2. Replace the legacy RunJob table.
|
|
49
|
+
--
|
|
50
|
+
-- The previous schema only had `id` and `createdAt`, and was applied via
|
|
51
|
+
-- `prisma db push` (no migration history) — so any rows in it are throwaway.
|
|
52
|
+
-- We DROP IF EXISTS to handle both fresh production DBs and dev DBs that have
|
|
53
|
+
-- the old shape.
|
|
54
|
+
-- ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
DROP TABLE IF EXISTS "RunJob" CASCADE;
|
|
57
|
+
|
|
58
|
+
CREATE TABLE "RunJob" (
|
|
59
|
+
"id" TEXT NOT NULL,
|
|
60
|
+
"type" "RunJobType" NOT NULL,
|
|
61
|
+
"status" "RunJobStatus" NOT NULL DEFAULT 'PENDING',
|
|
62
|
+
"classId" TEXT,
|
|
63
|
+
"userId" TEXT,
|
|
64
|
+
"bullJobId" TEXT,
|
|
65
|
+
"input" JSONB NOT NULL,
|
|
66
|
+
"result" JSONB,
|
|
67
|
+
"error" TEXT,
|
|
68
|
+
"attempts" INTEGER NOT NULL DEFAULT 0,
|
|
69
|
+
"startedAt" TIMESTAMP(3),
|
|
70
|
+
"finishedAt" TIMESTAMP(3),
|
|
71
|
+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
72
|
+
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
73
|
+
|
|
74
|
+
CONSTRAINT "RunJob_pkey" PRIMARY KEY ("id")
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
CREATE INDEX "RunJob_userId_type_status_idx" ON "RunJob"("userId", "type", "status");
|
|
78
|
+
CREATE INDEX "RunJob_classId_status_idx" ON "RunJob"("classId", "status");
|
|
79
|
+
CREATE INDEX "RunJob_status_createdAt_idx" ON "RunJob"("status", "createdAt");
|
|
80
|
+
|
|
81
|
+
-- ---------------------------------------------------------------------------
|
|
82
|
+
-- 2b. Track PDF source URLs on worksheets for idempotent PDF conversion.
|
|
83
|
+
-- ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
ALTER TABLE "Worksheet" ADD COLUMN "sourceUrl" TEXT;
|
|
86
|
+
|
|
87
|
+
CREATE UNIQUE INDEX "Worksheet_classId_sourceUrl_key"
|
|
88
|
+
ON "Worksheet"("classId", "sourceUrl");
|
|
89
|
+
|
|
90
|
+
-- ---------------------------------------------------------------------------
|
|
91
|
+
-- 3. Message.status: GenerationStatus -> JobStatus.
|
|
92
|
+
--
|
|
93
|
+
-- Old enum had NOT_STARTED / PENDING / GENERATING / COMPLETED / FAILED /
|
|
94
|
+
-- CANCELLED. New JobStatus drops GENERATING (never written). The old default
|
|
95
|
+
-- of NOT_STARTED is dropped: a row without an AI lifecycle now has NULL.
|
|
96
|
+
-- GENERATING is mapped to PENDING defensively in case any row carries it.
|
|
97
|
+
-- ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
ALTER TABLE "Message" ALTER COLUMN "status" DROP DEFAULT;
|
|
100
|
+
|
|
101
|
+
ALTER TABLE "Message"
|
|
102
|
+
ALTER COLUMN "status" TYPE "JobStatus"
|
|
103
|
+
USING (
|
|
104
|
+
CASE "status"::text
|
|
105
|
+
WHEN 'NOT_STARTED' THEN NULL
|
|
106
|
+
WHEN 'GENERATING' THEN 'PENDING'::"JobStatus"
|
|
107
|
+
WHEN 'PENDING' THEN 'PENDING'::"JobStatus"
|
|
108
|
+
WHEN 'COMPLETED' THEN 'COMPLETED'::"JobStatus"
|
|
109
|
+
WHEN 'FAILED' THEN 'FAILED'::"JobStatus"
|
|
110
|
+
WHEN 'CANCELLED' THEN 'CANCELLED'::"JobStatus"
|
|
111
|
+
ELSE NULL
|
|
112
|
+
END
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
-- ---------------------------------------------------------------------------
|
|
116
|
+
-- 4. StudentQuestionProgress.status: GenerationStatus -> JobStatus.
|
|
117
|
+
--
|
|
118
|
+
-- Default kept at NOT_STARTED. GENERATING -> PENDING defensively.
|
|
119
|
+
-- ---------------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
ALTER TABLE "StudentQuestionProgress" ALTER COLUMN "status" DROP DEFAULT;
|
|
122
|
+
|
|
123
|
+
ALTER TABLE "StudentQuestionProgress"
|
|
124
|
+
ALTER COLUMN "status" TYPE "JobStatus"
|
|
125
|
+
USING (
|
|
126
|
+
CASE "status"::text
|
|
127
|
+
WHEN 'NOT_STARTED' THEN 'NOT_STARTED'::"JobStatus"
|
|
128
|
+
WHEN 'PENDING' THEN 'PENDING'::"JobStatus"
|
|
129
|
+
WHEN 'GENERATING' THEN 'PENDING'::"JobStatus"
|
|
130
|
+
WHEN 'COMPLETED' THEN 'COMPLETED'::"JobStatus"
|
|
131
|
+
WHEN 'FAILED' THEN 'FAILED'::"JobStatus"
|
|
132
|
+
WHEN 'CANCELLED' THEN 'CANCELLED'::"JobStatus"
|
|
133
|
+
ELSE NULL
|
|
134
|
+
END
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
ALTER TABLE "StudentQuestionProgress"
|
|
138
|
+
ALTER COLUMN "status" SET DEFAULT 'NOT_STARTED';
|
|
139
|
+
|
|
140
|
+
-- ---------------------------------------------------------------------------
|
|
141
|
+
-- 5. Drop the unused Comment.status column and the legacy enum.
|
|
142
|
+
-- ---------------------------------------------------------------------------
|
|
143
|
+
|
|
144
|
+
ALTER TABLE "Comment" DROP COLUMN "status";
|
|
145
|
+
|
|
146
|
+
DROP TYPE "GenerationStatus";
|
|
147
|
+
|
|
148
|
+
-- ---------------------------------------------------------------------------
|
|
149
|
+
-- 6. Add runJobId FKs and supporting indexes.
|
|
150
|
+
-- ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
ALTER TABLE "Message"
|
|
153
|
+
ADD COLUMN "runJobId" TEXT,
|
|
154
|
+
ADD CONSTRAINT "Message_runJobId_fkey"
|
|
155
|
+
FOREIGN KEY ("runJobId") REFERENCES "RunJob"("id")
|
|
156
|
+
ON DELETE SET NULL ON UPDATE CASCADE;
|
|
157
|
+
|
|
158
|
+
CREATE INDEX "Message_runJobId_idx" ON "Message"("runJobId");
|
|
159
|
+
|
|
160
|
+
ALTER TABLE "StudentQuestionProgress"
|
|
161
|
+
ADD COLUMN "runJobId" TEXT,
|
|
162
|
+
ADD CONSTRAINT "StudentQuestionProgress_runJobId_fkey"
|
|
163
|
+
FOREIGN KEY ("runJobId") REFERENCES "RunJob"("id")
|
|
164
|
+
ON DELETE SET NULL ON UPDATE CASCADE;
|
|
165
|
+
|
|
166
|
+
CREATE INDEX "StudentQuestionProgress_runJobId_idx" ON "StudentQuestionProgress"("runJobId");
|