npm i -S node-thread-decorator
- Import
import { RunInNewThread } from "node-thread-decorator";
- Usage
import { Controller, Get } from '@nestjs/common';
import { RunInNewThread } from 'node-thread-decorator';
@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 `@RunInNewThread`.
*
* @param {number} milliseconds - The time to block the thread in milliseconds.
*/
@RunInNewThread()
blockMainThread(milliseconds: number) {
const start = Date.now();
while (Date.now() - start < milliseconds) {
// Looping until time has passed
}
}
/**
* 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> {
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 Thread Decorator. Thanks for your contributions!
It is available under the MIT license. License