All files / src/cronjobs precompute-data-for-report-master.cronjob.ts

52% Statements 13/25
64.29% Branches 9/14
33.33% Functions 1/3
47.83% Lines 11/23

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 511x 1x 1x 1x 1x 1x     1x 1x     1x   1x   1x                                                                    
import { UserRepository as FlowRepository } from '@logchain/repositories';
import { FlowJourneyService } 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 PrecomputeDataForReportMasterCronJob extends CronJob {
  private readonly lock_key = '1721014491';
  constructor(
    @repository(FlowRepository)
    public flowRepository: FlowRepository,
    @service(FlowJourneyService)
    public flowJourneyService: FlowJourneyService,
  ) {
    super({
      name: 'Precompute data for report master cronjob',
      onTick: async () => {
        await this.performCronJob();
      },
      cronTime: `*/${+(process?.env?.PRECOMPUTE_PENDING_HOURS ?? 5)} * * * *`, // Run every 5 minutes
      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_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.flowJourneyService.precomputeReportMaster();
    // ------------------------------
 
    // Your code end here
    const finishedAt = new Date();
    console.log(`*** Cronjob: ${this.name} finished at ${finishedAt}.`);
 
    await this.flowRepository.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
  }
}