diff --git a/api/next-shim.js b/api/next-shim.js index bf536c50e..d60ba8654 100644 --- a/api/next-shim.js +++ b/api/next-shim.js @@ -42,7 +42,7 @@ AV.Cloud.define('weeklyPushStatsToSlack', () => weeklyPushStatsToSlack()) AV.Cloud.define('monthlyPushStatsToSlack', () => monthlyPushStatsToSlack()) const { cancelTicketExportJob, getTicketExportJobInfo } = require('../next/api/dist/ticket/export') -AV.Cloud.define('cancelExportJob', (req) => { +AV.Cloud.define('cancelTicketExportJob', (req) => { if (!req.params.jobId) { console.error('Cloud Function - removeExportJob: missing jobId') return diff --git a/next/api/src/ticket/export/index.ts b/next/api/src/ticket/export/index.ts index 767b0334b..1ec18b6c9 100644 --- a/next/api/src/ticket/export/index.ts +++ b/next/api/src/ticket/export/index.ts @@ -1,7 +1,7 @@ -import { createQueue } from '@/queue'; -import { SortItem } from '@/middleware'; -import { ExportTicketTask } from '@/model/ExportTicketTask'; -import exportTicket, { FilterOptions } from './ExportTicket'; +import { createQueue } from '@/queue' +import { SortItem } from '@/middleware' +import { ExportTicketTask } from '@/model/ExportTicketTask' +import exportTicket, { FilterOptions } from './ExportTicket' export interface JobData { params: FilterOptions; @@ -15,68 +15,81 @@ export interface JobData { const queue = createQueue('ticket:exported', { limiter: { max: 1, - duration: 1000, + duration: 1000 }, defaultJobOptions: { removeOnComplete: true, - removeOnFail: true, - }, -}); + removeOnFail: true + } +}) queue.process(async (job, done) => { try { - const result = await exportTicket(job.data); - done(null, result); + const result = await exportTicket(job.data) + done(null, result) } catch (error) { - done(error as Error); + done(error as Error) } -}); +}) queue.on('completed', async (job, result) => { - const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }); + const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }) if (task) { await task.update( { downloadUrl: result.url, ticketCount: result.ticketCount, status: 'complete', - completedAt: new Date(), + completedAt: new Date() }, { useMasterKey: true } - ); + ) } -}); +}) queue.on('failed', async (job, err) => { - console.error('[export ticket]:', job.data, err); - const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }); + console.error('[export ticket]:', job.data, err) + const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }) if (task) { await task.update( { - status: 'failed', + status: 'failed' }, { useMasterKey: true } - ); + ) } -}); +}) export async function createTicketExportJob(jobData: Omit) { const task = await ExportTicketTask.create( { ACL: {}, operatorId: jobData.userId, - status: 'processing', + status: 'processing' }, { useMasterKey: true } - ); + ) await queue.add({ ...jobData, date: new Date().toISOString(), - taskId: task.id, - }); + taskId: task.id + }) } export async function cancelTicketExportJob(jobId: string) { + if (jobId === 'all') { + const jobs = await queue.getJobs(['waiting', 'active', 'delayed']) + for (const job of jobs) { + await cancelJob(job.id) + } + + return + } + + await cancelJob(jobId) +} + +async function cancelJob(jobId: string | number) { const job = await queue.getJob(jobId) if (!job) { console.log(`cancelTicketExportJob: job ${jobId} not found`) @@ -86,17 +99,18 @@ export async function cancelTicketExportJob(jobId: string) { console.log(`cancelTicketExportJob: canceling job ${jobId}`) await job.remove() - const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }); + const task = await ExportTicketTask.find(job.data.taskId, { useMasterKey: true }) if (task) { await task.update( { - status: 'canceled', + status: 'canceled' }, { useMasterKey: true } - ); + ) } } + export async function getTicketExportJobInfo() { const jobs = await queue.getJobs(['waiting', 'active', 'delayed', 'completed', 'failed']) console.log('getTicketExportJobInfo')