Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of running background jobs in thin without needing a separate background job processor (or spawning threads)
#Save this file as
#In one terminal run: thin start --rackup
#In another run: curl -v localhost:3000
class CleverMailSender
class << self; attr_accessor :emails_to_send; end
self.emails_to_send = []
def initialize(app)
@app = app
def call(env)
status, headers, body =
EM.next_tick do
while(email = CleverMailSender.emails_to_send.pop)
puts "sending e-mail: #{email}"
5.times{|x| puts x; sleep 0.5}
puts "sent"
[status, headers, body]
use CleverMailSender
run lambda{ |env|
CleverMailSender.emails_to_send << "Hi Mom"
[200, {"Content-Type" => "text/html"}, ["Hello World\n"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment