rbatch 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,19 +1,21 @@
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")
2
2
 
3
- RBatch:Ruby-base Batch-Script Framework
3
+ RBatch:Ruby-base Batch Script Framework
4
4
  =============
5
5
 
6
- About RBatch (version 2)
6
+ About RBatch (for ver 2)
7
7
  --------------
8
8
 
9
- RBatch is Ruby-base Batch Script Framework. RBatch has many fanctions to help with your making a batch script such as "data backup" or "proccess starting".
10
9
 
11
- There are following functions.
10
+ RBatch is Ruby-base Batch script framework. RBatch help to make a batch script such as "data backup" or "proccess start ".
11
+
12
+ There are following functions.
12
13
 
13
14
  * Auto Logging
15
+ * Auto Library Loading
14
16
  * Auto Mail Sending
15
17
  * Auto Config Reading
16
- * External Command Wrapper
18
+ * External Command Wrapper
17
19
  * Double Run Check
18
20
 
19
21
 
@@ -34,279 +36,301 @@ Manual
34
36
 
35
37
  ### RBatch home directory
36
38
 
37
- When you set ${RB_HOME} environment variable, RBatch home directory is fix at ${RB_HOME}.
39
+ When you set `${RB_HOME}` environment variable, RBatch home directory is fix at `${RB_HOME}`.
38
40
 
39
- When you do NOT set ${RB_HOME}, ${RB_HOME} is the parent directory of the directory which script is located at. In other words, default of ${RB_HOME} is "(script path)/../" .
41
+ When you do NOT set `${RB_HOME}`, `${RB_HOME}` is the parent directory of the directory which script is located at. In other words, default of `${RB_HOME}` is `(script path)/../` .
40
42
 
41
- ### Directory Structure and Naming Convention
43
+ ### Directory Structure and File Naming Convention
42
44
 
43
- RBach has convention of file naming and directorory structure.
45
+ RBach has convention of file naming and directory structure.
44
46
 
45
- If you make a script on "${RB_HOME}/bin/hoge.rb", script's config file is "${RB_HOME}/conf/hoge.yaml" , and log file is "${RB_HOME}/log/YYYYMMDD_HHMMSS_hoge.rb".
47
+ If you make a script on `${RB_HOME}/bin/hoge.rb`, libraries are `${RB_HOME}/lib/*.rb`, script's config file is `${RB_HOME}/conf/hoge.yaml` , and log file is output at `${RB_HOME}/log/YYYYMMDD_HHMMSS_hoge.log`.
46
48
 
47
49
  For example
48
- ```
49
- ${RB_HOME}/ <--- RBatch home
50
- |
51
- |-.rbatchrc <--- RBatch Run Conf
52
- |
53
- |-bin <--- Scripts
54
- | |- A.rb
55
- | |- B.rb
56
- |
57
- |-conf <--- Configuration files
58
- | |- A.yaml
59
- | |- B.yaml
60
- |
61
- |-log <--- Log files
62
- |- YYYYMMDD_HHMMSS_A.log
63
- |- YYYYMMDD_HHMMSS_B.log
64
- ```
50
+
51
+ ${RB_HOME} <--- RBatch home
52
+ |
53
+ |- .rbatchrc <--- RBatch Run-Conf
54
+ |
55
+ |- bin <--- Scripts
56
+ | |- A.rb
57
+ | |- B.rb
58
+ |
59
+ |- conf <--- Config files
60
+ | |- A.yaml
61
+ | |- B.yaml
62
+ |
63
+ |- log <--- Log files
64
+ | |- YYYYMMDD_HHMMSS_A.log
65
+ | |- YYYYMMDD_HHMMSS_B.log
66
+ |
67
+ |- lib <--- Libraries
68
+ |- lib_X.rb
69
+ |- lib_Y.rb
65
70
 
66
71
  ### Auto Logging
67
72
 
68
- Use "Auto Logging block", RBatch automatically writes to logfile.
69
- The default location of log file is ${RB_HOME}/log/YYYYMMDD_HHMMSS_${PROG_NAME}.log .
70
- If an exception occuerd, then RBatch write a stack trace to logfile.
73
+ Use auto logging block `RBatch::Log`, RBatch automatically output logfiles.
74
+ The default location of log file is `${RB_HOME}/log/YYYYMMDD_HHMMSS_(script base).log`.
75
+ If an exception is raised, then RBatch write the stack trace to the logfile.
71
76
 
72
77
  sample
73
78
 
74
- script : ${RB_HOME}/bin/sample1.rb
75
- ```
76
- require 'rbatch'
77
-
78
- RBatch::Log.new(){ |log| # Logging block
79
- log.info "info string"
80
- log.error "error string"
81
- raise "exception"
82
- }
83
- ```
84
-
85
- logfile : ${RB_HOME}/log/20121020_005953_sample1.log
86
- ```
87
- # Logfile created on 2012-10-20 00:59:53 +0900 by logger.rb/25413
88
- [2012-10-20 00:59:53 +900] [INFO ] info string
89
- [2012-10-20 00:59:53 +900] [ERROR] error string
90
- [2012-10-20 00:59:53 +900] [FATAL] Caught exception; existing 1
91
- [2012-10-20 00:59:53 +900] [FATAL] exception (RuntimeError)
92
- [backtrace] test.rb:6:in `block in <main>'
93
- [backtrace] /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize'
94
- [backtrace] test.rb:3:in `new'
95
- [backtrace] test.rb:3:in `<main>'
96
- ```
79
+ script : `${RB_HOME}/bin/sample1.rb`
80
+
81
+ require 'rbatch'
82
+ RBatch::Log.new(){ |log| # Logging block
83
+ log.info "info string"
84
+ log.error "error string"
85
+ raise "exception"
86
+ }
87
+
88
+ logfile : `${RB_HOME}/log/20121020_005953_sample1.log`
89
+
90
+ # Logfile created on 2012-10-20 00:59:53 +0900 by logger.rb/25413
91
+ [2012-10-20 00:59:53 +900] [INFO ] info string
92
+ [2012-10-20 00:59:53 +900] [ERROR] error string
93
+ [2012-10-20 00:59:53 +900] [FATAL] Caught exception; existing 1
94
+ [2012-10-20 00:59:53 +900] [FATAL] exception (RuntimeError)
95
+ [backtrace] test.rb:6:in `block in <main>'
96
+ [backtrace] /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize'
97
+ [backtrace] test.rb:3:in `new'
98
+ [backtrace] test.rb:3:in `<main>'
99
+
100
+ ### Auto Library Loading
101
+
102
+ If you make libraries at `${RB_HOME}/lib/*.rb`, those files are required before script run.
97
103
 
98
104
  ### Auto Mail Sending
99
105
 
100
- By using "log_send_mail" option, when an error occurs in script, RBatch sends an error-mail automatically.
106
+ By using `log_send_mail` option, when an error or fatal log is output in script, RBatch sends error-mail.
101
107
 
102
108
  ### Auto Config Reading
103
109
 
104
- By using RBatch, your script read a configuration file easily.
105
- If you make configuration file which is located ${RB_HOME}/conf/"(program base name).yaml", this file is read automatically.
110
+ If you make configuration file which is located `${RB_HOME}/conf/"(script base).yaml"`, this file is read automatically.
106
111
 
107
112
  sample
108
113
 
109
- config : ${RB_HOME}/conf/sample2.yaml
110
- ```
111
- key: value
112
- array:
113
- - item1
114
- - item2
115
- - item3
116
- ```
117
-
118
- script : ${RB_HOME}/bin/sample2.rb
119
- ```
120
- require 'rbatch'
121
- p RBatch::config
122
- => {"key" => "value", "array" => ["item1", "item2", "item3"]}
123
- p RBatch::config["key"]
124
- => "value"
125
-
126
- # If key does not exist , raise exception
127
- p RBatch::config["not_exist"]
128
- => Raise Exception
129
- ```
130
-
131
- If you can use common configuration file which is read from all scripts,
132
- you make "${RB_HOME}/conf/common.yaml" .
114
+ config : `${RB_HOME}/conf/sample2.yaml`
115
+
116
+ key: value
117
+ array:
118
+ - item1
119
+ - item2
120
+ - item3
121
+
122
+
123
+ script : `${RB_HOME}/bin/sample2.rb`
124
+
125
+ require 'rbatch'
126
+ p RBatch::config
127
+ => {"key" => "value", "array" => ["item1", "item2", "item3"]}
128
+ p RBatch::config["key"]
129
+ => "value"
130
+
131
+ # If key does not exist , raise exception
132
+ p RBatch::config["not_exist"]
133
+ => Raise Exception
134
+
135
+
136
+ If you can use a common config file which is read from all scripts, you make `${RB_HOME}/conf/common.yaml`.
137
+ You can change name of common config file by using option `common_conf_name`.
133
138
 
134
139
  ### External Command Wrapper
135
140
 
136
141
  RBatch provide a function which wrap external command (such as 'ls').
137
142
 
138
- This function return a result object which contain command's STDOUT, STDERR ,and exit status.
143
+ This function return a result object which contain command's "STDOUT", "STDERR" ,and "exit status".
139
144
 
140
145
  sample
141
- ```
142
- require 'rbatch'
143
- r = RBatch::cmd("ls")
144
- p r.stdout
145
- => "fileA\nfileB\n"
146
- p r.stderr
147
- => ""
148
- p r.status
149
- => 0
150
- ```
146
+
147
+ require 'rbatch'
148
+ r = RBatch::cmd("ls")
149
+ p r.stdout
150
+ => "fileA\nfileB\n"
151
+ p r.stderr
152
+ => ""
153
+ p r.status
154
+ => 0
155
+
156
+ If you want to set a timeout of external command, you can use `cmd_timeout` option.
151
157
 
152
158
  ### Double Run Check
153
159
 
154
- Using "forbid_double_run: true" option, two same name scripts cannot run at the same time.
160
+ Using `forbid_double_run` option, two same name scripts cannot run at the same time.
155
161
 
156
162
  Customize
157
163
  --------------
158
164
  If you want to customize RBatch, you have two methods.
159
165
 
160
- (1) Write Run-Conf (${RB_HOME}/.rbatchrc).
161
- (2) Pass an option object to constructor in your script.
166
+ * (1) Write Run-Conf `${RB_HOME}/.rbatchrc`.
167
+ * (2) Pass an option object to constructor in your script.
162
168
 
163
169
  When an option is set in both (1) and (2), (2) is prior to (1).
164
170
 
165
171
  #### Customize by writing Run-Conf (.rbatchrc)
166
172
 
167
- If you make a follow config file, configurations effect to all scripts.
168
-
169
- ${RB_HOME}/.rbatchrc
170
-
171
- Run-Conf Sample
172
- ```
173
- # RBatch Run-Conf (.rbatchrc)
174
- #
175
- # This format is YAML.
176
- #
177
-
178
- # -------------------
179
- # Global setting
180
- # -------------------
181
-
182
- # Conf Directory
183
- #
184
- # Default is "<home>/conf"
185
- #
186
- # <home> is replaced to ${RB_HOME}
187
- #
188
- #conf_dir: <home>/config
189
- #conf_dir: /etc/rbatch/
190
-
191
- # Common config file name
192
- #
193
- # Default is "common.yaml"
194
- #
195
- #common_conf_name: share.yaml
196
-
197
- # Forbit Script Double Run
198
- #
199
- # Default is false.
200
- # If this option is true, two same name scripts cannot run at the same time.
201
- #
202
- #forbid_double_run: true
203
- #forbid_double_run: false
204
-
205
- # -------------------
206
- # Cmd setting
207
- # -------------------
208
-
209
- # Raise Exception
210
- #
211
- # Default is false.
212
- # If command exit status is not 0, raise exception.
213
- #
214
- #cmd_raise : true
215
- #cmd_raise : false
216
-
217
- # Command Timeout
218
- #
219
- # Default is 0 [sec].
220
- #
221
- #cmd_timeout: 5
222
-
223
- # -------------------
224
- # Log setting
225
- # -------------------
226
-
227
- # Log Directory
228
- #
229
- # Default is "<home>/log"
230
- #
231
- # <home> is replaced to ${RB_HOME}
232
- #
233
- #log_dir: <home>/rb_log
234
- #log_dir: /var/log/rbatch/
235
-
236
- # Log File Name
237
- #
238
- # Default is "<date>_<time>_<prog>.log".
239
- # Reservation words are follows.
240
- # <data> is replaced to YYYYMMDD date string
241
- # <time> is replaced to HHMMSS time string
242
- # <prog> is replaced to Program file base name (except extention).
243
- # <host> is replaced to Hostname.
244
- #
245
- #log_name : "<date>_<time>_<prog>.log"
246
- #log_name : "<date>_<prog>.log"
247
-
248
- # Append log or not
249
- #
250
- # Default is ture.
251
- #
252
- #log_append : true
253
- #log_append : false
254
-
255
- # Log Level
256
- #
257
- # Default is "info".
258
- # Effective values are "debug","info","wran","error",and "fatal".
259
- #
260
- #log_level : "debug"
261
- #log_level : "info"
262
- #log_level : "warn"
263
- #log_level : "error"
264
- #log_level : "fatal"
265
-
266
- # Print log string both file and STDOUT
267
- #
268
- # Default is false.
269
- #
270
- #log_stdout : true
271
- #log_stdout : false
272
-
273
- # Delete old log files
274
- #
275
- # Default is false.
276
- # If this is true, delete old log file when RBatch::Log.new is called.
277
- # A log file to delete is a log file which was made by the RBatch::Log instance,
278
- # and log filename format include "<date>".
279
- #
280
- #log_delete_old_log: true
281
- #log_delete_old_log: false
282
-
283
- # The day of leaving log files
284
- #
285
- # Default is 7.
286
- #
287
- #log_delete_old_log_date: 14
288
-
289
- # Send mail or not
290
- #
291
- # Default is false.
292
- # When log.error(msg) or log.fatal(msg) called , send e-mail including "msg".
293
- #
294
- #log_send_mail : true
295
-
296
- # Mail parameters
297
- #
298
- #log_mail_to : "xxx@sample.com"
299
- #log_mail_from : "xxx@sample.com"
300
- #log_mail_server_host : "localhost"
301
- #log_mail_server_port : 25
302
-
303
- ```
173
+ Sample of RBatch Run-Conf `${RB_HOME}/.rbatchrc`.
174
+
175
+ # RBatch Run-Conf (.rbatchrc)
176
+ #
177
+ # This format is YAML.
178
+ #
179
+
180
+ # -------------------
181
+ # Global setting
182
+ # -------------------
183
+
184
+ # Conf Directory
185
+ #
186
+ # Default is "<home>/conf"
187
+ # <home> is replaced to ${RB_HOME}
188
+ #
189
+ #conf_dir: <home>/config/
190
+ #conf_dir: /etc/rbatch/
191
+
192
+ # Common config file name
193
+ #
194
+ # Default is "common.yaml"
195
+ #
196
+ #common_conf_name: share.yaml
197
+
198
+ # Library Directory
199
+ #
200
+ # Default is "<home>/lib"
201
+ # <home> is replaced to ${RB_HOME}
202
+ #
203
+ #lib_dir: /usr/local/lib/rbatch/
204
+
205
+ # Auto Library Load
206
+ #
207
+ # Default is true
208
+ # If true, require "(library directory)/*.rb" before script run.
209
+ #
210
+ #auto_lib_load: true
211
+ #auto_lib_load: false
212
+
213
+ # Forbit Script Double Run
214
+ #
215
+ # Default is false.
216
+ # If true, two same name scripts cannot run at the same time.
217
+ #
218
+ #forbid_double_run: true
219
+ #forbid_double_run: false
220
+
221
+ # -------------------
222
+ # Cmd setting
223
+ # -------------------
224
+
225
+ # Raise Exception
226
+ #
227
+ # Default is false.
228
+ # If true, when command exit status is not 0, raise exception.
229
+ #
230
+ #cmd_raise : true
231
+ #cmd_raise : false
232
+
233
+ # Command Timeout
234
+ #
235
+ # Default is 0 [sec].
236
+ #
237
+ #cmd_timeout: 5
238
+
239
+ # -------------------
240
+ # Log setting
241
+ # -------------------
242
+
243
+ # Log Directory
244
+ #
245
+ # Default is "<home>/log"
246
+ # <home> is replaced to ${RB_HOME}
247
+ #
248
+ #log_dir: <home>/rb_log
249
+ #log_dir: /var/log/rbatch/
250
+
251
+ # Log File Name
252
+ #
253
+ # Default is "<date>_<time>_<prog>.log".
254
+ # <data> is replaced to YYYYMMDD date string
255
+ # <time> is replaced to HHMMSS time string
256
+ # <prog> is replaced to Program file base name (except extention).
257
+ # <host> is replaced to Hostname.
258
+ #
259
+ #log_name : "<date>_<time>_<prog>.log"
260
+ #log_name : "<date>_<prog>.log"
261
+
262
+ # Append log or not
263
+ #
264
+ # Default is ture.
265
+ #
266
+ #log_append : true
267
+ #log_append : false
268
+
269
+ # Log Level
270
+ #
271
+ # Default is "info".
272
+ # Effective values are "debug","info","wran","error",and "fatal".
273
+ #
274
+ #log_level : "debug"
275
+ #log_level : "info"
276
+ #log_level : "warn"
277
+ #log_level : "error"
278
+ #log_level : "fatal"
279
+
280
+ # Print log string both file and STDOUT
281
+ #
282
+ # Default is false.
283
+ #
284
+ #log_stdout : true
285
+ #log_stdout : false
286
+
287
+ # Delete old log files
288
+ #
289
+ # Default is false.
290
+ # If this is true, delete old log file when RBatch::Log.new is called.
291
+ # A log file to delete is a log file which was made by the
292
+ # RBatch::Log instance, and log filename format include "<date>".
293
+ #
294
+ #log_delete_old_log: true
295
+ #log_delete_old_log: false
296
+
297
+ # The day of leaving log files
298
+ #
299
+ # Default is 7.
300
+ #
301
+ #log_delete_old_log_date: 14
302
+
303
+ # Send mail or not
304
+ #
305
+ # Default is false.
306
+ # When log.error(msg) or log.fatal(msg) called , send e-mail
307
+ # including "msg".
308
+ #
309
+ #log_send_mail : true
310
+
311
+ # Mail parameters
312
+ #
313
+ #log_mail_to : "xxx@sample.com"
314
+ #log_mail_from : "xxx@sample.com"
315
+ #log_mail_server_host : "localhost"
316
+ #log_mail_server_port : 25
317
+
318
+ # Mix RBatch Message to Log
319
+ #
320
+ # Default is true.
321
+ # If true, put RBatch message to log file(s) which is opened at time.
322
+ # Example of RBatch Message
323
+ # [RBatch] Load Config : "../conf/hoge.yaml"
324
+ #
325
+ #mix_rbatch_msg_to_log : true
326
+ #mix_rbatch_msg_to_log : false
327
+
304
328
 
305
329
  ### Customize by passing option object to constructor
306
330
 
307
- If you want to set options in a script, you hand an argument of options to a constructor.
331
+ If you want to change options in a script, you pass an options object to the constructor of RBatch::Log or RBatch::Cmd.
308
332
 
309
- #### class RBatch::Log
333
+ #### option of RBatch::Log
310
334
 
311
335
  opt = {
312
336
  :name => "<date>_<time>_<prog>.log",
@@ -321,21 +345,20 @@ If you want to set options in a script, you hand an argument of options to a con
321
345
  :mail_from => "rbatch.localhost",
322
346
  :mail_server_host => "localhost",
323
347
  :mail_server_port => 25
324
- }
325
-
348
+ }
326
349
  RBatch::Log.new(opt)
327
350
 
328
- #### class RBatch::Cmd
351
+ #### option of RBatch::Cmd
329
352
 
330
353
  opt = {
331
354
  :raise => false,
332
355
  :timeout => 0
333
356
  }
334
-
335
357
  RBatch::Log.new(cmd_str, opt)
336
358
 
337
359
 
338
360
  Migration from version 1 to version 2
339
361
  --------------
340
362
 
341
- Move "${RB_HOME}/conf/rbatch.yaml" to "${RB_HOME}/.rbatchrc" .
363
+ Move `${RB_HOME}/conf/rbatch.yaml` to `${RB_HOME}/.rbatchrc` .
364
+ That's all.
data/bin/rbatch-init CHANGED
@@ -1,5 +1,3 @@
1
- #!/usr/local/bin/ruby
2
-
3
1
  contents = {}
4
2
 
5
3
  contents[".rbatchrc"] = <<EOF
@@ -27,10 +25,25 @@ contents[".rbatchrc"] = <<EOF
27
25
  #
28
26
  #common_conf_name: share.yaml
29
27
 
28
+ # Library Directory
29
+ #
30
+ # Default is "<home>/lib"
31
+ # <home> is replaced to ${RB_HOME}
32
+ #
33
+ #lib_dir: /usr/local/lib/rbatch/
34
+
35
+ # Auto Library Load
36
+ #
37
+ # Default is true
38
+ # If true, require "(library directory)/*.rb" before script run.
39
+ #
40
+ #auto_lib_load: true
41
+ #auto_lib_load: false
42
+
30
43
  # Forbit Script Double Run
31
44
  #
32
45
  # Default is false.
33
- # If this option is true, two same name scripts cannot run at the same time.
46
+ # If true, two same name scripts cannot run at the same time.
34
47
  #
35
48
  #forbid_double_run: true
36
49
  #forbid_double_run: false
@@ -42,7 +55,7 @@ contents[".rbatchrc"] = <<EOF
42
55
  # Raise Exception
43
56
  #
44
57
  # Default is false.
45
- # If command exit status is not 0, raise exception.
58
+ # If true, when command exit status is not 0, raise exception.
46
59
  #
47
60
  #cmd_raise : true
48
61
  #cmd_raise : false
@@ -60,7 +73,6 @@ contents[".rbatchrc"] = <<EOF
60
73
  # Log Directory
61
74
  #
62
75
  # Default is "<home>/log"
63
- #
64
76
  # <home> is replaced to ${RB_HOME}
65
77
  #
66
78
  #log_dir: <home>/rb_log
@@ -69,7 +81,6 @@ contents[".rbatchrc"] = <<EOF
69
81
  # Log File Name
70
82
  #
71
83
  # Default is "<date>_<time>_<prog>.log".
72
- # Reservation words are follows.
73
84
  # <data> is replaced to YYYYMMDD date string
74
85
  # <time> is replaced to HHMMSS time string
75
86
  # <prog> is replaced to Program file base name (except extention).
@@ -133,6 +144,16 @@ contents[".rbatchrc"] = <<EOF
133
144
  #log_mail_server_host : "localhost"
134
145
  #log_mail_server_port : 25
135
146
 
147
+ # Mix RBatch Message to Log
148
+ #
149
+ # Default is true.
150
+ # If true, put RBatch message to log file(s) which is opened at time.
151
+ # Example of RBatch Message
152
+ # [RBatch] Load Config : "../conf/hoge.yaml"
153
+ #
154
+ #mix_rbatch_msg_to_log : true
155
+ #mix_rbatch_msg_to_log : false
156
+
136
157
  EOF
137
158
 
138
159
  contents["bin/hello_world.rb"] = <<EOF