简体   繁体   English

如何从 Rails 应用程序内部运行 heroku restart?

[英]How to run heroku restart from inside of a rails app?

I understand that from the console I can run heroku restart .我知道从控制台我可以运行heroku restart What I'd like to do is to have a button in my app (admin console), where pushing that button runs a heroku restart .我想做的是在我的应用程序(管理控制台)中有一个按钮,按下该按钮会运行heroku restart Does anyone know how to do that and if it's possible?有谁知道如何做到这一点,如果可能的话? So the code would look something like this:所以代码看起来像这样:

<button id="heroku_restart">Restart</button>

$("#heroku_restart").click(function() {
    $.post('/restart', {}).done(function(response) {
        alert(response)
    })
})

class AdminsController

    # this is the action mapped to the route /restart
    def restart 
        # code for heroku restart
    end
end

So per @vpibano, as of this writing, doing it with the platform-api is a breeze.因此,根据@vpibano,在撰写本文时,使用platform-api是轻而易举的。 Here's the action POSTed to by a button on my website:这是我网站上的按钮发布的操作:

def restart
    heroku = PlatformAPI.connect_oauth(ENV["heroku_oauth_token"]) 
    heroku.dyno.restart_all("lastmingear")
    render nothing: true
end

As per the description mentioned in the post, the one way of doing it is :根据帖子中提到的描述,一种方法是:

1) First locate the server.pid file 1)首先找到server.pid文件

pid_file = Rails.root.join("tmp", "pids", "server.opid")

2) Now, truncate the contents of the file 2)现在,截断文件的内容

File.open(pid_file, "w") {|f| f.truncate(0)}

3) Finally, run the server using Kernel module: 3) 最后,使用 Kernel 模块运行服务器:

Kernel.exec("rails s")

Note: As rightly, mentioned by @vpibano you will need authentication to access your app.注意:正如@vpibano 所提到的,您将需要身份验证才能访问您的应用程序。

This is not a working model but a way to achieve the requirement.这不是一个工作模型,而是实现需求的一种方式。

Hope it helps!!希望能帮助到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM