rbatch 1.13.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/CHANGELOG +15 -0
  2. data/README.ja.md +81 -49
  3. data/README.md +88 -63
  4. data/Rakefile +2 -31
  5. data/bin/rbatch-init +57 -32
  6. data/{rdocs → doc}/RBatch/Cmd.html +66 -59
  7. data/{rdocs → doc}/RBatch/CmdException.html +9 -3
  8. data/{rdocs → doc}/RBatch/CmdResult.html +40 -34
  9. data/{rdocs → doc}/RBatch/CommonConfig/Exception.html +9 -3
  10. data/{rdocs → doc}/RBatch/CommonConfig.html +22 -17
  11. data/{rdocs → doc}/RBatch/Config/Exception.html +9 -3
  12. data/{rdocs → doc}/RBatch/Config.html +21 -16
  13. data/{rdocs/RBatchException.html → doc/RBatch/DoubleRunCheckException.html} +33 -30
  14. data/doc/RBatch/DoubleRunChecker.html +277 -0
  15. data/{rdocs → doc}/RBatch/Log.html +134 -138
  16. data/doc/RBatch/RunConf/Exception.html +159 -0
  17. data/doc/RBatch/RunConf.html +557 -0
  18. data/doc/RBatch.html +655 -0
  19. data/doc/created.rid +9 -0
  20. data/{rdocs → doc}/index.html +46 -20
  21. data/{rdocs → doc}/lib/rbatch/cmd_rb.html +4 -2
  22. data/{rdocs → doc}/lib/rbatch/common_config_rb.html +2 -2
  23. data/{rdocs → doc}/lib/rbatch/config_rb.html +2 -2
  24. data/doc/lib/rbatch/double_run_checker_rb.html +56 -0
  25. data/{rdocs → doc}/lib/rbatch/log_rb.html +2 -2
  26. data/doc/lib/rbatch/run_conf_rb.html +56 -0
  27. data/{rdocs → doc}/lib/rbatch/version_rb.html +2 -2
  28. data/{rdocs → doc}/lib/rbatch_rb.html +7 -3
  29. data/lib/rbatch/cmd.rb +20 -23
  30. data/lib/rbatch/common_config.rb +2 -3
  31. data/lib/rbatch/config.rb +1 -2
  32. data/lib/rbatch/double_run_checker.rb +24 -0
  33. data/lib/rbatch/log.rb +37 -59
  34. data/lib/rbatch/run_conf.rb +109 -0
  35. data/lib/rbatch/version.rb +1 -1
  36. data/lib/rbatch.rb +37 -56
  37. data/spec/{cmd_test.spec → cmd_spec.rb} +6 -6
  38. data/spec/common_config_spec.rb +54 -0
  39. data/spec/{config_test.spec → config_spec.rb} +2 -1
  40. data/spec/double_run_checker_spec.rb +10 -0
  41. data/spec/{log_test.spec → log_spec.rb} +22 -22
  42. data/spec/rbatch_spec.rb +24 -0
  43. data/spec/run_conf_spec.rb +85 -0
  44. metadata +66 -65
  45. data/rdocs/RBatch.html +0 -713
  46. data/rdocs/created.rid +0 -7
  47. data/spec/rbatch_test.spec +0 -25
  48. data/test/cases/test_cmd.rb +0 -134
  49. data/test/cases/test_common_config.rb +0 -41
  50. data/test/cases/test_config.rb +0 -41
  51. data/test/cases/test_log.rb +0 -613
  52. data/test/mocks/PrintArgs.exe +0 -0
  53. data/test/mocks/win_cmd.exe +0 -0
  54. /data/{rdocs → doc}/images/brick.png +0 -0
  55. /data/{rdocs → doc}/images/brick_link.png +0 -0
  56. /data/{rdocs → doc}/images/bug.png +0 -0
  57. /data/{rdocs → doc}/images/bullet_black.png +0 -0
  58. /data/{rdocs → doc}/images/bullet_toggle_minus.png +0 -0
  59. /data/{rdocs → doc}/images/bullet_toggle_plus.png +0 -0
  60. /data/{rdocs → doc}/images/date.png +0 -0
  61. /data/{rdocs → doc}/images/find.png +0 -0
  62. /data/{rdocs → doc}/images/loadingAnimation.gif +0 -0
  63. /data/{rdocs → doc}/images/macFFBgHack.png +0 -0
  64. /data/{rdocs → doc}/images/package.png +0 -0
  65. /data/{rdocs → doc}/images/page_green.png +0 -0
  66. /data/{rdocs → doc}/images/page_white_text.png +0 -0
  67. /data/{rdocs → doc}/images/page_white_width.png +0 -0
  68. /data/{rdocs → doc}/images/plugin.png +0 -0
  69. /data/{rdocs → doc}/images/ruby.png +0 -0
  70. /data/{rdocs → doc}/images/tag_green.png +0 -0
  71. /data/{rdocs → doc}/images/wrench.png +0 -0
  72. /data/{rdocs → doc}/images/wrench_orange.png +0 -0
  73. /data/{rdocs → doc}/images/zoom.png +0 -0
  74. /data/{rdocs → doc}/js/darkfish.js +0 -0
  75. /data/{rdocs → doc}/js/jquery.js +0 -0
  76. /data/{rdocs → doc}/js/quicksearch.js +0 -0
  77. /data/{rdocs → doc}/js/thickbox-compressed.js +0 -0
  78. /data/{rdocs → doc}/rdoc.css +0 -0
data/lib/rbatch/log.rb CHANGED
@@ -38,21 +38,6 @@ module RBatch
38
38
  #
39
39
  class Log
40
40
  @@verbose = false
41
- @@def_opt = {
42
- :name => "<date>_<time>_<prog>.log",
43
- :dir => File.join(RBatch::home_dir , "log"),
44
- :append => true,
45
- :level => "info",
46
- :stdout => false,
47
- :quiet => false,
48
- :delete_old_log => false,
49
- :delete_old_log_date => 7,
50
- :send_mail => false,
51
- :mail_to => nil,
52
- :mail_from => "rbatch.localhost",
53
- :mail_server_host => "localhost",
54
- :mail_server_port => 25
55
- }
56
41
  @@log_level_map = {
57
42
  "debug" => Logger::DEBUG,
58
43
  "info" => Logger::INFO,
@@ -85,7 +70,6 @@ module RBatch
85
70
  # - +:level+ (String) = log level. Default is "info". ["debug"|"info"|"warn"|"error"|"fatal"] .
86
71
  # - +:append+ (Boolean) = appned to log or not(=overwrite). Default is ture.
87
72
  # - +:stdout+ (Boolean) = output both the log file and STDOUT. Default is false.
88
- # - +:quiet+ (Boolean) = output only logfile, don't output to STDOUT. Default is true.
89
73
  # ==== Block params
90
74
  # +log+ = Instance of +Logger+
91
75
  # ==== Sample
@@ -95,37 +79,34 @@ module RBatch
95
79
  #
96
80
  def initialize(opt = nil)
97
81
  # parse option
98
- @opt = @@def_opt.clone
99
- @@def_opt.each_key do |key|
100
- if opt != nil && opt[key] != nil
101
- # use argument
102
- @opt[key] = opt[key]
103
- elsif RBatch.rbatch_config != nil \
104
- && RBatch.rbatch_config["log_" + key.to_s] != nil
105
- # use config
106
- @opt[key] = RBatch.rbatch_config["log_" + key.to_s]
107
- else
108
- # use default
82
+ tmp = {}
83
+ if opt.nil?
84
+ @opt=RBatch.run_conf.clone
85
+ else
86
+ opt.each_key do |key|
87
+ tmp[("log_" + key.to_s).to_sym] = opt[key]
109
88
  end
89
+ @opt=RBatch.run_conf.merge(tmp)
110
90
  end
111
- puts "option = " + @opt.to_s if @@verbose
91
+
112
92
  # determine log file name
113
93
  @prog_base = Pathname(File.basename(RBatch.program_name)).sub_ext("").to_s
114
- @file_name = @opt[:name].clone
94
+ @file_name = @opt[:log_name].clone
115
95
  @file_name.gsub!("<date>", Time.now.strftime("%Y%m%d"))
116
96
  @file_name.gsub!("<time>", Time.now.strftime("%H%M%S"))
117
97
  @file_name.gsub!("<prog>", @prog_base)
118
- @file_name.gsub!("<host>", RBatch::hostname)
119
- path = File.join(@opt[:dir],@file_name)
98
+ @file_name.gsub!("<host>", @opt[:log_hostname])
99
+ @log_dir = @opt[:log_dir].gsub("<home>",RBatch.home_dir)
100
+ path = File.join(@log_dir,@file_name)
120
101
  # create Logger instance
121
102
  begin
122
- if @opt[:append] && File.exist?(path)
103
+ if @opt[:log_append] && File.exist?(path)
123
104
  @log = Logger.new(open(path,"a"))
124
105
  else
125
106
  @log = Logger.new(open(path,"w"))
126
107
  end
127
108
  rescue Errno::ENOENT => e
128
- STDERR.puts "RBatch ERROR: Can not open log file - #{path}" if ! @opt[:quiet]
109
+ RBatch.journal :error, "Can not open log file - #{path}"
129
110
  raise e
130
111
  end
131
112
  # set logger option
@@ -137,21 +118,18 @@ module RBatch
137
118
  "#{head} #{msg}\n"
138
119
  end
139
120
  end
140
- @log.formatter = @opt[:formatter] if @opt[:formatter]
141
- @log.level = @@log_level_map[@opt[:level]]
121
+ @log.level = @@log_level_map[@opt[:log_level]]
142
122
  @log.formatter = formatter
143
- if @opt[:stdout]
123
+ if @opt[:log_stdout]
144
124
  # ccreate Logger instance for STDOUT
145
125
  @stdout_log = Logger.new(STDOUT)
146
- @stdout_log.formatter = @opt[:formatter] if @opt[:formatter]
147
- @stdout_log.level = @@log_level_map[@opt[:level]]
126
+ @stdout_log.level = @@log_level_map[@opt[:log_level]]
148
127
  @stdout_log.formatter = formatter
149
128
  end
150
- puts "Log file: " + path if ! @opt[:quiet]
129
+ RBatch.journal :info,"Start Logging. Log file: " + path
151
130
  # delete old log
152
- self.delete_old_log(@opt[:delete_old_log_date]) if @opt[:delete_old_log]
131
+ self.delete_old_log(@opt[:log_delete_old_log_date]) if @opt[:log_delete_old_log]
153
132
  # Start logging
154
- self.info("Start Logging. (PID=#{$$.to_s})") if ! @opt[:quiet]
155
133
  if block_given?
156
134
  begin
157
135
  yield self
@@ -166,54 +144,54 @@ module RBatch
166
144
  end
167
145
 
168
146
  def fatal(a)
169
- @stdout_log.fatal(a) if @opt[:stdout]
147
+ @stdout_log.fatal(a) if @opt[:log_stdout]
170
148
  @log.fatal(a)
171
- send_mail(a) if @opt[:send_mail]
149
+ send_mail(a) if @opt[:log_send_mail]
172
150
  end
173
151
 
174
152
  def error(a)
175
- @stdout_log.error(a) if @opt[:stdout]
153
+ @stdout_log.error(a) if @opt[:log_stdout]
176
154
  @log.error(a)
177
- send_mail(a) if @opt[:send_mail]
155
+ send_mail(a) if @opt[:log_send_mail]
178
156
  end
179
157
 
180
158
  def warn(a)
181
- @stdout_log.warn(a) if @opt[:stdout]
159
+ @stdout_log.warn(a) if @opt[:log_stdout]
182
160
  @log.warn(a)
183
161
  end
184
162
 
185
163
  def info(a)
186
- @stdout_log.info(a) if @opt[:stdout]
164
+ @stdout_log.info(a) if @opt[:log_stdout]
187
165
  @log.info(a)
188
166
  end
189
167
 
190
168
  def debug(a)
191
- @stdout_log.debug(a) if @opt[:stdout]
169
+ @stdout_log.debug(a) if @opt[:log_stdout]
192
170
  @log.debug(a)
193
171
  end
194
172
 
195
173
  def close
196
- @stdout_log.close if @opt[:stdout]
174
+ @stdout_log.close if @opt[:log_stdout]
197
175
  @log.close
198
176
  end
199
177
 
200
178
  # Delete old log files.
201
- # If @opt[:name] is not include "<date>", then do nothing.
179
+ # If @opt[:log_name] is not include "<date>", then do nothing.
202
180
  #
203
181
  # ==== Params
204
182
  # - +date+ (Integer): The day of leaving log files
205
183
  #
206
184
  def delete_old_log(date = 7)
207
- if Dir.exists?(@opt[:dir]) && @opt[:name].include?("<date>")
208
- Dir::foreach(@opt[:dir]) do |file|
185
+ if Dir.exists?(@log_dir) && @opt[:log_name].include?("<date>")
186
+ Dir::foreach(@log_dir) do |file|
209
187
  r = Regexp.new("^" \
210
- + @opt[:name].gsub("<prog>",@prog_base)\
188
+ + @opt[:log_name].gsub("<prog>",@prog_base)\
211
189
  .gsub("<time>","[0-2][0-9][0-5][0-9][0-5][0-9]")\
212
190
  .gsub("<date>","([0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9])")\
213
191
  + "$")
214
192
  if r =~ file && Date.strptime($1,"%Y%m%d") <= Date.today - date
215
- puts "Delete old log file: " + File.join(@opt[:dir] , file) if ! @opt[:quiet]
216
- File::delete(File.join(@opt[:dir] , file))
193
+ RBatch.journal :info, "Delete old log file: " + File.join(@log_dir , file)
194
+ File::delete(File.join(@log_dir , file))
217
195
  end
218
196
  end
219
197
  end
@@ -224,8 +202,8 @@ module RBatch
224
202
  # send mail
225
203
  def send_mail(msg)
226
204
  body = <<EOT
227
- From: <#{@opt[:mail_from]}>
228
- To: <#{@opt[:mail_to]}>
205
+ From: <#{@opt[:log_mail_from]}>
206
+ To: <#{@opt[:log_mail_to]}>
229
207
  Subject: [RBatch] #{RBatch.program_name} has error
230
208
  Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
231
209
  Mime-Version: 1.0
@@ -234,8 +212,8 @@ Content-Transfer-Encoding: 7bit
234
212
 
235
213
  #{msg}
236
214
  EOT
237
- Net::SMTP.start(@opt[:mail_server_host],@opt[:mail_server_port] ) {|smtp|
238
- smtp.send_mail(body,@opt[:mail_from],@opt[:mail_to])
215
+ Net::SMTP.start(@opt[:log_mail_server_host],@opt[:log_mail_server_port] ) {|smtp|
216
+ smtp.send_mail(body,@opt[:log_mail_from],@opt[:log_mail_to])
239
217
  }
240
218
  end
241
219
  end # end class
@@ -0,0 +1,109 @@
1
+ require 'tmpdir'
2
+ require 'yaml'
3
+ module RBatch
4
+ class RunConf
5
+ @opt
6
+ @yaml
7
+ attr_reader :run_conf_path,:home_dir
8
+ @@def_opt = {
9
+ :conf_dir => "<home>/conf",
10
+ :common_conf_name => "common.yaml",
11
+ :forbid_double_run => false,
12
+ :cmd_raise => false,
13
+ :cmd_timeout => 0,
14
+ :log_dir => "<home>/log",
15
+ :log_name => "<date>_<time>_<prog>.log",
16
+ :log_append => true,
17
+ :log_level => "info",
18
+ :log_stdout => false,
19
+ :log_delete_old_log => false,
20
+ :log_delete_old_log_date => 7,
21
+ :log_send_mail => false,
22
+ :log_hostname => "unknownhost",
23
+ :log_mail_to => nil,
24
+ :log_mail_from => "rbatch.localhost",
25
+ :log_mail_server_host => "localhost",
26
+ :log_mail_server_port => 25
27
+ }
28
+ def initialize(run_conf_path,home_dir)
29
+ @run_conf_path = run_conf_path
30
+ @home_dir = home_dir
31
+ reset
32
+ load
33
+ end
34
+
35
+ def reset()
36
+ @opt = @@def_opt.clone
37
+ case RUBY_PLATFORM
38
+ when /mswin|mingw/
39
+ @opt[:log_hostname] = ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
40
+ when /cygwin|linux/
41
+ @opt[:log_hostname] = ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
42
+ else
43
+ @opt[:log_hostname] = "unknownhost"
44
+ end
45
+ end
46
+
47
+ def load()
48
+ begin
49
+ @yaml = YAML::load_file(@run_conf_path)
50
+ rescue
51
+ # when run_conf does not exist, do nothing.
52
+ @yaml = false
53
+ end
54
+ if @yaml
55
+ @yaml.each_key do |key|
56
+ if @@def_opt.has_key?(key.to_sym)
57
+ @opt[key.to_sym]=@yaml[key]
58
+ else
59
+ raise RBatch::RunConf::Exception, "\"#{key}\" is not available option"
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def reload()
66
+ reset
67
+ load
68
+ end
69
+
70
+ def merge!(opt)
71
+ opt.each_key do |key|
72
+ if @opt.has_key?(key)
73
+ @opt[key] = opt[key]
74
+ else
75
+ raise RBatch::RunConf::Exception, "\"#{key}\" is not available option"
76
+ end
77
+ end
78
+ end
79
+
80
+ def merge(opt)
81
+ tmp = @opt.clone
82
+ opt.each_key do |key|
83
+ if tmp.has_key?(key)
84
+ tmp[key] = opt[key]
85
+ else
86
+ raise RBatch::RunConf::Exception, "\"#{key}\" is not available option"
87
+ end
88
+ end
89
+ return tmp
90
+ end
91
+
92
+ def[](key)
93
+ if @opt[key].nil?
94
+ raise RBatch::RunConf::Exception, "Value of key=\"#{key}\" is nil"
95
+ end
96
+ @opt[key]
97
+ end
98
+
99
+ def[]=(key,value)
100
+ if ! @opt.has_key?(key)
101
+ raise RBatch::RunConf::Exception, "Key=\"#{key}\" does not exist"
102
+ end
103
+ @opt[key]=value
104
+ end
105
+ end
106
+
107
+ class RunConf::Exception < Exception ; end
108
+
109
+ end
@@ -1,3 +1,3 @@
1
1
  module RBatch
2
- VERSION = "1.13.1"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/rbatch.rb CHANGED
@@ -1,74 +1,55 @@
1
1
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
- require 'digest'
3
2
  require 'yaml'
3
+
4
4
  module RBatch
5
- @@opt = {}
6
- @@rbatch_config = nil
5
+ @@program_name = $PROGRAM_NAME
6
+ @@program_base = File.basename($PROGRAM_NAME)
7
+ @@home_dir = nil
8
+ @@run_conf_path = nil
9
+ @@run_conf = nil
10
+ @@journal_verbose = 3
11
+ @@journal_verbose_map = { :error => 1, :warn => 2, :info => 3, :debug => 4}
7
12
  module_function
8
- def program_name ; @@opt[:program_name] ; end
9
- def home_dir ; @@opt[:home_dir] ; end
10
- def home_dir=(d) ; @@opt[:home_dir]=d ; end
11
- def hostname ; @@opt[:hostname] ; end
12
- def rbatch_config ; @@rbatch_config ; end
13
- def opt ; @@opt ; end
14
- def init
15
- @@opt[:program_name] = $PROGRAM_NAME
16
- @@opt[:home_dir] = ENV["RB_HOME"] ? ENV["RB_HOME"] : File.join(File.dirname(@@opt[:program_name]) , "..")
17
- case RUBY_PLATFORM
18
- when /mswin|mingw/
19
- @@opt[:hostname] = ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
20
- when /cygwin|linux/
21
- @@opt[:hostname] = ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
22
- else
23
- @@opt[:hostname] = "unknownhost"
24
- end
25
- load_rbatch_config
26
- end
27
- def rbatch_config_path
28
- File.join(@@opt[:home_dir],"conf","rbatch.yaml")
29
- end
30
- def config_dir
31
- File.join(RBatch.home_dir,"conf")
32
- end
33
- def log_dir
34
- File.join(RBatch.home_dir,"log")
13
+ def program_name ; @@program_name ; end
14
+ def program_base ; @@program_base ; end
15
+ def home_dir ; @@home_dir ; end
16
+ def run_conf_path ; @@run_conf_path ; end
17
+ def run_conf ; @@run_conf ; end
18
+ def conf_dir ; @@run_conf[:conf_dir].gsub("<home>",@@home_dir) ; end
19
+ def log_dir ; @@run_conf[:log_dir].gsub("<home>",@@home_dir) ; end
20
+ def journal(level,str)
21
+ puts "[RBatch] " + str if @@journal_verbose_map[level] <= @@journal_verbose
35
22
  end
36
- def load_rbatch_config
37
- if File.exist?(rbatch_config_path)
38
- @@rbatch_config = YAML::load_file(rbatch_config_path)
39
- if @@rbatch_config == false
40
- @@rbatch_config = nil
41
- end
23
+ def init
24
+ @@journal_verbose = ENV["RB_VERBOSE"].to_i if ENV["RB_VERBOSE"]
25
+ if ENV["RB_HOME"]
26
+ @@home_dir = ENV["RB_HOME"]
27
+ RBatch.journal :info,"RB_HOME : \"#{@@home_dir}\" (defined by $RB_HOME)"
42
28
  else
43
- @@rbatch_config = nil
44
- end
45
- end
46
- def double_run_check
47
- # double run check
48
- if ( @@rbatch_config != nil && @@rbatch_config["forbid_double_run"] )
49
- lock_file="rbatch_lock_" + Digest::MD5.hexdigest(@@opt[:program_name])
50
- if Dir.exists? @@opt[:tmp_dir]
51
- Dir::foreach(@@opt[:tmp_dir]) do |f|
52
- if (Regexp.new(lock_file) =~ f)
53
- raise RBatchException, "Script double run is forbid about \"#{@@opt[:program_name]}\""
54
- end
55
- end
56
- end
57
- # make lockfile
58
- Tempfile::new(lock_file,@@opt[:tmp_dir])
29
+ @@home_dir = File.join(File.dirname(@@program_name) , "..")
30
+ RBatch.journal :info,"RB_HOME : \"#{@@home_dir}\" (default)"
59
31
  end
32
+ @@run_conf_path = File.join(@@home_dir,".rbatchrc")
33
+ RBatch.journal :info,"Run-Conf: \"#{@@run_conf_path}\""
34
+
35
+ @@run_conf = RunConf.new(@@run_conf_path,@@home_dir)
36
+ RBatch.journal :debug,"RBatch option : #{@@run_conf.inspect}"
60
37
  end
61
38
  end
62
39
 
63
- # RBatch Exception
64
- class RBatchException < Exception ; end
65
-
66
40
  # main
41
+ require 'rbatch/run_conf'
42
+ require 'rbatch/double_run_checker'
43
+
67
44
  RBatch::init
45
+ if ( RBatch.run_conf[:forbid_double_run] )
46
+ RBatch::DoubleRunChecker.check(File.basename(RBatch.program_name)) #raise error if check is NG
47
+ RBatch::DoubleRunChecker.make_lock_file(File.basename(RBatch.program_name))
48
+ end
68
49
 
69
50
  require 'rbatch/log'
70
51
  require 'rbatch/config'
71
52
  require 'rbatch/common_config'
72
53
  require 'rbatch/cmd'
73
54
 
74
- RBatch::double_run_check
55
+ RBatch.journal :info,"Start \"#{RBatch.program_name}\" under RBatch (PID=#{$$.to_s})"
@@ -1,9 +1,13 @@
1
1
  require 'tmpdir'
2
+ ENV["RB_VERBOSE"]="0"
2
3
  ENV["RB_HOME"]=Dir.tmpdir
3
4
 
4
5
  require 'rbatch'
5
6
 
6
7
  describe RBatch::Cmd do
8
+ before :each do
9
+ RBatch.run_conf.reset
10
+ end
7
11
 
8
12
  it "run command which status is 0" do
9
13
  result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'"
@@ -110,18 +114,14 @@ describe RBatch::Cmd do
110
114
  Dir::mkdir @config_dir if ! Dir.exists? @config_dir
111
115
  end
112
116
  describe "raise" do
113
- before :all do
114
- open( @config_file , "w" ){|f| f.write("cmd_raise: true")}
115
- RBatch.load_rbatch_config
117
+ before :each do
118
+ RBatch.run_conf[:cmd_raise] = true
116
119
  end
117
120
  it "raise error when command status is not 0" do
118
121
  expect {
119
122
  RBatch::cmd "ruby -e 'exit 1;'"
120
123
  }.to raise_error(RBatch::CmdException)
121
124
  end
122
- after :each do
123
- FileUtils.rm @config_file
124
- end
125
125
  end
126
126
  end
127
127
  end
@@ -0,0 +1,54 @@
1
+ require 'tmpdir'
2
+ ENV["RB_VERBOSE"]="0"
3
+ ENV["RB_HOME"]=Dir.tmpdir
4
+
5
+ require 'rbatch'
6
+
7
+ describe RBatch::CommonConfig do
8
+ before :all do
9
+ @config_dir=File.join(Dir.tmpdir,"conf")
10
+ @config_file = File.join(@config_dir , "common.yaml")
11
+ Dir::mkdir @config_dir if ! Dir.exists? @config_dir
12
+ end
13
+
14
+ before :each do
15
+ end
16
+
17
+ after :each do
18
+ FileUtils.rm @config_file if File.exists? @config_file
19
+ end
20
+
21
+ after :all do
22
+ end
23
+
24
+ it "read config" do
25
+ open( @config_file , "w" ){|f| f.write("key: value")}
26
+ expect(RBatch.common_config["key"]).to eq "value"
27
+ end
28
+
29
+ it "raise error when config does not exist" do
30
+ expect {
31
+ RBatch.common_config
32
+ }.to raise_error(Errno::ENOENT)
33
+ end
34
+
35
+ it "read config twice" do
36
+ open( @config_file , "w" ){|f| f.write("key: value")}
37
+ expect(RBatch.common_config["key"]).to eq "value"
38
+ expect(RBatch.common_config["key"]).to eq "value"
39
+ end
40
+
41
+ it "raise error when read value which key does not exist" do
42
+ open( @config_file , "w" ){|f| f.write("key: value")}
43
+ expect {
44
+ RBatch.common_config["not_exist"]
45
+ }.to raise_error(RBatch::CommonConfig::Exception)
46
+ end
47
+
48
+ it "success when common_conf_name changed" do
49
+ conf=File.join(Dir.tmpdir,"global.yaml")
50
+ open( conf , "w" ){|f| f.write("key4: value4")}
51
+ RBatch.run_conf[:common_conf_name]="global.yaml"
52
+ expect(RBatch.common_config["key4"]).to eq "value4"
53
+ end
54
+ end
@@ -1,11 +1,12 @@
1
1
  require 'tmpdir'
2
+ ENV["RB_VERBOSE"]="0"
2
3
  ENV["RB_HOME"]=Dir.tmpdir
3
4
 
4
5
  require 'rbatch'
5
6
 
6
7
  describe RBatch::Config do
7
8
  before :all do
8
- @config_dir=RBatch.config_dir
9
+ @config_dir=File.join(Dir.tmpdir,"conf")
9
10
  @config_file = File.join(@config_dir , "rspec.yaml")
10
11
  Dir::mkdir @config_dir if ! Dir.exists? @config_dir
11
12
  end
@@ -0,0 +1,10 @@
1
+ require 'rbatch/double_run_checker'
2
+
3
+ describe RBatch::DoubleRunChecker do
4
+ it "check" do
5
+ expect{RBatch::DoubleRunChecker.check("hoge")}.to_not raise_error
6
+ RBatch::DoubleRunChecker.make_lock_file("hoge")
7
+ expect{RBatch::DoubleRunChecker.check("hoge")}.to raise_error(RBatch::DoubleRunCheckException)
8
+ expect{RBatch::DoubleRunChecker.check("bar")}.to_not raise_error
9
+ end
10
+ end
@@ -1,4 +1,5 @@
1
1
  require 'tmpdir'
2
+ ENV["RB_VERBOSE"]="0"
2
3
  ENV["RB_HOME"]=Dir.tmpdir
3
4
 
4
5
  require 'rbatch'
@@ -6,21 +7,20 @@ require 'rbatch'
6
7
  describe RBatch::Log do
7
8
 
8
9
  before :all do
9
- @dir = RBatch.log_dir
10
+ @dir = File.join(Dir.tmpdir,"log")
10
11
  Dir::mkdir(@dir)if ! Dir.exists? @dir
11
12
  end
12
13
 
13
14
  before :each do
14
- # set quiet option
15
- confstr = "log_quiet: true\n"
16
- open( RBatch.rbatch_config_path , "w" ){|f| f.write(confstr)}
17
- RBatch.load_rbatch_config
15
+ open( RBatch.run_conf_path , "w" ){|f| f.write("")}
16
+ RBatch.run_conf.reload
18
17
  end
19
18
 
20
19
  after :each do
21
20
  Dir::foreach(@dir) do |f|
22
21
  File::delete(File.join(@dir , f)) if ! (/\.+$/ =~ f)
23
22
  end
23
+ FileUtils.rm(RBatch.run_conf_path) if File.exist?(RBatch.run_conf_path)
24
24
  end
25
25
 
26
26
  it "is run" do
@@ -271,8 +271,8 @@ describe RBatch::Log do
271
271
  describe "option by config" do
272
272
  it "change log name" do
273
273
  confstr = "log_name: name1.log"
274
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
275
- RBatch.load_rbatch_config
274
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
275
+ RBatch.run_conf.reload
276
276
  RBatch::Log.new() do | log |
277
277
  log.info("hoge")
278
278
  end
@@ -284,8 +284,8 @@ describe RBatch::Log do
284
284
  it "change log dir" do
285
285
  @tmp = Dir.tmpdir
286
286
  confstr = "log_name: c.log\nlog_dir: " + @tmp
287
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
288
- RBatch.load_rbatch_config
287
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
288
+ RBatch.run_conf.reload
289
289
  RBatch::Log.new({:name => "c.log", :dir=> @tmp }) do | log |
290
290
  log.info("hoge")
291
291
  end
@@ -295,9 +295,9 @@ describe RBatch::Log do
295
295
  end
296
296
 
297
297
  it "is append mode" do
298
- confstr = "log_name: a.log\nappend: true"
299
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
300
- RBatch.load_rbatch_config
298
+ confstr = "log_name: a.log\nlog_append: true"
299
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
300
+ RBatch.run_conf.reload
301
301
  RBatch::Log.new() do | log |
302
302
  log.info("line1")
303
303
  end
@@ -313,8 +313,8 @@ describe RBatch::Log do
313
313
 
314
314
  it "is overwrite mode" do
315
315
  confstr = "log_name: a.log\nlog_append: false"
316
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
317
- RBatch.load_rbatch_config
316
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
317
+ RBatch.run_conf.reload
318
318
  RBatch::Log.new() do | log |
319
319
  log.info("line1")
320
320
  end
@@ -330,8 +330,8 @@ describe RBatch::Log do
330
330
 
331
331
  it "is warn level" do
332
332
  confstr = "log_name: a.log\nlog_level: warn"
333
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
334
- RBatch.load_rbatch_config
333
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
334
+ RBatch.run_conf.reload
335
335
  RBatch::Log.new() do | log |
336
336
  log.debug("test_debug")
337
337
  log.info("test_info")
@@ -351,8 +351,8 @@ describe RBatch::Log do
351
351
 
352
352
  it "delete old log file which name include <date>" do
353
353
  confstr = "log_delete_old_log: true"
354
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
355
- RBatch.load_rbatch_config
354
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
355
+ RBatch.run_conf.reload
356
356
  loglist = [*0..20].map do |day|
357
357
  File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
358
358
  end
@@ -371,8 +371,8 @@ describe RBatch::Log do
371
371
  describe "option by both argument and config" do
372
372
  it "is prior to argument than config" do
373
373
  confstr = "log_name: a.log"
374
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
375
- RBatch.load_rbatch_config
374
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
375
+ RBatch.run_conf.reload
376
376
  RBatch::Log.new({:name => "b.log"}) do | log |
377
377
  log.info("hoge")
378
378
  end
@@ -417,8 +417,8 @@ describe RBatch::Log do
417
417
 
418
418
  it "option by config" do
419
419
  confstr = "log_name: e.log"
420
- open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
421
- RBatch.load_rbatch_config
420
+ open( RBatch.run_conf_path , "a" ){|f| f.write(confstr)}
421
+ RBatch.run_conf.reload
422
422
  log = RBatch::Log.new()
423
423
  log.info("hoge")
424
424
  log.close