kanamei-keystone 0.0.14 → 0.0.15
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/example/batch_sample.rb +12 -11
- data/lib/keystone.rb +1 -1
- data/lib/keystone/batch/base.rb +66 -22
- data/lib/keystone/core_ext/object.rb +1 -0
- metadata +1 -1
data/example/batch_sample.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'keystone'
|
3
|
+
require "optparse"
|
3
4
|
|
4
5
|
include Keystone::Batch::Base
|
5
6
|
|
6
|
-
|
7
|
-
puts "set mail addr"
|
8
|
-
exit
|
9
|
-
end
|
7
|
+
# 実行時に--helpでオプション一覧
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
has_option = {}
|
10
|
+
opts = option_parser
|
11
|
+
opts.on("-v value") do |v|
|
12
|
+
has_option[:v] = v
|
13
|
+
end
|
14
|
+
opts.on("-w who") do |v|
|
15
|
+
has_option[:w] = v
|
16
|
+
end
|
18
17
|
|
19
18
|
execute() do
|
20
19
|
info "batch process01"
|
20
|
+
info "who=#{has_option[:w]}"
|
21
|
+
sleep 4
|
21
22
|
raise 'error occur'
|
22
23
|
end
|
data/lib/keystone.rb
CHANGED
data/lib/keystone/batch/base.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require 'digest/md5'
|
3
|
+
require "optparse"
|
3
4
|
|
4
5
|
module Keystone::Batch
|
5
6
|
#
|
@@ -18,12 +19,18 @@ module Keystone::Batch
|
|
18
19
|
# ERROR_MAIL_STMP_PORT (エラーメール送信SMTPポート) 設定されてない場合は25
|
19
20
|
#
|
20
21
|
module Base
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
#
|
22
|
+
def option_parser
|
23
|
+
@options ||= OptionParser.new
|
24
|
+
end
|
25
|
+
|
26
|
+
# [options]
|
27
|
+
# プログラムより指定するバッチ動作オプション(ハッシュ値)
|
28
|
+
# :error_mail_to
|
29
|
+
# :error_mail_from
|
30
|
+
# :error_mail_smtp_addr
|
31
|
+
# :error_mail_smtp_port
|
32
|
+
# :double_process_check
|
33
|
+
# :auto_recover
|
27
34
|
#
|
28
35
|
# バッチの主処理をこのメソッドへのブロック引数として定義してください
|
29
36
|
#
|
@@ -32,21 +39,51 @@ module Keystone::Batch
|
|
32
39
|
#
|
33
40
|
# include Keystone::Batch::Base
|
34
41
|
#
|
35
|
-
# # ERROR_MAIL_TOを設定しておけば自動でエラーメールが送信される
|
36
|
-
# ERROR_MAIL_TO = ARGV[0]
|
37
|
-
#
|
38
42
|
# execute() do
|
39
43
|
# info "batch process01"
|
40
44
|
# end
|
41
45
|
#
|
42
|
-
def execute(
|
46
|
+
def execute(options={},&process)
|
47
|
+
options[:error_mail_to] = nil unless options.key?(:error_mail_to)
|
48
|
+
options[:error_mail_from] = nil unless options.key?(:error_mail_from)
|
49
|
+
options[:error_mail_smtp_addr] = "127.0.0.1" unless options.key?(:error_mail_smtp_addr)
|
50
|
+
options[:error_mail_smtp_port] = 25 unless options.key?(:error_mail_smtp_port)
|
51
|
+
options[:double_process_check] = true unless options.key?(:double_process_check)
|
52
|
+
options[:auto_recover] = true unless options.key?(:auto_recover)
|
53
|
+
|
54
|
+
double_process_check = options[:double_process_check]
|
55
|
+
auto_recover = options[:auto_recover]
|
56
|
+
|
43
57
|
debug "caller=#{caller}"
|
44
58
|
pg_path = if File.expand_path(caller[0]) =~ /(.*):\d*\z/
|
45
59
|
$1
|
60
|
+
else
|
61
|
+
raise "must not happen!! can not get caller value"
|
62
|
+
end
|
63
|
+
pid_file = nil
|
64
|
+
opts = option_parser()
|
65
|
+
opts.on("--lockfile LOCK_FILE_PATH") do |v|
|
66
|
+
double_process_check = true unless double_process_check
|
67
|
+
pid_file = v
|
68
|
+
end
|
69
|
+
|
70
|
+
opts.on("--error_mail_to ADDRESS") do |v|
|
71
|
+
options[:error_mail_to] = v
|
46
72
|
end
|
47
73
|
|
74
|
+
opts.on("--error_mail_from ADDREDD") do |v|
|
75
|
+
options[:error_mail_from] = v
|
76
|
+
end
|
77
|
+
opts.on("--error_mail_smtp_addr ADDRESS") do |v|
|
78
|
+
options[:error_mail_smtp_addr] = v
|
79
|
+
end
|
80
|
+
opts.on("--error_mail_smtp_port PORT") do |v|
|
81
|
+
options[:error_mail_smtp_port] = Integer(v)
|
82
|
+
end
|
83
|
+
|
84
|
+
opts.parse!(ARGV)
|
85
|
+
|
48
86
|
info "start script(#{pg_path})"
|
49
|
-
warn "ERROR_MAIL_TO not defined.if you want error mail automatically,set this value." unless Module.constants.include?("ERROR_MAIL_TO")
|
50
87
|
script_started_at = Time.now
|
51
88
|
double_process_check_worked = false
|
52
89
|
begin
|
@@ -55,21 +92,21 @@ module Keystone::Batch
|
|
55
92
|
#pg_path = File.expand_path($0)
|
56
93
|
pg_name = File.basename(pg_path)
|
57
94
|
hash = Digest::MD5.hexdigest(pg_path)
|
58
|
-
pid_file = "/tmp/.#{pg_name}.#{hash}.pid"
|
95
|
+
pid_file = "/tmp/.#{pg_name}.#{hash}.pid" unless pid_file
|
59
96
|
debug pid_file
|
60
97
|
if File.exists?(pid_file)
|
61
98
|
pid = File.open(pid_file).read.chomp
|
62
99
|
pid_list = `ps -e | awk '{print $1}'`
|
63
|
-
if pid_list =~ /#{pid}/
|
100
|
+
if (pid != nil && pid != "" ) && pid_list =~ /#{pid}/
|
64
101
|
warn "pid:#{pid} still running"
|
65
102
|
double_process_check_worked = true
|
66
103
|
return nil
|
67
104
|
else
|
68
105
|
if auto_recover
|
69
|
-
warn "
|
106
|
+
warn "lock file still exists[pid=#{pid}],but process does not found.auto_recover enabled.so process continues"
|
70
107
|
else
|
71
108
|
double_process_check_worked = true
|
72
|
-
raise "
|
109
|
+
raise "lock file still exists[pid=#{pid}],but process does not found.auto_recover disabled.so process can not continue"
|
73
110
|
end
|
74
111
|
end
|
75
112
|
end
|
@@ -80,7 +117,7 @@ module Keystone::Batch
|
|
80
117
|
return (yield process)
|
81
118
|
rescue => e
|
82
119
|
error e
|
83
|
-
send_error_mail(e)
|
120
|
+
send_error_mail(e,options)
|
84
121
|
ensure
|
85
122
|
unless double_process_check_worked
|
86
123
|
File.delete(pid_file) if double_process_check
|
@@ -97,16 +134,21 @@ module Keystone::Batch
|
|
97
134
|
# [exception]
|
98
135
|
# エクセプションクラスインスタンス
|
99
136
|
#
|
100
|
-
def send_error_mail(exception)
|
101
|
-
if
|
137
|
+
def send_error_mail(exception,options)
|
138
|
+
if options[:error_mail_to]
|
102
139
|
host = Keystone::Os.get()
|
103
140
|
title = %|error occur at "#{host.hostname}" [#{exception.message}]|
|
104
141
|
|
105
|
-
mail_to =
|
142
|
+
mail_to = options[:error_mail_to]
|
106
143
|
mail_to = [mail_to] if mail_to.is_a?(String)
|
107
|
-
mail_from =
|
108
|
-
smtp_addr =
|
109
|
-
smtp_port =
|
144
|
+
mail_from = options[:error_mail_from] ? options[:error_mail_from] : mail_to[0]
|
145
|
+
smtp_addr = options[:error_mail_smtp_addr]
|
146
|
+
smtp_port = options[:error_mail_smtp_port]
|
147
|
+
|
148
|
+
debug "mail_to=#{mail_to}"
|
149
|
+
debug "mail_from=#{mail_from}"
|
150
|
+
debug "smtp_addr=#{smtp_addr}"
|
151
|
+
debug "smtp_port=#{smtp_port}"
|
110
152
|
|
111
153
|
body = <<-BODY
|
112
154
|
==== error message ====
|
@@ -117,6 +159,8 @@ module Keystone::Batch
|
|
117
159
|
#{host.dump}
|
118
160
|
BODY
|
119
161
|
Keystone::Mail::Send.send(mail_from,mail_to,title,body,smtp_addr,smtp_port)
|
162
|
+
else
|
163
|
+
warn "ERROR_MAIL_TO not defined.if you want error mail automatically,set this value."
|
120
164
|
end
|
121
165
|
end
|
122
166
|
end
|