自动重启我的 heroku 应用程序 [英] Restart my heroku application automatically

查看:30
本文介绍了自动重启我的 heroku 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个终端命令会重新启动我的 heroku 应用程序:

This terminal command restarts my heroku application:

heroku restart

有没有办法运行一个脚本来运行这个命令并每小时重新启动我的应用程序?

Is there a way to run a script that will run this command and restart my application every hour?

推荐答案

实际上我只需要为我的应用解决这个问题,并写了一篇关于它的更多细节的帖子.基本上,您现在需要 heroku-api gem,因为 heroku gem 已被 CLI 取代.然后你需要一个 rake 任务、几个配置变量和 heroku 调度程序插件(免费,除了最短的 dyno 时间).

I actually just had to solve this problem for my apps and wrote a post on it with more details. Basically, you need the heroku-api gem now since the heroku gem is replaced by the CLI. Then you need a rake task, a couple of config variables and the heroku scheduler plugin (free except for minimal dyno time).

rake 任务如下所示:

The rake task looks like this:

namespace :heroku do
  desc 'restarts all the heroku dynos so we can control when they restart'
  task :restart do
    Heroku::API.
      new(username: ENV['HEROKU_USERNAME'], password: ENV['HEROKU_PASSWORD']).
      post_ps_restart(ENV['HEROKU_APP_NAME'])
  end
end

您还可以将其设置为使用您的 API 令牌,而不是将您的用户名和密码放入配置中.这仅在您不希望您的共同贡献者/同事知道您的密码或您在 Heroku 上的主帐户的密码时才重要.

You can also set it up to use your API token instead of putting your username and password into the config. This only matters if you don't want your co-contributors/coworkers knowing your password or the password to your main account on Heroku.

heroku config:set HEROKU_USERNAME=[username] HEROKU_PASSWORD=[password] HEROKU_APP_NAME=[app_name] -a [app_name]

现在,继续部署和测试:

Now, go ahead and deploy and test:

git push [heroku_remote_name] [feature_branch]:master
heroku run rake heroku:restart -a [app_name]

最后,我们需要设置任务以按计划运行.我选择使用免费的 Heroku cron 附加组件:

Lastly, we’ll need to set up the task to run this on schedule. I’ve chosen to go with the free Heroku cron add-on:

heroku addons:add scheduler:standard -a [app_name]
heroku addons:open scheduler -a [app_name]

这将在您的浏览器中打开调度程序 UI,您可以创建一个调度工作程序来随时运行 rake 任务.我们每天只需要它一次,而且我们选择在当天的第一个预定作业之前运行它.

This will open up the scheduler UI in your browser and you can create a scheduled worker to run the rake task whenever you’d like. We only need it once per day and we’re choosing to run it before our first scheduled job of the day.

我的原始帖子与 CSS 相关(请参阅下面的更新 2):

My original post with frigged up CSS (see update2 below):

https://web.archive.org/web/20150612091315/http://engineering.korrelate.com/2013/08/21/restart-heroku-dynos-on-your-terms/

更新

我将任务名称从内爆"更改为重新启动",以便更清楚地了解正在发生的事情.Implode 是一个有趣的名字,但除此之外几乎毫无用处.

I changed the name of the task from "implode" to "restart" to be way more clear as to what is happening. Implode is a fun name but pretty much useless otherwise.

更新2

显然我的公司删除了博文.我在这里添加了更多的代码并且我已经更新了链接,但是 CSS 看起来像一只狗把它扔了.我很抱歉.

Apparently my company removed the blog post. I'm adding more of the code here and I've updated the link, but the CSS looks like a dog threw it up. My apologies.

这篇关于自动重启我的 heroku 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆