z1 is a Node.js cluster management program. It works on Linux Debian, Ubuntu, and other Debian based distributions.
When using Node.js on a web server, one will somehow come to the point where he wants to start multiple processes for one app. The main goal of z1 is to simplify 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
- use revents 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
- add WORKERS 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
Via NPM
sudo npm install z1 -g
Note:
You might want to run z1 resurrect
automatically after rebooting your system. It will start the z1 daemon and all the
apps that were running before.
(see: install command)
Before you can start your Node.js app, you need to add a few things to your
package.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 value is the number of CPU-cores in your system.
- output (optional) - A directory for the log and error files. (Default:
~/.z1/<yourAppname>
) - devPorts (optional) - Ports for development
- devWorkers (optional) - Workers for development
Important: z1 needs to know when your Node.js program (e.g. a web server) is successfully started. If you app uses ports, z1 will automatically 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 the z1.ready() method.
Example package.json file:
{
"name": "homepage",
"main": "index.js",
"ports": [8080],
"workers": 2
}
If you are running z1 locally, you can set NODE_ENV=development
. This will cause z1 to use the devPorts
and devWorkers
(if specified) instead of the ports
and workers
properties from the package.json
. The default
timeout for stop and restart will be set to 0ms.
You can set different environment variables for each app. The start command automatically applies the environment 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 variables can't be overwritten.
After you prepared the package.json file of your app, you can now start it. First go to the
directory where the package.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 the package.json
file. If your app was
successfully 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 the logs of all workers are being displayed.
Options
If you want to start your app with different settings than the ones specified in the package.json
, you can add them to
to the z1 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 the package.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 the z1 restart
command is the name that was specified in the package.json
when you started
the app. If you are running this command inside the directory of the Node.js application, z1 will automatically detect
the name.
Output of the example from above:
Options
--timeout 10000
--timeout
is a number specifying the maximal time (in ms) that the old workers can continue to run after they are
killed. The timeout allows old processes to finish their active requests while not accepting new ones. If all
requests are finished, or the timeout is exceeded, the old processes get killed. The default value is 30000 (30s). If
you 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 they exit.
- 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 typed exit, you can use the following to start all the apps that were running before:
z1 resurrect
Note: If you are starting a new app before z1 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 be
passed to the workers. In your code you can get the "hello" as argv.
process.argv[2] === 'hello' // true
Besides the CLI, you can also require z1 to control your apps with a Node.js program.
const z1 = 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 the package.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 set immediate to true
, the promise
gets 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 resolves
to 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 the
following 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 if z1.resurrect()
or z1.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 function must be called if an app does not use any port. It will tell the z1 daemon that your app has been started successfully.
Example:
const z1 = require('z1')
// ...your program...
z1.ready()