Hello to all habrovchanam, fans of Yii and Node.js. There have been no articles about Jii for a long time, but the work did not stand still and I have something to tell!
In this article, we will look at how to configure an application and launch applications as several processes.
For those who hears about this framework for the first time, I recommend reading
previous articles or
visiting the site .
In short,
Jii is a framework, the architecture and API of which is based on the PHP framework Yii 2.0, taking the best sides out of it and retaining the advantages of JavaScript.
Configuration
On Habré there were already many articles about how you need / can organize the configuration of the application. From my own experience I can say that the configuration strongly depends on the specifics and scope of the application.
Therefore, when creating a configuration in Jii, I tried first to create a constructor, and not a set of restrictions and rules.
In addition to the environment name, the Jii configuration provides for dividing the application into several smaller ones, each of which may be responsible for a specific task. One can implement the transport (http / comet), the other for asynchronous calculations in the background, the third - the demon that periodically monitors something, and so on.
This distribution is usually observed only in large applications (projects), so Jii will not overload the programmer with unnecessary abstractions to create a simple application.
')
Examples
Let's look at a few configuration examples, each of which will be more complicated than the previous one.
Basic
The simplest configuration for a single application, without separation into environments, but with the ability to override the configuration through an additional configuration file.
If you put all this in the
server.js file and start
node server.js , the following will happen:
- Using the native Cluster module, a master application (process) will be launched, the code of which is inside jii-workers ;
- The master application in a separate process will start our application with the configuration that we have shown above and will monitor its state — if it dies, it will restart it.
- When you run our application, an instance of Jii.application.WebApplication will be created and filled with our configuration
- Components that support the service interface (have start, stop methods, for example, HttpServer) will start the start method - the application will start its work.
Full
GitHub Code
Standard
A more complex configuration, depending on the environment name, has several applications, each of which may have several duplicate workers.
var Jii = require('jii'); require('jii-urlmanager'); require('jii-httpserver'); require('jii-comet'); var custom = require('./config'); require('jii-workers')
Unlike the simplest example, we needed to create several applications (
http and
comet ). We set their names as the first optional argument of the
application function. We passed the second parameter to the configuration, but not as a finished object, but as a function. This function will be called immediately (in this case twice), and the name of the environment and the name of the application specified in the first parameter will be passed as arguments. A kind of syntactic sugar, no more.
The remaining parts of the configuration are separated by the
app / config / * files, all the code can be seen on the
github .
Advanced
Well, briefly talk about the broader form of configuration, where there are many applications and everything is fragmented into folders and files.
var Jii = require('jii'); var custom = require('./config'); require('jii-workers') .setEnvironment(custom.environment)
In this example, the
application method is called several times to declare different applications. The rest is all the same.
All sample code can be seen
here .
Scaling
Node.js a worker can only occupy one processor core, so they usually recommend creating several workers according to the number of process cores. Considering that with high memory consumption and the CPU, the node “sticks”, it is better to increase this amount at least twice.
In the Jii configuration, for each application, you can specify the number of processes on which you want to run the application. Scaling occurs by means of
Cluster . Each worker will be assigned an index, this is useful if you need to distribute a server to different ports.
Total
In fact, there are a lot of configuration options, as I said before - it is different for each application. The tool in the form of the
jii-workers module only allows to simplify its task.
The Jii configuration is not a silver bullet, it is intended only for creating Jii applications.
Let me remind you, Jii is an open source project, so I will be very happy if someone joins its development. Write to affka@affka.ru.
Framework Site -
jiiframework.ruGitHub -
github.com/jiisoftLiked? Put a
star on the githaba! :)