Skip to content

Commit

Permalink
feat: allows to cancel all jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
uffy committed May 31, 2024
1 parent 915009a commit 41c6e76
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion api/next-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 42 additions & 28 deletions next/api/src/ticket/export/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,68 +15,81 @@ export interface JobData {
const queue = createQueue<JobData>('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<JobData, 'date' | 'taskId'>) {
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`)
Expand All @@ -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')
Expand Down

0 comments on commit 41c6e76

Please sign in to comment.