Fluentd::Agent::Log is now dependent class. Use composite

This commit is contained in:
uu59 2015-02-10 14:58:50 +09:00
parent 9b2d968bc1
commit b5d4be2659
12 changed files with 35 additions and 22 deletions

View File

@ -22,7 +22,7 @@ module SettingConcern
@fluentd.agent.config_append @setting.to_config
if @fluentd.agent.running?
unless @fluentd.agent.restart
@setting.errors.add(:base, @fluentd.agent.log_tail(1).first)
@setting.errors.add(:base, @fluentd.agent.log.tail(1).first)
return render "shared/settings/show"
end
end

View File

@ -26,7 +26,7 @@ module SettingHistoryConcern
if @fluentd.agent.dryrun
flash = { success: t('messages.dryrun_is_passed') }
else
flash = { danger: @fluentd.agent.log_tail(1).first }
flash = { danger: @fluentd.agent.log.tail(1).first }
end
redirect_to :back, flash: flash
end

View File

@ -2,7 +2,7 @@ class Fluentd::AgentsController < ApplicationController
before_action :find_fluentd
def start
run_action(__method__) { @fluentd.agent.log_tail(1).first }
run_action(__method__) { @fluentd.agent.log.tail(1).first }
redirect_to daemon_path(@fluentd), status: 303 # 303 is change HTTP Verb GET
end
@ -12,12 +12,12 @@ class Fluentd::AgentsController < ApplicationController
end
def restart
run_action(__method__) { @fluentd.agent.log_tail(1).first }
run_action(__method__) { @fluentd.agent.log.tail(1).first }
redirect_to daemon_path(@fluentd), status: 303 # 303 is change HTTP Verb GET
end
def log_tail
@logs = @fluentd.agent.log_tail(params[:limit]).reverse if @fluentd
@logs = @fluentd.agent.log.tail(params[:limit]).reverse if @fluentd
render json: @logs
end

View File

@ -39,7 +39,7 @@ class Fluentd::SettingsController < ApplicationController
if dryrun(params[:config])
flash.now[:success] = I18n.t('messages.dryrun_is_passed')
else
flash.now[:danger] = @fluentd.agent.last_error_message
flash.now[:danger] = @fluentd.agent.log.last_error_message
end
@config = params[:config]
render "edit"

View File

@ -41,11 +41,11 @@ class FluentdController < ApplicationController
def errors
@error_duration_days = 5
@errors = @fluentd.agent.errors_since(@error_duration_days.days.ago)
@errors = @fluentd.agent.log.errors_since(@error_duration_days.days.ago)
end
def raw_log
send_data @fluentd.agent.log, type: "application/octet-stream", filename: File.basename(@fluentd.log_file)
send_data @fluentd.agent.log.to_s, type: "application/octet-stream", filename: File.basename(@fluentd.log_file)
end
private

View File

@ -38,7 +38,7 @@ class MiscController < ApplicationController
File.unlink(path) if File.exists?(path)
Zip::File.open(path, Zip::File::CREATE) do |zip|
zip.get_output_stream('fluentd.log') {|f| f.puts fluentd.agent.log }
zip.get_output_stream('fluentd.log') {|f| f.puts fluentd.agent.log.to_s }
zip.add("fluentd-ui.log", log_path)
add_env_file_to(zip)

View File

@ -5,7 +5,7 @@ class TutorialsController < ApplicationController
helper_method :tutorial_ready?
def index
@log = @fluentd.agent.log_tail.reverse if @fluentd
@log = @fluentd.agent.log.tail.reverse if @fluentd
end
def chapter1

View File

@ -21,7 +21,6 @@ class Fluentd
def self.included(base)
base.include Fluentd::Agent::ProcessOperation
base.include Fluentd::Agent::Log
end
# define these methods on each Agent class
@ -43,6 +42,10 @@ class Fluentd
extra_options[:log_file] || self.class.default_options[:log_file]
end
def log
@log ||= Fluentd::Agent::Log.new(log_file)
end
def config_file
extra_options[:config_file] || self.class.default_options[:config_file]
end

View File

@ -1,7 +1,13 @@
class Fluentd
class Agent
module Log
def log
class Log
attr_reader :log_file
def initialize(path)
@log_file = path
end
def to_s
return "" unless File.exists?(log_file)
File.read(log_file) # TODO: large log file
end
@ -28,7 +34,7 @@ class Fluentd
recent_errors(1).first.try(:[], :subject) || ""
end
def log_tail(limit = nil)
def tail(limit = nil)
return [] unless File.exists?(log_file)
limit = limit.to_i rescue 0

View File

@ -34,7 +34,7 @@ class Fluentd
def exec_dryrun(command, file_path = nil)
Bundler.with_clean_env do
unless system("#{command} -q --dry-run #{options_to_argv(config_file: file_path)}", out: File::NULL, err: File::NULL)
raise ::Fluentd::Agent::ConfigError, last_error_message
raise ::Fluentd::Agent::ConfigError, "TODO"
end
end
end

View File

@ -1,6 +1,8 @@
require 'spec_helper'
describe Fluentd::AgentsController do
let(:log) { double('log').as_null_object }
before do
allow(controller).to receive(:current_user).and_return true
allow(controller).to receive(:find_fluentd).and_return(nil)
@ -32,13 +34,15 @@ describe Fluentd::AgentsController do
it "starts" do
expect(@agent).to receive(:start).and_return false
expect(@agent).to receive(:log_tail).with(1).and_return ["some message"]
expect(@agent).to receive(:log).and_return(log)
expect(log).to receive(:tail).with(1)
put :start
end
it "restarts" do
expect(@agent).to receive(:restart).and_return false
expect(@agent).to receive(:log_tail).with(1).and_return ["some message"]
expect(@agent).to receive(:log).and_return(log)
expect(log).to receive(:tail).with(1)
put :restart
end
end

View File

@ -35,7 +35,7 @@ shared_examples_for "Fluentd::Agent has common behavior" do |klass|
before { Timecop.freeze(now) }
after { Timecop.return }
subject { instance.errors_since(days.days.ago) }
subject { instance.log.errors_since(days.days.ago) }
context "has no errors" do
let(:logfile) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) }
@ -73,7 +73,7 @@ shared_examples_for "Fluentd::Agent has common behavior" do |klass|
describe "#recent_errors" do
context "have 0 error log" do
let(:logfile) { File.expand_path("./spec/support/fixtures/error0.log", Rails.root) }
subject { instance.recent_errors(2) }
subject { instance.log.recent_errors(2) }
it "empty array" do
should be_empty
@ -82,10 +82,10 @@ shared_examples_for "Fluentd::Agent has common behavior" do |klass|
context "have 2 error log" do
let(:logfile) { File.expand_path("./spec/support/fixtures/error2.log", Rails.root) }
subject { instance.recent_errors(2) }
subject { instance.log.recent_errors(2) }
describe "limit" do
subject { instance.recent_errors(limit).length }
subject { instance.log.recent_errors(limit).length }
context "=1" do
let(:limit) { 1 }
@ -111,7 +111,7 @@ shared_examples_for "Fluentd::Agent has common behavior" do |klass|
context "have 3 errors log includeing sequential 2 error log" do
let(:logfile) { File.expand_path("./spec/support/fixtures/error3.log", Rails.root) }
subject { instance.recent_errors(3) }
subject { instance.log.recent_errors(3) }
it "count 3 errors" do
subject[0][:subject].should include("3 Address already in use - bind(2)")