feat: Auto remove cancelled subscriptions

This commit is contained in:
Faruk AYDIN
2023-04-11 23:19:11 +02:00
parent 596be24d92
commit d1344457dd
6 changed files with 89 additions and 6 deletions

View File

@@ -0,0 +1,46 @@
import { Worker } from 'bullmq';
import { DateTime } from 'luxon';
import * as Sentry from '../helpers/sentry.ee';
import redisConfig from '../config/redis';
import logger from '../helpers/logger';
import Subscription from '../models/subscription.ee';
export const worker = new Worker(
'remove-cancelled-subscriptions',
async () => {
await Subscription.query()
.delete()
.where({
status: 'cancelled',
})
.andWhere(
'cancellation_effective_date',
'<=',
DateTime.now().startOf('day').toISODate()
);
},
{ connection: redisConfig }
);
worker.on('completed', (job) => {
logger.info(
`JOB ID: ${job.id} - The cancelled subscriptions have been removed!`
);
});
worker.on('failed', (job, err) => {
const errorMessage = `
JOB ID: ${job.id} - ERROR: The cancelled subscriptions can not be removed! ${err.message}
\n ${err.stack}
`;
logger.error(errorMessage);
Sentry.captureException(err, {
extra: {
jobId: job.id,
},
});
});
process.on('SIGTERM', async () => {
await worker.close();
});