如何简单地防止Heroku休眠?

337

在Heroku的免费应用程序中,动态资源似乎会保持空闲状态 - 我的应用程序流量非常低,但是对于我的用户需要等待20多秒才能启动新的动态资源来说,这也不是真正可接受的情况。

坦白地说,在那种等待时间下,许多人甚至会在第一页显示之前就离开。

所以,我有一个问题:当我的流量每天只有个位数时,我是否应该支付36美元/月来节省每个用户尴尬的长达20秒的等待时间呢?

有没有办法解决这个问题?


13
根据Heroku的声明,从2015年7月1日起将不再支持此功能:https://blog.heroku.com/archives/2015/5/7/heroku-free-dynos。然而,他们推出了一种名为“爱好者”计划的服务,每月仅需支付7美元。 - Cristian
2
现在有许多虚拟服务器可用,每月仅需5美元(或按小时计费),提供完整的shell访问等功能。我已经使用DigitalOcean一年多了,但我相信还有其他竞争对手。如果Heroku不再免费,我认为这是一个更好的解决方案。 - sricks
2
@sricks 一个完整的VPS(需要您管理、更新、配置Git服务器、Web服务器、DB服务器、防火墙、Git钩子、备份、WAL-E等等)与Heroku(一种PaaS)是不同的东西。在某些情况下,您需要前者,在其他情况下,您需要后者,但原始资金应该是主要指标。时间和风险也应该考虑进去。 - elithrar
如何在 RoR 应用程序上安装 New Relic 代理?http://newrelic.com/ruby - James Brown
3
截至2016年6月1日,如果您使用信用卡进行验证,这将再次成为可能。[公告](https://devcenter.heroku.com/changelog-items/907)您将获得每月1000小时的免费使用时间,但一个月只有730小时。假设您只运行一个动态网络范本,那么您可以免费全天候使用。 - Luke B
24个回答

2

我认为最简单的解决方法是每30分钟自己ping一下自己的服务器。以下是我在Node.js项目中使用的代码,以防止休眠。

const request = require('request');
const ping = () => request('https://<my-app-name>.herokuapp.com/', (error, response, body) => {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print body of response received
});
setInterval(ping, 20*60*1000); // I have set to 20 mins interval

1

另一种可行的解决方案:wokeDyno 这是一个博客文章,介绍了它的工作原理: 在应用程序中集成它非常容易:

/* Example: as used with an Express app */

const express = require("express")
const wakeDyno = require("woke-dyno");

// create an Express app
const app = express();

// start the server, then call wokeDyno(url).start()
app.listen(PORT, () => {
    wakeDyno(DYNO_URL).start(); // DYNO_URL should be the url of your Heroku app
});

0
如果您正在使用带有Express的Node.js,则可以添加一个端点,每10分钟调用一次自身。
路由器:
app.get("/keep-alive",require("path/to/keepAlive.js").keepAlive);

keepAlive.js

let interval;

function keepAlive(req, res) {
   if(interval) return res.end();
   
   interval = setInterval(() => {
      fetch("http://your-heroku-subdomain/keep-alive")
         .catch(err => {/*handle error here*/});
      }
   ,60_000);


   return res.end();
}

module.exports = { keepAlive }

0

Freshping 是另一个免费的资源,可以让你的免费 Heroku 应用程序 24/7 保持活跃。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接