lib/rbatch.rb in rbatch-1.13.1 vs lib/rbatch.rb in rbatch-2.0.0
- old
+ new
@@ -1,74 +1,55 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
-require 'digest'
require 'yaml'
+
module RBatch
- @@opt = {}
- @@rbatch_config = nil
+ @@program_name = $PROGRAM_NAME
+ @@program_base = File.basename($PROGRAM_NAME)
+ @@home_dir = nil
+ @@run_conf_path = nil
+ @@run_conf = nil
+ @@journal_verbose = 3
+ @@journal_verbose_map = { :error => 1, :warn => 2, :info => 3, :debug => 4}
module_function
- def program_name ; @@opt[:program_name] ; end
- def home_dir ; @@opt[:home_dir] ; end
- def home_dir=(d) ; @@opt[:home_dir]=d ; end
- def hostname ; @@opt[:hostname] ; end
- def rbatch_config ; @@rbatch_config ; end
- def opt ; @@opt ; end
+ def program_name ; @@program_name ; end
+ def program_base ; @@program_base ; end
+ def home_dir ; @@home_dir ; end
+ def run_conf_path ; @@run_conf_path ; end
+ def run_conf ; @@run_conf ; end
+ def conf_dir ; @@run_conf[:conf_dir].gsub("<home>",@@home_dir) ; end
+ def log_dir ; @@run_conf[:log_dir].gsub("<home>",@@home_dir) ; end
+ def journal(level,str)
+ puts "[RBatch] " + str if @@journal_verbose_map[level] <= @@journal_verbose
+ end
def init
- @@opt[:program_name] = $PROGRAM_NAME
- @@opt[:home_dir] = ENV["RB_HOME"] ? ENV["RB_HOME"] : File.join(File.dirname(@@opt[:program_name]) , "..")
- case RUBY_PLATFORM
- when /mswin|mingw/
- @@opt[:hostname] = ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
- when /cygwin|linux/
- @@opt[:hostname] = ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
+ @@journal_verbose = ENV["RB_VERBOSE"].to_i if ENV["RB_VERBOSE"]
+ if ENV["RB_HOME"]
+ @@home_dir = ENV["RB_HOME"]
+ RBatch.journal :info,"RB_HOME : \"#{@@home_dir}\" (defined by $RB_HOME)"
else
- @@opt[:hostname] = "unknownhost"
+ @@home_dir = File.join(File.dirname(@@program_name) , "..")
+ RBatch.journal :info,"RB_HOME : \"#{@@home_dir}\" (default)"
end
- load_rbatch_config
+ @@run_conf_path = File.join(@@home_dir,".rbatchrc")
+ RBatch.journal :info,"Run-Conf: \"#{@@run_conf_path}\""
+
+ @@run_conf = RunConf.new(@@run_conf_path,@@home_dir)
+ RBatch.journal :debug,"RBatch option : #{@@run_conf.inspect}"
end
- def rbatch_config_path
- File.join(@@opt[:home_dir],"conf","rbatch.yaml")
- end
- def config_dir
- File.join(RBatch.home_dir,"conf")
- end
- def log_dir
- File.join(RBatch.home_dir,"log")
- end
- def load_rbatch_config
- if File.exist?(rbatch_config_path)
- @@rbatch_config = YAML::load_file(rbatch_config_path)
- if @@rbatch_config == false
- @@rbatch_config = nil
- end
- else
- @@rbatch_config = nil
- end
- end
- def double_run_check
- # double run check
- if ( @@rbatch_config != nil && @@rbatch_config["forbid_double_run"] )
- lock_file="rbatch_lock_" + Digest::MD5.hexdigest(@@opt[:program_name])
- if Dir.exists? @@opt[:tmp_dir]
- Dir::foreach(@@opt[:tmp_dir]) do |f|
- if (Regexp.new(lock_file) =~ f)
- raise RBatchException, "Script double run is forbid about \"#{@@opt[:program_name]}\""
- end
- end
- end
- # make lockfile
- Tempfile::new(lock_file,@@opt[:tmp_dir])
- end
- end
end
-# RBatch Exception
-class RBatchException < Exception ; end
-
# main
+require 'rbatch/run_conf'
+require 'rbatch/double_run_checker'
+
RBatch::init
+if ( RBatch.run_conf[:forbid_double_run] )
+ RBatch::DoubleRunChecker.check(File.basename(RBatch.program_name)) #raise error if check is NG
+ RBatch::DoubleRunChecker.make_lock_file(File.basename(RBatch.program_name))
+end
require 'rbatch/log'
require 'rbatch/config'
require 'rbatch/common_config'
require 'rbatch/cmd'
-RBatch::double_run_check
+RBatch.journal :info,"Start \"#{RBatch.program_name}\" under RBatch (PID=#{$$.to_s})"