Class: Unrestful::JobsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Unrestful::JobsController
- Includes:
- ActionController::Live, JwtSecured, Utils
- Defined in:
- app/controllers/unrestful/jobs_controller.rb
Instance Method Summary collapse
Methods included from Utils
Instance Method Details
#live ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/controllers/unrestful/jobs_controller.rb', line 16 def live job = AsyncJob.new(job_id: params[:job_id]) response.headers['Content-Type'] = 'text/event-stream' # this might be messy but will breakout of redis subscriptions when # the app needs to be shutdown trap(:INT) { raise StreamInterrupted } # this is to keep redis connection alive during long sessions ticker = safe_thread "ticker:#{job.job_id}" do loop { job.redis.publish("unrestful:heartbeat", 1); sleep 5 } end sender = safe_thread "sender:#{job.job_id}" do job.subscribe do |on| on. do |chn, | # we need to add a newline at the end or # it will get stuck in the buffer msg = .end_with?("\n") ? : "#{}\n" response.stream.write msg end end end ticker.join sender.join rescue Redis::TimeoutError # ignore this rescue AsyncError => exc render json: Unrestful::FailResponse.render(exc., exc: exc) , status: :not_found rescue IOError # ignore as this could be the client disconnecting during streaming job.unsubscribe if job rescue StreamInterrupted job.unsubscribe if job ensure ticker.kill if ticker sender.kill if sender response.stream.close job.close if job end |
#status ⇒ Object
9 10 11 12 13 14 |
# File 'app/controllers/unrestful/jobs_controller.rb', line 9 def status job = AsyncJob.new(job_id: params[:job_id]) render(json: Unrestful::FailResponse.render("job #{job.job_id} doesn't exist"), status: :not_found) and return unless job.valid? render json: job.as_json end |