Karim Tarek
Karim Tarek

Reputation: 401

How to restart my node js app when the CPU usage reach 100% on Amazon EC2 and the server stops

Currently i am using forever to handle crashes,etc on EC2 but i want some way to manage restarting the app when the CPU usage on the server reaches 100%.

The way it works now is that when the CPU usage reach 100% the app stops running and if i didn't notice the alarms sent by amazon on my mail the app remains down until i restart manually it again using forever.

What i want is a way for when the cpu usage reach 90% or higher it restarts the app, should i use another module other than forever and if so any suggestions ?

Upvotes: 3

Views: 2786

Answers (2)

Andrew
Andrew

Reputation: 1354

Check out Forever: https://github.com/nodejitsu/forever

You can use it for the exact situation described. Thought i'd try to figure out why you're hitting max CPU.

Upvotes: 0

Dragu
Dragu

Reputation: 3297

I recommand you to reduce your CPU usage BUT, I use a similar tricks but to restart when memory usage is to high (due to very small memory leak)

You need module "usage"

var usage       = require('usage');

then:

CHECK_CPU_USAGE_INTERVAL    = 1000*60; // every minute
HIGH_CPU_USAGE_LIMIT        = 90; // percentage

autoRestart = setInterval(function()
{
    usage.lookup(process.pid, function(err, result) 
    {
        if(!err)
        {
            if(result.cpu > HIGH_CPU_USAGE_LIMIT)
            {
                // log
                console.log('restart due to high cpu usage');

                // restart because forever will respawn your process
                process.exit();
            }
        }
    });
}, CHECK_CPU_USAGE_INTERVAL);

Upvotes: 1

Related Questions