diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index 8bdd54e..78bc4e7 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -1,10 +1,12 @@ require 'crono' +require 'optparse' module Crono class CLI include Singleton def run + parse_options(ARGV) load_rails print_banner start_working_loop @@ -20,7 +22,7 @@ module Crono require 'rails' require File.expand_path("config/environment.rb") ::Rails.application.eager_load! - require File.expand_path("config/cronotab.rb") + require File.expand_path(Crono.config.cronotab) end def run_job(klass) @@ -30,10 +32,20 @@ module Crono def start_working_loop loop do - klass, time = Config.instance.schedule.next + klass, time = config.schedule.next sleep(time - Time.now) run_job(klass) 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 diff --git a/lib/crono/config.rb b/lib/crono/config.rb index 3e51e30..eb583ac 100644 --- a/lib/crono/config.rb +++ b/lib/crono/config.rb @@ -1,11 +1,14 @@ module Crono class Config + CRONOTAB = "config/cronotab.rb" include Singleton attr_accessor :schedule + attr_accessor :cronotab def initialize self.schedule = Schedule.new + self.cronotab = CRONOTAB end end diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 113928f..8162603 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -12,6 +12,7 @@ describe Crono::CLI 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(:start_working_loop) + expect(cli).to receive(:parse_options) cli.run end end @@ -26,4 +27,11 @@ describe Crono::CLI do describe "#start_working_loop" do it "should start working loop" 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