rbatch 2.1.6 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.ja.md +5 -2
- data/README.md +12 -8
- data/lib/rbatch/cmd.rb +51 -10
- data/lib/rbatch/config.rb +47 -10
- data/lib/rbatch/controller.rb +1 -0
- data/lib/rbatch/double_run_checker.rb +2 -0
- data/lib/rbatch/journal.rb +1 -0
- data/lib/rbatch/log.rb +111 -41
- data/lib/rbatch/run_conf.rb +1 -0
- data/lib/rbatch/variables.rb +2 -0
- data/lib/rbatch/version.rb +1 -1
- data/lib/rbatch.rb +46 -0
- data/spec/rbatch/config_spec.rb +26 -0
- metadata +3 -61
- data/doc/RBatch/Cmd.html +0 -321
- data/doc/RBatch/CmdException.html +0 -164
- data/doc/RBatch/CmdResult.html +0 -494
- data/doc/RBatch/CommonConfig/Exception.html +0 -159
- data/doc/RBatch/CommonConfig.html +0 -456
- data/doc/RBatch/Config/Exception.html +0 -159
- data/doc/RBatch/Config.html +0 -416
- data/doc/RBatch/Controller.html +0 -374
- data/doc/RBatch/DoubleRunCheckException.html +0 -164
- data/doc/RBatch/DoubleRunChecker.html +0 -279
- data/doc/RBatch/Journal.html +0 -332
- data/doc/RBatch/Log.html +0 -667
- data/doc/RBatch/LogException.html +0 -164
- data/doc/RBatch/RBatch/RBatch/ConfigException.html +0 -164
- data/doc/RBatch/RBatch/RBatch.html +0 -163
- data/doc/RBatch/RBatch/VariablesException.html +0 -164
- data/doc/RBatch/RBatch.html +0 -165
- data/doc/RBatch/RunConf/Exception.html +0 -164
- data/doc/RBatch/RunConf.html +0 -503
- data/doc/RBatch/Variables.html +0 -437
- data/doc/RBatch.html +0 -411
- data/doc/created.rid +0 -11
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +0 -197
- data/doc/js/darkfish.js +0 -118
- data/doc/js/jquery.js +0 -32
- data/doc/js/quicksearch.js +0 -114
- data/doc/js/thickbox-compressed.js +0 -10
- data/doc/lib/rbatch/cmd_rb.html +0 -60
- data/doc/lib/rbatch/common_config_rb.html +0 -56
- data/doc/lib/rbatch/config_rb.html +0 -56
- data/doc/lib/rbatch/controller_rb.html +0 -66
- data/doc/lib/rbatch/double_run_checker_rb.html +0 -56
- data/doc/lib/rbatch/journal_rb.html +0 -52
- data/doc/lib/rbatch/log_rb.html +0 -60
- data/doc/lib/rbatch/run_conf_rb.html +0 -56
- data/doc/lib/rbatch/variables_rb.html +0 -54
- data/doc/lib/rbatch/version_rb.html +0 -52
- data/doc/lib/rbatch_rb.html +0 -54
- data/doc/rdoc.css +0 -763
data/.gitignore
CHANGED
data/README.ja.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[
|
|
1
|
+
[English](https://github.com/fetaro/rbatch/blob/master/README.md "english") | [Japanese ](https://github.com/fetaro/rbatch/blob/master/README.ja.md "japanese") | [Document (YardDoc)](http://fetaro.github.io/rbatch/index.html)
|
|
2
2
|
|
|
3
3
|
RBatch:Ruby-base バッチ スクリプト フレームワーク
|
|
4
4
|
=============
|
|
@@ -30,7 +30,7 @@ RBatchはRubyで書かれたシンプルなバッチスクリプトのフレー
|
|
|
30
30
|
$ ruby bin/hello_world.rb
|
|
31
31
|
$ cat log/YYYYMMDD_HHMMSS_hello_world.log
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
機能概要
|
|
34
34
|
--------------
|
|
35
35
|
|
|
36
36
|
### RBatchホームディレクトリ
|
|
@@ -166,6 +166,9 @@ p r.status # => 0
|
|
|
166
166
|
|
|
167
167
|
`forbid_double_run`のオプションを利用すれば、RBatchを利用したプログラムの二重起動チェックができます。
|
|
168
168
|
|
|
169
|
+
ドキュメント
|
|
170
|
+
--------------
|
|
171
|
+
こちらにあります→ [Document (YardDoc)](http://fetaro.github.io/rbatch/index.html)
|
|
169
172
|
|
|
170
173
|
サンプル
|
|
171
174
|
--------------
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[
|
|
1
|
+
[English](https://github.com/fetaro/rbatch/blob/master/README.md "english") | [Japanese ](https://github.com/fetaro/rbatch/blob/master/README.ja.md "japanese") | [Document (YardDoc)](http://fetaro.github.io/rbatch/index.html)
|
|
2
2
|
|
|
3
3
|
RBatch: Framework for Ruby-based Batch Script
|
|
4
4
|
=============
|
|
@@ -31,7 +31,7 @@ Quick Start
|
|
|
31
31
|
$ ruby bin/hello_world.rb
|
|
32
32
|
$ cat log/YYYYMMDD_HHMMSS_hello_world.log
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
Overview
|
|
35
35
|
--------------
|
|
36
36
|
|
|
37
37
|
### RBatch home directory
|
|
@@ -166,6 +166,11 @@ You have no use for an error handring.
|
|
|
166
166
|
|
|
167
167
|
Using `forbid_double_run` option, two same name scripts cannot run at the same time.
|
|
168
168
|
|
|
169
|
+
Manual
|
|
170
|
+
--------------
|
|
171
|
+
|
|
172
|
+
Manual -> [Document (YardDoc)](http://fetaro.github.io/rbatch/index.html)
|
|
173
|
+
|
|
169
174
|
Sample
|
|
170
175
|
--------------
|
|
171
176
|
|
|
@@ -366,14 +371,13 @@ Sample of RBatch Run-Conf `${RB_HOME}/.rbatchrc`.
|
|
|
366
371
|
# Delete old log files
|
|
367
372
|
#
|
|
368
373
|
# Default is false.
|
|
369
|
-
# If this is true, delete old log
|
|
370
|
-
#
|
|
371
|
-
# RBatch::Log instance, and log filename format include "<date>".
|
|
374
|
+
# If this is true, delete old log files when RBatch::Log.new is called.
|
|
375
|
+
# If log filename format does not include "<date>", do nothing.
|
|
372
376
|
#
|
|
373
377
|
#log_delete_old_log : true
|
|
374
378
|
#log_delete_old_log : false
|
|
375
379
|
|
|
376
|
-
# Expire
|
|
380
|
+
# Expire Days of Log Files
|
|
377
381
|
#
|
|
378
382
|
# Default is 7.
|
|
379
383
|
#
|
|
@@ -382,8 +386,8 @@ Sample of RBatch Run-Conf `${RB_HOME}/.rbatchrc`.
|
|
|
382
386
|
# Send Mail
|
|
383
387
|
#
|
|
384
388
|
# Default is false.
|
|
385
|
-
# When log.error
|
|
386
|
-
#
|
|
389
|
+
# When log.error is called, log.fatal is called,
|
|
390
|
+
# or rescue an Exception, send e-mail.
|
|
387
391
|
#
|
|
388
392
|
#log_send_mail : true
|
|
389
393
|
|
data/lib/rbatch/cmd.rb
CHANGED
|
@@ -4,12 +4,27 @@ require 'tmpdir'
|
|
|
4
4
|
require 'timeout'
|
|
5
5
|
module RBatch
|
|
6
6
|
|
|
7
|
+
# External command wrapper
|
|
7
8
|
class Cmd
|
|
9
|
+
# @private
|
|
8
10
|
@@def_vars
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
# @private
|
|
13
|
+
# @param [RBatch::Variables] vars
|
|
14
|
+
def Cmd.def_vars=(vars) ; @@def_vars=vars ; end
|
|
15
|
+
|
|
16
|
+
# command string
|
|
10
17
|
@cmd_str
|
|
18
|
+
|
|
19
|
+
# option
|
|
11
20
|
@opt
|
|
21
|
+
|
|
22
|
+
# @private
|
|
12
23
|
@vars
|
|
24
|
+
|
|
25
|
+
# @param [String] cmd_str command string such as "ls -l"
|
|
26
|
+
# @option opt [Boolean] :raise
|
|
27
|
+
# @option opt [Integer] :timeout
|
|
13
28
|
def initialize(cmd_str,opt = nil)
|
|
14
29
|
raise(CmdException,"Command string is nil") if cmd_str.nil?
|
|
15
30
|
@cmd_str = cmd_str
|
|
@@ -24,6 +39,9 @@ module RBatch
|
|
|
24
39
|
end
|
|
25
40
|
end
|
|
26
41
|
|
|
42
|
+
# Run command
|
|
43
|
+
# @raise [RBatch::CmdException]
|
|
44
|
+
# @return [RBatch::CmdResult]
|
|
27
45
|
def run()
|
|
28
46
|
stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir)
|
|
29
47
|
stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir)
|
|
@@ -39,7 +57,7 @@ module RBatch
|
|
|
39
57
|
Process.kill('SIGINT', pid)
|
|
40
58
|
raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Success to kill process : PID=#{pid}" )
|
|
41
59
|
rescue
|
|
42
|
-
raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. Fail to kill process : PID=#{pid}" )
|
|
60
|
+
raise(CmdException,"Run time of command \"#{@cmd_str}\" is over #{@vars[:cmd_timeout].to_s} sec. But Fail to kill process : PID=#{pid}" )
|
|
43
61
|
end
|
|
44
62
|
end
|
|
45
63
|
else
|
|
@@ -53,30 +71,53 @@ module RBatch
|
|
|
53
71
|
end
|
|
54
72
|
end
|
|
55
73
|
|
|
74
|
+
# Result of external command wrapper
|
|
56
75
|
class CmdResult
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
@
|
|
60
|
-
|
|
76
|
+
|
|
77
|
+
# Tmp file including STDOUT String
|
|
78
|
+
# @return [File]
|
|
79
|
+
attr_reader :stdout_file
|
|
80
|
+
|
|
81
|
+
# Tmp file including STDERROR String
|
|
82
|
+
# @return [File]
|
|
83
|
+
attr_reader :stderr_file
|
|
84
|
+
|
|
85
|
+
# Exit status
|
|
86
|
+
# @return [Integer]
|
|
87
|
+
attr_reader :status
|
|
88
|
+
|
|
89
|
+
# Command string
|
|
90
|
+
# @return [String]
|
|
91
|
+
attr_reader :cmd_str
|
|
92
|
+
|
|
93
|
+
# @private
|
|
61
94
|
def initialize(stdout_file, stderr_file, status, cmd_str)
|
|
62
95
|
@stdout_file = stdout_file
|
|
63
96
|
@stderr_file = stderr_file
|
|
64
97
|
@status = status
|
|
65
98
|
@cmd_str = cmd_str
|
|
66
99
|
end
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def cmd_str ; @cmd_str ; end
|
|
100
|
+
|
|
101
|
+
# STDOUT String
|
|
102
|
+
# @return [String]
|
|
71
103
|
def stdout
|
|
72
104
|
File.read(@stdout_file)
|
|
73
105
|
end
|
|
106
|
+
|
|
107
|
+
# STDERR String
|
|
108
|
+
# @return [String]
|
|
74
109
|
def stderr
|
|
75
110
|
File.read(@stderr_file)
|
|
76
111
|
end
|
|
112
|
+
|
|
113
|
+
# Return hash including cmd_str, std_out, std_err, and status
|
|
114
|
+
# @return [Hash]
|
|
77
115
|
def to_h
|
|
78
116
|
{:cmd_str => @cmd_str,:stdout => stdout, :stderr => stderr, :status => status}
|
|
79
117
|
end
|
|
118
|
+
|
|
119
|
+
# Return string including cmd_str, std_out, std_err, and status
|
|
120
|
+
# @return [String]
|
|
80
121
|
def to_s
|
|
81
122
|
to_h.to_s
|
|
82
123
|
end
|
data/lib/rbatch/config.rb
CHANGED
|
@@ -4,34 +4,42 @@ require 'pathname'
|
|
|
4
4
|
module RBatch
|
|
5
5
|
|
|
6
6
|
class Config
|
|
7
|
+
# Config file path
|
|
7
8
|
@path
|
|
9
|
+
|
|
10
|
+
# Actual data
|
|
8
11
|
@hash
|
|
12
|
+
|
|
13
|
+
# @param [String] path Config file path
|
|
9
14
|
def initialize(path)
|
|
10
15
|
@path = path
|
|
11
16
|
begin
|
|
12
|
-
@hash = YAML::load_file(@path)
|
|
17
|
+
@hash = ConfigElement.new(YAML::load_file(@path))
|
|
13
18
|
rescue Errno::ENOENT => e
|
|
14
19
|
@hash = nil
|
|
15
20
|
end
|
|
16
21
|
end
|
|
22
|
+
|
|
23
|
+
# Config value
|
|
24
|
+
# @param [Object] key Config key.
|
|
25
|
+
# @raise [RBatch::ConfigException]
|
|
17
26
|
def[](key)
|
|
18
27
|
if @hash.nil?
|
|
19
28
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
|
20
|
-
end
|
|
21
|
-
if @hash[key].nil?
|
|
22
|
-
if key.class == Symbol
|
|
23
|
-
raise RBatch::ConfigException, "Value of key(:#{key} (Symbol)) is nil. By any chance, dou you mistake key class Symbol for String?"
|
|
24
|
-
elsif key.class == String
|
|
25
|
-
raise RBatch::ConfigException, "Value of key(\"#{key}\" (String)) is nil"
|
|
26
|
-
else
|
|
27
|
-
raise RBatch::ConfigException, "Value of key(#{key}) is nil."
|
|
28
|
-
end
|
|
29
29
|
else
|
|
30
30
|
@hash[key]
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
+
|
|
34
|
+
# Config file path
|
|
35
|
+
# @return [String]
|
|
33
36
|
def path ; @path ; end
|
|
37
|
+
|
|
38
|
+
# Config file exists or not
|
|
39
|
+
# @return [Boolean]
|
|
34
40
|
def exist? ; ! @hash.nil? ; end
|
|
41
|
+
|
|
42
|
+
# @return [Hash]
|
|
35
43
|
def to_h
|
|
36
44
|
if @hash.nil?
|
|
37
45
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
|
@@ -39,6 +47,8 @@ module RBatch
|
|
|
39
47
|
@hash
|
|
40
48
|
end
|
|
41
49
|
end
|
|
50
|
+
|
|
51
|
+
# @return [String]
|
|
42
52
|
def to_s
|
|
43
53
|
if @hash.nil?
|
|
44
54
|
raise RBatch::ConfigException, "Config file \"#{@path}\" does not exist"
|
|
@@ -48,6 +58,33 @@ module RBatch
|
|
|
48
58
|
end
|
|
49
59
|
end
|
|
50
60
|
|
|
61
|
+
class ConfigElement < Hash
|
|
62
|
+
def initialize(hash)
|
|
63
|
+
hash.each_key do |key|
|
|
64
|
+
if hash[key].class == Hash
|
|
65
|
+
self[key] = ConfigElement.new(hash[key])
|
|
66
|
+
else
|
|
67
|
+
self[key] = hash[key]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def[](key)
|
|
73
|
+
if self.has_key?(key)
|
|
74
|
+
super
|
|
75
|
+
else
|
|
76
|
+
if key.class == Symbol
|
|
77
|
+
raise RBatch::ConfigException, "Value of key(:#{key} (Symbol)) does not exist. By any chance, dou you mistake key class Symbol for String?"
|
|
78
|
+
elsif key.class == String
|
|
79
|
+
raise RBatch::ConfigException, "Value of key(\"#{key}\" (String)) does not exist"
|
|
80
|
+
else
|
|
81
|
+
raise RBatch::ConfigException, "Value of key(#{key}) does not exist."
|
|
82
|
+
end
|
|
83
|
+
raise
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
51
88
|
class RBatch::ConfigException < StandardError ; end
|
|
52
89
|
end
|
|
53
90
|
|
data/lib/rbatch/controller.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require 'tmpdir'
|
|
2
2
|
require 'tempfile'
|
|
3
3
|
module RBatch
|
|
4
|
+
# @private
|
|
4
5
|
module DoubleRunChecker
|
|
5
6
|
module_function
|
|
6
7
|
def lock_file_name(p)
|
|
@@ -20,5 +21,6 @@ module RBatch
|
|
|
20
21
|
end
|
|
21
22
|
end
|
|
22
23
|
|
|
24
|
+
# @private
|
|
23
25
|
class DoubleRunCheckException < Exception ; end
|
|
24
26
|
end
|
data/lib/rbatch/journal.rb
CHANGED
data/lib/rbatch/log.rb
CHANGED
|
@@ -6,6 +6,7 @@ require 'net/smtp'
|
|
|
6
6
|
|
|
7
7
|
module RBatch
|
|
8
8
|
class Log
|
|
9
|
+
# @private
|
|
9
10
|
@@FORMATTER = proc do |severity, datetime, progname, msg|
|
|
10
11
|
head = "[#{datetime}] [" + sprintf("%-5s",severity) +"]"
|
|
11
12
|
if msg.is_a? Exception
|
|
@@ -14,6 +15,8 @@ module RBatch
|
|
|
14
15
|
"#{head} #{msg}\n"
|
|
15
16
|
end
|
|
16
17
|
end
|
|
18
|
+
|
|
19
|
+
# @private
|
|
17
20
|
@@STDOUT_FORMATTER = proc do |severity, datetime, progname, msg|
|
|
18
21
|
head = "[" + sprintf("%-5s",severity) +"]"
|
|
19
22
|
if msg.is_a? Exception
|
|
@@ -22,6 +25,8 @@ module RBatch
|
|
|
22
25
|
"#{head} #{msg}\n"
|
|
23
26
|
end
|
|
24
27
|
end
|
|
28
|
+
|
|
29
|
+
# @private
|
|
25
30
|
@@LOG_LEVEL_MAP = {
|
|
26
31
|
"debug" => Logger::DEBUG,
|
|
27
32
|
"info" => Logger::INFO,
|
|
@@ -29,20 +34,82 @@ module RBatch
|
|
|
29
34
|
"error" => Logger::ERROR,
|
|
30
35
|
"fatal" => Logger::FATAL
|
|
31
36
|
}
|
|
37
|
+
|
|
38
|
+
# @private
|
|
32
39
|
@@def_vars
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
|
|
41
|
+
# @private
|
|
42
|
+
# @param [RBatch::Variables] v
|
|
43
|
+
def Log.def_vars=(v)
|
|
44
|
+
raise ArgumentError, "type mismatch: #{v} for #RBatch::Variables" if ! v.kind_of?(RBatch::Variables)
|
|
45
|
+
@@def_vars=v
|
|
36
46
|
end
|
|
47
|
+
|
|
48
|
+
# @private
|
|
49
|
+
# @return [RBatch::Variables]
|
|
37
50
|
def Log.def_vars ; @@def_vars ; end
|
|
51
|
+
|
|
52
|
+
# @private
|
|
38
53
|
@@journal
|
|
39
|
-
def Log.journal=(a) ; @@journal=a ; end
|
|
40
54
|
|
|
55
|
+
# @private
|
|
56
|
+
# @param [RBatch::Journal] j
|
|
57
|
+
def Log.journal=(j) ; @@journal=j ; end
|
|
58
|
+
|
|
59
|
+
# @private
|
|
41
60
|
@vars
|
|
61
|
+
|
|
62
|
+
# @private
|
|
42
63
|
@opt
|
|
64
|
+
|
|
65
|
+
# @private
|
|
43
66
|
@log
|
|
67
|
+
|
|
68
|
+
# @private
|
|
44
69
|
@stdout_log
|
|
45
70
|
|
|
71
|
+
# External command wrapper
|
|
72
|
+
# @option opt [String] :dir Output directory
|
|
73
|
+
# @option opt [String] :name
|
|
74
|
+
# Log file name.
|
|
75
|
+
# Default is "<date>_<time>_<prog>.log".
|
|
76
|
+
# <data> is replaced to YYYYMMDD date string
|
|
77
|
+
# <time> is replaced to HHMMSS time string
|
|
78
|
+
# <prog> is replaced to Program file base name (except extention).
|
|
79
|
+
# <host> is replaced to Hostname.
|
|
80
|
+
# @option opt [Boolean] :append
|
|
81
|
+
# @option opt [String] :level
|
|
82
|
+
# Effective values are "debug","info","wran","error",and "fatal".
|
|
83
|
+
# @option opt [Boolean] :stdout
|
|
84
|
+
# Print log string both log file and STDOUT
|
|
85
|
+
# @option opt [Boolean] :delete_old_log
|
|
86
|
+
# If this is true, delete old log files when this is called.
|
|
87
|
+
# If log filename does not include "<date>", do nothing.
|
|
88
|
+
# @option opt [Integer] :delete_old_log_date
|
|
89
|
+
# @option opt [Boolean] :send_mail
|
|
90
|
+
# When log.error(str) is called,
|
|
91
|
+
# log.fatal(str) is called , or rescue an Exception,
|
|
92
|
+
# send e-mail.
|
|
93
|
+
# @option opt [String] :mail_to
|
|
94
|
+
# @option opt [String] :mail_from
|
|
95
|
+
# @option opt [String] :mail_server_host
|
|
96
|
+
# @option opt [Integer] :mail_server_port
|
|
97
|
+
# @raise [RBatch::LogException]
|
|
98
|
+
# @yield [log] RBatch::Log instance
|
|
99
|
+
# @return [RBatch::Log]
|
|
100
|
+
# @example
|
|
101
|
+
# require 'rbatch'
|
|
102
|
+
# RBatch::Log.new{ |log|
|
|
103
|
+
# log.info "info string"
|
|
104
|
+
# log.error "error string"
|
|
105
|
+
# raise "exception" # => rescued in this block
|
|
106
|
+
# }
|
|
107
|
+
# @example use option
|
|
108
|
+
# require 'rbatch'
|
|
109
|
+
# RBatch::Log.new({:name => "hoge.log"}){ |log|
|
|
110
|
+
# log.info "info string"
|
|
111
|
+
# }
|
|
112
|
+
|
|
46
113
|
def initialize(opt=nil)
|
|
47
114
|
@opt = opt
|
|
48
115
|
@vars = @@def_vars.clone
|
|
@@ -78,7 +145,7 @@ module RBatch
|
|
|
78
145
|
@stdout_log.formatter = @@STDOUT_FORMATTER
|
|
79
146
|
end
|
|
80
147
|
# delete old log
|
|
81
|
-
|
|
148
|
+
delete_old_log(@vars[:log_delete_old_log_date]) if @vars[:log_delete_old_log]
|
|
82
149
|
# Start logging
|
|
83
150
|
@@journal.put 1,"Logging Start: \"#{@path}\""
|
|
84
151
|
@@journal.add_log(self)
|
|
@@ -89,61 +156,63 @@ module RBatch
|
|
|
89
156
|
if e.status == 0
|
|
90
157
|
exit 0
|
|
91
158
|
else
|
|
92
|
-
|
|
93
|
-
|
|
159
|
+
fatal(e)
|
|
160
|
+
fatal("Caught SystemExit. RBatch Exit with status " + e.status.to_s)
|
|
94
161
|
exit e.status
|
|
95
162
|
end
|
|
96
163
|
rescue Exception => e
|
|
97
|
-
|
|
98
|
-
|
|
164
|
+
fatal(e)
|
|
165
|
+
fatal("Caught exception. RBatch Exit with status 1")
|
|
99
166
|
exit 1
|
|
100
167
|
ensure
|
|
101
|
-
|
|
168
|
+
close
|
|
102
169
|
end
|
|
103
170
|
end
|
|
104
171
|
end
|
|
105
172
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def error(a)
|
|
113
|
-
@stdout_log.error(a) if @vars[:log_stdout]
|
|
114
|
-
@log.error(a)
|
|
115
|
-
send_mail(a) if @vars[:log_send_mail]
|
|
173
|
+
# Out put log with ERROR level
|
|
174
|
+
# @param [String] str log string
|
|
175
|
+
def fatal(str)
|
|
176
|
+
@stdout_log.fatal(str) if @vars[:log_stdout]
|
|
177
|
+
@log.fatal(str)
|
|
178
|
+
send_mail(str) if @vars[:log_send_mail]
|
|
116
179
|
end
|
|
117
180
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
181
|
+
# Out put log with ERROR level
|
|
182
|
+
# @param [String] str log string
|
|
183
|
+
def error(str)
|
|
184
|
+
@stdout_log.error(str) if @vars[:log_stdout]
|
|
185
|
+
@log.error(str)
|
|
186
|
+
send_mail(str) if @vars[:log_send_mail]
|
|
121
187
|
end
|
|
122
188
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
189
|
+
# Out put log with WARN level
|
|
190
|
+
# @param [String] str log string
|
|
191
|
+
def warn(str)
|
|
192
|
+
@stdout_log.warn(str) if @vars[:log_stdout]
|
|
193
|
+
@log.warn(str)
|
|
126
194
|
end
|
|
127
195
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
196
|
+
# Out put log with INFO level
|
|
197
|
+
# @param [String] str log string
|
|
198
|
+
def info(str)
|
|
199
|
+
@stdout_log.info(str) if @vars[:log_stdout]
|
|
200
|
+
@log.info(str)
|
|
131
201
|
end
|
|
132
202
|
|
|
133
|
-
|
|
134
|
-
|
|
203
|
+
# Out put log with DEBUG level
|
|
204
|
+
# @param [String] str log string
|
|
205
|
+
def debug(str)
|
|
206
|
+
@stdout_log.debug(str) if @vars[:log_stdout]
|
|
207
|
+
@log.debug(str)
|
|
135
208
|
end
|
|
136
209
|
|
|
137
|
-
|
|
138
|
-
|
|
210
|
+
# @private
|
|
211
|
+
def journal(str)
|
|
212
|
+
@log.info("[RBatch] " + str)
|
|
139
213
|
end
|
|
140
214
|
|
|
141
|
-
|
|
142
|
-
# If @vars[:log_name] is not include "<date>", then do nothing.
|
|
143
|
-
#
|
|
144
|
-
# ==== Params
|
|
145
|
-
# - +date+ (Integer): The day of leaving log files
|
|
146
|
-
#
|
|
215
|
+
private
|
|
147
216
|
def delete_old_log(date = 7)
|
|
148
217
|
if Dir.exists?(@vars[:log_dir]) && @vars.raw_value(:log_name).include?("<date>")
|
|
149
218
|
Dir::foreach(@vars[:log_dir]) do |file|
|
|
@@ -160,9 +229,10 @@ module RBatch
|
|
|
160
229
|
end
|
|
161
230
|
end
|
|
162
231
|
|
|
163
|
-
|
|
232
|
+
def close
|
|
233
|
+
@log.close
|
|
234
|
+
end
|
|
164
235
|
|
|
165
|
-
# send mail
|
|
166
236
|
def send_mail(msg)
|
|
167
237
|
body = <<EOT
|
|
168
238
|
From: <#{@vars[:log_mail_from]}>
|
data/lib/rbatch/run_conf.rb
CHANGED
data/lib/rbatch/variables.rb
CHANGED
|
@@ -2,6 +2,7 @@ require 'pathname'
|
|
|
2
2
|
require 'rbatch/run_conf'
|
|
3
3
|
|
|
4
4
|
module RBatch
|
|
5
|
+
# @private
|
|
5
6
|
class Variables
|
|
6
7
|
attr :vars,:run_conf,:merged_opt
|
|
7
8
|
def initialize(run_conf=nil)
|
|
@@ -74,5 +75,6 @@ module RBatch
|
|
|
74
75
|
end
|
|
75
76
|
|
|
76
77
|
end
|
|
78
|
+
# @private
|
|
77
79
|
class RBatch::VariablesException < StandardError ; end
|
|
78
80
|
end
|
data/lib/rbatch/version.rb
CHANGED
data/lib/rbatch.rb
CHANGED
|
@@ -3,15 +3,61 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
|
|
|
3
3
|
require 'rbatch/controller'
|
|
4
4
|
|
|
5
5
|
module RBatch
|
|
6
|
+
# @private
|
|
6
7
|
@@ctrl = nil
|
|
8
|
+
|
|
7
9
|
module_function
|
|
10
|
+
|
|
11
|
+
# @private
|
|
8
12
|
def init
|
|
9
13
|
@@ctrl = RBatch::Controller.new
|
|
10
14
|
end
|
|
15
|
+
|
|
16
|
+
# @private
|
|
11
17
|
def ctrl ; @@ctrl ; end
|
|
18
|
+
|
|
19
|
+
# @private
|
|
12
20
|
def vars ; @@ctrl.vars ; end
|
|
21
|
+
|
|
22
|
+
# Return Config Object
|
|
23
|
+
# @raise [RBatch::ConfigException]
|
|
24
|
+
# @return [RBatch::Config]
|
|
25
|
+
# @example RB_HOME/conf/hoge.yaml
|
|
26
|
+
# key: value
|
|
27
|
+
# array:
|
|
28
|
+
# - item1
|
|
29
|
+
# - item2
|
|
30
|
+
# - item3
|
|
31
|
+
# @example ${RB_HOME}/bin/hoge.rb
|
|
32
|
+
# p RBatch.config["key"] # => "value"
|
|
33
|
+
# p RBatch.config["array"] # => ["item1", "item2", "item3"]
|
|
34
|
+
# p RBatch.config["not_exist"] # => Raise RBatch::ConfigException
|
|
13
35
|
def config ; @@ctrl.config ; end
|
|
36
|
+
|
|
37
|
+
# Return Common-Config Object
|
|
38
|
+
# @raise [RBatch::ConfigException]
|
|
39
|
+
# @return [RBatch::Config]
|
|
40
|
+
# @example RB_HOME/conf/common.yaml
|
|
41
|
+
# key: value
|
|
42
|
+
# array:
|
|
43
|
+
# - item1
|
|
44
|
+
# - item2
|
|
45
|
+
# - item3
|
|
46
|
+
# @example ${RB_HOME}/bin/hoge.rb
|
|
47
|
+
# p RBatch.common_config["key"] # => "value"
|
|
48
|
+
# p RBatch.common_config["array"] # => ["item1", "item2", "item3"]
|
|
49
|
+
# p RBatch.common_config["not_exist"] # => Raise RBatch::ConfigException
|
|
14
50
|
def common_config ; @@ctrl.common_config ; end
|
|
51
|
+
|
|
52
|
+
# Shortcut of RBatch::Cmd.new(cmd_str,opt).run
|
|
53
|
+
# @see RBatch::Cmd
|
|
54
|
+
# @example
|
|
55
|
+
# r = RBatch.cmd("ls")
|
|
56
|
+
# p r.stdout # => "fileA\nfileB\n"
|
|
57
|
+
# p r.stderr # => ""
|
|
58
|
+
# p r.status # => 0
|
|
59
|
+
# @example
|
|
60
|
+
# r = RBatch.cmd("rsync /foo /bar",{:timeout => 10})
|
|
15
61
|
def cmd(cmd_str,opt=nil) ; @@ctrl.cmd(cmd_str,opt) ; end
|
|
16
62
|
end
|
|
17
63
|
|
data/spec/rbatch/config_spec.rb
CHANGED
|
@@ -65,3 +65,29 @@ describe RBatch::Config do
|
|
|
65
65
|
}.to raise_error(RBatch::ConfigException)
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
|
+
|
|
69
|
+
describe RBatch::ConfigElement do
|
|
70
|
+
it "" do
|
|
71
|
+
hash = {
|
|
72
|
+
"a" => "av" ,
|
|
73
|
+
"b" => {
|
|
74
|
+
"c" => "cv",
|
|
75
|
+
"d" => {
|
|
76
|
+
"e" => "ev",
|
|
77
|
+
"f" => [1,2,3]
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
ce = RBatch::ConfigElement.new(hash)
|
|
82
|
+
expect(ce["a"]).to eq "av"
|
|
83
|
+
expect(ce["b"].class).to eq RBatch::ConfigElement
|
|
84
|
+
expect(ce["b"]["c"]).to eq "cv"
|
|
85
|
+
expect(ce["b"]["d"].class).to eq RBatch::ConfigElement
|
|
86
|
+
expect(ce["b"]["d"]["e"]).to eq "ev"
|
|
87
|
+
expect(ce["b"]["d"]["f"][1]).to eq 2
|
|
88
|
+
expect { ce["noexist"] }.to raise_error(RBatch::ConfigException)
|
|
89
|
+
expect { ce["noexist"]["noexist"] }.to raise_error(RBatch::ConfigException)
|
|
90
|
+
expect { ce["b"]["noexist"] }.to raise_error(RBatch::ConfigException)
|
|
91
|
+
expect { ce["b"]["d"]["noexist"] }.to raise_error(RBatch::ConfigException)
|
|
92
|
+
end
|
|
93
|
+
end
|