public
Created

Example of running background jobs in thin without needing a separate background job processor (or spawning threads)

  • Download Gist
hax_background_jobs_in_thin.ru
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#Save this file as hax_background_jobs_in_thin.ru
#In one terminal run: thin start --rackup hax_background_jobs_in_thin.ru
#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
end
def call(env)
status, headers, body = @app.call(env)
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"
end
end
[status, headers, body]
end
end
 
use CleverMailSender
run lambda{ |env|
CleverMailSender.emails_to_send << "Hi Mom"
[200, {"Content-Type" => "text/html"}, ["Hello World\n"]]
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.