Guest User

Untitled

a guest
Apr 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. I am working on a smallish web framework for serving web services, totally asynchronously.A Sample controller looks like this:
  2.  
  3. class HomeController < MsfService::Controller
  4. def index
  5. request_lamda = lambda do |t|
  6. t.quote("IBM")
  7. end
  8. response_lambda = lambda do |quote|
  9. @ready_to_render = true
  10. render :html => "#{quote.last}"
  11. end
  12. backend_server.defer(request_lamda,response_lambda)
  13. end
  14. end
  15.  
  16. In a word when data request in request proc is ready then only view is rendered. So, I can't invoke call then and there as rack specification requires and hence I have modified thin a bit so as:
  17.  
  18. def process
  19. @request.threaded = false
  20. pre_process
  21. end
  22.  
  23. def pre_process
  24. # Add client info to the request env
  25. @request.remote_address = remote_address
  26.  
  27. # Process the request calling the Rack adapter
  28. @app.call(@request.env) do |result|
  29. post_process(result)
  30. end
  31. rescue Object
  32. handle_error
  33. terminate_request
  34. post_process(nil) # Signal to post_process that the request could not be processed
  35. end
  36.  
  37. Originally post_process immediately gets called after pre_process, but here I am attaching post_process as a lambda so as it gets called only when response is ready. Above code works, but it partially breaks rack. I am unable to use CommonLogger or other helper classes.
Add Comment
Please, Sign In to add comment