diff --git a/app/controllers/concerns/setting_concern.rb b/app/controllers/concerns/setting_concern.rb index 91c0207..6cf5d77 100644 --- a/app/controllers/concerns/setting_concern.rb +++ b/app/controllers/concerns/setting_concern.rb @@ -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 diff --git a/app/controllers/concerns/setting_history_concern.rb b/app/controllers/concerns/setting_history_concern.rb index 49dd440..12accf3 100644 --- a/app/controllers/concerns/setting_history_concern.rb +++ b/app/controllers/concerns/setting_history_concern.rb @@ -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 diff --git a/app/controllers/fluentd/agents_controller.rb b/app/controllers/fluentd/agents_controller.rb index bdf3ebb..471e71c 100644 --- a/app/controllers/fluentd/agents_controller.rb +++ b/app/controllers/fluentd/agents_controller.rb @@ -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 diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index b9ef70f..e89eeea 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -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" diff --git a/app/controllers/fluentd_controller.rb b/app/controllers/fluentd_controller.rb index ec52c4e..d0f479b 100644 --- a/app/controllers/fluentd_controller.rb +++ b/app/controllers/fluentd_controller.rb @@ -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 diff --git a/app/controllers/misc_controller.rb b/app/controllers/misc_controller.rb index ffd4348..f91ffdf 100644 --- a/app/controllers/misc_controller.rb +++ b/app/controllers/misc_controller.rb @@ -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) diff --git a/app/controllers/tutorials_controller.rb b/app/controllers/tutorials_controller.rb index 4458b64..c7a26cd 100644 --- a/app/controllers/tutorials_controller.rb +++ b/app/controllers/tutorials_controller.rb @@ -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 diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index d4a3eb0..99fa970 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -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 diff --git a/app/models/fluentd/agent/log.rb b/app/models/fluentd/agent/log.rb index 3b5aa23..15bf1b9 100644 --- a/app/models/fluentd/agent/log.rb +++ b/app/models/fluentd/agent/log.rb @@ -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 diff --git a/app/models/fluentd/agent/process_operation.rb b/app/models/fluentd/agent/process_operation.rb index 9889ffb..3fa6c1b 100644 --- a/app/models/fluentd/agent/process_operation.rb +++ b/app/models/fluentd/agent/process_operation.rb @@ -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 diff --git a/spec/controllers/fluentd/agents_controller_spec.rb b/spec/controllers/fluentd/agents_controller_spec.rb index f4ac667..28c2451 100644 --- a/spec/controllers/fluentd/agents_controller_spec.rb +++ b/spec/controllers/fluentd/agents_controller_spec.rb @@ -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 diff --git a/spec/support/fluentd_agent_common_behavior.rb b/spec/support/fluentd_agent_common_behavior.rb index b485ddb..da58bdc 100644 --- a/spec/support/fluentd_agent_common_behavior.rb +++ b/spec/support/fluentd_agent_common_behavior.rb @@ -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)")