app/controllers/extface/handler_controller.rb in extface-0.1.5 vs app/controllers/extface/handler_controller.rb in extface-0.1.6

- old
+ new

@@ -11,29 +11,11 @@ def pull # request.body.read usable? : YES! return number of bytes received by device unless device.present? render nothing: true, status: :not_found else - response.headers['Content-Type'] = 'text/event-stream' - # find current job or get new one - Extface.redis_block do |r| - start = Time.now - if job = device.jobs.active.find_by(id: cookies[:extface]) || device.jobs.active.try(:first) - cookies.permanent[:extface] = job.id - p "Processing job #{job.id}" - list, data = r.blpop(job.id, timeout: 1) - while data - response.stream.write data - r.publish(job.id, "OK") - if (Time.now - start) > 3.seconds - p "Will continue next time" - #break - end - list, data = r.blpop(job.id, timeout: 1) - end - end - end #redis block + stream_job end rescue => e p e.message render nothing: true, status: :internal_server_error ensure @@ -54,16 +36,18 @@ if bytes_porcessed = device.driver.handle(@full_buffer) Extface.redis_block do |r| r.set device.uuid, r.get(device.uuid)[bytes_porcessed] end end + stream_job # stream right now :) status = :ok end - render nothing: true, status: status rescue => e p e.message render nothing: true, status: :internal_server_error + ensure + response.stream.close end def settings render text: 'settings' end @@ -73,8 +57,30 @@ @device ||= extfaceable.extface_devices.find_by(uuid: params[:device_uuid]) end def require_device render status: :not_found if device.nil? + end + + def stream_job + response.headers['Content-Type'] = 'text/event-stream' + # find current job or get new one + Extface.redis_block do |r| + start = Time.now + if job = device.jobs.active.find_by(id: cookies[:extface]) || device.jobs.active.try(:first) + cookies.permanent[:extface] = job.id + p "Processing job #{job.id}" + list, data = r.blpop(job.id, timeout: 1) + while data + response.stream.write data + r.publish(job.id, "OK") + if (Time.now - start) > 3.seconds + p "Will continue next time" + #break + end + list, data = r.blpop(job.id, timeout: 1) + end + end + end #redis block end end end