Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Status } from '@logchain/models';
import { CompanyRegulatoryHistoriesRepository, CompanyRegulatoryRepository } from '@logchain/repositories';
import { CronJob, cronJob } from '@loopback/cron';
import { repository } from '@loopback/repository';
import moment from 'moment';
@cronJob()
export class ExpireRegulatoryFilingCronJob extends CronJob {
private readonly lock_key = '1701060607';
constructor(
@repository(CompanyRegulatoryRepository)
public companyRegulatoryRepo: CompanyRegulatoryRepository,
@repository(CompanyRegulatoryHistoriesRepository)
public companyRegulatoryHistoriesRepo: CompanyRegulatoryHistoriesRepository,
) {
super({
name: 'Expire regulatory filing',
onTick: async () => {
await this.performCronJob();
},
cronTime: '0 0 23 * * *', // Daily at midnight
start: true,
});
}
async performCronJob() {
const isUnLocked = await this.companyRegulatoryRepo.execute(`SELECT pg_try_advisory_lock(${this.lock_key});`);
if (!isUnLocked[0].pg_try_advisory_lock) {
return;
}
await this.companyRegulatoryRepo.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
const now = moment();
const startAt = now.toDate();
const endTimeOfYesterday = now.subtract(1, 'day').endOf('day').toDate();
console.log(`*** Cronjob: ${this.name} started at ${startAt}`);
const expiredCompanyRegulatory = await this.companyRegulatoryRepo.find({
where: {
status: Status.CompanyRegulatory.Accepted,
duration_end: {
lte: endTimeOfYesterday
}
}
});
if (expiredCompanyRegulatory?.length) {
const expiredCompanyRegulatoryIds = expiredCompanyRegulatory.map(({ id }) => id);
await this.companyRegulatoryRepo.updateAll({
status: Status.CompanyRegulatory.Expired,
}, {
id: { inq: expiredCompanyRegulatoryIds }
});
await this.companyRegulatoryHistoriesRepo.createAll(expiredCompanyRegulatory.map((item) => ({
company_id: item.company_id,
company_regulatory_id: item.id,
history_info: item,
comment: "Regulatory subscription expired"
})));
}
const finishedAt = new Date();
console.log(`*** Cronjob: ${this.name} finished at ${finishedAt}. Update to Expired total ${expiredCompanyRegulatory.length} regulatory filing in ${(finishedAt.getTime() - startAt.getTime()) / 1000}ms`);
await this.companyRegulatoryRepo.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
}
}
|