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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { UserRepository } from '@logchain/repositories';
import { UserService } from '@logchain/services';
import { service } from '@loopback/core';
import { CronJob, cronJob } from '@loopback/cron';
import { repository } from '@loopback/repository';
import moment from 'moment';
@cronJob()
export class ProcessDeletedAccountsCronJob extends CronJob {
private readonly lock_key = '1726642024';
constructor(
@repository(UserRepository)
public userRepository: UserRepository,
@service(UserService)
public userService: UserService,
) {
super({
name: 'Process deleted accounts cronjob',
onTick: async () => {
await this.performCronJob();
},
cronTime: `* * * * *`, // Run every minute
start: true,
});
}
async performCronJob() {
const isUnLocked = await this.userRepository.execute(`SELECT pg_try_advisory_lock(${this.lock_key});`);
if (!isUnLocked[0].pg_try_advisory_lock) {
return;
}
await this.userRepository.execute(`SELECT pg_advisory_lock(${this.lock_key});`);
const now = moment();
const startAt = now.toDate();
console.log(`*** Cronjob: ${this.name} started at ${startAt}`);
// Your code start here
// Start call service from here
await this.userService.processDeletedAccounts();
// ------------------------------
// Your code end here
const finishedAt = new Date();
console.log(`*** Cronjob: ${this.name} finished at ${finishedAt}.`);
await this.userRepository.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
}
}
|