Getting restart count from PM2 node - pm2

I'm trying to monitor the amount of the restarts, cpu and memory in PM2 module managed microservices and create an alert if the module is restarting using AWS cloud watch.
pm2 list
Command returns the data in a UI formatted way which I would like to avoid parsing.
Is there any way to get the number of process restarts in a more machine-readable friendly format than the one returned by the pm2 list command.
I looked at the pm2 get command but can't find documentation about the keys I can use there.

You can get all kinds of details (including restarts) in json format with
pm2 prettylist (pretty)
or with
pm2 jlist (raw).
pm2 also has an api:
var pm2 = require('pm2');
// Connect or launch PM2
pm2.connect(function(err) {
// Start a script on the current folder
pm2.start('test.js', { name: 'test' }, function(err, proc) {
if (err) throw new Error('err');
// Get all processes running
pm2.list(function(err, process_list) {
console.log(process_list);
// Disconnect to PM2
pm2.disconnect(function() { process.exit(0) });
});
});
});
Details on the api: pm2-api

Related

How to connect to IPFS node started programmatically using ipfs-core from a java server?

I am programmatically starting an IPFS node using JS ipfs-core(npm package) with a custom repository using a different storage backend(similar to S3). Now once the node is started in the AWS instance, I want to send requests to the node using a remote client written in Java.
java-ipfs-http-client can connect to the API port. But, the API and gateway service does not get initiated when the node is started. The Java server will be running on a different machine.
Is it possible to access the ipfs node started using ipfs-core programmatically from a java server running on a different instance?
Found the solution.
When we initialize node programmatically, we need to manually start API/Gateway in the following way.
import * as IPFS from 'ipfs-core'
import { HttpApi } from 'ipfs-http-server'
import { HttpGateway } from 'ipfs-http-gateway'
async function startIpfsNode () {
const ipfs = await IPFS.create()
const httpApi = new HttpApi(ipfs)
await httpApi.start()
const httpGateway = new HttpGateway(ipfs)
await httpGateway.start()
}
startIpfsNode()
This will start the ipfs node along with the API and Gateway
The configuration of API and Gateway port can be changed programmatically in the following way
const ipfs = IPFS.create()
await ipfs.config.set('Addresses.API', '/ip4/127.0.0.1/tcp/5002');
await ipfs.config.set('Addresses.Gateway', '/ip4/127.0.0.1/tcp/9090');
Once the API is started, the IPFS node can accessed from a Java Program using java-ipfs-http-client

How can I make Jelastic start PM2 to launch an 'npm' command instead of a file?

I'm using a Jelastic Node.js PM2 environment and I want my app to be started with something like the following:
pm2 start npm --name "app name" -- start
(my server is not a JS file).
The command runs fine if I use a Jelastic 'npm' environment, but I'd rather have the benefits of PM2.
I tried setting various APP_FILE (start, npm start, a pm2 config file path), Entry Points and PROCESS_MANAGER_FILE, without success. I usually get this error:
Node ID : 53209
-----------------------
result 1 Failed to start
Stopping nodejs server[ OK ] Starting nodejs server [FAILED]
The comment from #Jelastic worked! Indeed using a PM2 'ecosystem file' works in Jelastic.
Set APP_FILE (or possibly PROCESS_MANAGER_FILE) to ecosystem.config.js (This is relative to ROOT_DIR)
The content of this file should look something like this:
module.exports = {
apps: [
{
script: "yarn",
args: "--cwd myserver1 start",
name: "myserver1",
},
// You can use this setup to start multiple processes too.
{
script: "yarn",
args: "--cwd myserver2 start",
name: "myserver2",
},
],
};
--cwd tells yarn to switch the Current Working Directory. If you use npm, you can use --prefix instead.
Read more about PM2 ecoystem files: https://pm2.keymetrics.io/docs/usage/application-declaration/

Terminate running (watch) process (gulp-4.0)

I've implemented a task to run a server and react (reload) on changes of my source files.
export function serveDev (gulp) {
return () => {
const bs = browserSync.create();
const stream = bs.init(config.browsersync.opts);
gulp.watch(`${config.source}/components/**/*.js`, gulp.series('scripts'));
gulp.watch(`${config.source}/js/**/*.js`, gulp.series('scripts'));
gulp.watch(config.browsersync.watch).on('change', bs.reload);
return stream;
};
}
I'm using gulp-4.0 and I'm running this task from the command line.
What's the correct implementation to terminate this task correctly when the user hits CTRL+C?
When I terminate this running task with the key-shortcut CTRL+C I get the following error:
The following tasks did not complete: serve, sync
Did you forget to signal async completion?
The task is working properly until the user hits CTRL+C. When the signal coming from CTRL+C reach the task the error described above is printed out. I would like to know how to catch or how to react properly on the termination signal coming from CTRL+C?
You can kill the background process on terminal with pkill command like:
pkill gulp

Refresh Azure Redis Cache every 15 minutes

I need to sync data from MySQL Database to Redis Cache every 15 minutes so that cache as latest data.
I am using ubuntu for hosting (Node.js) webservcies. So everytime there is call for rest api it needs to fetch data from cache and serve it.
So now do I need write a background job to sync MySQL data to Cache memory.
If I need to write a background job can I write In node.js and sync it and run as a background job in Ubuntu using crontab command.
Yes. You can write a nodejs script and run it thru crontab command to sync data from MySQL to Redis.
Per my experience, you need some nodejs packages below to help implement the needs.
NodeJS ORM for MySQL:
Sequelize: http://docs.sequelizejs.com/en/latest/ (npm install
sequelize mysql)
Redis Client for NodeJS:
ioredis: https://github.com/luin/ioredis (npm install ioredis)
node_redis: https://github.com/NodeRedis/node_redis (npm install
redis)
The sample code ~/sync-mysql-redis.js:
// Create a mysql client connection
var Sequelize = require('sequelize');
var sequelize = new Sequelize('mysql://user:pass#azure_mysql_host:3306/dbname');
// Create a redis client using node_redis
var redis = require("redis");
var client = redis.createClient(6379, '<redis_host>');
// Query entities data from MySQL table
sequelize.query("SELECT * FROM `t_entity`", { type: sequelize.QueryTypes.SELECT})
.then(function(entities) {
for(var entity in entites) { // for-each entity from entites list
var hash_key = entity.Id // for example, get the entity id as redis hash
for(var prop in entity) { // for-each property from entity
client.hset([hash_key, prop, entity[prop]], redis.print); // mapping a mysql table record to a redis hash
}
}
});
For crontab configuration, you need to vim /etc/crontab as root or sudo user:
$ sudo vim /etc/crontab
# Add a crontab record to run nodejs script interval 15 mins
*/15 * * * * node \home\user\sync-mysql-redis.js

Startup script from Bitbucket (https) fail to download, but works if instance is reset

I am programatically launching a new instance using the Compute Engine API for Go [1], and a tool I made called vmproxy [2].
The problem I have is that if I launch a preemptible VM using a startup-script-url pointing to https://bitbucket.org/ronoaldo/debian-custom/raw/tip/tools/autobuild, the build script fails to download. I can see in the serial console output that the the startup script metadata is there, and that it attempts to be downloaded with curl, but that part fails.
However, if I reset the instance via the developers console, the script is properly downloaded and runs nicelly.
The code I am using to setup the instance is:
// Ronolinux is a VM Proxy that runs an live systems build on Compute Engine
var (
Ronolinux = &vmproxy.VM{
Path: "/",
Instance: vmproxy.Instance{
Name: "ronolinux-buildd",
Zone: "us-central1-f",
Image: vmproxy.ResourcePrefix + "/debian-cloud/global/images/debian-8-jessie-v20150915",
MachineType: "n1-standard-1",
Metadata: map[string]string{
"startup-script-url": "https://bitbucket.org/ronoaldo/debian-custom/raw/tip/tools/autobuild",
"shutdown-script": `!#/bin/bash
gsutil cp /var/log/startupscript.log gs://ronoaldo/ronolinux/build-$(date +%Y%m%d%H%M%S).log
`,
},
Scopes: []string{ storageReadWrite },
},
}
)
[1] https://godoc.org/google.golang.org/api/compute/v1
[2] https://godoc.org/ronoaldo.gopkg.net/aetools/vmproxy
If your startup script is not hosted on Cloud Storage, there is a random chance the download will fail. If you look at the serial console output, make sure to scroll horizontally, as it will not wrap long lines. In my case, the error line was very long, and this hidded the real end of the message:
(... long curl on-line progress output )
curl: (7) Failed to connect to bitbucket.org port 443: Connection timed out
(...)
Your host must respond within a 10s timeout. In my case, the first boot usually failed to contact Bitbucket, hence failing to download the script; a VM reset also made things work, as the network latency outside Google Cloud were probably better.
I ended up moving to host the script on cloud storage to avoid these issues.