在nodejs中重启heroku dyno

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25618792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:50:45  来源:igfitidea点击:

Restart heroku dyno in nodejs

node.jsheroku

提问by user1660601

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.

我想重新启动 nodejs 应用程序以更改其 ip。我将如何在应用程序本身中执行此操作?我曾尝试强制崩溃,但是如果应用程序在十分钟内崩溃两次,那么 heroku 将在第一次崩溃后十分钟重新启动 dyno。

采纳答案by brennebeck

Take a look at thisnode wrapper for the v3 API and thisapi command.

查看v3 API 和api 命令的节点包装器。

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

如果确实需要在应用程序内部完成,您可能需要创建某种条件来触发此操作。

回答by gaelgillard

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

如果您安装了 Heroku CLI,则可以heroku restart在应用程序的文件夹中运行或运行heroku restart --app application_name.

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

如果您没有安装它,您可以在此处找到有关它的信息

回答by Dhyey

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

您可以直接从 heroku 的 Web 仪表板重新启动 nodejs dynos:

Dashboard -> More -> Restart all dynos

仪表板 -> 更多 -> 重启所有 dynos

enter image description here

在此处输入图片说明

回答by Jason

Using the Heroku v3 APIit is possible using the request node module

使用Heroku v3 API可以使用请求节点模块

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 wrapperthat provides a similar experience, but is poorly documented and requires an understanding of the v3 APIanyway

还有一个提供类似体验的节点包装器,但文档不全,无论如何都需要了解v3 API

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 deletemethod 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

  • 我个人确实发现使用删除方法有点令人担忧。使用该delete方法时应谨慎,/apps/$$appName$$仅端点将删除该应用程序。这是个人经验

  • 对于以上任何一个,如果您省略dyno名称,您将重新启动应用程序下的所有dyno