From cbc33324cc4d8128ae30bc622057337bcb70ec8b Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Tue, 3 Mar 2015 19:33:56 +0200 Subject: [PATCH] Refactor to remove global variables --- lib/crono.rb | 1 - lib/crono/cli.rb | 29 +++++++++++++++++++++-------- lib/crono/config.rb | 7 ------- lib/crono/logger.rb | 14 -------------- lib/crono/performer_proxy.rb | 7 ++++--- spec/cli_spec.rb | 6 +++--- spec/config_spec.rb | 6 ++++-- spec/logger_spec.rb | 11 ----------- spec/performer_proxy_spec.rb | 2 +- 9 files changed, 33 insertions(+), 50 deletions(-) delete mode 100644 lib/crono/logger.rb delete mode 100644 spec/logger_spec.rb diff --git a/lib/crono.rb b/lib/crono.rb index 483f399..ccf9ced 100644 --- a/lib/crono.rb +++ b/lib/crono.rb @@ -3,7 +3,6 @@ end require "active_support/all" require "crono/version.rb" -require "crono/logger.rb" require "crono/period.rb" require "crono/schedule.rb" require "crono/config.rb" diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index 9d34927..ba65e70 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -2,11 +2,24 @@ require 'crono' require 'optparse' module Crono + mattr_accessor :schedule + class CLI include Singleton + attr_accessor :config + attr_accessor :schedule + attr_accessor :logger def run + self.config = Config.new + self.schedule = Schedule.new + Crono.schedule = schedule + parse_options(ARGV) + + logfile = config.daemonize ? config.logfile : STDOUT + self.logger = Logger.new(logfile) + load_rails print_banner start_working_loop @@ -22,7 +35,7 @@ module Crono require 'rails' require File.expand_path("config/environment.rb") ::Rails.application.eager_load! - require File.expand_path(Crono.config.cronotab) + require File.expand_path(config.cronotab) end def run_job(klass) @@ -32,7 +45,7 @@ module Crono def start_working_loop loop do - klass, time = config.schedule.next + klass, time = schedule.next sleep(time - Time.now) run_job(klass) end @@ -42,16 +55,16 @@ module Crono OptionParser.new do |opts| opts.banner = "Usage: crono [options]" - opts.on("-C", "--cronotab PATH", "Path to cronotab file (Default: #{Crono.config.cronotab})") do |cronotab| - Crono.config.cronotab = cronotab + opts.on("-C", "--cronotab PATH", "Path to cronotab file (Default: #{config.cronotab})") do |cronotab| + config.cronotab = cronotab end - opts.on("-L", "--logfile PATH", "Path to writable logfile (Default: #{Crono.config.logfile})") do |logfile| - Crono.config.logfile = logfile + opts.on("-L", "--logfile PATH", "Path to writable logfile (Default: #{config.logfile})") do |logfile| + config.logfile = logfile end - opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{Crono.config.daemonize})") do |daemonize| - Crono.config.daemonize = daemonize + opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize| + config.daemonize = daemonize end end.parse!(argv) diff --git a/lib/crono/config.rb b/lib/crono/config.rb index 40faec3..a50c01f 100644 --- a/lib/crono/config.rb +++ b/lib/crono/config.rb @@ -1,23 +1,16 @@ module Crono class Config - include Singleton CRONOTAB = "config/cronotab.rb" LOGFILE = "log/crono.rb" - attr_accessor :schedule attr_accessor :cronotab attr_accessor :logfile attr_accessor :daemonize def initialize - self.schedule = Schedule.new self.cronotab = CRONOTAB self.logfile = LOGFILE self.daemonize = false end end - - def self.config - Config.instance - end end diff --git a/lib/crono/logger.rb b/lib/crono/logger.rb deleted file mode 100644 index 1b27ed8..0000000 --- a/lib/crono/logger.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Crono - class Logger < ::Logger - include Singleton - - def initialize - super(Crono.config.daemonize ? Crono.config.logfile : STDOUT) - self.level = Logger::DEBUG - end - end - - def self.logger - Logger.instance - end -end diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index fc5be53..5edb3b8 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -1,15 +1,16 @@ module Crono class PerformerProxy - def initialize(performer) + def initialize(performer, schedule) @performer = performer + @schedule = schedule end def every(period, *args) - Crono.config.schedule.add(@performer, Period.new(period, *args)) + @schedule.add(@performer, Period.new(period, *args)) end end def self.perform(performer) - PerformerProxy.new(performer) + PerformerProxy.new(performer, Crono.schedule) end end diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index dfe5d99..b3d16c5 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -31,17 +31,17 @@ describe Crono::CLI do describe "#parse_options" do it "should set cronotab" do cli.send(:parse_options, ["--cronotab", "/tmp/cronotab.rb"]) - expect(Crono.config.cronotab).to be_eql "/tmp/cronotab.rb" + expect(cli.config.cronotab).to be_eql "/tmp/cronotab.rb" end it "should set logfile" do cli.send(:parse_options, ["--logfile", "log/crono.log"]) - expect(Crono.config.logfile).to be_eql "log/crono.log" + expect(cli.config.logfile).to be_eql "log/crono.log" end it "should set daemonize" do cli.send(:parse_options, ["--daemonize"]) - expect(Crono.config.daemonize).to be true + expect(cli.config.daemonize).to be true end end end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index fdb0c38..62e01e4 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -3,8 +3,10 @@ require "spec_helper" describe Crono::Config do describe "#initialize" do it "should initialize schedule" do - @config = Crono.config - expect(@config.schedule).to be_a(Crono::Schedule) + @config = Crono::Config.new + expect(@config.cronotab).to be Crono::Config::CRONOTAB + expect(@config.logfile).to be Crono::Config::LOGFILE + expect(@config.daemonize).to be false end end end diff --git a/spec/logger_spec.rb b/spec/logger_spec.rb deleted file mode 100644 index 0773569..0000000 --- a/spec/logger_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -require "spec_helper" - -describe Crono::Logger do - describe "#initialize" do - it "should initialize logger" do - expect { - Crono.logger.info("Test") - }.to_not raise_error - end - end -end diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index 6cf9bb5..30e5539 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -6,7 +6,7 @@ end describe Crono::PerformerProxy do it "should add job and period to schedule" do - expect(Crono.config.schedule).to receive(:add).with(TestJob, kind_of(Crono::Period)) + expect(Crono.schedule).to receive(:add).with(TestJob, kind_of(Crono::Period)) Crono.perform(TestJob).every(2.days, at: "15:30") end end