npm i -S node-decorator-child-process
- Import
import { RunInNewProcess } from "node-decorator-child-process";
- Usage
import { Controller, Get } from '@nestjs/common';
import { RunInNewProcess } from 'node-decorator-child-process';
@Controller()
export class HealthController {
/**
* Blocks the main thread for a specified duration in milliseconds.
* This function runs in a new process due to the decorator `@RunInNewProcess`.
*
* @param {number} milliseconds - The time to block the thread in milliseconds.
*/
@RunInNewProcess()
blockMainThread(milliseconds: number) {
const start = Date.now();
while (Date.now() - start < milliseconds) {
// Looping until time has passed
}
console.log("Finished new process", process.pid);
}
/**
* Endpoint that returns health information of the service.
* It will block the main thread for 10 seconds to simulate a heavy operation.
*
* @returns {Promise<string>} - A promise that resolves when the health check is completed.
*/
@Get(['/health', '/'])
async getHealth(): Promise<string> {
console.log("Started process", process.pid);
this.blockMainThread(10000); // Simulating blocking the thread for 10 seconds
console.log("Health check completed");
return "APP UP!"
}
}
The following is a list of all the people that have contributed Node Process Decorator. Thanks for your contributions!
It is available under the MIT license. License