-
Notifications
You must be signed in to change notification settings - Fork 313
/
chunks.js
757 lines (685 loc) · 28.9 KB
/
chunks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
// @ts-check
const AWS = require('aws-sdk');
const async = require('async');
const child_process = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const PassThroughStream = require('stream').PassThrough;
const tar = require('tar');
const util = require('util');
const { v4: uuidv4 } = require('uuid');
const { chalk, chalkDim } = require('../lib/chalk');
const logger = require('../lib/logger');
const serverJobs = require('../lib/server-jobs');
const namedLocks = require('../lib/named-locks');
const courseDB = require('../sync/course-db');
const { sqlDb, sqlLoader } = require('@prairielearn/prairielib');
const globalConfig = require('./config');
const sql = sqlLoader.loadSqlEquiv(__filename);
/** @typedef {"elements" | "elementExtensions" | "clientFilesCourse" | "serverFilesCourse" | "clientFilesCourseInstance" | "clientFilesAssessment" | "question"} ChunkType */
/**
* @typedef {Object} ChunkMetadata
* @property {ChunkType} type The type of this particular chunk
* @property {string} [questionName] The question name (QID) of a chunk, if applicable
* @property {string} [questionUuid] The question UUID of a chunk, if applicable
* @property {string} [courseInstanceName] The course instance name of a chunk, if applicable
* @property {string} [courseInstanceUuid] The course instance UUID of a chunk, if applicable
* @property {string} [assessmentName] The assessment name (TID) of a chunk, if applicable
* @property {string} [assessmentUuid] the Assessment UUID of a chunk, if applicable
*/
/**
* @typedef {Object} CourseInstanceChunks
* @property {boolean} clientFilesCourseInstance
* @property {Set<string>} assessments
*/
/**
* @typedef {Object} CourseChunks
* @property {boolean} elements
* @property {boolean} elementExtensions
* @property {boolean} clientFilesCourse
* @property {boolean} serverFilesCourse
* @property {Set<string>} questions
* @property {{ [id: string]: CourseInstanceChunks }} courseInstances
*/
/**
* Identifies the files that changes between two commits in a given course.
*
* @param {string} coursePath The course directory to diff
* @param {string} oldHash The old (previous) hash for the diff
* @param {string} newHash The new (current) hash for the diff
* @returns {Promise<string[]>} List of changed files
*/
module.exports.identifyChangedFiles = async (coursePath, oldHash, newHash) => {
const { stdout } = await util.promisify(child_process.exec)(
`git diff --name-only ${oldHash}..${newHash}`,
{ cwd: coursePath },
);
return stdout.trim().split('\n');
};
/**
* Given a list of files that have changed (such as that produced by
* `git diff --name-only`), returns a data structure describing the chunks
* that need to be generated.
*
* @param {string[]} changedFiles A list of files that changed in a given sync.
* @param {import('../sync/course-db').CourseData} courseData The "full course" that was loaded from disk.
* @returns {CourseChunks}
*/
module.exports.identifyChunksFromChangedFiles = (changedFiles, courseData) => {
/** @type {CourseChunks} */
const courseChunks = {
elements: false,
elementExtensions: false,
clientFilesCourse: false,
serverFilesCourse: false,
courseInstances: {},
questions: new Set(),
};
changedFiles.forEach(changedFile => {
if (changedFile.startsWith('elements/')) {
courseChunks.elements = true;
}
if (changedFile.startsWith('elementExtensions/')) {
courseChunks.elementExtensions = true;
}
if (changedFile.startsWith('serverFilesCourse/')) {
courseChunks.serverFilesCourse = true;
}
if (changedFile.startsWith('clientFilesCourse/')) {
courseChunks.clientFilesCourse = true;
}
if (changedFile.startsWith('questions/')) {
// Here's where things get interesting. Questions can be nested in
// directories, so we need to figure out which of the potentially
// deeply-nested directories is the root of a particular question.
const pathComponents = changedFile.split(path.sep).slice(1);
// Progressively join more and more path components until we get
// something that corresponds to an actual question
let questionId = null;
for (let i = 1; i < pathComponents.length; i++) {
const candidateQuestionId = path.join(...pathComponents.slice(0, i));
if (courseData.questions[candidateQuestionId]) {
questionId = candidateQuestionId;
break;
}
}
if (questionId) {
// This chunk corresponds to a question!
courseChunks.questions.add(questionId);
}
}
if (changedFile.startsWith('courseInstances/')) {
// This could be one of two things: `clientFilesCourseInstance` or
// `clientFileAssessment`.
const pathComponents = changedFile.split(path.sep).slice(1);
const clientFilesCourseInstanceIndex = pathComponents.indexOf('clientFilesCourseInstance');
const assessmentsIndex = pathComponents.indexOf('assessments');
const clientFilesAssessmentIndex = pathComponents.indexOf('clientFilesAssessment');
if (clientFilesCourseInstanceIndex >= 0) {
// Let's validate that the preceeding path components correspond
// to an actual course instance
const courseInstanceId = path.join(...pathComponents.slice(0, clientFilesCourseInstanceIndex));
if (courseData.courseInstances[courseInstanceId]) {
if (!courseChunks.courseInstances[courseInstanceId]) {
courseChunks.courseInstances[courseInstanceId] = {
assessments: new Set(),
clientFilesCourseInstance: true,
};
}
courseChunks.courseInstances[courseInstanceId].clientFilesCourseInstance = true;
return;
}
}
// Important: fall through to account for weird things like people putting
// `clientFilesCourseInstance` directories inside of `clientFileAssessment`
// for some strange reason.
if (
assessmentsIndex >= 0 &&
clientFilesAssessmentIndex >= 0 &&
clientFilesAssessmentIndex > assessmentsIndex
) {
// We probably care about this file - let's validate that by
// splitting up the path into chunks that hopefully correspond
// to course instance IDs and assessment IDs.
const courseInstanceId = path.join(...pathComponents.slice(0, assessmentsIndex));
const assessmentId = path.join(...pathComponents.slice(assessmentsIndex + 1, clientFilesAssessmentIndex));
if (
courseData.courseInstances[courseInstanceId] &&
courseData.courseInstances[courseInstanceId].assessments[assessmentId]
) {
// This corresponds to something that we need to
// create/update a chunk for!
if (!courseChunks.courseInstances[courseInstanceId]) {
courseChunks.courseInstances[courseInstanceId] = {
assessments: new Set(),
clientFilesCourseInstance: false,
};
}
courseChunks.courseInstances[courseInstanceId].assessments.add(assessmentId);
}
}
}
});
return courseChunks;
};
/**
* Returns all the chunks the are currently stored for the given course.
*
* @param {string} courseId
*/
module.exports.getAllChunksForCourse = async (courseId) => {
const result = await sqlDb.queryAsync(sql.select_course_chunks, { course_id: courseId });
return result.rows;
};
/**
* Given a course ID, computes a list of all chunks that need to be
* (re)generated.
*
* @param {Object} options
* @param {string} options.coursePath
* @param {string} options.courseId
* @param {import('../sync/course-db').CourseData} options.courseData
* @param {string[]} options.changedFiles
* @returns {Promise<ChunkMetadata[]>}
*/
module.exports.identifyChunksToGenerate = async ({ coursePath, courseId, courseData, changedFiles }) => {
const rawCourseChunks = await module.exports.getAllChunksForCourse(courseId);
// Build a data structure from the result of getAllChunksForCourse so that
// we can efficiently query to see if a given chunk exists
/** @type {CourseChunks} */
const existingCourseChunks = {
elements: false,
elementExtensions: false,
serverFilesCourse: false,
clientFilesCourse: false,
courseInstances: {},
questions: new Set(),
};
rawCourseChunks.forEach(courseChunk => {
switch (courseChunk.type) {
case 'elements':
case 'elementExtensions':
case 'serverFilesCourse':
case 'clientFilesCourse':
existingCourseChunks[courseChunk.type] = true;
break;
case 'question':
existingCourseChunks.questions.add(courseChunk.question_name);
break;
case 'clientFilesCourseInstance': {
const courseInstanceName = courseChunk.course_instance_name;
if (!existingCourseChunks.courseInstances[courseInstanceName]) {
existingCourseChunks.courseInstances[courseInstanceName] = {
assessments: new Set(),
clientFilesCourseInstance: true,
};
}
existingCourseChunks.courseInstances[courseInstanceName].clientFilesCourseInstance = true;
break;
}
case 'clientFilesAssessment': {
const courseInstanceName = courseChunk.course_instance_name;
const assessmentName = courseChunk.assessment_name;
if (!existingCourseChunks.courseInstances[courseInstanceName]) {
existingCourseChunks.courseInstances[courseInstanceName] = {
assessments: new Set(),
clientFilesCourseInstance: false,
};
}
existingCourseChunks.courseInstances[courseInstanceName].assessments.add(assessmentName);
break;
}
}
});
const changedCourseChunks = module.exports.identifyChunksFromChangedFiles(changedFiles, courseData);
// Now, let's compute the set of chunks that we need to generate
/** @type {ChunkMetadata[]} */
const chunksToGenerate = [];
// First: elements, clientFilesCourse, and serverFilesCourse
await async.each(['elements', 'elementExtensions', 'clientFilesCourse', 'serverFilesCourse'], async (/** @type {"elements" | "elementExtensions" | "clientFilesCourse" | "serverFilesCourse"} */ chunkType) => {
const hasChunkDirectory = await fs.pathExists(path.join(coursePath, chunkType));
if (hasChunkDirectory && (!existingCourseChunks[chunkType] || changedCourseChunks[chunkType])) {
chunksToGenerate.push({ type: chunkType });
}
});
// Next: questions
Object.entries(courseData.questions).forEach(([qid, questionInfo]) => {
if (!existingCourseChunks.questions.has(qid) || changedCourseChunks.questions.has(qid)) {
chunksToGenerate.push({
type: 'question',
questionName: qid,
questionUuid: questionInfo.uuid,
});
}
});
// Next: course instances and their assessments
await async.each(Object.entries(courseData.courseInstances), async ([ciid, courseInstanceInfo]) => {
const hasClientFilesCourseInstanceDirectory = await fs.pathExists(
path.join(coursePath, 'courseInstances', ciid, 'clientFilesCourseInstance'),
);
if (hasClientFilesCourseInstanceDirectory && (
(!existingCourseChunks.courseInstances[ciid] ||
!existingCourseChunks.courseInstances[ciid].clientFilesCourseInstance) ||
(changedCourseChunks.courseInstances[ciid] &&
changedCourseChunks.courseInstances[ciid].clientFilesCourseInstance)
)) {
chunksToGenerate.push({
type: 'clientFilesCourseInstance',
courseInstanceName: ciid,
courseInstanceUuid: courseInstanceInfo.courseInstance.uuid,
});
}
await async.each(Object.entries(courseInstanceInfo.assessments), async ([tid, assessmentInfo]) => {
const hasClientFilesAssessmentDirectory = await fs.pathExists(
path.join(coursePath, 'courseInstances', ciid, 'assessments', tid, 'clientFilesAssessment'),
);
if (hasClientFilesAssessmentDirectory && (
(!existingCourseChunks.courseInstances[ciid] ||
!existingCourseChunks.courseInstances[ciid].assessments ||
!existingCourseChunks.courseInstances[ciid].assessments.has(tid)) ||
(changedCourseChunks.courseInstances[ciid] &&
changedCourseChunks.courseInstances[ciid].assessments &&
changedCourseChunks.courseInstances[ciid].assessments.has(tid))
)) {
chunksToGenerate.push({
type: 'clientFilesAssessment',
courseInstanceName: ciid,
assessmentName: tid,
courseInstanceUuid: courseInstanceInfo.courseInstance.uuid,
assessmentUuid: assessmentInfo.uuid,
});
}
});
});
return chunksToGenerate;
};
/**
*
* @param {string} coursePath
* @param {string} courseId
* @param {ChunkMetadata[]} chunksToGenerate
*/
module.exports.createAndUploadChunks = async (coursePath, courseId, chunksToGenerate) => {
const generatedChunks = [];
await async.eachLimit(chunksToGenerate, globalConfig.chunksMaxParallelUpload, async (chunk) => {
let chunkDirectory;
switch (chunk.type) {
case 'elements':
case 'elementExtensions':
case 'clientFilesCourse':
case 'serverFilesCourse':
chunkDirectory = path.join(coursePath, chunk.type);
break;
case 'question':
chunkDirectory = path.join(coursePath, 'questions', chunk.questionName);
break;
case 'clientFilesCourseInstance':
chunkDirectory = path.join(
coursePath,
'courseInstances',
chunk.courseInstanceName,
'clientFilesCourseInstance',
);
break;
case 'clientFilesAssessment':
chunkDirectory = path.join(
coursePath,
'courseInstances',
chunk.courseInstanceName,
'assessments',
chunk.assessmentName,
'clientFilesAssessment',
);
break;
}
// Generate a UUId for this chunk
const chunkUuid = uuidv4();
// Let's create a tarball for this chunk and send it off to S3
const tarball = tar.create({
gzip: true,
cwd: chunkDirectory,
}, ['.']);
const passthrough = new PassThroughStream();
tarball.pipe(passthrough);
const params = {
Bucket: globalConfig.chunksS3Bucket,
Key: `${chunkUuid}.tar.gz`,
Body: passthrough,
};
const s3 = new AWS.S3();
await s3.upload(params).promise();
generatedChunks.push({ ...chunk, uuid: chunkUuid });
});
// Now that the new chunks have been uploaded, update their status in the database
await sqlDb.callAsync('chunks_insert', [courseId, generatedChunks]);
};
/**
* Returns the paths to the chunks directories for the given course
* ID. The "downloads" directory will hold in-progress chunk
* downloads, the "chunks" directory will hold fully-downloaded chunk
* zip files, the "unpacked" directory will hold unpacked zips, and
* the "course" directory is the reconstructed directory hierarchy
* that mimics the source repo.
*
* @param {string} courseId The ID of the course in question
*/
module.exports.getChunksDirectoriesForCourseId = (courseId) => {
const baseDirectory = path.join(globalConfig.chunksConsumerDirectory, `course-${courseId}`);
return {
base: baseDirectory,
downloads: path.join(baseDirectory, 'downloads'),
chunks: path.join(baseDirectory, 'chunks'),
unpacked: path.join(baseDirectory, 'unpacked'),
course: path.join(baseDirectory, 'course'),
};
};
/**
* Returns the absolute path to the course directory that should be used at
* runtime for things like serving course files, executing question code, etc.
* If chunks are enabled, this will be same as the "course" directory from
* `getChunksDirectoriesForCourseId`. Otherwise, this returns the path of the
* course that was passed in. This abstraction allows calling code to not need
* to know if chunks are enabled or not.
*
* This function is designed to take a course object like one would get from
* `res.locals.course`. If such an object isn't readily available, you can
* just construct one with a course ID and course path.
*
* @param {Object} course The course object
* @param {string} course.id The database ID of the course
* @param {string} course.path The path to the course source (not the chunks)
*/
module.exports.getRuntimeDirectoryForCourse = (course) => {
if (globalConfig.chunksConsumer) {
return module.exports.getChunksDirectoriesForCourseId(course.id).course;
} else {
return course.path;
}
};
/**
*
* @param {Object} options
* @param {string} options.coursePath
* @param {string} options.courseId
* @param {import('../sync/course-db').CourseData} options.courseData
* @param {string} [options.oldHash]
* @param {string} [options.newHash]
*/
module.exports.updateChunksForCourse = async ({ coursePath, courseId, courseData, oldHash, newHash }) => {
let changedFiles = [];
if (oldHash && newHash) {
changedFiles = await module.exports.identifyChangedFiles(coursePath, oldHash, newHash);
}
const chunksToGenerate = await module.exports.identifyChunksToGenerate({
coursePath,
courseId,
courseData,
changedFiles,
});
await module.exports.createAndUploadChunks(coursePath, courseId, chunksToGenerate);
};
/**
* Generates all chunks for a list of courses.
*
* @param {number[]} course_ids
* @param {number} authn_user_id
*/
module.exports.generateAllChunksForCourseList = async (course_ids, authn_user_id) => {
const jobSequenceOptions = {
user_id: authn_user_id,
authn_user_id: authn_user_id,
type: 'generate_all_chunks',
description: 'Generate all chunks for a list of courses',
};
const job_sequence_id = await serverJobs.createJobSequenceAsync(jobSequenceOptions);
// don't await this, we want it to run in the background
module.exports._generateAllChunksForCourseListWithJobSequence(course_ids, authn_user_id, job_sequence_id);
// return immediately, while the generation is still running
return job_sequence_id;
};
/**
* Helper function to actually generate all chunks for a list of courses.
*
* @param {number[]} course_ids
* @param {number} authn_user_id
* @param {number} job_sequence_id
*/
module.exports._generateAllChunksForCourseListWithJobSequence = async (course_ids, authn_user_id, job_sequence_id) => {
try {
for (let i = 0; i < course_ids.length; i++) {
const course_id = course_ids[i];
const jobOptions = {
course_id: null, /* Set the job's course_id to null so we can find it from the admin page */
type: 'generate_all_chunks',
description: `Generate all chunks for course ID = ${course_id}`,
job_sequence_id,
user_id: authn_user_id,
authn_user_id,
last_in_sequence: (i == course_ids.length - 1),
};
const job = await serverJobs.createJobAsync(jobOptions);
job.info(chalkDim(`Course ID = ${course_id}`));
try {
await module.exports._generateAllChunksForCourseWithJob(course_id, authn_user_id, job);
job.succeed();
} catch (err) {
job.error(chalk.red(JSON.stringify(err)));
await job.failAsync(err);
throw err;
}
}
} catch (err) {
try {
await serverJobs.failJobSequenceAsync(job_sequence_id);
} catch (err) {
logger.error(`Failed to fail job_sequence_id=${job_sequence_id}`);
}
}
};
/**
* Helper function to generate all chunks for a single course.
*
* @param {number} course_id
* @param {number} authn_user_id
* @param {object} job
*/
module.exports._generateAllChunksForCourseWithJob = async (course_id, authn_user_id, job) => {
job.info(chalk.bold(`Looking up course directory`));
const result = await sqlDb.queryOneRowAsync(sql.select_course_dir, {course_id});
let courseDir = result.rows[0].path;
job.info(chalkDim(`Found course directory = ${courseDir}`));
courseDir = path.resolve(process.cwd(), courseDir);
job.info(chalkDim(`Resolved course directory = ${courseDir}`));
const lockName = 'coursedir:' + courseDir;
job.info(chalk.bold(`Acquiring lock ${lockName}`));
const lock = await namedLocks.waitLockAsync(lockName, {});
job.info(chalkDim(`Acquired lock`));
job.info(chalk.bold(`Loading course data from ${courseDir}`));
const courseData = await courseDB.loadFullCourseNew(courseDir);
job.info(chalkDim(`Loaded course data`));
job.info(chalk.bold(`Generating all chunks`));
const chunkOptions = {
coursePath: courseDir,
courseId: String(course_id),
courseData,
};
await module.exports.updateChunksForCourse(chunkOptions);
job.info(chalkDim(`Generated all chunks`));
await namedLocks.releaseLockAsync(lock);
job.info(chalkDim(`Released lock`));
job.info(chalk.green(`Successfully generated chunks for course ID = ${course_id}`));
};
/**
* @typedef {Object} ElementsChunk
* @property {"elements"} type
*/
/**
* @typedef {Object} ElementExtensionsChunk
* @property {"elementExtensions"} type
*/
/**
* @typedef {Object} ClientFilesCourseChunk
* @property {"clientFilesCourse"} type
*/
/**
* @typedef {Object} ServerFilesCourseChunk
* @property {"serverFilesCourse"} type
*/
/**
* @typedef {Object} ClientFilesCourseInstanceChunk
* @property {"clientFilesCourseInstance"} type
* @property {string} courseInstanceId
*/
/**
* @typedef {Object} ClientFilesAssessmentChunk
* @property {"clientFilesAssessment"} type
* @property {string} courseInstanceId
* @property {string} assessmentId
*/
/**
* @typedef {Object} QuestionChunk
* @property {"question"} type
* @property {string} questionId
*/
/** @typedef {ElementsChunk | ElementExtensionsChunk | ClientFilesCourseChunk | ServerFilesCourseChunk | ClientFilesCourseInstanceChunk | ClientFilesAssessmentChunk | QuestionChunk} Chunk */
/**
* @typedef {Object} DatabaseChunk
* @property {ChunkType} type
* @property {string} uuid
* @property {string} course_instance_name
* @property {string} assessment_name
* @property {string} question_name
*/
/**
*
* @param {string} courseId
* @param {DatabaseChunk} chunk
*/
const ensureChunk = async (courseId, chunk) => {
const courseChunksDirs = module.exports.getChunksDirectoriesForCourseId(courseId);
const downloadPath = path.join(courseChunksDirs.downloads, `${chunk.uuid}.tar.gz`);
const chunkPath = path.join(courseChunksDirs.chunks, `${chunk.uuid}.tar.gz`);
const unpackPath = path.join(courseChunksDirs.unpacked, chunk.uuid);
let relativeTargetPath;
switch (chunk.type) {
case 'elements':
case 'elementExtensions':
case 'serverFilesCourse':
case 'clientFilesCourse':
relativeTargetPath = chunk.type;
break;
case 'clientFilesCourseInstance':
relativeTargetPath = path.join(
'courseInstances',
chunk.course_instance_name,
'clientFilesCourseInstance',
);
break;
case 'clientFilesAssessment':
relativeTargetPath = path.join(
'courseInstances',
chunk.course_instance_name,
'assessments',
chunk.assessment_name,
'clientFilesAssessment',
);
break;
case 'question':
relativeTargetPath = path.join('questions', chunk.question_name);
break;
default:
throw new Error(`unknown type for chunk=${JSON.stringify(chunk)}`);
}
const targetPath = path.join(courseChunksDirs.course, relativeTargetPath);
const relativeUnpackPath = path.relative(path.dirname(targetPath), unpackPath);
// We have a chunk installed if we have a symlink targetPath -> relativeUnpackPath
let chunkExists = false;
try {
const linkString = await fs.readlink(targetPath);
if (linkString == relativeUnpackPath) {
chunkExists = true;
}
} catch (err) {
// Allow ENOENT errors to continue, because they mean we don't have the chunk
if (err.code != 'ENOENT') throw err;
}
if (chunkExists) {
// If we have the correct link then this chunk is unpacked and
// installed. We're good to go!
return;
}
// Otherwise, we need to download and untar the chunk. We'll download it
// to the "downloads" path first, then rename it to the "chunks" path.
const params = {
Bucket: globalConfig.chunksS3Bucket,
Key: `${chunk.uuid}.tar.gz`,
};
await fs.ensureDir(path.dirname(downloadPath));
const s3 = new AWS.S3();
await new Promise((resolve, reject) => {
s3.getObject(params)
.createReadStream()
.on('error', err => {logger.error(`Could not download chunk ${chunk.uuid}: ${err}`); reject(err);})
.on('end', () => resolve())
.pipe(fs.createWriteStream(downloadPath));
});
await fs.move(downloadPath, chunkPath, {overwrite: true});
// Once the chunk has been downloaded, we need to untar it. In
// case we had an earlier unpack attempt, we will remove the
// existing unpack directory to ensure a clean slate.
await fs.remove(unpackPath);
await fs.ensureDir(unpackPath);
await tar.extract({
file: chunkPath,
cwd: unpackPath,
});
// Finally, link targetPath -> relativeUnpackPath
// Note that ensureSymlink() won't overwrite an existing targetPath
// See:
// https://github.com/jprichardson/node-fs-extra/pull/869
// https://github.com/jprichardson/node-fs-extra/issues/786
// https://github.com/jprichardson/node-fs-extra/pull/826
// As a work-around, we symlink a temporary name and move it over targetPath
const tmpPath = `${targetPath}-${chunk.uuid}`;
await fs.ensureSymlink(relativeUnpackPath, tmpPath);
await fs.rename(tmpPath, targetPath);
};
/** @type {Map<string, Promise>} */
const pendingChunksMap = new Map();
/**
* Ensures that specific chunks for a course are loaded. These chunks will either be pulled
* from S3 if they do not exist, or the existing on-disk chunks will be used if they are
* still the latest version.
*
* @param {string} courseId
* @param {Chunk | Chunk[]} chunks to load. This can either be a single chunk or an array of chunks.
*/
module.exports.ensureChunksForCourseAsync = async (courseId, chunks) => {
if (!globalConfig.chunksConsumer) {
/* We only need to worry if we are a chunk consumer server */
return;
}
if (!Array.isArray(chunks)) {
chunks = [chunks];
}
// First, query the database to identify the UUID + associated name(s) of each desired chunk
// "Names" in this case referrs to question/course instance/assessment names.
const dbChunks = await sqlDb.queryAsync(sql.select_metadata_for_chunks, {
course_id: courseId,
chunks_arr: JSON.stringify(chunks),
});
// Now, ensure each individual chunk is loaded and untarred to the correct
// place on disk.
await async.eachLimit(dbChunks.rows, globalConfig.chunksMaxParallelDownload, async (chunk) => {
const pendingChunkKey = `${courseId}-${chunk.uuid}`;
const pendingChunkPromise = pendingChunksMap.get(pendingChunkKey);
if (pendingChunkPromise) {
// If this chunk is already being loaded, reuse the existing promise
return pendingChunkPromise;
}
const chunkPromise = ensureChunk(courseId, chunk);
pendingChunksMap.set(pendingChunkKey, chunkPromise);
return chunkPromise;
});
};
module.exports.ensureChunksForCourse = util.callbackify(module.exports.ensureChunksForCourseAsync);