Skip to content

Commit

Permalink
improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NiloCK committed Dec 27, 2024
1 parent 80ea8a1 commit 224ac11
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 55 deletions.
3 changes: 2 additions & 1 deletion packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"@types/ffmpeg-static": "^2.0.0",
"@types/hashids": "^1.0.30",
"@types/jest": "^29.5.3",
"@types/node": "^10.12.18",
"@types/morgan": "^1.9.9",
"@types/node": "16",
"@types/pouchdb": "^6.3.2",
"babel-jest": "^29.6.1",
"eslint": "^7.18.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/express/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import console from 'console';
import morgan from 'morgan';
import logger from './logger';

process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

logger.info(`Express app running version: ${ENV.VERSION}`);

const port = 3000;
Expand Down
112 changes: 62 additions & 50 deletions packages/express/src/attachment-preprocessing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,86 @@ const Q = new AsyncProcessQueue<AttachmentProcessingRequest, Result>(processDocA
* @param courseID
*/
export function postProcessCourse(courseID: string): void {
logger.info(`Following course ${courseID}`);

const crsString = `coursedb-${courseID}`;

CouchDB.db.follow(
crsString,
{
feed: 'continuous',
db: crsString,
include_docs: false,
},
filterFactory(courseID)
);
try {
logger.info(`Following course ${courseID}`);

const crsString = `coursedb-${courseID}`;

CouchDB.db.follow(
crsString,
{
feed: 'continuous',
db: crsString,
include_docs: false,
},
filterFactory(courseID)
);
} catch (e) {
logger.error(`Error in postProcessCourse: ${e}`);
}
}

/**
* Connect to CouchDB, monitor changes to uploaded card data,
* perform post-processing on uploaded media
*/
export default async function postProcess(): Promise<void> {
logger.info(`Following all course databases for changes...`);
const lookupDB = await useOrCreateDB(COURSE_DB_LOOKUP);
const courses = await lookupDB.list({
include_docs: true,
});
try {
logger.info(`Following all course databases for changes...`);
const lookupDB = await useOrCreateDB(COURSE_DB_LOOKUP);
const courses = await lookupDB.list({
include_docs: true,
});

for (const course of courses.rows) {
postProcessCourse(course.id);
for (const course of courses.rows) {
postProcessCourse(course.id);
}
} catch (e) {
logger.error(`Error in postProcess: ${e}`);
}
}

function filterFactory(courseID: string) {
const courseDatabase = CouchDB.use(`coursedb-${courseID}`);

return async function filterChanges(error, changeItem: nano.DatabaseChangesResultItem) {
const docNoAttachments = await courseDatabase.get(changeItem.id, {
attachments: false,
});

if (
docNoAttachments._attachments &&
Object.keys(docNoAttachments._attachments).length > 0 &&
(docNoAttachments['processed'] === undefined || docNoAttachments['processed'] === false)
) {
const doc = await courseDatabase.get(changeItem.id, {
attachments: true,
try {
const docNoAttachments = await courseDatabase.get(changeItem.id, {
attachments: false,
});
const processingRequest: AttachmentProcessingRequest = {
courseID,
docID: doc._id,
fields: [],
};
const atts = doc._attachments;
for (const attachment in atts) {
const content_type: string = atts[attachment]['content_type'];
logger.info(`Course: ${courseID}`);
logger.info(`\tAttachment ${attachment} in:`);
logger.info(`\t${doc._id}`);
logger.info(` should be processed...`);

if (content_type.includes('audio')) {
processingRequest.fields.push({
name: attachment,
mimetype: content_type,
});

if (
docNoAttachments._attachments &&
Object.keys(docNoAttachments._attachments).length > 0 &&
(docNoAttachments['processed'] === undefined || docNoAttachments['processed'] === false)
) {
const doc = await courseDatabase.get(changeItem.id, {
attachments: true,
});
const processingRequest: AttachmentProcessingRequest = {
courseID,
docID: doc._id,
fields: [],
};
const atts = doc._attachments;
for (const attachment in atts) {
const content_type: string = atts[attachment]['content_type'];
logger.info(`Course: ${courseID}`);
logger.info(`\tAttachment ${attachment} in:`);
logger.info(`\t${doc._id}`);
logger.info(` should be processed...`);

if (content_type.includes('audio')) {
processingRequest.fields.push({
name: attachment,
mimetype: content_type,
});
}
}
Q.addRequest(processingRequest);
}
Q.addRequest(processingRequest);
} catch (e) {
logger.error(`Error processing doc ${changeItem.id}: ${e}`);
}
};
}
Expand Down
15 changes: 11 additions & 4 deletions packages/express/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,13 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==

"@types/morgan@^1.9.9":
version "1.9.9"
resolved "https://registry.yarnpkg.com/@types/morgan/-/morgan-1.9.9.tgz#d60dec3979e16c203a000159daa07d3fb7270d7f"
integrity sha512-iRYSDKVaC6FkGSpEVVIvrRGw0DfJMiQzIn3qr2G5B3C//AWkulhXgaBd7tS9/J79GWSYMTHGs7PfI5b3Y8m+RQ==
dependencies:
"@types/node" "*"

"@types/ms@*":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
Expand All @@ -1526,10 +1533,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.4.3.tgz#ca83a5a006e0d5709a3fe8966112f26436071d81"
integrity sha512-Yu3+r4Mn/iY6Mf0aihncZQ1qOjOUrCiodbHHY1hds5O+7BbKp9t+Li7zLO13zO8j9L2C6euz8xsYQP0rjGvVXw==

"@types/node@^10.12.18":
version "10.17.60"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.60.tgz#35f3d6213daed95da7f0f73e75bcc6980e90597b"
integrity sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw==
"@types/node@16":
version "16.18.122"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.122.tgz#54948ddbe2ddef8144ee16b37f160e3f99c32397"
integrity sha512-rF6rUBS80n4oK16EW8nE75U+9fw0SSUgoPtWSvHhPXdT7itbvmS7UjB/jyM8i3AkvI6yeSM5qCwo+xN0npGDHg==

"@types/pouchdb-adapter-cordova-sqlite@*":
version "1.0.1"
Expand Down

0 comments on commit 224ac11

Please sign in to comment.