batch_manager 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Yzg5MzM1MGQzMTFhNDJlM2YzZDljODRmNjQ0NzQyODJkZDI1YTU4Mg==
4
+ NzcyOGI4YjBhYTFjZmFjNGJhMWZiYTc4M2EyYmRiMjNmYjk0NTM1ZQ==
5
5
  data.tar.gz: !binary |-
6
- MzViMWU3NTE1OGNjNDU2MzcxMDA0M2U0ZWI1ZmE3ZDY3ZDkzZjcwZA==
6
+ MDZlMDJjZjNiMDgwOTA2ZjYxMWQxM2NiOTM0NWI5ZmUyODZjZTNiOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzQ0NDcyOTQyYTNmNmMyZTUyMTc5NjE0YTA0YjQ1ZTg5N2VkMWVjOTYyZjIy
10
- NjhhN2U3ODZhMzZlNGQ0MzkwNmUyMjc0NmU1MzhjNjU1YTYwZDRmNWUxODhm
11
- ZGFhMmE4NThjMzQxYTdkZjE3MDAwMDQ2NjIxNDc0YjdmOTY1Nzk=
9
+ MGQ3Y2I4NDA4NmVjYWVhNTJiMGQ2MjJkYWM1Y2IzNmU3Y2ZiNDQyZjkyNmFh
10
+ OWE0ZjNkNDMyMjQzNjQ4NTdiMDkyNjYwOTE1YWFkMjE1YWEyZDNlNGFiZWY2
11
+ MDcwYTNjZDg1YmZlYjAyYjg4MDllZjQzMzMxNzc3ODkwOTk3NDM=
12
12
  data.tar.gz: !binary |-
13
- Mjc4ZTVmYTUzMGE3ZDhiMTQ3NDJhMmM1MjExMWRiZjI0ZDc0YmMxMGE3Yjdj
14
- ODA4ODRkNGE3YjI4ZmJhYTg5M2EyMWFjNGM4NDk4MzJmMThiY2ZlYTZkMDA5
15
- NjlkNjQ1NTQ1OWI0ZGNiYThiM2MyN2U1NTAyZjBlOTIyNzExNWE=
13
+ NGQxZmFhNjIzYjFhZWM5OGY4YmYyNDc1ZDk5NjI3ZDg2YjhlYzE3ZDk4MzBk
14
+ MjFkN2RmYTczNTJmZGNhZGFkZTAxODQ3MGNhOTY0MzAyYzQ4MGZiMTRhNDNl
15
+ Zjc0ZDI0NjcyZTczMWFhNDVmMDliYTU4NWIxMDdkZjI1YmFjMjQ=
@@ -1,19 +1,36 @@
1
+ require 'socket'
2
+
1
3
  module BatchManager
2
4
  class ApplicationController < ActionController::Base
5
+ helper_method :resque_supported?, :local_resque_worker?, :resque_worker_hostname
6
+
3
7
  def resque_supported?
4
8
  begin
5
9
  require 'resque'
6
- if defined?(Resque)
7
- queue_names = []
8
- Resque.workers.map do |worker|
9
- queue_names += worker.id.split(':')[-1].split(',')
10
- end
11
- return true if queue_names.include?(BatchManager::ExecBatchWorker.queue_name) || queue_names.include?("*")
12
- end
13
- false
10
+ defined?(Resque) && resque_worker
14
11
  rescue
15
12
  false
16
13
  end
17
14
  end
15
+
16
+ def local_resque_worker?
17
+ Socket.gethostname == resque_worker_hostname
18
+ end
19
+
20
+ def resque_worker_hostname
21
+ resque_worker.id.split(':')[0]
22
+ end
23
+
24
+ private
25
+
26
+ def resque_worker
27
+ @resque_worker ||= begin
28
+ workers = Resque.workers.select do |worker|
29
+ queue_names = worker.id.split(':')[-1].split(',')
30
+ queue_names.include?(BatchManager::ExecBatchWorker.queue_name) || queue_names.include?("*")
31
+ end
32
+ workers.first
33
+ end
34
+ end
18
35
  end
19
36
  end
@@ -7,7 +7,6 @@ module BatchManager
7
7
  helper_method :escape_batch_name
8
8
 
9
9
  def index
10
- @resque_supported = resque_supported?
11
10
  @details = BatchManager::Monitor.details
12
11
  end
13
12
 
@@ -41,7 +40,11 @@ module BatchManager
41
40
  def exec
42
41
  if resque_supported?
43
42
  Resque.enqueue(BatchManager::ExecBatchWorker, @batch_name, :wet => @wet)
44
- redirect_to(log_batches_url(:batch_name => @batch_name, :wet => @wet, :refresh => true))
43
+ if local_resque_worker?
44
+ redirect_to(log_batch_url(:batch_name => @batch_name, :wet => @wet, :refresh => true))
45
+ else
46
+ redirect_to(batches_url, :notice => "(#{@batch_name}) Task added to the remote resque worker.")
47
+ end
45
48
  else
46
49
  BatchManager::Executor.exec(@batch_name, :wet => @wet)
47
50
  redirect_to(batches_url)
@@ -79,7 +82,7 @@ module BatchManager
79
82
  end
80
83
 
81
84
  def log_file
82
- 20.times do
85
+ 5.times do
83
86
  begin
84
87
  @log_file ||= File.open(BatchManager::Logger.log_file_path(@batch_name, @wet), 'r')
85
88
  return @log_file
@@ -1,8 +1,12 @@
1
1
  <div class="group title">
2
2
  <h2 style="float:left;">Batches</h2>
3
3
  <div style="float:right;text-align:right;">
4
- <% if @resque_supported %>
5
- <div class="label info">Resque worker found</div>
4
+ <% if resque_supported? %>
5
+ <% if local_resque_worker? %>
6
+ <div class="label info">Resque worker found</div>
7
+ <% else %>
8
+ <div class="label info">Resque worker found on <%= resque_worker_hostname %></div>
9
+ <% end %>
6
10
  <% else %>
7
11
  <div class="label warn">Resque or Resque worker not found</div>
8
12
  <% end %>
@@ -2,7 +2,7 @@
2
2
  <script>
3
3
  function read_log() {
4
4
  offset = $("#offset").val();
5
- $.get('<%= async_read_log_batch_url(escape_batch_name(@batch_name), :wet => @wet) %>' + '&offset=' + offset, function(data) {
5
+ $.get('<%= async_read_log_batch_url(escape_batch_name(@batch_name), :wet => @wet, 1 => 1) %>' + '&offset=' + offset, function(data) {
6
6
  $("#log_box").val($("#log_box").val() + data.content);
7
7
  $("#offset").val(data.offset);
8
8
  scrollLogBoxDown();
@@ -4,11 +4,12 @@ module BatchManager
4
4
 
5
5
  class << self
6
6
  def exec(batch_file, options = {})
7
- batch_file_path = batch_full_path(batch_file)
8
- if File.exist?(batch_file_path)
9
- batch_status = BatchManager::BatchStatus.new(batch_file_path)
10
- if options[:force] || !options[:wet] || batch_status.can_run?
11
- exec_batch_script(batch_file_path, batch_status, options[:wet])
7
+ @batch_file_path = batch_full_path(batch_file)
8
+ if File.exist?(@batch_file_path)
9
+ @batch_status = BatchManager::BatchStatus.new(@batch_file_path)
10
+ @wet = options[:wet]
11
+ if options[:force] || !@wet || @batch_status.can_run?
12
+ record_run_duration { exec_batch_script }
12
13
  else
13
14
  raise "Cannot run this batch."
14
15
  end
@@ -19,31 +20,32 @@ module BatchManager
19
20
 
20
21
  protected
21
22
 
22
- def exec_batch_script(batch_file_path, batch_status, is_wet)
23
- logger = BatchManager::Logger.new(batch_status.name, is_wet)
24
- write_log_header(is_wet)
23
+ def record_run_duration
24
+ @logger = BatchManager::Logger.new(@batch_status.name, @wet)
25
25
  start_at = Time.now
26
- begin
27
- @wet = is_wet
28
- eval(File.read(batch_file_path))
29
- end_at = Time.now
30
- logger.info "Completed at: #{end_at.strftime("%Y-%m-%d %H:%M:%S")} (#{(end_at - start_at).to_i}s)"
31
- batch_status.update_schema if is_wet
32
- rescue => e
33
- logger.error e
34
- end_at = Time.now
35
- logger.info "Failed at: #{end_at.strftime("%Y-%m-%d %H:%M:%S")} (#{(end_at - start_at).to_i}s)"
36
- ensure
37
- puts "Log saved at: #{BatchManager.logger.log_file}" if logger.log_file
38
- logger.close
39
- end
26
+ yield
27
+ end_at = Time.now
28
+ @logger.info "End at: #{end_at.strftime("%Y-%m-%d %H:%M:%S")} (#{(end_at - start_at).to_i}s)"
29
+ @logger.close
30
+ end
31
+
32
+ def exec_batch_script
33
+ write_log_header
34
+ eval(File.read(@batch_file_path))
35
+ @logger.info "Succeeded."
36
+ @batch_status.update_schema if @wet
37
+ rescue => e
38
+ @logger.error e
39
+ @logger.info "Failed."
40
+ ensure
41
+ puts "Log saved at: #{@logger.log_file}" if @logger.log_file
40
42
  end
41
43
 
42
- def write_log_header(is_wet)
43
- BatchManager.logger.info "=============================="
44
- BatchManager.logger.info "= #{is_wet ? 'WET' : 'DRY'} RUN"
45
- BatchManager.logger.info "= Ran at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
46
- BatchManager.logger.info "=============================="
44
+ def write_log_header
45
+ @logger.info "=============================="
46
+ @logger.info "= #{@wet ? 'WET' : 'DRY'} RUN"
47
+ @logger.info "= Ran at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
48
+ @logger.info "=============================="
47
49
  end
48
50
  end
49
51
  end
@@ -1,3 +1,3 @@
1
1
  module BatchManager
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen