rbatch 2.1.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/README.ja.md +14 -5
  2. data/README.md +164 -154
  3. data/bin/rbatch-init +21 -13
  4. data/doc/RBatch/Cmd.html +300 -316
  5. data/doc/RBatch/CmdException.html +146 -141
  6. data/doc/RBatch/CmdResult.html +467 -470
  7. data/doc/RBatch/Config.html +392 -433
  8. data/doc/RBatch/Controller.html +374 -0
  9. data/doc/RBatch/DoubleRunCheckException.html +146 -141
  10. data/doc/RBatch/DoubleRunChecker.html +258 -256
  11. data/doc/RBatch/Journal.html +332 -0
  12. data/doc/RBatch/Log.html +636 -805
  13. data/doc/RBatch/LogException.html +164 -0
  14. data/doc/RBatch/RBatch/RBatch/ConfigException.html +164 -0
  15. data/doc/RBatch/RBatch/RBatch.html +163 -0
  16. data/doc/RBatch/RBatch/VariablesException.html +164 -0
  17. data/doc/RBatch/RBatch.html +165 -0
  18. data/doc/RBatch/RunConf/Exception.html +146 -141
  19. data/doc/RBatch/RunConf.html +478 -532
  20. data/doc/RBatch/Variables.html +437 -0
  21. data/doc/RBatch.html +388 -862
  22. data/doc/created.rid +11 -9
  23. data/doc/index.html +178 -184
  24. data/doc/js/darkfish.js +9 -7
  25. data/doc/lib/rbatch/cmd_rb.html +46 -44
  26. data/doc/lib/rbatch/config_rb.html +42 -42
  27. data/doc/lib/rbatch/controller_rb.html +66 -0
  28. data/doc/lib/rbatch/double_run_checker_rb.html +42 -42
  29. data/doc/lib/rbatch/journal_rb.html +52 -0
  30. data/doc/lib/rbatch/log_rb.html +46 -46
  31. data/doc/lib/rbatch/run_conf_rb.html +42 -42
  32. data/doc/lib/rbatch/variables_rb.html +54 -0
  33. data/doc/lib/rbatch/version_rb.html +38 -38
  34. data/doc/lib/rbatch_rb.html +40 -52
  35. data/doc/rdoc.css +365 -308
  36. data/lib/rbatch/cmd.rb +15 -58
  37. data/lib/rbatch/config.rb +7 -7
  38. data/lib/rbatch/controller.rb +37 -61
  39. data/lib/rbatch/double_run_checker.rb +0 -0
  40. data/lib/rbatch/journal.rb +40 -0
  41. data/lib/rbatch/log.rb +71 -129
  42. data/lib/rbatch/run_conf.rb +13 -24
  43. data/lib/rbatch/variables.rb +82 -0
  44. data/lib/rbatch/version.rb +1 -1
  45. data/lib/rbatch.rb +7 -36
  46. data/sample/.rbatchrc +41 -7
  47. data/spec/01_rbach_spec.rb +99 -0
  48. data/spec/{cmd_spec.rb → rbatch/cmd_spec.rb} +40 -43
  49. data/spec/rbatch/config_spec.rb +67 -0
  50. data/spec/rbatch/controller_spec.rb +18 -0
  51. data/spec/{double_run_checker_spec.rb → rbatch/double_run_checker_spec.rb} +3 -0
  52. data/spec/rbatch/journal_spec.rb +29 -0
  53. data/spec/rbatch/log_spec.rb +350 -0
  54. data/spec/{run_conf_spec.rb → rbatch/run_conf_spec.rb} +13 -5
  55. data/spec/rbatch/variables_spec.rb +68 -0
  56. data/spec/spec_helper.rb +4 -5
  57. metadata +33 -17
  58. data/lib/rbatch/common_config.rb +0 -0
  59. data/spec/common_config_spec.rb +0 -85
  60. data/spec/config_spec.rb +0 -79
  61. data/spec/log_spec.rb +0 -430
  62. data/spec/rbatch_spec.rb +0 -22
data/lib/rbatch/cmd.rb CHANGED
@@ -1,87 +1,52 @@
1
1
  require 'fileutils'
2
2
  require 'tempfile'
3
3
  require 'tmpdir'
4
-
4
+ require 'timeout'
5
5
  module RBatch
6
6
 
7
- # External command runcher.
8
- #
9
- #This module is a wrapper of Kernel#spawn.
10
- #
11
- # * Arguments(cmd_params) are inputed to Kernel#spawn directly and run command.
12
- # * Return an object of RBatch::CmdResult which includes stdout, stderr, and exit status.
13
- #
14
- # ==== Sample 1
15
- # require 'rbatch'
16
- # result = RBatch::cmd("ls")
17
- # p result.stdout
18
- # => "fileA\nfileB\n"
19
- #
20
- # ==== Sample 2 (use option)
21
- # require 'rbatch'
22
- # result = RBatch::cmd("ls",{:timeout => 1})
23
- # p result.stdout
24
- # => "fileA\nfileB\n"
25
- #
26
- # ==== Sample 3 (use instance)
27
- # require 'rbatch'
28
- # cmd = RBatch::Cmd.new("ls")
29
- # result = cmd.run
30
- # p result.stdout
31
- # => "fileA\nfileB\n"
32
- #
33
7
  class Cmd
8
+ @@def_vars
9
+ def Cmd.def_vars=(a) ; @@def_vars=a ; end
34
10
  @cmd_str
35
11
  @opt
36
-
37
- # Cmd instance
38
- #
39
- # ==== Params
40
- # +cmd_str+ = Command string such as "ls -l"
41
- # +opt+ = Option hash object.
42
- # - +:raise+ (Boolean) = If command exit status is not 0, raise exception. Default is false.
43
- # - +:timeout+ (Integer) = If command timeout , raise exception and kill process. Default is 0 sec ( 0 means disable) .
12
+ @vars
44
13
  def initialize(cmd_str,opt = nil)
45
14
  raise(CmdException,"Command string is nil") if cmd_str.nil?
46
15
  @cmd_str = cmd_str
47
- tmp = {}
48
- if opt.nil?
49
- @opt=RBatch.run_conf.clone
50
- else
16
+ @vars = @@def_vars.clone
17
+ if ! opt.nil?
18
+ # change opt key from "hoge" to "log_hoge"
19
+ tmp = {}
51
20
  opt.each_key do |key|
52
21
  tmp[("cmd_" + key.to_s).to_sym] = opt[key]
53
22
  end
54
- @opt=RBatch.run_conf.merge(tmp)
23
+ @vars.merge!(tmp)
55
24
  end
56
25
  end
57
26
 
58
- # Run command
59
- #
60
- # ==== Return
61
- # instance of RBatch::CmdResult
62
27
  def run()
63
28
  stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir)
64
29
  stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir)
65
30
  pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"])
66
31
  status = nil
67
- if @opt[:cmd_timeout] != 0
32
+ if @vars[:cmd_timeout] != 0
68
33
  begin
69
- timeout(@opt[:cmd_timeout]) do
34
+ timeout(@vars[:cmd_timeout]) do
70
35
  status = Process.waitpid2(pid)[1] >> 8
71
36
  end
72
37
  rescue Timeout::Error => e
73
38
  begin
74
39
  Process.kill('SIGINT', pid)
75
- raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" )
40
+ raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" )
76
41
  rescue
77
- raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@opt[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" )
42
+ raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" )
78
43
  end
79
44
  end
80
45
  else
81
46
  status = Process.waitpid2(pid)[1] >> 8
82
47
  end
83
48
  result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str)
84
- if @opt[:cmd_raise] && status != 0
49
+ if @vars[:cmd_raise] && status != 0
85
50
  raise(CmdException,"Command exit status is not 0. result: " + result.to_s)
86
51
  end
87
52
  return result
@@ -117,13 +82,5 @@ module RBatch
117
82
  end
118
83
  end
119
84
 
120
- class CmdException < Exception ; end
121
-
122
- module_function
123
-
124
- # shortcut of RBatch::Cmd
125
- def cmd(cmd_str,opt = nil)
126
- Cmd.new(cmd_str,opt).run
127
- end
128
-
85
+ class CmdException < StandardError ; end
129
86
  end
data/lib/rbatch/config.rb CHANGED
@@ -16,15 +16,15 @@ module RBatch
16
16
  end
17
17
  def[](key)
18
18
  if @hash.nil?
19
- raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
19
+ raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
20
20
  end
21
21
  if @hash[key].nil?
22
22
  if key.class == Symbol
23
- raise RBatch::Config::Exception, "Value of key(:#{key} (Symbol)) is nil. By any chance, dou you mistake key class Symbol for String?"
23
+ raise RBatch::ConfigException, "Value of key(:#{key} (Symbol)) is nil. By any chance, dou you mistake key class Symbol for String?"
24
24
  elsif key.class == String
25
- raise RBatch::Config::Exception, "Value of key(\"#{key}\" (String)) is nil"
25
+ raise RBatch::ConfigException, "Value of key(\"#{key}\" (String)) is nil"
26
26
  else
27
- raise RBatch::Config::Exception, "Value of key(#{key}) is nil."
27
+ raise RBatch::ConfigException, "Value of key(#{key}) is nil."
28
28
  end
29
29
  else
30
30
  @hash[key]
@@ -34,20 +34,20 @@ module RBatch
34
34
  def exist? ; ! @hash.nil? ; end
35
35
  def to_h
36
36
  if @hash.nil?
37
- raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
37
+ raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
38
38
  else
39
39
  @hash
40
40
  end
41
41
  end
42
42
  def to_s
43
43
  if @hash.nil?
44
- raise RBatch::Config::Exception, "Config file \"#{@path}\" does not exist"
44
+ raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
45
45
  else
46
46
  @hash.to_s
47
47
  end
48
48
  end
49
49
  end
50
50
 
51
- class RBatch::Config::Exception < Exception; end
51
+ class RBatch::ConfigException < StandardError ; end
52
52
  end
53
53
 
@@ -1,75 +1,51 @@
1
+ require 'rbatch/variables'
2
+ require 'rbatch/journal'
3
+ require 'rbatch/run_conf'
4
+ require 'rbatch/double_run_checker'
5
+ require 'rbatch/log'
6
+ require 'rbatch/config'
7
+ require 'rbatch/cmd'
8
+
1
9
  module RBatch
2
10
  class Controller
3
- @@journal_verbose_map = { :error => 1, :warn => 2, :info => 3, :debug => 4}
4
- attr :program_name,:program_path
5
- attr :home_dir,:log_dir,:conf_dir,:lib_dir
6
- attr :run_conf_path, :run_conf
7
- attr :config, :config_path
8
- attr :common_config, :common_config_path
9
- attr :journals,:logs
11
+ attr :vars,:config,:common_config,:journal,:user_logs
10
12
  def initialize
11
- @journals = []
12
- @logs = []
13
- if ENV["RB_VERBOSE"]
14
- @journal_verbose = ENV["RB_VERBOSE"].to_i
15
- else
16
- @journal_verbose = 3
17
- end
18
- @program_name = $PROGRAM_NAME
19
- @program_base = File.basename($PROGRAM_NAME)
20
- @program_path = File.expand_path(@program_name)
21
- if ENV["RB_HOME"]
22
- @home_dir = File.expand_path(ENV["RB_HOME"])
23
- else
24
- @home_dir = File.expand_path(File.join(File.dirname(@program_name) , ".."))
25
- end
26
- @run_conf_path = File.join(@home_dir,".rbatchrc")
27
- @run_conf = RunConf.new(@run_conf_path,@home_dir)
28
- journal :info, "=== START RBatch === (PID=#{$$.to_s})"
29
- journal :debug,"RB_HOME : \"#{@home_dir}\""
30
- journal :info, "Load Run-Conf: \"#{@run_conf_path}\""
31
- journal :debug,"RBatch option : #{@run_conf.inspect}"
32
- @lib_dir = @run_conf[:lib_dir].gsub("<home>",@home_dir)
33
- @conf_dir = @run_conf[:conf_dir].gsub("<home>",@home_dir)
34
- @log_dir = @run_conf[:log_dir].gsub("<home>",@home_dir)
35
- # common config
36
- @common_config_path = File.join(@conf_dir,@run_conf[:common_conf_name])
37
- @common_config = RBatch::Config.new(@common_config_path)
38
- journal :info, "Load Config : \"#{@common_config_path}\"" if ! @common_config.nil?
39
- # user config
40
- @config_path = File.join(@conf_dir,Pathname(File.basename(@program_name)).sub_ext(".yaml").to_s)
41
- @config = RBatch::Config.new(@config_path)
42
- journal :info, "Load Config : \"#{@config_path}\"" if ! @config.nil?
13
+ @vars = RBatch::Variables.new()
14
+ RBatch::Journal.def_vars = @vars
15
+ RBatch::Log.def_vars = @vars
16
+ RBatch::Cmd.def_vars = @vars
17
+ @journal = RBatch::Journal.new()
18
+ RBatch::Log.journal = @journal
19
+ @user_logs = []
20
+ @journal.put 1,"=== START RBatch === (PID=#{$$.to_s})"
21
+ @journal.put 1, "RB_HOME : \"#{@vars[:home_dir]}\""
22
+ @journal.put 1, "Load Run-Conf: \"#{@vars[:run_conf_path]}\""
23
+ @journal.put 2, "RBatch Variables : #{@vars.inspect}"
24
+ @common_config = RBatch::Config.new(@vars[:common_config_path])
25
+ @journal.put 1, "Load Config : \"#{@vars[:common_config_path]}\"" if ! @common_config.nil?
26
+ @config = RBatch::Config.new(@vars[:config_path])
27
+ @journal.put 1, "Load Config : \"#{@vars[:config_path]}\"" if ! @config.nil?
28
+
43
29
  # double_run_check
44
- if ( @run_conf[:forbid_double_run] )
45
- RBatch::DoubleRunChecker.check(@program_base) #raise error if check is NG
46
- RBatch::DoubleRunChecker.make_lock_file(@program_base)
30
+ if ( @vars[:forbid_double_run])
31
+ RBatch::DoubleRunChecker.check(@pvars[:rogram_base]) #raise error if check is NG
32
+ RBatch::DoubleRunChecker.make_lock_file(@vars[:program_base])
47
33
  end
48
34
  # load_lib
49
- if @run_conf[:auto_lib_load] && Dir.exist?(@lib_dir)
50
- Dir::foreach(@lib_dir) do |file|
35
+ if @vars[:auto_lib_load] && Dir.exist?(@vars[:lib_dir])
36
+ Dir::foreach(@vars[:lib_dir]) do |file|
51
37
  if /.*rb/ =~ file
52
- require File.join(@lib_dir,File.basename(file,".rb"))
53
- journal :info, "Load Library : \"#{File.join(@lib_dir,file)}\" "
38
+ require File.join(@vars[:lib_dir],File.basename(file,".rb"))
39
+ @journal.put 1, "Load Library : \"#{File.join(@vars[:lib_dir],file)}\" "
54
40
  end
55
41
  end
56
42
  end
57
- journal :info,"Start Script : \"#{@program_path}\""
43
+ @journal.put 1, "Start Script : \"#{@vars[:program_path]}\""
58
44
  end #end def
59
- def journal(level,str)
60
- if @@journal_verbose_map[level] <= @journal_verbose
61
- str = "[RBatch] " + str
62
- puts str
63
- @journals << str
64
- @logs.each do |log|
65
- if RBatch.run_conf[:mix_rbatch_msg_to_log]
66
- log.journal(str)
67
- end
68
- end
69
- end
70
- end
71
- def add_log(log)
72
- @logs << log
45
+ def config ; @config ; end
46
+ def common_config ; @common_config ; end
47
+ def cmd(cmd_str,opt)
48
+ RBatch::Cmd.new(cmd_str,opt).run
73
49
  end
74
50
  end
75
51
  end
File without changes
@@ -0,0 +1,40 @@
1
+ module RBatch
2
+ class Journal
3
+ @@def_vars
4
+ def Journal.def_vars=(a) ; @@def_vars=a ; end
5
+ attr :journals,:journal_verbose,:user_logs
6
+ def initialize(verbose=nil)
7
+ if verbose.nil?
8
+ if ENV["RB_VERBOSE"]
9
+ @journal_verbose = ENV["RB_VERBOSE"].to_i
10
+ else
11
+ @journal_verbose = 1
12
+ end
13
+ else
14
+ @journal_verbose = verbose
15
+ end
16
+ @journals = []
17
+ @user_logs = []
18
+ end
19
+ def put(level,str)
20
+ if level <= @journal_verbose
21
+ @journals << str
22
+ str = "[RBatch] " + str
23
+ puts str
24
+ @user_logs.each do |log|
25
+ if @@def_vars[:mix_rbatch_journal_to_logs]
26
+ log.journal(str)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ def add_log(log)
32
+ @user_logs << log
33
+ if @@def_vars[:mix_rbatch_journal_to_logs]
34
+ @journals.each do |j|
35
+ log.journal(j)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/rbatch/log.rb CHANGED
@@ -3,140 +3,82 @@ require 'fileutils'
3
3
  require 'pathname'
4
4
  require 'net/smtp'
5
5
 
6
+
6
7
  module RBatch
7
- #=== About RBatch::Log
8
- #
9
- #By using Logging block, RBatch writes to logfile automatically.
10
- #
11
- #The default location of log file is "${RB_HOME}/log/YYYYMMDD_HHMMSS_(program base name).log" .
12
- #
13
- #If an exception occuerd, then RBatch writes back trace to logfile.
14
- #
15
- #=== Sample
16
- #
17
- #script : ${RB_HOME}/bin/sample1.rb
18
- #
19
- # require 'rbatch'
20
- #
21
- # RBatch::Log.new(){ |log| # Logging block
22
- # log.info "info string"
23
- # log.error "error string"
24
- # raise "exception"
25
- # }
26
- #
27
- #
28
- #logfile : ${RB_HOME}/log/20121020_005953_sample1.log
29
- #
30
- # [2012-10-20 00:59:53 +900] [INFO ] info string
31
- # [2012-10-20 00:59:53 +900] [ERROR] error string
32
- # [2012-10-20 00:59:53 +900] [FATAL] Caught exception; existing 1
33
- # [2012-10-20 00:59:53 +900] [FATAL] exception (RuntimeError)
34
- # [backtrace] test.rb:6:in `block in <main>'
35
- # [backtrace] /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize'
36
- # [backtrace] test.rb:3:in `new'
37
- # [backtrace] test.rb:3:in `<main>'
38
- #
39
8
  class Log
40
- @@verbose = false
41
- @@log_level_map = {
9
+ @@FORMATTER = proc do |severity, datetime, progname, msg|
10
+ head = "[#{datetime}] [" + sprintf("%-5s",severity) +"]"
11
+ if msg.is_a? Exception
12
+ "#{head} #{msg}\n" + msg.backtrace.map{|s| " [backtrace] #{s}"}.join("\n") + "\n"
13
+ else
14
+ "#{head} #{msg}\n"
15
+ end
16
+ end
17
+ @@STDOUT_FORMATTER = proc do |severity, datetime, progname, msg|
18
+ head = "[" + sprintf("%-5s",severity) +"]"
19
+ if msg.is_a? Exception
20
+ "#{head} #{msg}\n" + msg.backtrace.map{|s| " [backtrace] #{s}"}.join("\n") + "\n"
21
+ else
22
+ "#{head} #{msg}\n"
23
+ end
24
+ end
25
+ @@LOG_LEVEL_MAP = {
42
26
  "debug" => Logger::DEBUG,
43
27
  "info" => Logger::INFO,
44
28
  "warn" => Logger::WARN,
45
29
  "error" => Logger::ERROR,
46
30
  "fatal" => Logger::FATAL
47
31
  }
48
-
49
- @opt # option
50
- @log # log instance for file
51
- @stdout_log # log instance for STDOUT
52
- @prog_base # program file name base
53
- @file_name # log file name
54
-
55
- # Set verbose mode flag.
56
- def Log.verbose=(bol); @@verbose = bol ; end
57
-
58
- # Get verbose mode flag.
59
- def Log.verbose ; @@verbose ; end
60
-
61
- # Get Option
62
- def opt; @opt ; end
63
-
64
- # Logging Block.
65
- #
66
- # ==== Params
67
- # +opt+ = Option hash object.
68
- # - +:name+ (String) = name of log file. Default is "<date>_<time>_<prog>.log". Reservation-words are "<date>","<time>","<prog>","<host>". "<date>" is replaced YYYYMMDD. "<time>" is replaced HHMMSS. "<prog>" is replaced a base-name of program file.
69
- # - +:dir+ (String) = log direcotry. Default is "${RB_HOME}/log"
70
- # - +:level+ (String) = log level. Default is "info". ["debug"|"info"|"warn"|"error"|"fatal"] .
71
- # - +:append+ (Boolean) = appned to log or not(=overwrite). Default is ture.
72
- # - +:stdout+ (Boolean) = output both the log file and STDOUT. Default is false.
73
- # ==== Block params
74
- # +log+ = Instance of +Logger+
75
- # ==== Sample
76
- # RBatch::Log.new({:dir => "/tmp", :level => "info"}){ |log|
77
- # log.info "info string"
78
- # }
79
- #
80
- def initialize(opt = nil)
81
- # parse option
82
- tmp = {}
83
- if opt.nil?
84
- @opt=RBatch.run_conf.clone
85
- else
32
+ @@def_vars
33
+ def Log.def_vars=(a)
34
+ raise ArgumentError, "type mismatch: #{a} for #RBatch::Variables" if ! a.kind_of?(RBatch::Variables)
35
+ @@def_vars=a
36
+ end
37
+ def Log.def_vars ; @@def_vars ; end
38
+ @@journal
39
+ def Log.journal=(a) ; @@journal=a ; end
40
+
41
+ @vars
42
+ @opt
43
+ @log
44
+ @stdout_log
45
+
46
+ def initialize(opt=nil)
47
+ @opt = opt
48
+ @vars = @@def_vars.clone
49
+ if ! opt.nil?
50
+ # change opt key from "hoge" to "log_hoge"
51
+ tmp = {}
86
52
  opt.each_key do |key|
87
53
  tmp[("log_" + key.to_s).to_sym] = opt[key]
88
54
  end
89
- @opt=RBatch.run_conf.merge(tmp)
55
+ @vars.merge!(tmp)
90
56
  end
91
-
92
- # determine log file name
93
- @prog_base = Pathname(File.basename(RBatch.ctrl.program_name)).sub_ext("").to_s
94
- @file_name = @opt[:log_name].clone
95
- @file_name.gsub!("<date>", Time.now.strftime("%Y%m%d"))
96
- @file_name.gsub!("<time>", Time.now.strftime("%H%M%S"))
97
- @file_name.gsub!("<prog>", @prog_base)
98
- @file_name.gsub!("<host>", @opt[:log_hostname])
99
- @log_dir = @opt[:log_dir].gsub("<home>",RBatch.ctrl.home_dir)
100
- path = File.join(@log_dir,@file_name)
57
+ @path = File.join(@vars[:log_dir],@vars[:log_name])
101
58
  # create Logger instance
102
59
  begin
103
- if @opt[:log_append] && File.exist?(path)
104
- @log = Logger.new(open(path,"a"))
60
+ if @vars[:log_append] && File.exist?(@path)
61
+ @log = Logger.new(open(@path,"a"))
105
62
  else
106
- @log = Logger.new(open(path,"w"))
63
+ @log = Logger.new(open(@path,"w"))
107
64
  end
108
65
  rescue Errno::ENOENT => e
109
- RBatch.ctrl.journal :error, "Can not open log file - #{path}"
110
- raise e
66
+ raise LogException,"Can not open log file - #{@path}"
111
67
  end
112
68
  # set logger option
113
- formatter = proc do |severity, datetime, progname, msg|
114
- head = "[#{datetime}] [" + sprintf("%-5s",severity) +"]"
115
- if msg.is_a? Exception
116
- "#{head} #{msg}\n" + msg.backtrace.map{|s| " [backtrace] #{s}"}.join("\n") + "\n"
117
- else
118
- "#{head} #{msg}\n"
119
- end
120
- end
121
- @log.level = @@log_level_map[@opt[:log_level]]
122
- @log.formatter = formatter
123
- if @opt[:log_stdout]
69
+ @log.level = @@LOG_LEVEL_MAP[@vars[:log_level]]
70
+ @log.formatter = @@FORMATTER
71
+ if @vars[:log_stdout]
124
72
  # ccreate Logger instance for STDOUT
125
73
  @stdout_log = Logger.new(STDOUT)
126
- @stdout_log.level = @@log_level_map[@opt[:log_level]]
127
- @stdout_log.formatter = formatter
74
+ @stdout_log.level = @@LOG_LEVEL_MAP[@vars[:log_level]]
75
+ @stdout_log.formatter = @@STDOUT_FORMATTER
128
76
  end
129
- RBatch.ctrl.journal :info,"Start Logging: \"#{path}\""
130
77
  # delete old log
131
- self.delete_old_log(@opt[:log_delete_old_log_date]) if @opt[:log_delete_old_log]
132
- # Firstly write journal to log
133
- if RBatch.run_conf[:mix_rbatch_msg_to_log]
134
- RBatch.ctrl.journals.each do |str|
135
- self.journal(str)
136
- end
137
- RBatch.ctrl.add_log(self)
138
- end
78
+ self.delete_old_log(@vars[:log_delete_old_log_date]) if @vars[:log_delete_old_log]
139
79
  # Start logging
80
+ @@journal.put 1,"Logging Start: \"#{@path}\""
81
+ @@journal.add_log(self)
140
82
  if block_given?
141
83
  begin
142
84
  yield self
@@ -151,29 +93,29 @@ module RBatch
151
93
  end
152
94
 
153
95
  def fatal(a)
154
- @stdout_log.fatal(a) if @opt[:log_stdout]
96
+ @stdout_log.fatal(a) if @vars[:log_stdout]
155
97
  @log.fatal(a)
156
- send_mail(a) if @opt[:log_send_mail]
98
+ send_mail(a) if @vars[:log_send_mail]
157
99
  end
158
100
 
159
101
  def error(a)
160
- @stdout_log.error(a) if @opt[:log_stdout]
102
+ @stdout_log.error(a) if @vars[:log_stdout]
161
103
  @log.error(a)
162
- send_mail(a) if @opt[:log_send_mail]
104
+ send_mail(a) if @vars[:log_send_mail]
163
105
  end
164
106
 
165
107
  def warn(a)
166
- @stdout_log.warn(a) if @opt[:log_stdout]
108
+ @stdout_log.warn(a) if @vars[:log_stdout]
167
109
  @log.warn(a)
168
110
  end
169
111
 
170
112
  def info(a)
171
- @stdout_log.info(a) if @opt[:log_stdout]
113
+ @stdout_log.info(a) if @vars[:log_stdout]
172
114
  @log.info(a)
173
115
  end
174
116
 
175
117
  def debug(a)
176
- @stdout_log.debug(a) if @opt[:log_stdout]
118
+ @stdout_log.debug(a) if @vars[:log_stdout]
177
119
  @log.debug(a)
178
120
  end
179
121
 
@@ -182,27 +124,26 @@ module RBatch
182
124
  end
183
125
 
184
126
  def close
185
- @stdout_log.close if @opt[:log_stdout]
186
127
  @log.close
187
128
  end
188
129
 
189
130
  # Delete old log files.
190
- # If @opt[:log_name] is not include "<date>", then do nothing.
131
+ # If @vars[:log_name] is not include "<date>", then do nothing.
191
132
  #
192
133
  # ==== Params
193
134
  # - +date+ (Integer): The day of leaving log files
194
135
  #
195
136
  def delete_old_log(date = 7)
196
- if Dir.exists?(@log_dir) && @opt[:log_name].include?("<date>")
197
- Dir::foreach(@log_dir) do |file|
137
+ if Dir.exists?(@vars[:log_dir]) && @vars.raw_value(:log_name).include?("<date>")
138
+ Dir::foreach(@vars[:log_dir]) do |file|
198
139
  r = Regexp.new("^" \
199
- + @opt[:log_name].gsub("<prog>",@prog_base)\
140
+ + @vars.raw_value(:log_name).gsub("<prog>",@vars[:program_base])\
200
141
  .gsub("<time>","[0-2][0-9][0-5][0-9][0-5][0-9]")\
201
142
  .gsub("<date>","([0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9])")\
202
143
  + "$")
203
144
  if r =~ file && Date.strptime($1,"%Y%m%d") <= Date.today - date
204
- RBatch.journal :info, "Delete old log file: " + File.join(@log_dir , file)
205
- File::delete(File.join(@log_dir , file))
145
+ @@journal.put 1, "Delete old log file: " + File.join(@vars[:log_dir] , file)
146
+ File::delete(File.join(@vars[:log_dir] , file))
206
147
  end
207
148
  end
208
149
  end
@@ -213,9 +154,9 @@ module RBatch
213
154
  # send mail
214
155
  def send_mail(msg)
215
156
  body = <<EOT
216
- From: <#{@opt[:log_mail_from]}>
217
- To: <#{@opt[:log_mail_to]}>
218
- Subject: [RBatch] #{RBatch.ctrl.program_name} has error
157
+ From: <#{@vars[:log_mail_from]}>
158
+ To: <#{@vars[:log_mail_to]}>
159
+ Subject: [RBatch] #{@vars[:program_name]} has error
219
160
  Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
220
161
  Mime-Version: 1.0
221
162
  Content-Type: text/plain; charset=ISO-2022-JP
@@ -223,10 +164,11 @@ Content-Transfer-Encoding: 7bit
223
164
 
224
165
  #{msg}
225
166
  EOT
226
- Net::SMTP.start(@opt[:log_mail_server_host],@opt[:log_mail_server_port] ) {|smtp|
227
- smtp.send_mail(body,@opt[:log_mail_from],@opt[:log_mail_to])
167
+ Net::SMTP.start(@vars[:log_mail_server_host],@vars[:log_mail_server_port] ) {|smtp|
168
+ smtp.send_mail(body,@vars[:log_mail_from],@vars[:log_mail_to])
228
169
  }
229
170
  end
230
171
  end # end class
172
+ class LogException < StandardError ; end
231
173
  end # end module
232
174
 
@@ -2,9 +2,8 @@ require 'tmpdir'
2
2
  require 'yaml'
3
3
  module RBatch
4
4
  class RunConf
5
- @opt
5
+ attr :path,:opt
6
6
  @yaml
7
- attr_reader :run_conf_path,:home_dir
8
7
  @@def_opt = {
9
8
  :conf_dir => "<home>/conf",
10
9
  :common_conf_name => "common.yaml",
@@ -21,35 +20,26 @@ module RBatch
21
20
  :log_delete_old_log => false,
22
21
  :log_delete_old_log_date => 7,
23
22
  :log_send_mail => false,
24
- :log_hostname => "unknownhost",
25
23
  :log_mail_to => nil,
26
24
  :log_mail_from => "rbatch.localhost",
27
25
  :log_mail_server_host => "localhost",
28
26
  :log_mail_server_port => 25,
29
- :mix_rbatch_msg_to_log => true
27
+ :rbatch_journal_level => 1,
28
+ :mix_rbatch_journal_to_logs => true
30
29
  }
31
- def initialize(run_conf_path,home_dir)
32
- @run_conf_path = run_conf_path
33
- @home_dir = home_dir
34
- reset
35
- load
36
- end
37
-
38
- def reset()
39
- @opt = @@def_opt.clone
40
- case RUBY_PLATFORM
41
- when /mswin|mingw/
42
- @opt[:log_hostname] = ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
43
- when /cygwin|linux/
44
- @opt[:log_hostname] = ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
30
+ def initialize(path=nil)
31
+ if path.nil?
32
+ @opt = @@def_opt.clone
45
33
  else
46
- @opt[:log_hostname] = "unknownhost"
34
+ @path = path
35
+ @opt = @@def_opt.clone
36
+ load
47
37
  end
48
38
  end
49
39
 
50
40
  def load()
51
41
  begin
52
- @yaml = YAML::load_file(@run_conf_path)
42
+ @yaml = YAML::load_file(@path)
53
43
  rescue
54
44
  # when run_conf does not exist, do nothing.
55
45
  @yaml = false
@@ -65,11 +55,10 @@ module RBatch
65
55
  end
66
56
  end
67
57
 
68
- def reload()
69
- reset
70
- load
58
+ def has_key?(key)
59
+ @opt.has_key?(key)
71
60
  end
72
-
61
+
73
62
  def merge!(opt)
74
63
  opt.each_key do |key|
75
64
  if @opt.has_key?(key)