Skip to content

@jacobo /hax_background_jobs_in_thin.ru
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Example of running background jobs in thin without needing a separate background job processor (or spawning threads)
#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"]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.