Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution context vs declare Global namespace in a CRON job proccess #544

Closed
cdiaz opened this issue Apr 5, 2018 · 3 comments
Closed

Execution context vs declare Global namespace in a CRON job proccess #544

cdiaz opened this issue Apr 5, 2018 · 3 comments

Comments

@cdiaz
Copy link

cdiaz commented Apr 5, 2018

I have a process that runs repetitively on a cron Job using Bull

This process is outside from my Nest application context, but I need to call methods of a component of my nest application.

I am using NestFactory.createApplicationContext () to obtain the instance of my component, on follow mode:

import { NestFactory } from '@nestjs/core';
import { BtcPriceModule } from '../../btcprice/btcprice.module';
import { BtcPriceService } from '../../btcprice/btcprice.service';

const BtcPriceProcessor = async (job) => {
  
  const context = await NestFactory.createApplicationContext(BtcPriceModule);
  const btcPriceService = context.get(BtcPriceService);

  return await btcPriceService.fetch(job.data.currency)
    .then(data=> Promise.resolve(data))
    .catch(err=> Promise.reject(err))
}

export { BtcPriceProcessor } 

But this causes that during each execution of the process (every minute) NestFactory and InstanceLoader are initialized

alt text

Another alternative that works for me is adding the app to global interface in the following way:

//global.d.ts
declare namespace NodeJS {
  interface Global {
    app: any;
  }
}
//main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  global.app = app;
  await app.listen(3000);
}
bootstrap();
import { BtcPriceModule } from '../../btcprice/btcprice.module';
import { BtcPriceService } from '../../btcprice/btcprice.service';

const BtcPriceProcessor = async (job) => {
  
  const btcPriceService = global.app.select(BtcPriceModule).get(BtcPriceService);
  return await btcPriceService.fetch(job.data.currency)
    .then(data=> Promise.resolve(data))
    .catch(err=> Promise.reject(err))
}

export { BtcPriceProcessor }

I would like to know what implications have in terms of performance and what is the best practice to do so?

The reproducible example is in this repository

@fwoelffel
Copy link
Contributor

fwoelffel commented Apr 17, 2018

I think you could also define a nest.instance.ts file with the following content:

// nest.instance.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
let instance = null;
export const getNestInstance = async () => {
    if (!instance) {
        instance = await NestFactory.create(AppModule);
    }
    return instance;
}

Then in you main.ts and every file where the Nest instance is needed:

// main.ts
import { getNestInstance } from './nest.instance.ts';
async function bootstrap() {
  const app = await getNestInstance();
  await app.listen(3000);
}
bootstrap();

This way, you make use of the Node.js caching mechanism and avoid the global variable definition (which is a bad practice).

PS: I haven't tested this code. Let me know if there's anything wrong with it.
PS2: It'd be really nice from @kamilmysliwiec to give us his opinion about this 🙂

@kamilmysliwiec
Copy link
Member

Either is fine. However, a solution without global variable seems to be a bit cleaner here. 🙂

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants