在Heroku的免费应用程序中,dyno似乎一直在空转——我的应用程序流量很低,但在我的情况下,我的用户不得不等待20多秒才能启动一个新的dyno,这也是不能接受的。
坦率地说,在这样的等待下,许多人甚至在第一页显示之前就离开了。
所以,我遇到了一个问题:当我每天的流量都在个位数时,我是否应该每月支付36美元来为每个用户节省令人尴尬的漫长20秒?
有没有办法解决这个问题?
在Heroku的免费应用程序中,dyno似乎一直在空转——我的应用程序流量很低,但在我的情况下,我的用户不得不等待20多秒才能启动一个新的dyno,这也是不能接受的。
坦率地说,在这样的等待下,许多人甚至在第一页显示之前就离开了。
所以,我遇到了一个问题:当我每天的流量都在个位数时,我是否应该每月支付36美元来为每个用户节省令人尴尬的漫长20秒?
有没有办法解决这个问题?
当前回答
你也可以试试http://kaffeine.herokuapp.com(由我制作),它是为了防止Heroku应用程序进入睡眠。它将每10分钟ping你的应用程序,所以你的应用程序不会进入睡眠状态。完全免费。
其他回答
这就是我的解。
使用谷歌应用程序脚本,设置时间触发器。
// main.js
function ping() {
UrlFetchApp.fetch("https://<Your app>.herokuapp.com/ping_from_GAS");
}
这很简单!
使用Node.js 0.10在我自己的Heroku应用上进行了测试和工作。X在2013年6月28日
var http = require('http'); //importing http
function startKeepAlive() {
setInterval(function() {
var options = {
host: 'your_app_name.herokuapp.com',
port: 80,
path: '/'
};
http.get(options, function(res) {
res.on('data', function(chunk) {
try {
// optional logging... disable after it's working
console.log("HEROKU RESPONSE: " + chunk);
} catch (err) {
console.log(err.message);
}
});
}).on('error', function(err) {
console.log("Error: " + err.message);
});
}, 20 * 60 * 1000); // load every 20 minutes
}
startKeepAlive();
在一个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();
}
}
一份cron工作就可以了。见https://cron-job.org。它是免费和可靠的。
还有一个有效的解决方案: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
});