forked from zendesk/samson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams_controller.rb
61 lines (48 loc) · 1.44 KB
/
streams_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
55
56
57
58
59
60
61
class StreamsController < ApplicationController
newrelic_ignore if respond_to?(:newrelic_ignore)
include ActionController::Live
include ApplicationHelper
include DeploysHelper
include JobsHelper
def show
response.headers['Content-Type'] = 'text/event-stream'
response.headers['Cache-Control'] = 'no-cache'
@job = Job.find(params[:id])
@execution = JobExecution.find_by_id(@job.id)
streamer = EventStreamer.new(response.stream, &method(:event_handler))
return response.stream.close unless @job.active? && @execution
@execution.viewers.push(current_user)
ActiveRecord::Base.clear_active_connections!
streamer.start(@execution.output)
end
private
def event_handler(event, data)
case event
when :viewers
viewers = data.uniq.reject {|user| user == current_user}
viewers.to_json(only: [:id, :name])
when :finished
finished_response
else
JSON.dump(msg: render_log(data))
end
end
def finished_response
@execution.viewers.delete(current_user) if @execution
ActiveRecord::Base.connection.verify!
@job.reload
@project = @job.project
@deploy = @job.deploy
if @deploy
JSON.dump(
title: deploy_page_title,
html: render_to_body(partial: 'deploys/header', formats: :html)
)
else
JSON.dump(
title: job_page_title,
html: render_to_body(partial: 'jobs/header', formats: :html)
)
end
end
end