Created
March 14, 2013 19:01
-
-
Save jacobo/5164180 to your computer and use it in GitHub Desktop.
Example of running background jobs in thin without needing a separate background job processor (or spawning threads)
This file contains hidden or 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_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