user1660601
user1660601

Reputation: 273

Restart heroku dyno in nodejs

I want to restart a nodejs app to change its ip. How would I do this within the app itself? I have tried forcing a crash however if the app crashes twice within ten minutes, then heroku will restart the dyno ten minutes after the first crash.

Upvotes: 22

Views: 30330

Answers (4)

Jason
Jason

Reputation: 821

Using the Heroku v3 API it is possible using the request node module

var token = 'youAPIKeyHere';
var appName = 'yourAppName here';
var dynoName = 'yourDynoHere';

var request = require('request');

request.delete(
    {
        url: 'https://api.heroku.com/apps/' + appName + '/dynos/',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/vnd.heroku+json; version=3',
            'Authorization': 'Bearer ' + token
        }
    },
    function(error, response, body) {
        // Do stuff
    }
);

There is also a node wrapper that provides a similar experience, but is poorly documented and requires an understanding of the v3 API anyway

var token = 'youAPIKeyHere';
var appName = 'yourAppName here';
var dynoName = 'yourDynoHere';

var Heroku = require('heroku-client');

var heroku = new Heroku({ token: token });
    heroku .delete('/apps/' + appName + '/dynos/' + dynoName)
           .then( x => console.log(x) );

I also found it useful to experiment in browser with this code

var token = 'youAPIKeyHere';
var appName = 'yourAppName here';
var dynoName = 'yourDynoHere';

var xhr = new XMLHttpRequest();
    xhr.open(
        'DELETE',
        'https://api.heroku.com/apps/' + appName + '/dynos/' + dynoName
    );
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.setRequestHeader('Accept', 'application/vnd.heroku+json; version=3');
    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
    xhr.onload = function() {
        console.log(xhr.response);
    };
    xhr.send();

  • I personally did find using the delete method a bit concerning. Caution should be taken with using the delete method and the /apps/$$appName$$ endpoint alone will delete the app. This is from personal experience

  • For any of the above, if you omit the dyno name, you will restart all dynos under the app

Upvotes: 13

Dhyey
Dhyey

Reputation: 4335

You can restart the nodejs dynos right from the web dashboard of heroku :

Dashboard -> More -> Restart all dynos

enter image description here

Upvotes: 9

brennebeck
brennebeck

Reputation: 450

Take a look at this node wrapper for the v3 API and this api command.

You'll likely need to create some sort of condition that triggers this, if it really needs to be done from within the app itself.

Upvotes: 6

gaelgillard
gaelgillard

Reputation: 2523

If you have the Heroku CLI installed, you can run heroku restart in the folder of your application or run heroku restart --app application_name.

If you don't have it installed, you can find information about it here.

Upvotes: 71

Related Questions