Skip to content

Commit

Permalink
getNextAttachmentDownloadJobs: Delete malformed jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal authored and indutny-signal committed Sep 7, 2022
1 parent 55a5c51 commit 192c85b
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions ts/sql/Server.ts
Expand Up @@ -26,6 +26,7 @@ import {
pick,
} from 'lodash';

import * as Errors from '../types/errors';
import { ReadStatus } from '../messages/MessageReadStatus';
import type { GroupV2MemberType } from '../model-types.d';
import type { ReactionType } from '../types/Reactions';
Expand Down Expand Up @@ -3405,10 +3406,10 @@ async function getNextAttachmentDownloadJobs(
const timestamp =
options && options.timestamp ? options.timestamp : Date.now();

const rows: JSONRows = db
const rows: Array<{ json: string; id: string }> = db
.prepare<Query>(
`
SELECT json
SELECT id, json
FROM attachment_downloads
WHERE pending = 0 AND timestamp <= $timestamp
ORDER BY timestamp DESC
Expand All @@ -3420,7 +3421,27 @@ async function getNextAttachmentDownloadJobs(
timestamp,
});

return rows.map(row => jsonToObject(row.json));
const INNER_ERROR = 'jsonToObject error';
try {
return rows.map(row => {
try {
return jsonToObject(row.json);
} catch (error) {
logger.error(
`getNextAttachmentDownloadJobs: Error with job '${row.id}', deleting. ` +
`JSON: '${row.json}' ` +
`Error: ${Errors.toLogFormat(error)}`
);
removeAttachmentDownloadJobSync(row.id);
throw new Error(INNER_ERROR);
}
});
} catch (error) {
if ('message' in error && error.message === INNER_ERROR) {
return getNextAttachmentDownloadJobs(limit, { timestamp });
}
throw error;
}
}
async function saveAttachmentDownloadJob(
job: AttachmentDownloadJobType
Expand Down Expand Up @@ -3480,9 +3501,12 @@ async function resetAttachmentDownloadPending(): Promise<void> {
`
).run();
}
async function removeAttachmentDownloadJob(id: string): Promise<void> {
function removeAttachmentDownloadJobSync(id: string): void {
return removeById(getInstance(), ATTACHMENT_DOWNLOADS_TABLE, id);
}
async function removeAttachmentDownloadJob(id: string): Promise<void> {
return removeAttachmentDownloadJobSync(id);
}
async function removeAllAttachmentDownloadJobs(): Promise<void> {
return removeAllFromTable(getInstance(), ATTACHMENT_DOWNLOADS_TABLE);
}
Expand Down

0 comments on commit 192c85b

Please sign in to comment.