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.
-- 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.
$ npm install
# 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.
-
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
-
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
- 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
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
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.
- Author - Bryan Ramirez
- Framework Website - https://nestjs.com
Nest framework and the code of this exercise are MIT licensed.