Skip to content

mikemajesty/node-decorator-child-process

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node Process Decorator

node version

Install

 npm i -S node-decorator-child-process

Usage

  • 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!

mikemajesty

License

It is available under the MIT license. License

Releases

No releases published

Packages

No packages published