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 66 67 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { UserProfile } from '@logchain/components/aws/services';
import { BaseEntity } from '@logchain/models';
import { FlowRepository } from '@logchain/repositories';
import { BaseService, FlowService } 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 UpdateFlowStatusAfterPendingTimeCronJob extends CronJob {
private readonly lock_key = '1709621709';
constructor(
@repository(FlowRepository)
public flowRepository: FlowRepository,
@service(FlowService)
public flowService: FlowService,
) {
super({
name: 'Update flow status after pending time cronjob',
onTick: async () => {
await this.performCronJob();
},
cronTime: '* * * * *', // Run every minute
start: true,
});
}
async performCronJob() {
const isUnLocked = await this.flowRepository.execute(`SELECT pg_try_advisory_lock(${this.lock_key});`);
if (!isUnLocked[0].pg_try_advisory_lock) {
return;
}
await this.flowRepository.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
const now = moment();
const startAt = now.toDate();
console.log(`*** Cronjob: ${this.name} started at ${startAt}`);
// Your code start here
const pendingFlows = await this.flowRepository.getPendingXMLFlows();
if (pendingFlows?.length) {
for(const pendingFlow of pendingFlows) {
const userProfile = { id: pendingFlow.created_by ?? 0, company_id: pendingFlow.company_id ?? 0 } as UserProfile;
this._bindUserProfile(this.flowService as BaseService<BaseEntity>, userProfile);
this.flowService.resetFilterBuilder();
await this.flowService.activateFlowById(pendingFlow.id, { forExternal: true });
}
}
// --------------------
const finishedAt = new Date();
console.log(`*** Cronjob: ${this.name} finished at ${finishedAt}. Total in ${(finishedAt.getTime() - startAt.getTime()) / 1000}ms`);
await this.flowRepository.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
}
/**
* Private function use to bind requesting company to global current_user
* @param {BaseService<BaseEntity>} serviceObj
* @param {UserProfile} profile
*/
private _bindUserProfile(serviceObj: BaseService<BaseEntity>, profile: UserProfile) {
if (!serviceObj.current_user) {
serviceObj.current_user = profile;
}
}
} |