Blog · Articles tagged with "rails" · show all articles »

How to make everything background-processable through Sidekiq

I recently switched from Resque to Sidekiq for background processing in a project I'm working on. Sidekiq has a very slick feel to it and is basically a supercharged version of Resque, using threads instead of processed for its workers.

One thing that has irritated me when using Resque was that one was supposed to create separate worker classes for each job that should be processed in the background. While that makes sense for "real" jobs, e.g. syncing the Mailchimp database with your own or exporting some data into a XML file and mailing it out, often I just wanted to make an existing (class or instance) method processable in the background, e.g. user.perform_async :set_timezone_from_ip, request.remote_ip or Account.perform_async :disable_overdue.

Sidekiq offers the delay and delay_for methods to make class and instance method calls asynchronous, as detailed in their Wiki. It also says the following on that Wiki page, though:

I strongly recommend avoid delaying methods on instances. This stores object state in Redis and which can get out of date, causing stale data problems."

Read on

How to compile custom Sass stylesheets dynamically during runtime

Update January 15 2016: As someone pointed out in a comment on the gist, this method doesn't seem to work anymore with recent versions of Sprockets.

Update May 29 2013: The name of the stylesheet files was changed slightly (hyphen replaced by underscore) after feedback in the comments.

In any Rails 3.1 (or newer) app where the user can change the style (e.g. layout, colors, dimensions) of certain items you come to the point where you wish you could use all of the sweetness of the Rails asset pipeline to generate custom stylesheets dynamically during runtime.

After a bit of googling I realized what it comes down to is imitating the process Rails performs when precompiling all assets on deploy, only that we want to want to compile Sass code that was generated dynamically, not read from a static file in app/assets/javascripts. After having a look at the assets:precompile Rake task which does the heavy lifting it's clear that Sprockets::StaticCompiler is the main suspect in this case.

Read on