forked from zendesk/samson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams_controller.rb
67 lines (55 loc) · 1.85 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
62
63
64
65
66
67
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'
response.headers['Access-Control-Allow-Origin'] = Rails.application.config.samson.uri.to_s
response.headers['Access-Control-Allow-Credentials'] = true
@job = Job.find(params[:id])
@execution = JobExecution.find_by_id(@job.id)
return response.stream.close unless @job.active? && @execution
@execution.viewers.push(current_user)
ActiveRecord::Base.clear_active_connections!
EventStreamer.new(response.stream, &method(:event_handler)).
start(@execution.output)
end
private
def event_handler(event, data)
case event
when :started, :finished
status_response(event)
when :viewers
viewers = data.uniq.reject {|user| user == current_user}
viewers.to_json(only: [:id, :name])
else
JSON.dump(msg: render_log(data))
end
end
def status_response(event)
@execution.viewers.delete(current_user) if event == :finished
# Need to reload data, as background thread updated the records on a separate DB connection,
# and .reload() doesn't bypass QueryCache'ing.
ActiveRecord::Base.uncached do
@job.reload
@project = @job.project
@deploy = @job.deploy
end
if @deploy
params = {
title: deploy_page_title,
html: render_to_body(partial: 'deploys/header', formats: :html)
}
params[:notification] = deploy_notification if event == :finished
JSON.dump(params)
else
JSON.dump(
title: job_page_title,
html: render_to_body(partial: 'jobs/header', formats: :html)
)
end
end
end