File

shared/common/aws-service.ts

Index

Properties
Methods

Constructor

constructor(debugLogsAllowed)

Constructor

@param debugLogAllower {boolean = false} - enable or disable additional logs

Parameters :
Name Optional
debugLogsAllowed No

Properties

Private Readonly debugLogsAllowed
Type : boolean
Default value : true

flag for additional debug logs

Private Readonly logger
Default value : new Logger(AwsService.name)

logger

Methods

allowCronRun
allowCronRun(forceCronRun)

Checks if cron run is allowed

@param forceCronRun - allow some methods to still be run on non cron running environments.

@return {boolean} - instance is lead

Parameters :
Name Optional Default value Description
forceCronRun No false
  • allow some methods to still be run on non cron running environments.
Returns : boolean
  • instance is lead
allowLeadRoutines
allowLeadRoutines()

Checks if lead tasks are allowed

@return {boolean} - instance is lead

Returns : boolean
  • instance is lead
Private isCurrentServerTheLeadInstance
isCurrentServerTheLeadInstance()

Checks if current instance (pod with one instance) is lead (can do cron/gameSave tasks)

@return {boolean} - is instance lead

Returns : boolean
  • is instance lead
import { Logger } from '@nestjs/common';

/**
 *  Method to check if environment configured to run cron tasks (env.RUN_CRON !== 'no')
 *
 *  @return {boolean}
 */
export const isCronRunningEnvironment = (): boolean => {
  if (!!process.env.WEBSOCKET_NO_REDIS) return true
  return process.env.RUN_CRON !== 'no';
};

export class AwsService {
  /** flag for additional debug logs */
  private readonly debugLogsAllowed: boolean = true;
  /** logger */
  private readonly logger = new Logger(AwsService.name);

  /**
   *  Constructor
   *
   *  @param debugLogAllower {boolean = false} - enable or disable additional logs
   */
  constructor(debugLogsAllowed = false) {
    this.debugLogsAllowed = debugLogsAllowed;
  }

  /**
   *  Checks if current instance (pod with one instance) is lead (can do cron/gameSave tasks)
   *
   *  @return {boolean} - is instance lead
   */
  private isCurrentServerTheLeadInstance(): boolean {
    if (process.env.NODE_ENV === 'development') return true;
    if (process.env.IS_LEAD_SERVER === 'yes') return true;
    if (!!process.env.WEBSOCKET_NO_REDIS) return true
    return false;
  }

  /**
   *  Checks if cron run is allowed
   *
   *  @param forceCronRun - allow some methods to still be run on non cron running environments.
   *
   *  @return {boolean} - instance is lead
   */
  allowCronRun(forceCronRun = false) {
    if (process.env.NODE_ENV === 'development') {
      return true;
    }

    if (!forceCronRun && isCronRunningEnvironment() === false) {
      return false;
    }

    // Check it everytime because the list of servers changes constantly,
    // eg the previous lead instance might be killed and now this server
    // is the new lead.
    return this.isCurrentServerTheLeadInstance();
  }

  /**
   *  Checks if lead tasks are allowed
   *
   *  @return {boolean} - instance is lead
   */
  allowLeadRoutines(): boolean {
    if (process.env.NODE_ENV === 'development') return true;
    if (!!process.env.WEBSOCKET_NO_REDIS) return true
    return this.isCurrentServerTheLeadInstance();
  }
}

results matching ""

    No results matching ""