Simplify attachment jobs SQL query

This commit is contained in:
Fedor Indutny 2021-06-01 10:13:10 -07:00 committed by GitHub
parent fcaa34d67a
commit e4d7e1e9c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -135,11 +135,17 @@ async function _maybeStartJob() {
const jobCount = getActiveJobCount();
const limit = MAX_ATTACHMENT_JOB_PARALLELISM - jobCount;
if (limit <= 0) {
logger.info(
'attachment_downloads/_maybeStartJob: reached active job limit, waiting'
);
return;
}
const nextJobs = await getNextAttachmentDownloadJobs(limit);
if (nextJobs.length <= 0) {
logger.info(
'attachment_downloads/_maybeStartJob: no attachment jobs to run'
);
return;
}
@ -148,10 +154,19 @@ async function _maybeStartJob() {
const secondJobCount = getActiveJobCount();
const needed = MAX_ATTACHMENT_JOB_PARALLELISM - secondJobCount;
if (needed <= 0) {
logger.info(
'attachment_downloads/_maybeStartJob: reached active job limit after ' +
'db query, waiting'
);
return;
}
const jobs = nextJobs.slice(0, Math.min(needed, nextJobs.length));
logger.info(
`attachment_downloads/_maybeStartJob: starting ${jobs.length} jobs`
);
for (let i = 0, max = jobs.length; i < max; i += 1) {
const job = jobs[i];
const existing = _activeAttachmentDownloadJobs[job.id];

View File

@ -4249,7 +4249,7 @@ async function getNextAttachmentDownloadJobs(
`
SELECT json
FROM attachment_downloads
WHERE pending = 0 AND timestamp < $timestamp
WHERE pending = 0 AND timestamp <= $timestamp
ORDER BY timestamp DESC
LIMIT $limit;
`