- Notifications
You must be signed in to change notification settings - Fork3
robojones/z1
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
z1 is a Node.js cluster management program. It works on Linux Debian, Ubuntu, and other Debian based distributions.
When using Node.js on aweb server, one will somehow come to the point where he wants to startmultipleprocesses for one app. The main goal of z1 is tosimplify the creation and management of Node.js processes.
This animation shows how simple it is to start a Node.js application with z1.
- v4.0.0
- userevents for data transmission between the CLI and the daemon.
Ctrl + C
can now be used to abort the starting process of apps.- remove upgrade command.
- v3.17.0
- addWORKERS environment variable
- v3.16.0
- add colors to cli
- v3.15.0
- display the app's logs during command execution
- v3.14.0
- add JSDoc to API
ViaNPM
sudo npm install z1 -g
Note:You might want to runz1 resurrect
automatically after rebooting your system. It will start the z1 daemon and all theapps that were running before.(see:install command)
Before you can start your Node.js app, you need to add a few things to yourpackage.json
file.
- name - The name of your app.
- main - The entry point of your app (The file that you would normally run with (
node <file>
). - ports(optional) - An array of port numbers that your app uses.
- workers(optional) - A number specifying how many processes should be created for your app. The default valueis the number of CPU-cores in your system.
- output(optional) - A directory for the log and error files. (Default:
~/.z1/<yourAppname>
) - devPorts(optional) - Ports fordevelopment
- devWorkers(optional) - Workers fordevelopment
Important:z1 needs to know when your Node.js program (e.g. a web server) is successfully started. If you app uses ports, z1 willautomatically know when it listens to all the specified ports. It will then assume, that you app is completely started.If you app does not use any ports, you must require z1 in your program and call thez1.ready() method.
Example package.json file:
{"name":"homepage","main":"index.js","ports": [8080],"workers":2}
If you are running z1 locally, you can setNODE_ENV=development
. This will cause z1 to use thedevPorts
anddevWorkers
(if specified) instead of theports
andworkers
properties from thepackage.json
. Thedefaulttimeout for stop and restart will be set to0ms.
You can set different environment variables for each app. Thestart command automatically applies theenvironment variables of the current shell to the app.
EXAMPLE=hello z1 start path/to/your/app
There are some environment variables that automatically z1 sets for each process:
- PORT - The first port you specified.
- PORTS - All ports that your app uses (separated by commas).
- APPNAME - The name of your app.
- PWD - The directory of your app.
- WORKERS - The number of workers started for the app.
These variablescan't be overwritten.
After youprepared the package.json file of your app, you can now start it. First go to thedirectory where thepackage.json
of your project is located. Type the following command into your terminal:
z1 start
This command works regardless of how many workers you have specified in thepackage.json
file. If your app wassuccessfully startet, the output should look like this:
As you may have noticed, each log is displayed twice. This happens because two workers are started for the app and thelogs of all workers are being displayed.
Options
If you want to start your app with different settings than the ones specified in thepackage.json
, you can add them toto thez1 start
command.
--name anotherName--ports 80,2020,8080--workers 4--output path/to/logs/
You can restart your app to apply updates for your app or changes to thepackage.json
. The restart process will be __gapless__ and no requests will be refused. Just type the following command:
z1 restart homepage
The first argument for thez1 restart
command is the name that was specified in thepackage.json
when you startedthe app. If you are running this command inside the directory of the Node.js application, z1 will automatically detectthe name.
Output of the example from above:
Options
--timeout 10000
--timeout
is a number specifying the maximal time (in ms) that the old workers cancontinue to run after they arekilled. The timeout allows old processes tofinish their active requests while not accepting new ones. If allrequests are finished, or the timeout is exceeded, the old processes get killed. The default value is 30000 (30s). Ifyou set it to "Infinity" the old processes might run forever.
z1 list
Displays a list of all running apps.
The output could look like this:
z1 info homepage
Shows more detailed information than z1 list.
Example output:
- pending - processes are currently starting.
- available - workers are listening to all the ports specified in the
package.json
- killed - workers are not listening for new connections. They will finish their outstanding requests before theyexit.
- revive count - shows you how often the workers of your app crashed since the last restart.
Options:
--name
- output the appname--dir
- output the directory of the app--ports
- output the ports that the app uses--pending
- output the number of pending workers--available
- output the number of available workers--killed
- output the number of killed workers--revive-count
- output how often the app has been revived
To stop an app just type:
z1 stop homepage
Example output:
Options
--timeout 10000
--timeout
is a number specifying the maximal time that the workers are allowed to run after they are killed (in ms).The default value is 30,000ms. If you set it to "infinity" the old processes might run forever.
z1 exit
This will kill the z1 daemon process and therefore all apps and workers.
After you typedexit, you can use the following to start all the apps that were running before:
z1 resurrect
Note: If you are starting a new app beforez1 resurrect
, the old apps will not be restored.
This command allows you to add and remove additional features.
- zsh - shell completion for zsh
- bash - shell completion for bash(coming soon)
- cron - cron job that resurrects z1 after a reboot
sudo z1 install zsh
You can start your app with custom arguments.
z1 start --name 'custom app' --ports 80 -- hello
This would start an app with the name 'custom app' that is listening on port 80. Everything behind the--
will bepassed to the workers. In your code you can get the "hello" as argv.
process.argv[2]==='hello'// true
Besides the CLI, you can alsorequire z1 to control your apps with a Node.js program.
constz1=require('z1')
Arguments
- dir
<String>
- Path to the directory where thepackage.json
of the app is located (Default: current directory) - args
<Array>
(optional) - Arguments for the workers - opt
<Object>
(optional) - Options that overwrite the ones from thepackage.json - env
<Object>
(optional) - Key-value-pairs to be added toprocess.env
in the workers. - immediate
<Boolean>
(optional) (Default:false
)
Returns a<Promise>
that gets resolved when the app is started. If you setimmediate totrue
, the promisegets resolved immediately after your command was transmitted to the daemon.
By default It resolves to an object with the following data:
{app:String,dir:String,started:Number}
- app - The name of the app specified in the
package.json
. You will need this in order to restart/stop the app. - dir - Absolute path to the directory where the
package.json
is located - started - Number of workers started for this app
Arguments
- app
<String>
- The name specified in thepackage.json
of the app you want to restart. - opt
<Object>
-(optional)- timeout
<Number>
- Maximum time until the old workers get killed (default: 30000ms). - signal
<String>
- Kill signal for the old workers
- timeout
- immediate
<Boolean>
(optional) (Default:false
)
Returns a<Promise>
that gets resolved when the new workers are available and the old ones are killed. It resolvesto an object with the following data:
{app:String,dir:String,started:Number,killed:Number}
- app - the app name
- dir - directory of the app
- started - Number of started workers
- killed - Number of killed workers
Arguments
- app
<String>
The name specified in thepackage.json
of the app you want to restart. - opt
<Object>
(optional)- timeout
<Number>
Maximum time until the old workers get killed (default: 30000ms). - signal
<String>
Kill signal
- timeout
- immediate
<Boolean>
(optional) (Default:false
)
Returns a<Promise>
that gets resolved when the old workers are killed. It resolves to an object with thefollowing data:
{app:String,killed:Number}
- app - the app name
- killed - Number of killed workers
Arguments
- app
<String>
The name of your app.
Returns a<Promise>
that gets resolved to an object that contains information about the app.
Example:
{name:'homepage',reviveCount:0,ports:[80],pending:0,available:2,killed:0}
Returns a<Promise>
that resolves to an object containing data about all running workers.
You can access the data for an app by using the name as key:
z1.list().then(data=>{console.log(data.stats)})
The output would be:
{homepage:{dir:'/home/user/apps/homepage',ports:[80],pending:0,available:2,killed:0}}
Returns a<Promise>
that resolves to an empty object. It gets resolves after the z1 daemon has exited.
- immediate
<Boolean>
(optional) (Default:false
)
Returns a<Promise>
that resolves to an object.
Example:
{started:2// two workers started}
It will be rejected ifz1.resurrect()
orz1.start()
was called before.
Resurrect will start all apps that were running before the daemon was killed.
Returns a<Promise>
that resolves when the ready signal has been transmitted.
This functionmust be called if an app does not use any port. It will tell the z1 daemon that your app has beenstarted successfully.
Example:
constz1=require('z1')// ...your program...z1.ready()
About
Node.js cluster manager