batch_manager 0.1.4 → 0.2.0

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
- ZmUxYjIzODRlMmUzZWViNmYyNGZmMjdjMjZlMjZmOGRlYjliMjY1MA==
4
+ MTc0ZWI3YzZkZWQ0Nzk3ZDMzMmYzNDFhNjNkMWIxZjgyM2JhN2YxNw==
5
5
  data.tar.gz: !binary |-
6
- YmM2NWMzY2RiN2IyNjRkNTY3ZTg0NDE4ZDExYWQ4Mzc2MzMyYTVjMQ==
6
+ MTU5MTVlMTQ5OTMwODM4ZWJlYTk5OWZlMzM3NGQxN2QzOTRhNDUyNg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- N2MxMTUyNWJjYjZmYjUzOTRiZTY2MDkxNjEyNzFkODRhM2Q3ZDNlMzU4MTAz
10
- ZTRjODMxYTkyOTljMWZlYTA3ZjYwMGUyZWFhZGIwNTIyZjA2MGVlMzg4OTJi
11
- ZGRkZjQwNjdlODE0NDgxM2VhZjJjZDQ4ZDUwNTM5ZmMyZjY0YWU=
9
+ MTcyZTg1ZGFhMTIyODFlY2M5MzdiYmYxMmVlYjYyMDViMWI2MjY2NmJkYzli
10
+ ZDI1NzlkNDIwMGNjNjcxMmI1M2EyYmFmZjUzOTgzNzgwM2VlMmRmNDY0NTg1
11
+ ZmZlYmFlNmFkODlmZjdmZjE2NWVkOWQxNTZiZGU3ODU2NzIwY2E=
12
12
  data.tar.gz: !binary |-
13
- ZjkzNGVlMWVkOGNmYzYzNjExNmVmMTRkNGI5Y2EwMmMyM2M1MDBmNmY2MTg1
14
- NTdlNzA2ODQ5YjE3NzIzNTBmNjQ4OGQ1Mjc1MjkxMTUyNjA5MjYzMjViOGM2
15
- YTU2MmQ5ZjI5YjliYzBkMWQ4ZjU1NzEzNWVmMGJlZDZlMmVhZjA=
13
+ NTU3NjA5M2E3OWRhMTk1ZGU2NTcwMWVjM2NjYjhlYjYxYWYxMDdiMWNkMjBh
14
+ MTk3ZWE3ODg2M2Q0ZDc2YmYwNGY2ZjkxN2ExOGQyNDAwYWQwNjdlM2ZjMDJl
15
+ OTUxYzg5Yjk3OThlNzlkOWUxMjBmNjNiNDMyMjI3NGI4MjMwNTY=
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = BatchManager {<img src="https://travis-ci.org/cctiger36/batch_manager.png?branch=master" alt="Build Status" />}[https://travis-ci.org/cctiger36/batch_manager] {<img src="https://badge.fury.io/rb/batch_manager.png" alt="Gem Version" />}[http://badge.fury.io/rb/batch_manager]
1
+ = BatchManager {<img src="https://travis-ci.org/cctiger36/batch_manager.png?branch=master" alt="Build Status" />}[https://travis-ci.org/cctiger36/batch_manager] {<img src="https://badge.fury.io/rb/batch_manager.png" alt="Gem Version" />}[http://badge.fury.io/rb/batch_manager] {<img src="https://coveralls.io/repos/cctiger36/batch_manager/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/cctiger36/batch_manager?branch=master]
2
2
 
3
3
  A rails plugin to manage batch scripts similar to migrations.
4
4
 
@@ -42,7 +42,6 @@ The generated batch files will have the header like:
42
42
  # =Batch Manager=
43
43
  # =created_at: 2013-05-24 13:10:25
44
44
  # =times_limit: 1
45
- # =auto_run: false
46
45
  # =group_name: GroupName
47
46
 
48
47
  Add the "=Batch Manager=" to tell BatchManager to manage this batch file.
@@ -61,13 +60,19 @@ Please use this command instead of 'rails runner' to run batch scripts.
61
60
 
62
61
  == Logger
63
62
 
64
- Please use Rails.logger in batch scripts to output logs, and then BatchManager will automatically also output logs to the file in the configured log directory.
63
+ Please use BatchManager.logger in batch scripts to output logs, and then BatchManager will automatically also output logs to the file in the configured log directory.
65
64
 
66
- Rails.logger.debug
67
- Rails.logger.info
68
- Rails.logger.warn
69
- Rails.logger.error
70
- Rails.logger.fatal
65
+ BatchManager.logger.debug
66
+ BatchManager.logger.info
67
+ BatchManager.logger.warn
68
+ BatchManager.logger.error
69
+ BatchManager.logger.fatal
70
+
71
+ When batches executed without BatchManager, it will write log to $stdout as default.
72
+
73
+ == Web interface
74
+
75
+ The web interface will at http://localhost:3000/batch_manager, you can also execute batches there.
71
76
 
72
77
  == Rake Tasks
73
78
 
@@ -78,9 +83,3 @@ show all batches
78
83
  show the details of batches
79
84
 
80
85
  bundle exec rake batch:details
81
-
82
- == TODO
83
-
84
- Save log automatically.
85
- Rake tasks to check the status.
86
- Admin interface.
data/bin/bm_exec CHANGED
@@ -16,8 +16,8 @@ end
16
16
  ARGV.delete(batch_file)
17
17
 
18
18
  APP_PATH = File.join('.', 'config', 'application')
19
- ENV["RAILS_ENV"] = options[:environment] || "development"
20
19
  require APP_PATH
20
+ #ENV["RAILS_ENV"] = options[:environment] || "development"
21
21
  Rails.application.require_environment!
22
22
 
23
23
  BatchManager::Executor.exec(batch_file, options)
@@ -1,7 +1,7 @@
1
1
  module BatchManager
2
2
  class BatchStatus
3
3
  include BatchManager::Utils
4
- attr_accessor :name, :created_at, :times_limit, :auto_run, :group_name, :managed
4
+ attr_accessor :name, :created_at, :times_limit, :group_name, :managed
5
5
 
6
6
  def initialize(path)
7
7
  @name = batch_name(path)
@@ -46,8 +46,6 @@ module BatchManager
46
46
  @times_limit = line.sub(/#\s*=times_limit:/, "").strip.to_i
47
47
  elsif line.include?("=created_at:")
48
48
  @created_at = Time.parse(line.sub(/#\s*=created_at:/, "").strip)
49
- elsif line.include?("=auto_run:")
50
- @auto_run = line.sub(/#\s*=auto_run:/, "").strip.downcase == "true"
51
49
  elsif line.include?("=group_name:")
52
50
  @group_name = line.sub(/#\s*=group_name:/, "").strip
53
51
  end
@@ -1,7 +1,5 @@
1
- require 'rails'
2
-
3
1
  module BatchManager
4
- class Railtie < ::Rails::Railtie
2
+ class Engine < ::Rails::Engine
5
3
  rake_tasks do
6
4
  Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |f| load f }
7
5
  end
@@ -7,13 +7,20 @@ module BatchManager
7
7
  batch_file_path = batch_full_path(batch_file)
8
8
  if File.exist?(batch_file_path)
9
9
  batch_status = BatchManager::BatchStatus.new(batch_file_path)
10
- if options[:force] || batch_status.can_run?
11
- @wet = options[:wet]
12
- BatchManager::Logger.new(batch_status.name, @wet)
10
+ @wet = options[:wet]
11
+ if !@wet || options[:force] || batch_status.can_run?
12
+ logger = BatchManager::Logger.new(batch_status.name, @wet)
13
13
  write_log_header(@wet)
14
- eval(File.read(batch_file_path))
15
- batch_status.update_schema
16
- puts "Log saved at: #{Rails.logger.log_file}" if Rails.logger.log_file
14
+ begin
15
+ eval(File.read(batch_file_path))
16
+ batch_status.update_schema if @wet
17
+ rescue => e
18
+ logger.error e
19
+ raise e
20
+ ensure
21
+ puts "Log saved at: #{BatchManager.logger.log_file}" if logger.log_file
22
+ logger.close
23
+ end
17
24
  else
18
25
  raise "Cannot run this batch."
19
26
  end
@@ -23,10 +30,10 @@ module BatchManager
23
30
  end
24
31
 
25
32
  def write_log_header(is_wet)
26
- Rails.logger.info "=============================="
27
- Rails.logger.info "= #{is_wet ? 'WET' : 'DRY'} RUN"
28
- Rails.logger.info "= Ran at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
29
- Rails.logger.info "=============================="
33
+ BatchManager.logger.info "=============================="
34
+ BatchManager.logger.info "= #{is_wet ? 'WET' : 'DRY'} RUN"
35
+ BatchManager.logger.info "= Ran at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
36
+ BatchManager.logger.info "=============================="
30
37
  end
31
38
  end
32
39
  end
@@ -6,21 +6,31 @@ module BatchManager
6
6
  attr_reader :log_file
7
7
  delegate :debug, :info, :warn, :error, :fatal, :add, :to => :logger
8
8
 
9
+ class << self
10
+ def log_file_path(batch_name, is_wet = false)
11
+ log_file_name = is_wet ? "#{batch_name}_wet" : batch_name
12
+ File.join(BatchManager.log_dir, log_file_name) + ".log"
13
+ end
14
+ end
15
+
9
16
  def initialize(batch_name, is_wet)
10
17
  @logger = Log4r::Logger.new(batch_name)
11
18
  @logger.outputters << Log4r::Outputter.stdout
12
19
  if BatchManager.save_log?
13
- log_file_name = is_wet ? "#{batch_name}_wet" : batch_name
14
- @log_file = log_file_path(log_file_name)
15
- @logger.outputters << Log4r::FileOutputter.new(log_file_name, :filename => @log_file)
20
+ @log_file = prepare_log_file(batch_name, is_wet)
21
+ @logger.outputters << Log4r::FileOutputter.new(File.basename(@log_file, ".log"), :filename => @log_file)
16
22
  end
17
- Rails.logger = self
23
+ BatchManager.logger = self
18
24
  end
19
25
 
20
- def log_file_path(file_name)
21
- file_path = File.join(BatchManager.log_dir, file_name) + ".log"
26
+ def prepare_log_file(batch_name, is_wet)
27
+ file_path = self.class.log_file_path(batch_name, is_wet)
22
28
  FileUtils.mkdir_p(File.dirname(file_path)) unless File.exist?(file_path)
23
29
  file_path
24
30
  end
31
+
32
+ def close
33
+ BatchManager.logger = nil
34
+ end
25
35
  end
26
36
  end
@@ -1,3 +1,3 @@
1
1
  module BatchManager
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/batch_manager.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require 'rails'
1
2
  require 'active_record'
2
- require 'batch_manager/railtie'
3
+ require 'batch_manager/engine'
3
4
 
4
5
  module BatchManager
6
+ mattr_accessor :logger
7
+
5
8
  class << self
6
9
  def batch_dir
7
10
  Rails.application.config.batch_manager.batch_dir
@@ -20,6 +23,10 @@ module BatchManager
20
23
  def signal
21
24
  "=Batch Manager="
22
25
  end
26
+
27
+ def logger
28
+ @@logger ||= ::Logger.new(STDOUT)
29
+ end
23
30
  end
24
31
  end
25
32
 
@@ -2,7 +2,6 @@
2
2
  # =Batch Manager=
3
3
  # =created_at: <%= Time.now.strftime "%Y-%m-%d %H:%M:%S" %>
4
4
  # =times_limit: 1
5
- # =auto_run: false
6
5
 
7
6
  wet_run = (ARGV[0] == "wet" || @wet)
8
7
 
@@ -11,9 +10,9 @@ ActiveRecord::Base.connection.with_standby do
11
10
  ActiveRecord::Base.transaction do
12
11
 
13
12
  if wet_run
14
- Rails.logger.info "Wet run completed!"
13
+ BatchManager.logger.info "Wet run completed!"
15
14
  else
16
- Rails.logger.warn "Rolling back."
15
+ BatchManager.logger.warn "Rolling back."
17
16
  raise ActiveRecord::Rollback
18
17
  end
19
18
  end
@@ -21,7 +21,7 @@ namespace :batch do
21
21
  else
22
22
  str << "%25s" % ""
23
23
  end
24
- str << "%25s" % status.created_at.strftime('%Y-%m-%d %H:%M:%S')
24
+ str << "%25s" % status.created_at.strftime('%Y-%m-%d %H:%M:%S') if status.created_at
25
25
  puts str
26
26
  end
27
27
  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.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen
@@ -104,10 +104,10 @@ extra_rdoc_files: []
104
104
  files:
105
105
  - bin/bm_exec
106
106
  - lib/batch_manager/batch_status.rb
107
+ - lib/batch_manager/engine.rb
107
108
  - lib/batch_manager/executor.rb
108
109
  - lib/batch_manager/logger.rb
109
110
  - lib/batch_manager/monitor.rb
110
- - lib/batch_manager/railtie.rb
111
111
  - lib/batch_manager/schema_batch.rb
112
112
  - lib/batch_manager/utils.rb
113
113
  - lib/batch_manager/version.rb