File

tbl/src/game-tbl/schedule.service.ts

Description

Schedule service responsible for scheduled tasks - currently it's only game time syncronization of game gateway

Index

Properties
Methods

Constructor

constructor(gameGateway: GameGateway)

constructor

Parameters :
Name Type Optional
gameGateway GameGateway No

Methods

Async checkAwsLead
checkAwsLead()
Decorators :
@Interval(5000)
@CatchAndLogError(LiveScheduleService.name)

Check if is aws lead once in 5 sec

Returns : Promise<void>
Async sendAwsMetrics
sendAwsMetrics()
Decorators :
@Interval(60000)
@CatchAndLogError(LiveScheduleService.name)

Send custom metrics once in 1 min (1000 * 60)

Returns : Promise<void>
syncTimes
syncTimes()
Decorators :
@Interval(1000)
@CatchAndLogError(LiveScheduleService.name)

Send timerSync event to /live socket.io namespace

Returns : void

Properties

Private awsInstanceId
Type : string
Default value : ''
Private awsIsLead
Default value : false
Private Readonly logger
Default value : new Logger(LiveScheduleService.name)

logger

import { Injectable, Logger } from '@nestjs/common';
import { Interval } from '@nestjs/schedule';
import { GameGateway } from './game-tbl.gateway';
import { AwsService } from '../../../shared/common/aws-service';
import { CatchAndLogError } from '../../../shared/common/catch-and-log-error.decorator';

/**
 *  Schedule service responsible for scheduled tasks - currently it's only game time syncronization of game gateway
 */
@Injectable()
export class LiveScheduleService {
  /** logger */
  private readonly logger = new Logger(LiveScheduleService.name);
  private awsInstanceId = '';
  private awsIsLead = false;

  /**
   *  constructor
   */
  constructor(private readonly gameGateway: GameGateway) {}

  /**
   *  Send timerSync event to /live socket.io namespace
   */
  @Interval(1000)
  @CatchAndLogError(LiveScheduleService.name)
  syncTimes(): void {
    if (!this.awsIsLead) {
      this.logger.log('Not a lead instance');
      return;
    }
    this.gameGateway.syncGameTimer();
  }

  /**
   *  Check if is aws lead once in 5 sec
   */
  @Interval(5000)
  @CatchAndLogError(LiveScheduleService.name)
  async checkAwsLead(): Promise<void> {
    const aws = new AwsService();
    this.awsIsLead = await aws.allowLeadRoutines();
    if (this.awsInstanceId === '') {
      this.awsInstanceId = await aws.getRunningInstanceId();
    }
  }

  /**
   *  Send custom metrics once in 1 min (1000 * 60)
   */
  @Interval(60000)
  @CatchAndLogError(LiveScheduleService.name)
  async sendAwsMetrics(): Promise<void> {
    const aws = new AwsService();
    const metrics = this.gameGateway.collectAwsMetrics();
    metrics.instanceId = this.awsInstanceId;
    await aws.publishMetrics(metrics);
  }
}

results matching ""

    No results matching ""