This article is a guest post by Ukpai Ugochi. She wrote this blog post through the Write for the Community program.
The goal of software firms is to ship products that add value to customers at the right time. Whether the firm follows the agile or DevOps architecture, its purpose is to satisfy customers with its products. It can be a little daunting to achieve since firms need to ensure no downtimes when delivering products to users.
Downtimes are very destructive for the reputation of any business. Imagine users trying to log into your application but cannot access it because your servers are down. This downtime will ruin the customer's trust in you, and they may not want to try your product again.
It is no different for a headless CMS like Strapi. With production process managers, developers can keep applications alive forever, reload them without downtime, and facilitate common system admin tasks.
This article will explore what process managers are, their use, and the different types of project managers. Also, we'll explore the process manager for Nodejs applications and how they can be used in a Strapi application.
Process management uses skills, tools, and knowledge to analyze, monitor, and meet process goals at a specific time. In contrast, process managers are tools and procedures that help actualize process goals at a particular time.
Process managers help engineers build and ship products to users on time in software development.
The demand for products from users increases daily. Therefore developers must embrace automation in the software development process. Project managers provide flexibility and allow you to ship and manage your application efficiently.
For instance, with process managers, you can keep your application alive so users can access them without downtime. Also, process managers allow you to monitor, manage and collaborate on your application with other engineers.
In the next section, we'll examine the importance of process management and why you should use one.
Servicing across the internet boils down to creating and shipping products that add value to customers at the right time. While numerous organizations ship products to users at the right time, they may release products that don't add value to the customer.
As such, customers complain about the low quality of products shipped to them. Other firms ship products that add quality to users but may ship them later.
These two methods will cause a decline in customer activity with the product. Let's look at why software engineers love process managers and how it enables software firms to ship products that add value to customers at the right time.
In software development, there are numerous process managers for Nodejs-based applications. This section will look at the process managers for NodeJS-based applications, which can be used in our Strapi application.
PM2 (Process Managers, 2) PM2, also called process manager 2, is a process manager for NodeJS applications with a built-in load balancer and where it can be used in a Strapi application. Let's look at how this process manager helps you deploy, monitor, and manage your Strapi application.
Firstly, we will create a Strapi project that we will be using to test our process managers. To create a new Strapi project, make sure you have all the requirements, then run the following command on your CLI (Command Line Interface).
1npx create-strapi-app my-project --quickstart
Log in to your Strapi admin panel: localhost:1337/admin/auth/login, and you will notice that your application is live.
Stop the server with CTRL + C
, log in to your admin panel, and you will notice that you're offline. The next step is to install PM2 globally in your working environment. Run the command below to install PM2.
1/**install with npm**/
2npm install pm2 -g
3
4/**install with yarn**/
5yarn global add pm2
Now, configure your Strapi application so that PM2 can access it. To configure your Strapi application, create a file in the root of your project and name it server.js
.
1const strapi = require('strapi');
2
3// start strapi application
4strapi().start();
5
6
7/my-project
8└─── api
9└─── build
10└─── config
11└─── extensions
12└─── public
13- favicon.ico
14- package.json
15- server.js
Since Strapi doesn't have an entry point file like most Node.js applications, this code above creates an entry point for our Strapi application. To start your application, run the following command:
1pm2 start server.js
Now, log in to your admin panel, and you will notice that you're live even when you stopped the server of your Strapi application. If you close down your CLI, you'll see that your application is still running.
To stop your application from running, stop PM2 with the following command:
1pm2 stop server.js
PM2 allows you to manage your application's log. Logs are critical in the development stage of your application. They help engineers monitor how their applications behave and, to some extent, help detect the cause of an error.
To print the log of your Strapi application, run the following command on your CLI.
1pm2 logs
Outputting logs without aggregating or sorting them is a considerable challenge for developers. With PM2, you can sort, aggregate, or manage your logs to suit your needs.
To choose the option that suits you most in managing your logs, run the following command on your CLI.
1pm2 logs -h
Also, you can clear your logs with PM2 by running the command below in your CLI.
1pm2 flush
You can configure PM2 to run all of the operations we've done above without needing to run those commands on your CLI. PM2 saves a lot of time, and I recommend you use this method in your Strapi application.
To configure PM2, you need an ecosystem.config.js
file. To generate this file automatically, run the command below in your CLI.
1pm2 init
Next, we'll input all of the commands we want PM2 to run on our application. For example, we can start our applications and send log details to an output file by replacing the content of ecosystem.config.js
with the command below.
1module.exports = {
2 apps: [
3 {
4 name: 'server',
5 script: 'npm',
6 args: 'start',
7 }, {
8 script: 'npm',
9 // seperate error logs
10 error_file: 'err.log',
11 out_file: 'out.log',
12 //combine error logs and other logs
13 log_file: 'combined.log',
14 time: true
15 }
16 ],
17};
You can deploy your application and monitor CPU/memory usage with PM2. To monitor your application's CPU usage, run the following command.
1pm2 monit
Also, you can deploy your application from GitHub, hosting it in your environment with the following syntax.
1{
2 "apps" : [{
3 "name" : "server",
4 "script" : "npm"
5 }],
6 "deploy" : {
7 // "production" is the environment's name
8 "production" : {
9 "user" : "ubuntu",
10 "host" : ["192.168.0.13"],
11 "ref" : "origin/master",
12 "repo" : "git@github.com:Username/repository.git",
13 "path" : "/var/www/my-repository",
14 "post-deploy" : "npm install; grunt dist"
15 },
16 }
17 }
PM2 allows integration with cloud providers like Heroku and Docker. You can use Nginx as an HTTP proxy, transpilers, and AWS Elastic Beanstalk.
Forever
Strapi supports integration with Forever. Forever is a process manager, just like PM2. Forever is a Node.js process manager that keeps your Node.js application alive forever. Most times, when you run a Node.js application, and there's an unexpected error, your server stops. What if you're not aware there's an error and can't restart your application?
Forever will restart your application whenever there's an error that can stop your server and keep your application alive forever. Without forever, you may need to run node index.js whenever your server shuts down unexpectedly.
Also, you can use your terminal while your application is running smoothly without the risk of shutting down your application.
To install forever globally in your working environment, run the following command on your CLI.
1npm install forever -g
Next, please navigate to the Strapi application we initially created on your CLI. Notice that the server.js
file is still intact as it'll be our entry point. Now, start your application by running the code below.
1forever start server.js
Navigate to your admin panel and notice that your application is live. Just like PM2, you can make all configurations for your application in a single file. In this case, you should create a JSON
file at the root of your folder.
For example, create a JSON
file and name it development. You can output logs into a file by specifying them in your JSON
file.
1{
2 // Comments are allowed!
3 "uid": "my-project",
4 "append": true,
5 "watch": true,
6 "script": "server.js",
7 "sourceDir": "/home/myuser/my-project",
8 "command": /Users/myuser/.nvm/versions/node/v4.1.2/bin/node
9 "logFile": "/home/myuser/logs/forever.log",
10 "outFile": "/home/myuser/logs/out.log",
11 "errFile": "/home/myuser/logs/error.log"
12}
Now you can run your application with this command.
1forever start ./forever/development.json
2
3/**or**/
4forever start /home/myuser/app/forever/development.json
To stop forever from keeping your application alive, run this command.
1forever stop server.js
StrongLoop Process Manager
StrongLoop process manager is a Node.js production process manager that allows you to build, manage and deploy your Node.js application and keep your processes and clusters alive forever.
This process manager has built-in load balancing and allows you to view performance metrics on your application. It is an alternative to PM2.
To install this process manager, run the following command in your command-line interface.
1npm install -g strongloop
After installation, navigate to your Strapi project's location from your CLI. Run your application with the following command.
1slc start
You can get a quick status overview of all worker PIDs, cluster IDs, and other essential information about your application by running the command below.
1slc ctl
Just like PM2 and forever, you can check logs from your application in the StrongLoop process manager. To do this, run the following command.
1slc ctl log-dump my-project --follow
The goal of a process manager is to mitigate downtime. With StrongLoop process manager, you can update your environment with no downtime.
1slc ctl env-set my-project NODE_ENV=production
StrongLoop process manager supports Ubuntu 12.04+, Red Hat Enterprise Linux, 7+, and Red Hat Enterprise Linux 5 and 6 environments for production use.
You can install StrongLoop process manager for production mode with the command below.
1// install via npm
2npm install -g strong-pm
3
4// install via docker
5curl -sSL https://strong-pm.io/docker.sh | sudo /bin/sh
Build your application for production mode with slc build
and deploy to your production host with the following syntax.
1slc deploy http://your.remote.host
StrongLoop Process Manager runs one process per CPU. So, it will run your app in a four-process cluster on a four-core system. You can change the cluster size by running the command below.
1slc ctl set-size my-project <size in number>
Supervisor
A Supervisor is a supervisor script for Node.js that runs your program and watches for code changes. It restarts your application and allows hot-code reloading-ish behavior without worrying about memory leaks.
In Node.js, every code change means you'll have to restart your application. Also, the supervisor restarts your application whenever there's an unexpected error that stops your server.
To use supervisor in your Strapi application, first install it. To install, run the command below in your CLI.
1npm i supervisor -g
Next, you'll start our Strapi application by running the command below in your CLI.
1supervisor start server.js
Notice that this command behaves like when you start your Strapi application with strapi start
.
If you want to restart your server manually, you can do so by typing rs in your CLI. Watch as your server restarts.
Process managers are magnificent tools if you know how to use them properly. They're vital if you want to ship features to users that add value to customers on time.
It is no different for Strapi, a headless CMS for NodeJS-based applications. Everyone wants automation that provides efficiency in an optimized manner; This is what process managers offer precisely.
This article has looked at what process managers are, why you need them as engineers, and how to use different NodeJS-based process managers in a Strapi application.
First, we created a simple Strapi application and configured an entry point for our application so that process managers can interact with it. Strapi, like most NodeJS-based applications, supports numerous process managers.
If you're finding it difficult to choose a project manager for your application, feel free to look at a comparison between PM2, StrongLoop process manager, and Forever here.
Ukpai is a full-stack JavaScript developer and loves to share knowledge about her transition from marine engineering to software development to encourage people who love software development and don't know where to begin.