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

View File

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