Forked from jacobo/hax_background_jobs_in_unicorn.ru
Last active
August 29, 2015 14:22
-
-
Save cinic/1d5da3585dd78ebb18b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Save this file as hax_background_jobs_in_unicorn.ru | |
#In one terminal run: unicorn hax_background_jobs_in_unicorn.ru | |
#In another run: curl -v localhost:8080 | |
class CleverMailSender | |
class << self; attr_accessor :emails_to_send; end | |
self.emails_to_send = [] | |
class BodyProxy | |
def initialize(body) | |
@body = body | |
end | |
def each | |
@body.each{|x| yield x} | |
while(email = CleverMailSender.emails_to_send.pop) | |
puts "sending e-mail: #{email}" | |
5.times{|x| puts x; sleep 0.5} | |
puts "sent" | |
end | |
end | |
end | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
unless headers["Content-Length"] | |
content_length = 0 | |
body = body.map{|x| content_length += x.to_s.bytesize; x.to_s} | |
headers["Content-Length"] = content_length.to_s | |
end | |
[status, headers, BodyProxy.new(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