Refactor to remove global variables

This commit is contained in:
Dzmitry Plashchynski
2015-03-03 19:33:56 +02:00
parent 4c59804472
commit cbc33324cc
9 changed files with 33 additions and 50 deletions

View File

@@ -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"

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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