Nest: Execution context vs declare Global namespace in a CRON job proccess

Created on 5 Apr 2018  路  3Comments  路  Source: nestjs/nest

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();

```js
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

question 馃檶

Most helpful comment

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

All 3 comments

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 :slightly_smiling_face:

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

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.

Was this page helpful?
0 / 5 - 0 ratings