batchbase 0.0.2 → 0.0.4

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 CHANGED
@@ -1,3 +1,4 @@
1
+ .DS_Store
1
2
  *.gem
2
3
  *.rbc
3
4
  *.lock
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source "http://rubygems.org"
2
-
3
2
  # Specify your gem's dependencies in batchbase.gemspec
4
3
  gemspec
data/batchbase.gemspec CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  s.description = %q{oreore batch base class}
13
13
  s.rubyforge_project = "batchbase"
14
14
 
15
- s.add_dependency "sys-proctable","0.9.1"
16
- s.add_dependency "kanamei_log_formatter","0.0.1"
15
+ s.add_dependency "sys-proctable",["0.9.1"]
16
+ s.add_dependency "kanamei_log_formatter",["0.0.1"]
17
17
 
18
18
  s.files = `git ls-files`.split("\n")
19
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/example/sample2.rb CHANGED
@@ -3,19 +3,30 @@ require 'batchbase'
3
3
 
4
4
  # usage type 2
5
5
 
6
+ DEFAULT_TEXT = 'without test'
7
+ DEFAULT_TEXT_TEST = 'test'
8
+
6
9
  class Batch
7
10
  include Batchbase::Core
8
11
 
9
- def proceed
12
+ def initialize
13
+ # 初期値設定
14
+ env_defaults = {:favorite_number=>1}
15
+ # オプションパーサーの設定追加
10
16
  opts = self.option_parser
11
17
  opts.on("-f", "--favorite_number=value",
12
18
  Integer,"favo"
13
19
  ) do |v|
14
20
  env[:favorite_number] = v
15
21
  end
22
+ # ここでオプションをパースしておく
23
+ parse_options(env_defaults)
24
+ end
16
25
 
26
+ def proceed(text)
17
27
  execute do
18
28
  logger.info env.inspect
29
+ logger.info text
19
30
  if env[:favorite_number]
20
31
  logger.info env[:favorite_number].to_s
21
32
  else
@@ -27,4 +38,10 @@ class Batch
27
38
  end
28
39
 
29
40
  b = Batch.new
30
- b.proceed
41
+ case b.env[:environment]
42
+ when 'test'
43
+ b.proceed(DEFAULT_TEXT_TEST)
44
+ else
45
+ b.proceed(DEFAULT_TEXT)
46
+ end
47
+
data/example/sample3.rb CHANGED
@@ -16,7 +16,8 @@ end
16
16
 
17
17
  set_signal_observer(:receive_signal)
18
18
 
19
- execute(:daemonize=>true) do
19
+ # :process_nameを指定するとps時の名前を指定できる
20
+ execute(:daemonize=>true,:process_name=>'batchbase_sample3') do
20
21
  logger.info 'test'
21
22
  logger.info env[:pid_file]
22
23
  3600.times do
@@ -11,10 +11,11 @@ module Batchbase
11
11
  SIGNALS = [ :QUIT, :INT, :TERM, :USR1, :USR2, :HUP ]
12
12
  #SIGNALS = [ :QUIT, :INT, :TERM ]
13
13
 
14
- DOUBLE_PROCESS_CHECK__OK = 1
15
- DOUBLE_PROCESS_CHECK__AUTO_RECOVERD = 2
16
- DOUBLE_PROCESS_CHECK__NG = 0
17
- DOUBLE_PROCESS_CHECK__STILL_RUNNING = -1
14
+ DOUBLE_PROCESS_CHECK__OK = 1
15
+ DOUBLE_PROCESS_CHECK__AUTO_RECOVERD = 2
16
+ DOUBLE_PROCESS_CHECK__NG = 0
17
+ DOUBLE_PROCESS_CHECK__STILL_RUNNING = -1
18
+ DOUBLE_PROCESS_CHECK__SAME_PROCESS_NAME = 3
18
19
 
19
20
  def option_parser
20
21
  @__option_parser ||= OptionParser.new
@@ -71,7 +72,7 @@ module Batchbase
71
72
  #
72
73
  # 内部的には
73
74
  # init
74
- # parse_options
75
+ # parse_options_inner
75
76
  # execute_inner
76
77
  # release
77
78
  # の順にコールしてます
@@ -82,22 +83,25 @@ module Batchbase
82
83
  # :auto_recover 初期値 false
83
84
  #
84
85
  def execute(options={},&process)
86
+ result = nil
85
87
  begin
86
88
  init
89
+ parse_options_inner(options,ARGV)
87
90
  logger.info "start script(#{pg_path})"
88
91
  logger.debug "caller=#{caller}"
89
- parse_options(options,ARGV)
90
92
  result = double_process_check_and_create_pid_file
91
93
  case result
92
94
  when DOUBLE_PROCESS_CHECK__OK,DOUBLE_PROCESS_CHECK__AUTO_RECOVERD
93
95
  if result == DOUBLE_PROCESS_CHECK__AUTO_RECOVERD
94
96
  logger.warn "lock file still exists[pid=#{env[:old_pid_from_pid_file]}:file=#{pid_file}],but process does not found.Auto_recover enabled.so process continues"
95
97
  end
96
- execute_inner(&process)
98
+ result = execute_inner(&process)
97
99
  when DOUBLE_PROCESS_CHECK__NG
98
100
  logger.error "lock file still exists[pid=#{env[:old_pid_from_pid_file]}:file=#{pid_file}],but process does not found.Auto_recover disabled.so process can not continue"
99
101
  when DOUBLE_PROCESS_CHECK__STILL_RUNNING
100
102
  logger.warn "pid:#{env[:old_pid_from_pid_file]} still running"
103
+ when DOUBLE_PROCESS_CHECK__SAME_PROCESS_NAME
104
+ logger.warn "process_name:#{env[:process_name]} still exists"
101
105
  else
102
106
  raise 'must not happen'
103
107
  end
@@ -107,6 +111,7 @@ module Batchbase
107
111
  release
108
112
  logger.info "finish script (%1.3fsec)" % (Time.now - @__script_started_at)
109
113
  end
114
+ result
110
115
  end
111
116
 
112
117
  module ClassMethods
@@ -116,6 +121,9 @@ module Batchbase
116
121
  process = Sys::ProcTable.ps(pid)
117
122
  process != nil && process.state == 'run'
118
123
  end
124
+
125
+ def env
126
+ end
119
127
  end
120
128
 
121
129
  def self.included(mod)
@@ -140,12 +148,17 @@ module Batchbase
140
148
  @__signal_observers << method_name
141
149
  end
142
150
 
151
+ def parse_options(options)
152
+ parse_options_inner(options,ARGV)
153
+ end
154
+
143
155
  private
144
156
 
145
157
  def init
146
158
  SIGNALS.each { |sig| trap(sig){r_signal(sig)} }
147
159
  @__script_started_at = Time.now
148
- raise 'already inited' if @__init
160
+ #raise 'already inited' if @__init
161
+ return if @__init
149
162
  @__init = true
150
163
  env[:pid] = $$
151
164
  if File.expand_path(caller[0]) =~ /(.*):\d*:in `.*?'\z/
@@ -155,21 +168,23 @@ module Batchbase
155
168
  end
156
169
  end
157
170
 
158
- def parse_options(options,argv)
159
- env[:double_process_check] = options[:double_process_check]
160
- env[:double_process_check] = true if env[:double_process_check] == nil
161
- env[:auto_recover] = options[:auto_recover]
162
- env[:auto_recover] = false if env[:auto_recover] == nil
163
- env[:environment] = options[:environment] ||= 'development'
164
- env[:pg_name] = File.basename(pg_path)
165
- env[:pid_file] = options[:pid_file]
166
- env[:daemonize] = options[:daemonize]
167
- env[:daemonize] = false if env[:daemonize] == nil
168
- env[:pid_file] ||= "/tmp/.#{env[:pg_name]}.#{Digest::MD5.hexdigest(pg_path)}.pid"
171
+ def parse_options_inner(options,argv)
172
+ init
173
+ return if @__parse_option
174
+ @__parse_option = true
175
+ options[:double_process_check] = true if options[:double_process_check].nil?
176
+ options[:auto_recover] ||= false
177
+ options[:environment] ||= 'development'
178
+ options[:pg_name] ||= File.basename(pg_path)
179
+ options[:pid_file] ||= "/tmp/.#{env[:pg_name]}.#{Digest::MD5.hexdigest(pg_path)}.pid"
180
+ options[:daemonize] ||= false
181
+ options.each do |k,v|
182
+ env[k] = v
183
+ end
169
184
 
170
185
  opts = option_parser
171
186
 
172
- opts.on("-e", "--environment=name",
187
+ opts.on("-e", "--environment name",
173
188
  String,"specifies the environment",
174
189
  "default: development") do |v|
175
190
  env[:environment] = v
@@ -179,12 +194,22 @@ module Batchbase
179
194
  env[:daemonize] = true
180
195
  end
181
196
 
197
+ opts.on("-p", "--process_name name",
198
+ String,"specifies the process name(it work with double process check)",
199
+ "default: nil") do |v|
200
+ env[:process_name] = v.clone.strip
201
+ end
202
+
182
203
  opts.on("-h","--help","show this help message.") { $stderr.puts opts; exit }
183
204
 
184
205
  opts.on("--lockfile LOCK_FILE_PATH","set lock file path") do |v|
185
206
  double_process_check = true
186
207
  env[:pid_file] = v
187
208
  end
209
+ opts.on("--log LOG_FILE_PATH","set log file path") do |v|
210
+ env[:log] = v
211
+ create_logger(v)
212
+ end
188
213
  opts.on("--double_process_check_off","disable double process check") do |v|
189
214
  env[:double_process_check] = false
190
215
  end
@@ -194,6 +219,10 @@ module Batchbase
194
219
 
195
220
  opts.parse!(argv)
196
221
 
222
+ if env[:process_name]
223
+ $0 = env[:process_name]
224
+ end
225
+
197
226
  if env[:auto_recover] == true
198
227
  env[:double_process_check] = true
199
228
  end
@@ -220,6 +249,15 @@ module Batchbase
220
249
  end
221
250
  end
222
251
  end
252
+
253
+ if env[:process_name]
254
+ Sys::ProcTable.ps do |other_process|
255
+ if other_process.cmdline.strip == env[:process_name] && $$ != other_process.pid
256
+ env[:double_process_check_problem] = true
257
+ return DOUBLE_PROCESS_CHECK__SAME_PROCESS_NAME
258
+ end
259
+ end
260
+ end
223
261
  File.open(pid_file, "w"){|f|f.write $$}
224
262
  env[:double_process_check_worked] = double_process_check_worked
225
263
  end
@@ -1,3 +1,3 @@
1
1
  module Batchbase
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,30 @@
1
+ class BatchByHand
2
+ include Batchbase::Core
3
+ skip_logging
4
+
5
+ TEST_FILE = '/tmp/.batchbase_batch_by_hand.txt'
6
+
7
+ def proceed(opt={})
8
+ @shutdown = false
9
+ set_signal_observer(:receive_signal)
10
+
11
+ execute(opt) do
12
+ sleep 20
13
+ File.write(TEST_FILE,$$)
14
+ if opt[:daemonize]
15
+ loop do
16
+ sleep 1
17
+ if @shutdown
18
+ #puts "shutdown by #{@shutdown}"
19
+ break
20
+ end
21
+ end
22
+ end
23
+ 11
24
+ end
25
+ end
26
+
27
+ def receive_signal(sig)
28
+ @shutdown = sig
29
+ end
30
+ end
@@ -9,7 +9,7 @@ class BatchTooLong
9
9
  set_signal_observer(:receive_signal)
10
10
  end
11
11
  end
12
- @shutdown = false
12
+ #@shutdown = false
13
13
  execute(opt) do
14
14
  100.times do
15
15
  sleep 1
@@ -19,6 +19,7 @@ class BatchTooLong
19
19
  end
20
20
 
21
21
  def receive_signal(sig)
22
+ #File.write('/tmp/.batchbase.batch_too_long.receive_signal.dat',"#{sig}")
22
23
  @shutdown = true
23
24
  end
24
25
 
data/test/by_hand.rb CHANGED
@@ -4,9 +4,9 @@
4
4
  $: << File.dirname(__FILE__)
5
5
  require 'test_helper'
6
6
 
7
- require 'batch'
7
+ require 'batch_by_hand'
8
8
 
9
- b = Batch.new
9
+ b = BatchByHand.new
10
10
  b.set_signal_observer(:receive_signal)
11
11
  b.proceed
12
12
 
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ $: << File.dirname(__FILE__)
4
+ require 'test_helper'
5
+ require 'batch'
6
+ require 'Fileutils'
7
+
8
+ include Batchbase::Core
9
+
10
+ create_logger('/dev/null')
11
+ #create_logger(STDOUT)
12
+
13
+ def receive_signal(sig)
14
+ logger.info("receive signal #{sig}")
15
+ @shutdown = true
16
+ end
17
+
18
+ result = execute do
19
+ set_signal_observer(:receive_signal)
20
+ 4.times do
21
+ logger.info('logged by pg_for_test')
22
+ sleep 1
23
+ break if @shutdown
24
+ end
25
+ 999999
26
+ end
27
+
28
+ File.write(FILE_PG_TEST,result.to_s)
29
+
data/test/test.rb CHANGED
@@ -7,7 +7,6 @@ require 'Fileutils'
7
7
 
8
8
  #require 'bundler'
9
9
  #Bundler.require
10
-
11
10
  #require 'sys/proctable'
12
11
 
13
12
  require 'batch'
@@ -17,12 +16,16 @@ class TestBatchbase < Test::Unit::TestCase
17
16
 
18
17
  PID_FILE_FORCE = '/tmp/.batchbase_test.pid'
19
18
  PID_FILE_DAEMONIZE_TEST = '/tmp/.batchbase_daemonize_test.pid'
19
+ LOG_FILE = '/tmp/batchbase_test.log'
20
+ PROCESS_NAME = 'batchbase_test_hogehoge'
20
21
 
21
22
  def setup
22
23
  delete_file(pid_file)
23
24
  delete_file(PID_FILE_FORCE)
24
25
  delete_file(PID_FILE_DAEMONIZE_TEST)
25
- delete_file(Batch::TEST_FILE)
26
+ delete_file(Batch::TEST_FILE) # HACKME したとまとめる、、、
27
+ delete_file(FILE_PG_TEST)
28
+ delete_file(LOG_FILE)
26
29
  end
27
30
 
28
31
  def new_batch_instance
@@ -38,14 +41,14 @@ class TestBatchbase < Test::Unit::TestCase
38
41
  def pid_file
39
42
  b = new_batch_instance
40
43
  b.send(:init)
41
- b.send(:parse_options,{},[])
44
+ b.send(:parse_options_inner,{},[])
42
45
  b.env[:pid_file]
43
46
  end
44
47
 
45
48
  def test_pid_file
46
49
  b = new_batch_instance
47
50
  b.send(:init)
48
- b.send(:parse_options,{},[])
51
+ b.send(:parse_options_inner,{},[])
49
52
  b.send(:double_process_check_and_create_pid_file)
50
53
  assert_equal true,File.exists?(b.env[:pid_file])
51
54
  b.send(:release)
@@ -56,7 +59,7 @@ class TestBatchbase < Test::Unit::TestCase
56
59
  FileUtils.touch(pid_file) # すでにpid_fileがあるとして
57
60
  b = new_batch_instance
58
61
  b.send(:init)
59
- b.send(:parse_options,{},[])
62
+ b.send(:parse_options_inner,{},[])
60
63
  result = b.send(:double_process_check_and_create_pid_file)
61
64
  assert_equal Batchbase::Core::DOUBLE_PROCESS_CHECK__NG,result
62
65
  File.delete(pid_file)
@@ -66,7 +69,7 @@ class TestBatchbase < Test::Unit::TestCase
66
69
  FileUtils.touch(pid_file) # すでにpid_fileがあるとして
67
70
  b = new_batch_instance
68
71
  b.send(:init)
69
- b.send(:parse_options,{:double_process_check=>false},[])
72
+ b.send(:parse_options_inner,{:double_process_check=>false},[])
70
73
  result = b.send(:double_process_check_and_create_pid_file)
71
74
  assert_equal Batchbase::Core::DOUBLE_PROCESS_CHECK__OK,result
72
75
  File.delete(pid_file)
@@ -76,7 +79,7 @@ class TestBatchbase < Test::Unit::TestCase
76
79
  FileUtils.touch(pid_file) # すでにpid_fileがあるとして
77
80
  b = new_batch_instance
78
81
  b.send(:init)
79
- b.send(:parse_options,{:auto_recover=>true},[])
82
+ b.send(:parse_options_inner,{:auto_recover=>true},[])
80
83
  result = b.send(:double_process_check_and_create_pid_file)
81
84
  assert_equal Batchbase::Core::DOUBLE_PROCESS_CHECK__AUTO_RECOVER,result
82
85
  assert_equal true,File.exists?(b.env[:pid_file]) # pid_fileは存在していないとだめ
@@ -86,7 +89,7 @@ class TestBatchbase < Test::Unit::TestCase
86
89
  def test_auto_recover
87
90
  b = new_batch_instance
88
91
  b.send(:init)
89
- b.send(:parse_options,{},[:auto_recover=>true])
92
+ b.send(:parse_options_inner,{},[:auto_recover=>true])
90
93
  b.send(:double_process_check_and_create_pid_file)
91
94
  assert_equal true,File.exists?(b.env[:pid_file])
92
95
  b.send(:release)
@@ -98,7 +101,7 @@ class TestBatchbase < Test::Unit::TestCase
98
101
  b = new_batch_instance
99
102
  b.send(:init)
100
103
  argv = []
101
- b.send(:parse_options,{},argv)
104
+ b.send(:parse_options_inner,{},argv)
102
105
  assert_equal b.env[:environment],'development'
103
106
  assert_equal b.env[:double_process_check],true
104
107
  assert_equal b.env[:auto_recover],false
@@ -107,7 +110,7 @@ class TestBatchbase < Test::Unit::TestCase
107
110
  b = new_batch_instance
108
111
  b.send(:init)
109
112
  argv = ['-e','test','--auto_recover']
110
- b.send(:parse_options,{},argv)
113
+ b.send(:parse_options_inner,{},argv)
111
114
  assert_equal b.env[:environment],'test'
112
115
  assert_equal b.env[:double_process_check],true
113
116
  assert_equal b.env[:auto_recover],true
@@ -115,14 +118,14 @@ class TestBatchbase < Test::Unit::TestCase
115
118
  b = new_batch_instance
116
119
  b.send(:init)
117
120
  argv = ['--double_process_check_off']
118
- b.send(:parse_options,{},argv)
121
+ b.send(:parse_options_inner,{},argv)
119
122
  assert_equal b.env[:double_process_check],false
120
123
 
121
124
  # オートリカバリー入れたらダブルプロセスチェックは強制ON
122
125
  b = new_batch_instance
123
126
  b.send(:init)
124
127
  argv = ['-e','test','--auto_recover','double_process_check_off']
125
- b.send(:parse_options,{},argv)
128
+ b.send(:parse_options_inner,{},argv)
126
129
  assert_equal b.env[:environment],'test'
127
130
  assert_equal b.env[:double_process_check],true
128
131
  assert_equal b.env[:auto_recover],true
@@ -130,8 +133,15 @@ class TestBatchbase < Test::Unit::TestCase
130
133
  b = new_batch_instance
131
134
  b.send(:init)
132
135
  argv = ['--lockfile','/tmp/.lockfile_test']
133
- b.send(:parse_options,{},argv)
136
+ b.send(:parse_options_inner,{},argv)
137
+ assert_equal b.env[:pid_file],'/tmp/.lockfile_test'
138
+
139
+ b = new_batch_instance
140
+ b.send(:init)
141
+ argv = ['--lockfile','/tmp/.lockfile_test']
142
+ b.send(:parse_options_inner,{:jojo=>123},argv)
134
143
  assert_equal b.env[:pid_file],'/tmp/.lockfile_test'
144
+ assert_equal b.env[:jojo],123
135
145
  end
136
146
 
137
147
  def test_options
@@ -140,7 +150,7 @@ class TestBatchbase < Test::Unit::TestCase
140
150
  b.send(:init)
141
151
  argv = []
142
152
  options = {:double_process_check=>false,:environment=>'test'}
143
- b.send(:parse_options,options,argv)
153
+ b.send(:parse_options_inner,options,argv)
144
154
  assert_equal b.env[:environment],'test'
145
155
  assert_equal b.env[:double_process_check],false
146
156
  assert_equal b.env[:auto_recover],false
@@ -151,7 +161,7 @@ class TestBatchbase < Test::Unit::TestCase
151
161
  b.send(:init)
152
162
  argv = []
153
163
  options = {:double_process_check=>false,:auto_recover=>true}
154
- b.send(:parse_options,options,argv)
164
+ b.send(:parse_options_inner,options,argv)
155
165
  assert_equal b.env[:double_process_check],true
156
166
  assert_equal b.env[:auto_recover],true
157
167
 
@@ -159,7 +169,7 @@ class TestBatchbase < Test::Unit::TestCase
159
169
  b.send(:init)
160
170
  argv = []
161
171
  options = {:pid_file=>'/tmp/.lock_test'}
162
- b.send(:parse_options,options,argv)
172
+ b.send(:parse_options_inner,options,argv)
163
173
  assert_equal b.env[:pid_file],'/tmp/.lock_test'
164
174
 
165
175
  # ミックスならARGVの指定の方を優先
@@ -167,7 +177,7 @@ class TestBatchbase < Test::Unit::TestCase
167
177
  b.send(:init)
168
178
  argv = ['--lockfile','/tmp/.lllock','--auto_recover']
169
179
  options = {:pid_file=>'/tmp/.lock_test',:auto_recover=>false,:double_process_check=>false}
170
- b.send(:parse_options,options,argv)
180
+ b.send(:parse_options_inner,options,argv)
171
181
  assert_equal b.env[:double_process_check],true
172
182
  assert_equal b.env[:auto_recover],true
173
183
  assert_equal b.env[:pid_file],'/tmp/.lllock'
@@ -184,7 +194,7 @@ class TestBatchbase < Test::Unit::TestCase
184
194
 
185
195
  b.send(:init)
186
196
  argv = ['--favorite_number','11']
187
- b.send(:parse_options,{},argv)
197
+ b.send(:parse_options_inner,{},argv)
188
198
  assert_equal 'development',b.env[:environment]
189
199
  assert_equal true,b.env[:double_process_check]
190
200
  assert_equal false,b.env[:auto_recover]
@@ -232,9 +242,9 @@ class TestBatchbase < Test::Unit::TestCase
232
242
  b.skip_logging
233
243
  b.proceed(:pid_file=>PID_FILE_FORCE)
234
244
  end
235
- sleep 3
236
- pid_by_file = File.read(PID_FILE_FORCE).chomp.to_i
245
+ sleep 2
237
246
  assert_equal true,Batch.is_there_process(pid)
247
+ pid_by_file = File.read(PID_FILE_FORCE).chomp.to_i
238
248
  assert_equal pid,pid_by_file
239
249
  # シグナルを送る
240
250
  # pid_fileを消して終了するか?
@@ -275,7 +285,7 @@ class TestBatchbase < Test::Unit::TestCase
275
285
  b.skip_logging
276
286
  b.proceed(:pid_file=>PID_FILE_FORCE,:signal_cancel=>true)
277
287
  end
278
- sleep 3
288
+ sleep 2
279
289
  pid_by_file = File.read(PID_FILE_FORCE).chomp.to_i
280
290
  assert_equal true,Batch.is_there_process(pid)
281
291
  assert_equal pid,pid_by_file
@@ -302,7 +312,7 @@ class TestBatchbase < Test::Unit::TestCase
302
312
 
303
313
  b2 = new_batch_instance
304
314
  b2.send(:init)
305
- b2.send(:parse_options,{:pid_file=>PID_FILE_FORCE},[])
315
+ b2.send(:parse_options_inner,{:pid_file=>PID_FILE_FORCE},[])
306
316
  result = b2.send(:double_process_check_and_create_pid_file)
307
317
  assert_equal Batch::DOUBLE_PROCESS_CHECK__STILL_RUNNING,result
308
318
  #b2.send(:execute_inner)
@@ -312,6 +322,95 @@ class TestBatchbase < Test::Unit::TestCase
312
322
  sleep 2
313
323
  end
314
324
 
325
+ # pidファイルを手で消したら所詮二重起動チェックから漏れるよね
326
+ def test_can_break_double_process_check
327
+ pid = fork do
328
+ b = new_batch_instance
329
+ b.proceed(:pid_file=>PID_FILE_FORCE)
330
+ end
331
+
332
+ sleep 0.5
333
+
334
+ b2 = new_batch_instance
335
+ b2.send(:init)
336
+ b2.send(:parse_options_inner,{:pid_file=>PID_FILE_FORCE},[])
337
+ result = b2.send(:double_process_check_and_create_pid_file)
338
+ assert_equal Batch::DOUBLE_PROCESS_CHECK__STILL_RUNNING,result
339
+ #b2.send(:execute_inner)
340
+ b2.send(:release)
341
+ # pid_fileまだは存在していないとだめ
342
+ assert_equal true,File.exists?(b2.env[:pid_file])
343
+
344
+ # しかしファイルを消されるとチェックは通る
345
+ File.delete(PID_FILE_FORCE)
346
+ b2 = new_batch_instance
347
+ b2.send(:init)
348
+ b2.send(:parse_options_inner,{:pid_file=>PID_FILE_FORCE},[])
349
+ result = b2.send(:double_process_check_and_create_pid_file)
350
+ assert_equal Batch::DOUBLE_PROCESS_CHECK__OK,result
351
+ #b2.send(:execute_inner)
352
+ b2.send(:release)
353
+
354
+ `kill #{pid}`
355
+ sleep 2
356
+ delete_file(PID_FILE_FORCE)
357
+ end
358
+
359
+ def test_double_process_check_type_exec
360
+ system "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE} &"
361
+ sleep 1
362
+ system "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE} &"
363
+ sleep 1
364
+ assert_equal Batch::DOUBLE_PROCESS_CHECK__STILL_RUNNING,File.read(FILE_PG_TEST).to_i
365
+
366
+ sleep 3
367
+ end
368
+
369
+ def test_can_break_double_process_check_exec
370
+ system "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE} &"
371
+ sleep 0.5
372
+ delete_file(PID_FILE_FORCE)
373
+ sleep 0.5
374
+ system "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE} &"
375
+ sleep 1
376
+ assert_equal false,File.exists?(FILE_PG_TEST) # 停止していないってことで
377
+ sleep 3
378
+ end
379
+
380
+ # process_nameまで指定するとプロセス上に同じ名前があってもうごきません
381
+ def test_double_process_check_with_process_name
382
+ cmd = "ruby ./test/pg_for_test.rb --process_name hogehogemorimoribatchbase --lockfile #{PID_FILE_FORCE} &"
383
+ system cmd
384
+ sleep 0.7
385
+ assert_equal false,File.exists?(FILE_PG_TEST)
386
+ delete_file(PID_FILE_FORCE)
387
+ sleep 0.5
388
+ system cmd
389
+ sleep 1
390
+
391
+ assert_equal Batch::DOUBLE_PROCESS_CHECK__SAME_PROCESS_NAME,File.read(FILE_PG_TEST).to_i
392
+ sleep 3
393
+ end
394
+
395
+ def test_with_log
396
+ assert_equal false, File.exists?(LOG_FILE)
397
+ cmd = "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE} --log #{LOG_FILE} &"
398
+ system cmd
399
+ sleep 1
400
+ assert_equal true, File.exists?(LOG_FILE)
401
+ system "kill `cat #{PID_FILE_FORCE}`"
402
+ sleep 1
403
+ end
404
+
405
+ def test_without_log
406
+ assert_equal false, File.exists?(LOG_FILE)
407
+ cmd = "ruby ./test/pg_for_test.rb --lockfile #{PID_FILE_FORCE}&"
408
+ system cmd
409
+ sleep 1
410
+ assert_equal false, File.exists?(LOG_FILE)
411
+ system "kill `cat #{PID_FILE_FORCE}`"
412
+ sleep 1
413
+ end
315
414
  #
316
415
  # HACKME
317
416
  # executeを読んだ際のメッセージ文言等でエラーが出る場合のフック、、、
data/test/test_helper.rb CHANGED
@@ -3,3 +3,5 @@ $: << File.dirname(__FILE__) + '/../lib'
3
3
 
4
4
  require 'batchbase'
5
5
  require 'batchbase/version'
6
+
7
+ FILE_PG_TEST = '/tmp/.batchbase.file_pg_test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batchbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-13 00:00:00.000000000 Z
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sys-proctable
@@ -62,9 +62,11 @@ files:
62
62
  - lib/batchbase/version.rb
63
63
  - readme.md
64
64
  - test/batch.rb
65
+ - test/batch_by_hand.rb
65
66
  - test/batch_too_long.rb
66
67
  - test/by_hand.rb
67
68
  - test/check_logger.rb
69
+ - test/pg_for_test.rb
68
70
  - test/test.rb
69
71
  - test/test_helper.rb
70
72
  homepage: ''
@@ -93,9 +95,11 @@ specification_version: 3
93
95
  summary: oreore batch base class
94
96
  test_files:
95
97
  - test/batch.rb
98
+ - test/batch_by_hand.rb
96
99
  - test/batch_too_long.rb
97
100
  - test/by_hand.rb
98
101
  - test/check_logger.rb
102
+ - test/pg_for_test.rb
99
103
  - test/test.rb
100
104
  - test/test_helper.rb
101
105
  has_rdoc: