When it comes to node.js process model, the first thing that comes to mind may be Cluster module, or Master/Worker process model. In the long-term use process, we gradually find that such application development will have limitations sooner or later. In addition to not being elegant enough, Every time a process is added or subtracted or customized, unpredictable problems arise.

In some specific scenarios, we even added an Agent process under the Master to run some middleware SDKS. In the process of tinkering, traditional inter-process communication and data exchange become difficult, and even the communication between workers and agents cannot be carried out elegantly.

In addition to some memory-sharing communication scenarios, Worker processes take up too much time to run some scheduled tasks, resulting in Web interface timeout and other problems (or system capacity reduction). Even worse, the scheduled tasks are put into the Agent to execute, directly dragging the underlying middleware services, causing the application to crash.

It looks like this:

Although the impact on usability is one thing, after all, the situation mentioned above is also a software quality problem, which can be avoided through adequate testing. The more scary thing is that the process model and framework become more and more tied. If you want to publish RPC services and do some task processing, you may not be able to do it under the complex process model. You can only let Node.js do some Web Server things, which to some extent limits node.js applications and makes it difficult to evolve and develop.

This is the most direct reason why we developed Pandora.js.

procfile.js

Faced with such difficulties, we began to prepare for some things around the Spring Festival last year. Of course, we also took some detours during this period. After many discussions, most of the scenes were straightened out.

First, we come up with a process structure definition file, which we call procfile.js.

Some of you may be familiar with the word procfile, which is the name of the Heroku process definition file. We can’t remember who came up with procfile.js, but we think it’s a good name, since procfile is an existing shorthand for Process File (we don’t have to make up the words ourselves and save ourselves the hassle).

Of course, giving Pandora. Js to manage processes is not just about creating processes for you, but also:

Pandora. Js will guard the created process.

Small to automatic restart, cutting log files, restart count.

Up to 30 + Metrics collection, automated full-link Trace tracking, docking with existing APMs such as Open-Falcon, and more.

However, today we will talk about the process related design thinking, how to install, how to start please check our GitHub documentation ~

Other parts such as monitoring, Metrics, and Trace will also be covered in the future

BTW

Project address: github.com/midwayjs/pa… , everyone feel good to also give a Star ~~

Simple example of procfile.js

If you were to define a process, a rough example would look like this:

procfile.js

// procfile.js is a normal Node.js module that must export a function // function(Pandora) whose first argument is Pandora, Module.exports = function(Pandora) {Pandora // defines a process, ProcessA. Process ('processA') // If scale is greater than 1, Scale (1) // node.js parameter.argv(['--expose-gc']) // defines the process environment variable, Env ({ENV_VAR1: 'VALUE_OF_ENV_VAR1}) / / startup sequence. The order (1) / / file address. The entrance to the process of entry ('. / app. Js'); /* pandora .process('processB'); pandora .process('processC'); pandora .process('processD'); and so on so on.... * /}Copy the code

The process called processA, which simply defines the entry as./app.js, is basically the same as Node./app.js.

For this scenario, we also provide another, simpler approach.

module.exports = function(pandora) {
  pandora.fork('processA', './app.js');
}
Copy the code

Scalable processes

As you can see above, the definition of process scaling in procfile.js relies on the following definitions:

Pandora. The process (' processX). Scale (5);Copy the code

The above definition means that the process named processX is extended by 5 copies.

The Scale value is important in pandora.js, because the user does not need to specify which process to start. The Cluster module is used for scaling (using the Master/Worker model).

Pandora. Js is automatically determined based on the Scale value.

The picture below is probably easier to understand:

To be continued

This article only introduces some of the capabilities of process definition, and there will be more to share in the future. This is just the first part of our journey to pandora.js.

What follows is a long list of daemons, how to monitor applications, how to track links, fascinating dashboards, and how to integrate with existing APMs like Open-Falcon. Stay tuned!

Finally, don’t forget to give a Star

Github.com/midwayjs/pa…

And finally, finally, we hire. More than half of our taobao front desk visits are on Node.js, and we also have the opportunity to make open source Node.js software, which is not a small challenge but also a small reward.

Illustration: unsplash.com/photos/F9o7… By @Tony Webster