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.['Content-Type'] = 'text/event-stream'
trap(:INT) { raise StreamInterrupted }
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.message do |chn, message|
msg = message.end_with?("\n") ? message : "#{message}\n"
response.stream.write msg
end
end
end
ticker.join
sender.join
rescue Redis::TimeoutError
rescue AsyncError => exc
render json: Unrestful::FailResponse.render(exc.message, exc: exc) , status: :not_found
rescue IOError
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
|