在Heroku的免费应用程序中,dyno似乎一直在空转——我的应用程序流量很低,但在我的情况下,我的用户不得不等待20多秒才能启动一个新的dyno,这也是不能接受的。
坦率地说,在这样的等待下,许多人甚至在第一页显示之前就离开了。
所以,我遇到了一个问题:当我每天的流量都在个位数时,我是否应该每月支付36美元来为每个用户节省令人尴尬的漫长20秒?
有没有办法解决这个问题?
在Heroku的免费应用程序中,dyno似乎一直在空转——我的应用程序流量很低,但在我的情况下,我的用户不得不等待20多秒才能启动一个新的dyno,这也是不能接受的。
坦率地说,在这样的等待下,许多人甚至在第一页显示之前就离开了。
所以,我遇到了一个问题:当我每天的流量都在个位数时,我是否应该每月支付36美元来为每个用户节省令人尴尬的漫长20秒?
有没有办法解决这个问题?
当前回答
你也可以试试http://kaffeine.herokuapp.com(由我制作),它是为了防止Heroku应用程序进入睡眠。它将每10分钟ping你的应用程序,所以你的应用程序不会进入睡眠状态。完全免费。
其他回答
如果你正在使用带有express的nodejs,你可以添加一个每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 }
我有一个只需要在周一到周五午餐时间运行的应用程序。 我刚刚在crontab中添加了以下脚本:
#!/bin/sh
# script to unidle heroku installation for the use with cronjob
# usage in crontab:
# */5 11-15 * * 1-5 /usr/local/bin/uptimer.sh http://www.example.com
# The command /usr/local/bin/uptimer.sh http://www.example.com will execute every 5th minute of 11am through 3pm Mondays through Fridays in every month.
# resources: http://www.cronchecker.net
echo url to unidle: $1
echo [UPTIMER]: waking up at:
date
curl $1
echo [UPTIMER]: awake at:
date
因此,对于任何应用程序,只需在crontab中删除另一行:
*/5 11-15 * * 1-5 /usr/local/bin/uptimer.sh http://www.example.com
在我看来,使用服务的“免费”层不应该为产品或面向客户的应用程序提供动力。虽然上面的解决方案可以防止Dyno空转,但请仔细考虑您正在做的事情。
如果没有其他方法,可以使用cron作业来ping你的站点,并在已知的低使用率时期(例如,夜间)禁用检查,以确保Heroku不会为其他所有人取消免费层。
你可以安装免费的New Relic插件。它有一个可用性监控功能,每分钟会ping你的站点两次,从而防止dyno空转。
或多或少与Jesse的解决方案相同,但可能与Heroku更融合…而且还有一些额外的功能(性能监控非常棒)。
注意:对于所有那些说它不起作用的人:我的答案中重要的部分是“可用性监视器”。仅仅安装插件是没有用的。您还需要使用heroku应用程序的URL设置可用性监视。
在一个spring应用程序中,每2分钟向根url路径发出一个HTTP请求 `
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;
public class HerokuNotIdle {
private static final Logger LOG = LoggerFactory.getLogger(HerokuNotIdle.class);
@Scheduled(fixedDelay=120000)
public void herokuNotIdle(){
LOG.debug("Heroku not idle execution");
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://yourapp.herokuapp.com/", Object.class);
}
}
记住,配置上下文以启用调度器并为调度器创建bean
@EnableScheduling
public class AppConfig {
@Bean
public HerokuNotIdle herokuNotIdle(){
return new HerokuNotIdle();
}
}