Add option -e to set environment

This commit is contained in:
Dzmitry Plashchynski
2015-03-04 14:25:17 +02:00
parent 0afdab02ac
commit e96d71552e
5 changed files with 15 additions and 1 deletions

View File

@@ -90,6 +90,7 @@ Usage: crono [options]
-L, --logfile PATH Path to writable logfile (Default: log/crono.log) -L, --logfile PATH Path to writable logfile (Default: log/crono.log)
-P, --pidfile PATH Path to pidfile (Default: tmp/pids/crono.pid) -P, --pidfile PATH Path to pidfile (Default: tmp/pids/crono.pid)
-d, --[no-]daemonize Daemonize process (Default: false) -d, --[no-]daemonize Daemonize process (Default: false)
-e, --environment ENV Application environment (Default: development)
``` ```
## License ## License

View File

@@ -53,6 +53,7 @@ module Crono
end end
def load_rails def load_rails
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = config.environment
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!
@@ -91,7 +92,10 @@ module Crono
opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize| opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize|
config.daemonize = daemonize config.daemonize = daemonize
end end
opts.on '-e', '--environment ENV', "Application environment (Default: #{config.environment})" do |env|
config.environment = env
end
end.parse!(argv) end.parse!(argv)
end end
end end

View File

@@ -8,12 +8,14 @@ module Crono
attr_accessor :logfile attr_accessor :logfile
attr_accessor :pidfile attr_accessor :pidfile
attr_accessor :daemonize attr_accessor :daemonize
attr_accessor :environment
def initialize def initialize
self.cronotab = CRONOTAB self.cronotab = CRONOTAB
self.logfile = LOGFILE self.logfile = LOGFILE
self.pidfile = PIDFILE self.pidfile = PIDFILE
self.daemonize = false self.daemonize = false
self.environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
end end
end end
end end

View File

@@ -49,5 +49,10 @@ describe Crono::CLI do
cli.send(:parse_options, ["--daemonize"]) cli.send(:parse_options, ["--daemonize"])
expect(cli.config.daemonize).to be true expect(cli.config.daemonize).to be true
end end
it "should set environment" do
cli.send(:parse_options, ["--environment", "production"])
expect(cli.config.environment).to be_eql("production")
end
end end
end end

View File

@@ -3,11 +3,13 @@ require "spec_helper"
describe Crono::Config do describe Crono::Config do
describe "#initialize" do describe "#initialize" do
it "should initialize with default configuration options" do it "should initialize with default configuration options" do
ENV["RAILS_ENV"] = "test"
@config = Crono::Config.new @config = Crono::Config.new
expect(@config.cronotab).to be Crono::Config::CRONOTAB expect(@config.cronotab).to be Crono::Config::CRONOTAB
expect(@config.logfile).to be Crono::Config::LOGFILE expect(@config.logfile).to be Crono::Config::LOGFILE
expect(@config.pidfile).to be Crono::Config::PIDFILE expect(@config.pidfile).to be Crono::Config::PIDFILE
expect(@config.daemonize).to be false expect(@config.daemonize).to be false
expect(@config.environment).to be_eql ENV["RAILS_ENV"]
end end
end end
end end