Thursday, March 15, 2012

Make scheduled jobs in heroku safer for your wallet.

I recently checked my current usage on Heroku to find that I had used a much higher than expected number of dyno-hours. The project CheckTheSock.com is a RC aviation website that provides some weather data. The forecast data is pulled every hour via FTP by a rake task that is run with a scheduled job in Heroku. Normally this takes around a minute to run and uses about 13 or 14 dyno-hours in a month. Less than half way through this month I found that it had already burned through over 200 dyno-hours.

After digging into the problem I found that the FTP connection was hanging for some reason. After a little research I came up with a solution that works pretty nicely and for more than just hanging FTP connections.

It turns out that ruby's standard library includes a library named Timeout which provides a way to terminate the execution of a thread. Using this library it is very simple to set a time limit for a block of code in a rake test.

task :task_that_takes_a_long_time_sometimes do
  Timeout.timeout(30) do
    #The code that takes a long time to run sometimes
  end
end

That will raise a Timeout::Error if the code takes longer than 30 seconds to run. The error will cause the rake task to exit, preventing unexpected dyno-hour usage.

12 comments:

Jodi W. Brown said...

So what do you think of what we are hearing here? Are you excited for this film and Debney's score?Executive Employers Review

Talha Yeasin said...

Plagiarism Checker are wonderful tools to hit upon plagiarism in files. However, you need to remember the fact that they can not access password protected areas or some thing that isn't always in their databases. Try this to get all http://www.plagiarism-checker.me/

Maani kamili said...

I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time? Thanks for the post..

Digital marketing company in Chennai

Sowmiya said...


Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. thus it is really a good conclusion work.

Online Reputation Management

Sathya G said...


this is really a very great blog. the information present in this blow will be very useful for us. thank you for sharing with us.
ROI Services in Chennai

Sathya G said...


this is really a very great blog. the information present in this blow will be very useful for us. thank you for sharing with us.
ROI Services in Chennai

Giri Mani 2 said...

This blog explains the details about changing the ways of doing that business. That is understand well and doing some different process. Provides he best output of others. Thanks for this blog.
Digital Marketing Company in Chennai

Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.

seo company in india

sathya said...

Excellent .. Amazing .. I will bookmark your blog and take the feeds additionally? I’m satisfied to find so many helpful information here within the put up, we want work out extra strategies in this regard, thanks for sharing..

Hadoop Training in Chennai

Base SAS Training in Chennai

tessa tessa said...

This is a great post. I like this topic.This site has lots of advantage. It helps me in many ways.Thanks for posting this again.

SEO Company in India

oracle fusion said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Your write up is a fine example of it.For more details about oracle fusion financial please check our website.

Oracle Fusion Financial Training Institute

Ramya Krishnan said...

Nice Blog, Thanks for sharing your best work.
Oracle DBA Training institute in chennai | Oracle DBA Training institute in velachery