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.4.5 app/controllers/extface/jobs_controller.rb
extface-0.4.4b app/controllers/extface/jobs_controller.rb
extface-0.4.4a app/controllers/extface/jobs_controller.rb
extface-0.4.4 app/controllers/extface/jobs_controller.rb
extface-0.4.3g app/controllers/extface/jobs_controller.rb
extface-0.4.3f app/controllers/extface/jobs_controller.rb
extface-0.4.3e app/controllers/extface/jobs_controller.rb
extface-0.4.3d app/controllers/extface/jobs_controller.rb
extface-0.4.3c app/controllers/extface/jobs_controller.rb
extface-0.4.3b app/controllers/extface/jobs_controller.rb
extface-0.4.3a app/controllers/extface/jobs_controller.rb
extface-0.4.2a app/controllers/extface/jobs_controller.rb