node-thread-decorator
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Node Thread Decorator

node version

Install

 npm i -S node-thread-decorator

Usage

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

mikemajesty

License

It is available under the MIT license. License

Readme

Keywords

none

Package Sidebar

Install

npm i node-thread-decorator

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

40.6 kB

Total Files

13

Last publish

Collaborators

  • mikemajesty