Commit f2303f9d authored by Jacob Schatz's avatar Jacob Schatz

Merge branch 'send-incremental-build-log' into 'master'

Update build log incrementally

Proof of concept implementation of incremental sending of build log to browser.

cc @jschatz1 @vsizov @grzesiek @tmaczukin 


See merge request !3737
parents 95163307 5bd356eb
class CiBuild class CiBuild
@interval: null @interval: null
@state: null
constructor: (build_url, build_status) -> constructor: (build_url, build_status, build_state) ->
clearInterval(CiBuild.interval) clearInterval(CiBuild.interval)
@state = build_state
@initScrollButtonAffix() @initScrollButtonAffix()
if build_status == "running" || build_status == "pending" if build_status == "running" || build_status == "pending"
...@@ -26,14 +29,18 @@ class CiBuild ...@@ -26,14 +29,18 @@ class CiBuild
CiBuild.interval = setInterval => CiBuild.interval = setInterval =>
if window.location.href.split("#").first() is build_url if window.location.href.split("#").first() is build_url
$.ajax $.ajax
url: build_url url: build_url + "/trace.json?state=" + encodeURIComponent(@state)
dataType: "json" dataType: "json"
success: (build) => success: (log) =>
if build.status == "running" @state = log.state
$('#build-trace code').html build.trace_html if log.status is "running"
$('#build-trace code').append '<i class="fa fa-refresh fa-spin"/>' if log.append
$('.fa-refresh').before log.html
else
$('#build-trace code').html log.html
$('#build-trace code').append '<i class="fa fa-refresh fa-spin"/>'
@checkAutoscroll() @checkAutoscroll()
else if build.status != build_status else if log.status isnt build_status
Turbolinks.visit build_url Turbolinks.visit build_url
, 4000 , 4000
......
...@@ -38,6 +38,14 @@ class Projects::BuildsController < Projects::ApplicationController ...@@ -38,6 +38,14 @@ class Projects::BuildsController < Projects::ApplicationController
end end
end end
def trace
respond_to do |format|
format.json do
render json: @build.trace_with_state(params[:state]).merge!(id: @build.id, status: @build.status)
end
end
end
def retry def retry
unless @build.retryable? unless @build.retryable?
return render_404 return render_404
......
...@@ -95,8 +95,12 @@ module Ci ...@@ -95,8 +95,12 @@ module Ci
end end
def trace_html def trace_html
html = Ci::Ansi2html::convert(trace) if trace.present? trace_with_state[:html] || ''
html || '' end
def trace_with_state(state = nil)
trace_with_state = Ci::Ansi2html::convert(trace, state) if trace.present?
trace_with_state || {}
end end
def timeout def timeout
......
- page_title "#{@build.name} (##{@build.id})", "Builds" - page_title "#{@build.name} (##{@build.id})", "Builds"
= render "header_title" = render "header_title"
- trace_with_state = @build.trace_with_state
.build-page .build-page
.row-content-block.top-block .row-content-block.top-block
...@@ -85,7 +86,9 @@ ...@@ -85,7 +86,9 @@
%pre.trace#build-trace %pre.trace#build-trace
%code.bash %code.bash
= preserve do = preserve do
= raw @build.trace_html = raw trace_with_state[:html]
- if @build.active?
%i{:class => "fa fa-refresh fa-spin"}
%div#down-build-trace %div#down-build-trace
...@@ -216,4 +219,4 @@ ...@@ -216,4 +219,4 @@
:javascript :javascript
new CiBuild("#{namespace_project_build_url(@project.namespace, @project, @build)}", "#{@build.status}") new CiBuild("#{namespace_project_build_url(@project.namespace, @project, @build)}", "#{@build.status}", "#{trace_with_state[:state]}")
...@@ -668,6 +668,7 @@ Rails.application.routes.draw do ...@@ -668,6 +668,7 @@ Rails.application.routes.draw do
post :cancel post :cancel
post :retry post :retry
post :erase post :erase
get :trace
get :raw get :raw
end end
......
...@@ -23,8 +23,8 @@ module Ci ...@@ -23,8 +23,8 @@ module Ci
cross: 0x10, cross: 0x10,
} }
def self.convert(ansi) def self.convert(ansi, state = nil)
Converter.new().convert(ansi) Converter.new.convert(ansi, state)
end end
class Converter class Converter
...@@ -84,22 +84,36 @@ module Ci ...@@ -84,22 +84,36 @@ module Ci
def on_107(s) set_bg_color(7, 'l') end def on_107(s) set_bg_color(7, 'l') end
def on_109(s) set_bg_color(9, 'l') end def on_109(s) set_bg_color(9, 'l') end
def convert(ansi) attr_accessor :offset, :n_open_tags, :fg_color, :bg_color, :style_mask
@out = ""
@n_open_tags = 0 STATE_PARAMS = [:offset, :n_open_tags, :fg_color, :bg_color, :style_mask]
reset()
def convert(raw, new_state)
reset_state
restore_state(raw, new_state) if new_state
start = @offset
ansi = raw[@offset..-1]
open_new_tag
s = StringScanner.new(ansi.gsub("<", "&lt;")) s = StringScanner.new(ansi)
while(!s.eos?) while(!s.eos?)
if s.scan(/\e([@-_])(.*?)([@-~])/) if s.scan(/\e([@-_])(.*?)([@-~])/)
handle_sequence(s) handle_sequence(s)
elsif s.scan(/\e(([@-_])(.*?)?)?$/)
break
elsif s.scan(/</)
@out << '&lt;'
else else
@out << s.scan(/./m) @out << s.scan(/./m)
end end
@offset += s.matched_size
end end
close_open_tags() close_open_tags()
@out
{ state: state, html: @out, text: ansi[0, @offset - start], append: start > 0 }
end end
def handle_sequence(s) def handle_sequence(s)
...@@ -121,6 +135,20 @@ module Ci ...@@ -121,6 +135,20 @@ module Ci
evaluate_command_stack(commands) evaluate_command_stack(commands)
open_new_tag
end
def evaluate_command_stack(stack)
return unless command = stack.shift()
if self.respond_to?("on_#{command}", true)
self.send("on_#{command}", stack)
end
evaluate_command_stack(stack)
end
def open_new_tag
css_classes = [] css_classes = []
unless @fg_color.nil? unless @fg_color.nil?
...@@ -138,20 +166,8 @@ module Ci ...@@ -138,20 +166,8 @@ module Ci
css_classes << "term-#{css_class}" if @style_mask & flag != 0 css_classes << "term-#{css_class}" if @style_mask & flag != 0
end end
open_new_tag(css_classes) if css_classes.length > 0 return if css_classes.empty?
end
def evaluate_command_stack(stack)
return unless command = stack.shift()
if self.respond_to?("on_#{command}", true)
self.send("on_#{command}", stack)
end
evaluate_command_stack(stack)
end
def open_new_tag(css_classes)
@out << %{<span class="#{css_classes.join(' ')}">} @out << %{<span class="#{css_classes.join(' ')}">}
@n_open_tags += 1 @n_open_tags += 1
end end
...@@ -163,6 +179,31 @@ module Ci ...@@ -163,6 +179,31 @@ module Ci
end end
end end
def reset_state
@offset = 0
@n_open_tags = 0
@out = ''
reset
end
def state
state = STATE_PARAMS.inject({}) do |h, param|
h[param] = send(param)
h
end
Base64.urlsafe_encode64(state.to_json)
end
def restore_state(raw, new_state)
state = Base64.urlsafe_decode64(new_state)
state = JSON.parse(state, symbolize_names: true)
return if state[:offset].to_i > raw.length
STATE_PARAMS.each do |param|
send("#{param}=".to_sym, state[param])
end
end
def reset def reset
@fg_color = nil @fg_color = nil
@bg_color = nil @bg_color = nil
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment