This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/server/logistics.js
Tom Moor 5b78cb8963 perf: Reuse redis connections where possible (#1157)
* reuse redis connections where possible

* redis -> ioredis
2020-01-13 18:17:41 -08:00

61 lines
1.3 KiB
JavaScript

// @flow
import debug from 'debug';
import mailer from './mailer';
import { Collection, Team } from './models';
import { archiveCollections } from './utils/zip';
import { createQueue } from './utils/queue';
const log = debug('logistics');
const logisticsQueue = createQueue('logistics');
const queueOptions = {
attempts: 2,
removeOnComplete: true,
backoff: {
type: 'exponential',
delay: 60 * 1000,
},
};
async function exportAndEmailCollections(teamId: string, email: string) {
log('Archiving team', teamId);
const team = await Team.findByPk(teamId);
const collections = await Collection.findAll({
where: { teamId },
order: [['name', 'ASC']],
});
const filePath = await archiveCollections(collections);
log('Archive path', filePath);
mailer.export({
to: email,
attachments: [
{
filename: `${team.name} Export.zip`,
path: filePath,
},
],
});
}
logisticsQueue.process(async job => {
log('Process', job.data);
switch (job.data.type) {
case 'export-collections':
return await exportAndEmailCollections(job.data.teamId, job.data.email);
default:
}
});
export const exportCollections = (teamId: string, email: string) => {
logisticsQueue.add(
{
type: 'export-collections',
teamId,
email,
},
queueOptions
);
};