Skip to content

Commit

Permalink
Log command output
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Jul 8, 2019
1 parent b981c7c commit 8142800
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/models/shipit/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def run_now!
end

def write(text)
log_output(text)
chunks.create!(text: text)
end

Expand Down Expand Up @@ -333,5 +334,17 @@ def status_key
def abort_key
"#{status_key}:aborting"
end

def log_output(text)
output_log_line_buffer.buffer(text) do |line|
Shipit.task_logger.info(
"[#{stack.repo_name}] #{line}"
)
end
end

def output_log_line_buffer
@output_log_line_buffer ||= LineBuffer.new
end
end
end
7 changes: 6 additions & 1 deletion lib/shipit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
require 'shipit/stat'
require 'shipit/strip_cache_control'
require 'shipit/cast_value'
require 'shipit/line_buffer'

SafeYAML::OPTIONS[:default_mode] = :safe
SafeYAML::OPTIONS[:deserialize_symbols] = false
Expand All @@ -59,7 +60,7 @@ module Shipit
delegate :table_name_prefix, to: :secrets

attr_accessor :disable_api_authentication, :timeout_exit_codes
attr_writer :internal_hook_receivers
attr_writer :internal_hook_receivers, :task_logger

self.timeout_exit_codes = [].freeze

Expand Down Expand Up @@ -195,6 +196,10 @@ def internal_hook_receivers
@internal_hook_receivers ||= []
end

def task_logger
@task_logger ||= Logger.new(nil)
end

protected

def revision_file
Expand Down
34 changes: 34 additions & 0 deletions lib/shipit/line_buffer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Shipit
class LineBuffer
def initialize(value = "")
@value = value
end

def buffer(text, &block)
@value += text
whole_lines.each(&block).tap { flush }
end

private

def whole_lines
whole? ? lines : lines[0..-2]
end

def flush
whole? ? clear : @value = lines.last
end

def whole?
@value.end_with?($/)
end

def lines
@value.split($/).reject(&:empty?)
end

def clear
@value.clear
end
end
end
13 changes: 13 additions & 0 deletions test/models/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ class TasksTest < ActiveSupport::TestCase
task = shipit_tasks(:shipit_with_title_parsing_issue)
assert_equal 'This task (title: Using the %{WRONG_VARIABLE_NAME}) cannot be shown due to an incorrect variable name. Check your shipit.yml file', task.title
end

test "#write sends line-buffered output to task logger" do
task = shipit_tasks(:shipit)

mock_task_logger = mock.tap do |m|
m.expects(:info).with("hello").once
m.expects(:info).never
end

Shipit.stubs(:task_logger).returns(mock_task_logger)

task.write("hello\nworld")
end
end
end
21 changes: 21 additions & 0 deletions test/unit/line_buffer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

module Shipit
class LineBufferTest < ActiveSupport::TestCase
setup { @buffer = LineBuffer.new }

test "buffers partial lines" do
refute_predicate(@buffer.buffer("a"), :any?)
assert_equal(["a"], @buffer.buffer("\n").to_a)
end

test "splits up multiple lines" do
assert_equal(["a", "b"], @buffer.buffer("a\nb\n").to_a)
end

test "ignores blanks" do
refute_predicate(@buffer.buffer("\n\n\n"), :any?)
assert_equal(["a", "b"], @buffer.buffer("a\n\n\nb\n").to_a)
end
end
end

0 comments on commit 8142800

Please sign in to comment.