Sha256: 69079e2a127a54418c51ad1f34d01fadfc89881ebade9927ed067b83dfc41a33

Contents?: true

Size: 1.37 KB

Versions: 52

Compression:

Stored size: 1.37 KB

Contents

require_dependency "extface/application_controller"

module Extface
  class JobsController < ApplicationController
    include ActionController::Live
    
    skip_before_filter :include_extra_module
    before_action :allow_cross_origin

    def allow_cross_origin
      headers['Access-Control-Allow-Origin'] = '*'
    end

    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
    rescue Timeout::Error
      #TODO invite reconnect
    ensure
      response.stream.close
    end

  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
extface-0.7.3 app/controllers/extface/jobs_controller.rb
extface-0.7.2 app/controllers/extface/jobs_controller.rb
extface-0.7.1 app/controllers/extface/jobs_controller.rb
extface-0.7.0 app/controllers/extface/jobs_controller.rb
extface-0.6.9 app/controllers/extface/jobs_controller.rb
extface-0.6.8 app/controllers/extface/jobs_controller.rb
extface-0.6.7 app/controllers/extface/jobs_controller.rb
extface-0.6.6 app/controllers/extface/jobs_controller.rb
extface-0.6.5 app/controllers/extface/jobs_controller.rb
extface-0.6.4 app/controllers/extface/jobs_controller.rb
extface-0.6.3 app/controllers/extface/jobs_controller.rb
extface-0.6.2 app/controllers/extface/jobs_controller.rb
extface-0.6.1 app/controllers/extface/jobs_controller.rb
extface-0.6.0 app/controllers/extface/jobs_controller.rb
extface-0.5.9 app/controllers/extface/jobs_controller.rb
extface-0.5.8 app/controllers/extface/jobs_controller.rb
extface-0.5.7 app/controllers/extface/jobs_controller.rb
extface-0.5.6 app/controllers/extface/jobs_controller.rb
extface-0.5.5 app/controllers/extface/jobs_controller.rb
extface-0.5.4b app/controllers/extface/jobs_controller.rb