{"id":3,"date":"2010-02-09T02:44:27","date_gmt":"2010-02-09T02:44:27","guid":{"rendered":"http:\/\/blog.railsware.com\/?p=3"},"modified":"2023-11-01T15:54:19","modified_gmt":"2023-11-01T12:54:19","slug":"background-jobs-evolution","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/background-jobs-evolution\/","title":{"rendered":"Background jobs evolution: Rake, custom daemons, Resque"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Background Rake tasks<\/h3>\n\n\n\n<p>When we start one of our projects there&nbsp;is always&nbsp;the need for some&nbsp;background tasks: updating online\/offline user counts, doing some remote API calls, fetching a fresh version of GeoIP database, etc.<br>The easiest and most straightforward solution was the creation of appropriate Rake tasks and scheduling their&nbsp;use using Cron. It is easy to implement, easy to test (just run a Rake task), and we used a perfect <a href=\"http:\/\/github.com\/javan\/whenever\">Whenever<\/a> gem for defining those Cron jobs with a clean Ruby syntax.<\/p>\n\n\n\n<p>As our&nbsp;projects progressed, the&nbsp;number of background tasks increased. Most of our Rake tasks had one great disadvantage: they were dependent on Rails itself, and so every time we launched those tasks new Rails instances were initialized. It was&nbsp;OK for us to have Rake jobs, each of which takes, let&#8217;s say, 5-7 seconds and 100mb of RAM to initialize Rails and then less than a few seconds for doing the job itself, because we had just 2-3 jobs per hour.<br>Once we got about 3-4 different tasks running, each 5-10 seconds for every job, Rails initialization time and its memory consumption became a problem. So we had to use find a thriftier solution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Ruby daemons<\/h3>\n\n\n\n<!--more-->\n\n\n\n<p>Custom Ruby daemons (as were shown in <a href=\"http:\/\/railscasts.com\/episodes\/129-custom-daemon\">Railscast<\/a> by Ryan Bates) seemed a good alternative to the Rake and Cron combination. We just moved scheduling from Cron to daemon classes and pulled task calls there from Rake tasks. So we ended up with a few daemons, each for a group of background tasks (it&#8217;s possible to put all the tasks into a single daemon, but this will complicate daemon logic dramatically). We also found it useful to&nbsp;use Cron for restarting deamons a few time per day as&nbsp;a way to fight Ruby memory bloats.<br>This solution was quite resource-friendly, but brought a lot of drawbacks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Daemons gem (or its launcher) was quite buggy. For example, it didn&#8217;t restart daemons properly: after each restart previous daemons were not unloaded from the memory.<\/li>\n\n\n\n<li>Debugging these daemons is hard as hell!<\/li>\n\n\n\n<li>Sometimes daemons died or become frozen unexpectedly, so we had to use a <a href=\"https:\/\/betterstack.com\/community\/comparisons\/cloud-monitoring-tools\/\" title=\"monitoring tool\">monitoring tool<\/a> (God) for checking daemon health and restarting it when needed, instead of general Cron restarts.<\/li>\n\n\n\n<li>It was impossible to check job progress and statistics without implementing additional logic in daemon and\/or web app.<\/li>\n\n\n\n<li>The solution was not scalable out of the box, and&nbsp;we had to implement&nbsp;a type&nbsp;of job queue logic if we wanted to distribute daemons into different server nodes.<\/li>\n<\/ol>\n\n\n\n<p>But still&nbsp;we were a manly men: we struggled, we fought the bugs and found some workarounds, we were even ready to build statistics support and a job queue into our daemons.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.flickr.com\/photos\/le_haricot\/132869163\/\"><img decoding=\"async\" src=\"\/\/farm1.static.flickr.com\/51\/132869163_3a547ea876.jpg\" alt=\"Job Queue\"\/><\/a><\/figure>\n\n\n\n<p>Yet, soon our project required a different type of asynchronous tasks \u2014 nonscheduled ones. I mean nonrecurring tasks that have to be addressed&nbsp;ASAP. Usually&nbsp;this means&nbsp;slow controller actions that can be offloaded from Rails to speed up request handling, for example actions that require remote API calls for rendering a page part.<br>That was a turning point, because we decided to stop just being manly men and to start being a little smarter. We had to improve our daemons or pick a solution that didn&#8217;t require us to reinvent the wheel, for example, by&nbsp;taking existing background jobs solutions like Workling or Delayed Job.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Resque rescued us from pain<\/h3>\n\n\n\n<p>Luckily, Github at about that time released their Resque tool inspired by Delayed Job (but more advanced) and based on Redis database&nbsp;for its&nbsp;job queue. Resque suited our needs very well. We picked it instead of a custom daemon and finally become happy!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">So, why do we love resque so much?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It just works!<\/li>\n\n\n\n<li>It uses Redis as the job queue storage, and&nbsp;Redis is blazing fast.<\/li>\n\n\n\n<li>It supports multiple job queues and queue priorities.<\/li>\n\n\n\n<li>Horizontal scaling is a peace of cake: you can just spread the workers over your server nodes.<\/li>\n\n\n\n<li>Resque is lightweight, and the code is extremely well documented. So there is always no mystification about &#8220;why it work this way, not that way?&#8221; \u2014 you can analyze all the Resque source code in less than an hour.<\/li>\n\n\n\n<li>It has a nice rack-compatible application for tracking jobs and workers&#8217; statistics built in. In addition you can just plug in&nbsp;HopToad (using instructions in the Resque source code) to track worker errors.<\/li>\n\n\n\n<li>As&nbsp;previously mentioned,&nbsp;Resque is lightweight and extremely well documented, so it is easy to extend! Just fork it on to GitHub and do whatever you want.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">OK, I see. So, what&#8217;s bad about Resque?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It uses Redis&nbsp;for the job queue storage. There is no clean way of using Redis as a cluster database at this moment, just a master-slave replication. So Redis may&nbsp;constitute a&nbsp;single point of failure unless you have additional failover logic in your application.<\/li>\n\n\n\n<li>Workers are pure Ruby. So you still need to deal with its monitoring with God, Monit or another monitoring tool.<\/li>\n\n\n\n<li>And as a pure Ruby tool Resque is not quite suited for multi-technology enterprise environments where you&#8217;ll prefer setting tasks as huge XML documents. Anyway, you can achieve this weird stuff with some small workarounds &#8230; remember? Resque is easy to extend!<\/li>\n\n\n\n<li>It doesn&#8217;t have any scheduling mechanism for scheduling task processing for specific times or time periods. Again, you can fix this&nbsp;by using cron and adding a few lines of code to your app. I&#8217;ll show you.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">How do we use it?<\/h4>\n\n\n\n<p>There are two types of tasks settings. The&nbsp;first one is for non-reccuring tasks: workers are using Rails, and tasks are queued&nbsp;directly from the rails app.<\/p>\n\n\n\n<p>The second&nbsp;type of tasks are recurring tasks that&nbsp;need to&nbsp;be scheduled. We use a special Rails controller for putting tasks outside of the main application. Restful HTTP client (launched as a Cron job) enqueues tasks on Resque. However, some people may&nbsp;consider this overengineering,&nbsp;since even&nbsp;a&nbsp;CURL-based client may be enough.<br>Whenever gem is used for managing Cron schedules of reccuring tasks. [Not sure about the logic of this sentence. The use of Gem triggers Cron scheduling for recurring tasks? Can we clarify? Thanks.]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Resque and PostgreSQL<\/h3>\n\n\n\n<p>There is an issue if you use PostgreSQL database and access it via ActiveRecord from Resque workers. It looks like Resque forks its workers and tries&nbsp;to reuse the same PostgreSQL connection, while PostgreSQL does not allow&nbsp;the use of&nbsp;the same connection from different processes. Of course, you can modify the Resque source code or tweak its workers but we found&nbsp;a much&nbsp;easier workaround: <a href=\"http:\/\/pgpool.projects.postgresql.org\/\">PgPool-II<\/a> for PostgreSQL connection pooling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are the&nbsp;alternatives?<\/h3>\n\n\n\n<p>Guys at a Ratepoint have much higher loads and require more flexibility. So they implemented their own amqp-consuming daemons based on a Daemon-kit Gem, Nanite and RabbitMQ as a job queue. And I know that&nbsp;they are quite happy with this custom solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background Rake tasks When we start one of our projects there&nbsp;is always&nbsp;the need for some&nbsp;background tasks: updating online\/offline user counts, doing some remote API calls, fetching a fresh version of GeoIP database, etc.The easiest and most straightforward solution was the creation of appropriate Rake tasks and scheduling their&nbsp;use using Cron. It is easy to implement,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["admin"],"class_list":["post-3","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"\/\/farm1.static.flickr.com\/51\/132869163_3a547ea876.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=3"}],"version-history":[{"count":21,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions"}],"predecessor-version":[{"id":16643,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions\/16643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/3764"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=3"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=3"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=3"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=3"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}