Sha256: 0f7e51cc08ef14324c02b0aa4e6bf503f892eedf2ec9315d1089153855642d39

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require_dependency "extface/application_controller"

module Extface
  class JobsController < ApplicationController
    include ActionController::Live

    def show
      response.headers['Content-Type'] = 'text/event-stream'
      @job = Job.find(params[:id])
      if @job.completed?
        response.stream.write("data: Job #{@job.id} completed!\n\n")
      elsif @job.failed?
        response.stream.write("data: Job #{@job.id} failed!\n\n")
      else
        #redis = Redis.new
        response.stream.write("data: Job #{@job.id} waiting for device connection...\n\n")
        Timeout.timeout(Extface.device_timeout) do #never stay too long, TODO add SSE option to reconnect
          Extface.redis_block do |r|
            r.subscribe(@job.id) do |on|
              on.message do |event, data|
                p "@@@@ #{event}: #{data}\n\n"
                response.stream.write("data: #{data}\n\n") unless data == 'OK'
                r.unsubscribe if data == "Job #{@job.id} completed!" || data == "Job #{@job.id} failed!" #FIXME stupid
              end
            end
          end
        end
      end
    ensure
      response.stream.close
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
extface-0.3.0c app/controllers/extface/jobs_controller.rb
extface-0.3.0b app/controllers/extface/jobs_controller.rb
extface-0.3.0a app/controllers/extface/jobs_controller.rb