ext_batch 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/ext_batch.gemspec +3 -0
- data/lib/ext_batch/main.rb +63 -81
- data/lib/ext_batch/version.rb +1 -1
- data/lib/templates/workers/ext_batch_monitor.rb +3 -2
- metadata +29 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9a0df57a53bf0e1deae15000a355b93914284a3
|
4
|
+
data.tar.gz: 03a7b9f81c2cf3b9f4d706e8700664c79c677e0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36c392fbfbcf6d85076c83b606ef7ddf18f250a7a62bd65366749f431e64aff4df5b6563776eb22fe75e18e19785e5d350bdac6737036fa1122343ca8eb94503
|
7
|
+
data.tar.gz: 0139654785e69ec6c75e207cb9a4485e56d18a88c6993231b8914228fa1ab119a97c363ac515a87ee2ae4528daec307882ef62f3d31592329f19a1cb87caeeff
|
data/ext_batch.gemspec
CHANGED
@@ -35,5 +35,8 @@ Gem::Specification.new do |spec|
|
|
35
35
|
|
36
36
|
spec.add_dependency "mail", "~> 2.5"
|
37
37
|
spec.add_dependency "sys-proctable", "~> 1.2"
|
38
|
+
spec.add_dependency "sidekiq", ">= 3.4"
|
39
|
+
spec.add_dependency "sidekiq-scheduler", "2.1.7"
|
40
|
+
|
38
41
|
spec.add_dependency "ext_logger", "~> 0.2"
|
39
42
|
end
|
data/lib/ext_batch/main.rb
CHANGED
@@ -16,6 +16,61 @@ class ExtBatch
|
|
16
16
|
SEND_MAIL_NO = 'no'
|
17
17
|
SEND_MAIL_YES = 'yes'
|
18
18
|
|
19
|
+
PID_FILE_PATH = ExtLogger.rails_root + '/tmp/batch_pids/'
|
20
|
+
|
21
|
+
def self.monitor_pid_file(batch=nil)
|
22
|
+
logger = batch if batch.is_a?(ExtBatch)
|
23
|
+
|
24
|
+
path = self.get_pid_file_path
|
25
|
+
if File.directory?(path)
|
26
|
+
Dir.foreach(path) do |file|
|
27
|
+
file_name = File.basename(file)
|
28
|
+
if file_name.include?('.pid')
|
29
|
+
pid = nil
|
30
|
+
|
31
|
+
# Get batch pid file
|
32
|
+
File.open(File.join(path, file_name), 'r') do |f|
|
33
|
+
while line = f.gets
|
34
|
+
pid = line.to_i
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Delete batch pid file
|
39
|
+
if pid && !ProcTable.ps(pid: pid)
|
40
|
+
logger.info "Delete file: #{file_name}, pid: #{pid}"
|
41
|
+
File.delete(file_name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Get file name for batch process pid
|
49
|
+
def self.get_pid_file_name
|
50
|
+
return @pid_file[:name] || ''
|
51
|
+
end
|
52
|
+
|
53
|
+
# Set file path for batch process pid
|
54
|
+
def self.set_pid_file_path(str)
|
55
|
+
@@pid_file_path = str if str.is_a?(String) && !is_blank?(str)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get file path for batch process pid
|
59
|
+
def self.get_pid_file_path
|
60
|
+
return @@pid_file_path || PID_FILE_PATH
|
61
|
+
end
|
62
|
+
|
63
|
+
# Set file name for batch process pid
|
64
|
+
def self.set_pid_file_name(str)
|
65
|
+
@pid_file[:name] = str if str.is_a?(String) && !is_blank?(str)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Set file name for batch process pid
|
69
|
+
def set_pid_flag(flag=true)
|
70
|
+
flag = true if !flag.is_a?(TrueClass) && !flag.is_a?(FalseClass)
|
71
|
+
@pid_flag = flag
|
72
|
+
end
|
73
|
+
|
19
74
|
# Initialize, next is parameters:
|
20
75
|
# 1rst: task object or batch name(string) (Default parameter)
|
21
76
|
# 2end: option (Influencing log size, file name format, etc.)
|
@@ -39,10 +94,9 @@ class ExtBatch
|
|
39
94
|
@start_time ||= Time.now
|
40
95
|
@last_time ||= @start_time
|
41
96
|
|
42
|
-
|
43
97
|
@pid_flag = true
|
98
|
+
@@pid_file_path ||= PID_FILE_PATH # Prevent duplication after file sharing by multiple servers
|
44
99
|
@pid_file = {
|
45
|
-
path: '',
|
46
100
|
name: '',
|
47
101
|
cmd: ''
|
48
102
|
}
|
@@ -183,77 +237,6 @@ class ExtBatch
|
|
183
237
|
return mail_tips(@log_name, opt)
|
184
238
|
end
|
185
239
|
|
186
|
-
# Set file name for batch process pid
|
187
|
-
def set_pid_flag(flag=true)
|
188
|
-
flag = true if !flag.is_a?(TrueClass) && !flag.is_a?(FalseClass)
|
189
|
-
@pid_flag = flag
|
190
|
-
end
|
191
|
-
|
192
|
-
# Get file name for batch process pid
|
193
|
-
def get_pid_file_name
|
194
|
-
return @pid_file[:name]
|
195
|
-
end
|
196
|
-
|
197
|
-
# Set file path for batch process pid
|
198
|
-
def set_pid_file_path(str)
|
199
|
-
@pid_file[:path] = str if str.is_a?(String) && !is_blank?(str)
|
200
|
-
end
|
201
|
-
|
202
|
-
# Get file path for batch process pid
|
203
|
-
def get_pid_file_path
|
204
|
-
return @pid_file[:path]
|
205
|
-
end
|
206
|
-
|
207
|
-
# Set file name for batch process pid
|
208
|
-
def set_pid_file_name(str)
|
209
|
-
@pid_file[:name] = str if str.is_a?(String) && !is_blank?(str)
|
210
|
-
end
|
211
|
-
|
212
|
-
# Get file name for batch process pid
|
213
|
-
def get_pid_file_name
|
214
|
-
return @pid_file[:name]
|
215
|
-
end
|
216
|
-
|
217
|
-
# Get test for pid
|
218
|
-
def get_pid_test(opt={})
|
219
|
-
|
220
|
-
# Everything
|
221
|
-
# ProcTable.ps{ |p|
|
222
|
-
# puts p.pid.to_s
|
223
|
-
# puts p.comm
|
224
|
-
# # ...
|
225
|
-
# }
|
226
|
-
|
227
|
-
# Just one process
|
228
|
-
# s = ProcTable.ps(pid: 2123)
|
229
|
-
# puts s.pid.to_s
|
230
|
-
# puts s.comm
|
231
|
-
# ...
|
232
|
-
|
233
|
-
# Return the results as an array of ProcTableStructs
|
234
|
-
puts "@task_name ### #{@task_name}"
|
235
|
-
|
236
|
-
task_name = 'irb'
|
237
|
-
pid = ''
|
238
|
-
a = ProcTable.ps
|
239
|
-
a.each_with_index do |p, idx|
|
240
|
-
if p.cmdline.include?(task_name)
|
241
|
-
@ext_logger.info "pid:#{p.pid}, cmd:#{p.comm}, cmdline: #{p.cmdline}"
|
242
|
-
pid = p.pid
|
243
|
-
break
|
244
|
-
end
|
245
|
-
# puts "pid:#{p.pid}, cmd:#{p.comm}, cmdline: #{p.cmdline}" if p.cmdline.include?(@task_name)
|
246
|
-
# puts "p.class: #{p.class}"
|
247
|
-
# puts p.to_s
|
248
|
-
# break if idx >= 10
|
249
|
-
# ...
|
250
|
-
end
|
251
|
-
|
252
|
-
puts "#{task_name}.pid: #{pid}"
|
253
|
-
|
254
|
-
File.new()
|
255
|
-
end
|
256
|
-
|
257
240
|
private
|
258
241
|
# Private function for initialize
|
259
242
|
# batch initialize
|
@@ -287,7 +270,7 @@ class ExtBatch
|
|
287
270
|
end
|
288
271
|
|
289
272
|
# Full log file name
|
290
|
-
log_path = rails_root + '/log/' + @log_name + '.log'
|
273
|
+
log_path = ExtLogger.rails_root + '/log/' + @log_name + '.log'
|
291
274
|
mkdir_more(log_path)
|
292
275
|
output_debug "#{func_name} Current log path: #{log_path}"
|
293
276
|
|
@@ -327,14 +310,13 @@ class ExtBatch
|
|
327
310
|
set_pid_flag(opt[:pid_flag]) if !is_blank?(opt[:pid_flag])
|
328
311
|
if @pid_flag
|
329
312
|
@pid_file = {
|
330
|
-
path: rails_root + '/tmp/batch_pids/', # Prevent duplication after file sharing by multiple servers
|
331
313
|
name: @task_name.gsub(':', '#').gsub(' ', '_') + '.pid',
|
332
314
|
cmd: @task_name
|
333
315
|
}
|
334
316
|
create_pid_file
|
335
317
|
end
|
336
318
|
|
337
|
-
message = "(#{rails_env}) #{@task_name} START"
|
319
|
+
message = "(#{ExtLogger.rails_env}) #{@task_name} START"
|
338
320
|
@ext_logger.info message
|
339
321
|
end
|
340
322
|
|
@@ -406,8 +388,8 @@ class ExtBatch
|
|
406
388
|
# Create batch process pid file
|
407
389
|
def create_pid_file
|
408
390
|
func_name = "[#{get_class_name}:private #{__method__.to_s}]"
|
409
|
-
if !is_blank?(@pid_file[:name]) && !is_blank?(
|
410
|
-
file_path = File.join(
|
391
|
+
if !is_blank?(@pid_file[:name]) && !is_blank?(@@pid_file_path)
|
392
|
+
file_path = File.join(@@pid_file_path, @pid_file[:name])
|
411
393
|
@ext_logger.info "#{func_name} pid_file_path: #{file_path}"
|
412
394
|
|
413
395
|
pid = nil
|
@@ -425,10 +407,10 @@ class ExtBatch
|
|
425
407
|
# ...
|
426
408
|
end
|
427
409
|
|
428
|
-
|
429
410
|
if pid
|
430
411
|
@ext_logger.info "#{func_name} #{@pid_file[:name]}.pid: #{pid}"
|
431
412
|
mkdir_more(file_path)
|
413
|
+
File.delete(file_path) if File.exist?(file_path)
|
432
414
|
|
433
415
|
File.open(file_path, 'w') do |f|
|
434
416
|
f.write pid
|
@@ -444,8 +426,8 @@ class ExtBatch
|
|
444
426
|
# Delete batch process pid file
|
445
427
|
def delete_pid_file
|
446
428
|
func_name = "[#{get_class_name}:private #{__method__.to_s}]"
|
447
|
-
if !is_blank?(@pid_file[:name]) && !is_blank?(
|
448
|
-
file_path = File.join(
|
429
|
+
if !is_blank?(@pid_file[:name]) && !is_blank?(@@pid_file_path)
|
430
|
+
file_path = File.join(@@pid_file_path, @pid_file[:name].gsub(' ', '_'))
|
449
431
|
|
450
432
|
File.delete(file_path) if File.exist?(file_path)
|
451
433
|
end
|
data/lib/ext_batch/version.rb
CHANGED
@@ -4,10 +4,11 @@ class ExtBatchMonitor
|
|
4
4
|
sidekiq_options :retry => false
|
5
5
|
|
6
6
|
def perform(*args)
|
7
|
+
include Sys
|
7
8
|
batch_name = File.basename(__FILE__).gsub('.rb', '')
|
8
|
-
batch = ExtBatch.new(batch_name)
|
9
|
+
batch = ExtBatch.new(batch_name, pid_flag: false)
|
9
10
|
begin
|
10
|
-
|
11
|
+
ExtBatch.monitor_pid_file(batch)
|
11
12
|
|
12
13
|
rescue Exception => e
|
13
14
|
batch.exception e
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ext_batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruby
|
@@ -66,6 +66,34 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sidekiq
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.4'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sidekiq-scheduler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.1.7
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.1.7
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
98
|
name: ext_logger
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|