Able to set place of cronotab.rb

This commit is contained in:
Dzmitry Plashchynski
2015-03-03 18:56:29 +02:00
parent b301b95ae5
commit 9675fc3517
3 changed files with 25 additions and 2 deletions

View File

@@ -1,10 +1,12 @@
require 'crono' require 'crono'
require 'optparse'
module Crono module Crono
class CLI class CLI
include Singleton include Singleton
def run def run
parse_options(ARGV)
load_rails load_rails
print_banner print_banner
start_working_loop start_working_loop
@@ -20,7 +22,7 @@ module Crono
require 'rails' require 'rails'
require File.expand_path("config/environment.rb") require File.expand_path("config/environment.rb")
::Rails.application.eager_load! ::Rails.application.eager_load!
require File.expand_path("config/cronotab.rb") require File.expand_path(Crono.config.cronotab)
end end
def run_job(klass) def run_job(klass)
@@ -30,10 +32,20 @@ module Crono
def start_working_loop def start_working_loop
loop do loop do
klass, time = Config.instance.schedule.next klass, time = config.schedule.next
sleep(time - Time.now) sleep(time - Time.now)
run_job(klass) run_job(klass)
end end
end end
def parse_options(argv)
OptionParser.new do |opts|
opts.banner = "Usage: crono [options]"
opts.on("-c", "--cronotab cronotab.rb", "Cronotab file (Default: #{Crono.config.cronotab})") do |cronotab|
Crono.config.cronotab = cronotab
end
end.parse!(argv)
end
end end
end end

View File

@@ -1,11 +1,14 @@
module Crono module Crono
class Config class Config
CRONOTAB = "config/cronotab.rb"
include Singleton include Singleton
attr_accessor :schedule attr_accessor :schedule
attr_accessor :cronotab
def initialize def initialize
self.schedule = Schedule.new self.schedule = Schedule.new
self.cronotab = CRONOTAB
end end
end end

View File

@@ -12,6 +12,7 @@ describe Crono::CLI do
it "should try to initialize rails with #load_rails and start working loop" do it "should try to initialize rails with #load_rails and start working loop" do
expect(cli).to receive(:load_rails) expect(cli).to receive(:load_rails)
expect(cli).to receive(:start_working_loop) expect(cli).to receive(:start_working_loop)
expect(cli).to receive(:parse_options)
cli.run cli.run
end end
end end
@@ -26,4 +27,11 @@ describe Crono::CLI do
describe "#start_working_loop" do describe "#start_working_loop" do
it "should start working loop" it "should start working loop"
end end
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"
end
end
end end