Giter Site home page Giter Site logo

nestjs-rx-workers-poc's Introduction

Description

It provides a simple example of the implementation of node child_process API as server workers using the observable pattern with RxJS.

You also can use this example as a reference for:

  • To understand how to implement RxJS on server-side. ๐Ÿš€
  • To understand how to create a new node child process and communicate with them using observable pattern.
  • A basic architecture to implement nestJS at enterprise-level applications.

THIS IS A POC. The use of child_process nodejs API It is not advisable to handle asynchrony queues (workers). If you need to run queues in NestJS I recommend the use of nestjs queues implementation for traditional architectures or use a microservices architecture based solution instead.

Project Structure Overview

    -- root
        -- src
            -- modules
                --  app
                    --  app.controller.ts // It contains the main route and service status route.
                    --  app.module.ts // Main application module.
                -- report
                    -- report.controller.ts // it contains the POST /generateMultipleIpReport controller to generate the reports.
                    -- report.module.ts
                    -- report.service.ts // it contains the worker launcher and communication. Also, it provides PROCESS_DELAY and MAX_ATTEMPTS variables configuration.
                    -- report.worker.ts // it contains the code that will be executed in the child node.
            -- main.ts // Application entry point.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Visit http://localhost:3000/api/ to see the Swagger documentation.

Testing multiple child_process feature

  • If you want to change the variables for retry attempts and delay time of workers update the report.service.ts file under the report module.

  • Run the application:

$ npm run start
  • Open the Activity Monitor of your operative system. Screen Shot 2020-10-30 at 12 35 07 PM

  • Run the following cURL instruction to make a POST call to generate a new IP report.

$ curl -d '{"ip": "8.8.8.8","reportServices": ["ipapi", "freegeoip"]}' -H "Content-Type: application/json" -X POST http://localhost:3000/generateMultipleIpReport

Screen Shot 2020-10-30 at 12 36 20 PM

  • Notice that for each report the system will create a new node child process to execute the task and then it will terminate the process.
$ curl -d '{"ip": "8.8.8.8","reportServices": ["ipapi", "freegeoip", "ipapi", "freegeoip", "ipapi", "freegeoip"]}' -H "Content-Type: application/json" -X POST http://localhost:3000/generateMultipleIpReport

Screen Shot 2020-10-30 at 12 39 40 PM

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest framework and the code of this exercise are MIT licensed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.