在Heroku的免费应用程序中,dyno似乎一直在空转——我的应用程序流量很低,但在我的情况下,我的用户不得不等待20多秒才能启动一个新的dyno,这也是不能接受的。

坦率地说,在这样的等待下,许多人甚至在第一页显示之前就离开了。

所以,我遇到了一个问题:当我每天的流量都在个位数时,我是否应该每月支付36美元来为每个用户节省令人尴尬的漫长20秒?

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


当前回答

在一个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();
}
}

其他回答

我认为最简单的解决方法是每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

我有一个只需要在周一到周五午餐时间运行的应用程序。 我刚刚在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

这就是我的解。

使用谷歌应用程序脚本,设置时间触发器。

// main.js
function ping() {
  UrlFetchApp.fetch("https://<Your app>.herokuapp.com/ping_from_GAS");
}

这很简单!

作为Pingdom的替代方案,我建议尝试Uptimerobot。它是免费的,并提供5分钟间隔的现场检查。这对我来说很好。

2015年5月7日更新:这将是不可能的,因为Heroku将改变他们的免费动态,以防止保持完整的24小时。

另一个重要的变化与动态睡眠或“空转”有关。虽然非付费应用总是在活动超时后休眠,但一些应用使用自动ping服务来防止这种行为。免费的dynos允许在24小时内保持18小时的清醒状态,在接下来的几周内,我们将开始通知用户超过这一限制的应用程序。随着爱好dyno的引入(每月7美元),我们要求你的应用在超时后休眠,或者升级到这个新选项。

什么时候能直播?根据他们的博客文章:

运行单个1X动态的应用程序不会累积任何其他动态 动态电荷将逐渐迁移到新的自由动态电荷 7月1日开始。

它在Heroku文档中说,拥有超过1个web dyno将永远不会闲置。可能有一个比皮埃尔建议的每小时0.09美元更便宜的解决方案。

文档