batch_manager 0.2.5 → 0.2.6

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
- ZjVlODRhN2E4NTAyNGM5M2RlYTQzZjJlODdjZDQxNmUzNmJkYzU4MQ==
4
+ ZGRhOWQ1ZmJhMmU4Nzg3NzJhMmExMWM3ZTZhOWRmYjM0YmFkZTU4NA==
5
5
  data.tar.gz: !binary |-
6
- ZTNlNWRiYTM0MWU2ZjAwYzQyMDljYTBiNjFhMDQ3YWE5MWYwZjkxNg==
6
+ Y2U4MGYxY2MwYTI4OGMzNmJiNjJmZTE5Yjg5YjZjOWQzNDcwNjZjZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTVhYmM4ZDFkZmVlZjExMGFlNTM0MjQ3YzdmYmRmNDQ3YmZiNDI2ODM0NWY4
10
- MDM3ODk0ZTM0NTc0ZDZhZmM4ZjkyNjE4YmJhYTJkOTZkMDE2ZTQ4Njg2ZTA2
11
- MjM5NDgyZjFhMWI0OGQyZWZlMWVhNzNiNGQ1N2RmOGI1MGVhYTg=
9
+ Y2YyMDgyNWE3MmYzYTYxMzg3ZTA1Y2Y2NDRhN2M5MjNmMmE4YTE2YWVhYThi
10
+ OTI1MDVhNzc0OGNlNDk5OTA3MDBlZWJmOTgwNTllYjI3MDVkYjI4NzZhNjRl
11
+ OTBmZjc1YWIzMThiNGM0ZWMyNWZlYWNhYTk0ZDQxOWI2YWM3MWI=
12
12
  data.tar.gz: !binary |-
13
- YWQ3ODlkMDQzOWI3ZjkxZDA2ODZhMDVkODdiOTZiOWI4YTYzNWJjMDg5NTJk
14
- Mzg5Njg1Yjk2M2UwOTNmODcyMzI4ZmVlYjBiYjg1ZDFjOGRmNWM4ZjkwM2Ni
15
- NzM1ZjcyYmM2NDNkODAwNWZjNmEyM2Y1MzlmYjFiMDUxN2E5NTA=
13
+ MTYzNzhhNWM1ZWE2NTk0MDAwYjk2M2NjZDk2ZTFlMjJjODQ3ZmI3ZTdjNjRm
14
+ ZGRhNzEwMWVkODFhZmQzMWM1Y2U0NzRhZjE2ZTEzZDY1MjgxZGIyNDIxZjdl
15
+ YzViOWUzN2U2Y2U5MDJmNTZlZDI0MDlhNGFmNTc0MjkxM2E5MmY=
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # BatchManager
2
+
3
+ [![Build Status](https://travis-ci.org/cctiger36/batch_manager.png?branch=master)](https://travis-ci.org/cctiger36/batch_manager) [![Gem Version](https://badge.fury.io/rb/batch_manager.png)](http://badge.fury.io/rb/batch_manager) [![Coverage Status](https://coveralls.io/repos/cctiger36/batch_manager/badge.png?branch=master)](https://coveralls.io/r/cctiger36/batch_manager?branch=master) [![Code Climate](https://codeclimate.com/github/cctiger36/batch_manager.png)](https://codeclimate.com/github/cctiger36/batch_manager)
4
+
5
+ A rails plugin to manage batch scripts similar to migrations.
6
+
7
+ ## Installation
8
+
9
+ Add to your Gemfile
10
+
11
+ gem 'batch_manager'
12
+
13
+ and bundle
14
+
15
+ bundle
16
+
17
+ initialize
18
+
19
+ bundle exec rails generate batch_manager:migration
20
+ bundle exec rake db:migrate
21
+
22
+ This will create a table 'schema_batches' like 'schema_migrations' to save the status of batches in database.
23
+
24
+ ## Configuration
25
+
26
+ config.batch_manager.batch_dir = "script/batch"
27
+ config.batch_manager.save_log = true
28
+ config.batch_manager.log_dir = "log/batch"
29
+
30
+ You can change the default configuration in config/application.rb
31
+
32
+ ## Batch Generator
33
+
34
+ bundle exec rails g batch test
35
+
36
+ This will generate the file 'test.rb' in the configured batch_dir with default template.
37
+
38
+ You can find the template in lib/generators/batch/templates, and modify it for yourself.
39
+
40
+ ## Batch Header
41
+
42
+ The generated batch files will have the header like:
43
+
44
+ # =Batch Manager=
45
+ # =created_at: 2013-05-24 13:10:25
46
+ # =times_limit: 1
47
+ # =group_name: GroupName
48
+
49
+ Add the "=Batch Manager=" to tell BatchManager to manage this batch file.
50
+
51
+ You can also add these headers to the existing batch files.
52
+
53
+ ## Execute Batch
54
+
55
+ bundle exec bm_exec [options] BATCH_FILE
56
+
57
+ Please use this command instead of 'rails runner' to run batch scripts.
58
+
59
+ option:
60
+ -f, --force
61
+ -w, --wet
62
+
63
+ ## Logger
64
+
65
+ 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.
66
+
67
+ BatchManager.logger.debug
68
+ BatchManager.logger.info
69
+ BatchManager.logger.warn
70
+ BatchManager.logger.error
71
+ BatchManager.logger.fatal
72
+
73
+ When batches executed without BatchManager, it will write log to $stdout as default.
74
+
75
+ ## Web interface
76
+
77
+ Mount the web interface in the routes file.
78
+
79
+ mount BatchManager::Engine, :at => "batch_manager"
80
+
81
+ You can also use web interface to execute batches.
82
+
83
+ If [resque](https://github.com/resque/resque) installed in you application, the batch script can be executed asynchronous. And the log can be checked on real time in the brower.
84
+
85
+ ## Rake Tasks
86
+
87
+ show all batches
88
+
89
+ bundle exec rake batch:list
90
+
91
+ show the details of batches
92
+
93
+ bundle exec rake batch:details
@@ -51,3 +51,8 @@ tr.even.disable {
51
51
  tr.odd.disable {
52
52
  background-color:#bbbbaa;
53
53
  }
54
+
55
+ #log_box {
56
+ width: 100%;
57
+ height:500px;
58
+ }
@@ -5,13 +5,41 @@ module BatchManager
5
5
  end
6
6
 
7
7
  def exec
8
- BatchManager::Executor.exec(params[:batch_name], :wet => params[:wet])
9
- redirect_to(batches_url)
8
+ @batch_name = params[:batch_name]
9
+ @wet = params[:wet]
10
+ if resque_supported?
11
+ Resque.enqueue(BatchManager::ExecBatchWorker, @batch_name, :wet => @wet)
12
+ @offset = log_file.size
13
+ else
14
+ BatchManager::Executor.exec(@batch_name, :wet => @wet)
15
+ redirect_to(batches_url)
16
+ end
17
+ end
18
+
19
+ def async_read_log
20
+ file = log_file
21
+ file.seek(params[:offset].to_i) if params[:offset].present?
22
+ render :json => {:content => file.read, :offset => file.size}
10
23
  end
11
24
 
12
25
  def log
13
26
  log_file = BatchManager::Logger.log_file_path(params[:batch_name], params[:wet])
14
27
  render :file => log_file, :content_type => Mime::TEXT, :layout => false
15
28
  end
29
+
30
+ private
31
+
32
+ def resque_supported?
33
+ begin
34
+ require 'resque'
35
+ defined?(Resque)
36
+ rescue
37
+ false
38
+ end
39
+ end
40
+
41
+ def log_file
42
+ File.open(BatchManager::Logger.log_file_path(params[:batch_name], params[:wet]), 'r')
43
+ end
16
44
  end
17
45
  end
@@ -0,0 +1,22 @@
1
+ <script>
2
+ function read_log() {
3
+ offset = $("#offset").val();
4
+ $.get('<%= async_read_log_batches_url(:batch_name => @batch_name, :wet => @wet) %>' + '&offset=' + offset, function(data) {
5
+ $("#log_box").val($("#log_box").val() + data.content);
6
+ $("#offset").val(data.offset);
7
+ scrollLogBoxDown();
8
+ });
9
+ }
10
+ function scrollLogBoxDown() {
11
+ var logBox = $('#log_box');
12
+ logBox.scrollTop(logBox[0].scrollHeight - logBox.height());
13
+ }
14
+ $(function() {
15
+ setInterval(read_log, 1000);
16
+ });
17
+ </script>
18
+
19
+ <h2><%= @batch_name %> <%= "WET RUN" if @wet %> Executing!<h2>
20
+
21
+ <%= text_area_tag :log, nil, :id => "log_box" %>
22
+ <%= hidden_field_tag :offset, @offset, :id => :offset %>
@@ -0,0 +1,10 @@
1
+ module BatchManager
2
+ class ExecBatchWorker
3
+ @queue = "batch_manager"
4
+
5
+ def self.perform(batch_name, options = {})
6
+ BatchManager::Executor.exec(batch_name, :wet => options[:wet])
7
+ end
8
+
9
+ end
10
+ end
data/config/routes.rb CHANGED
@@ -6,6 +6,7 @@ BatchManager::Engine.routes.draw do
6
6
  collection do
7
7
  get :exec
8
8
  get :log
9
+ get :async_read_log
9
10
  end
10
11
  end
11
12
 
@@ -7,20 +7,8 @@ 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
- @wet = options[:wet]
11
- if !@wet || options[:force] || batch_status.can_run?
12
- logger = BatchManager::Logger.new(batch_status.name, @wet)
13
- write_log_header(@wet)
14
- begin
15
- eval(File.read(batch_file_path))
16
- batch_status.update_schema if @wet
17
- rescue => e
18
- logger.error e
19
- logger.error "Failed."
20
- ensure
21
- puts "Log saved at: #{BatchManager.logger.log_file}" if logger.log_file
22
- logger.close
23
- end
10
+ if options[:force] || !options[:wet] || batch_status.can_run?
11
+ exec_batch_script(batch_file_path, batch_status, options[:wet])
24
12
  else
25
13
  raise "Cannot run this batch."
26
14
  end
@@ -29,6 +17,24 @@ module BatchManager
29
17
  end
30
18
  end
31
19
 
20
+ protected
21
+
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)
25
+ begin
26
+ @wet = is_wet
27
+ eval(File.read(batch_file_path))
28
+ batch_status.update_schema if is_wet
29
+ rescue => e
30
+ logger.error e
31
+ logger.error "Failed."
32
+ ensure
33
+ puts "Log saved at: #{BatchManager.logger.log_file}" if logger.log_file
34
+ logger.close
35
+ end
36
+ end
37
+
32
38
  def write_log_header(is_wet)
33
39
  BatchManager.logger.info "=============================="
34
40
  BatchManager.logger.info "= #{is_wet ? 'WET' : 'DRY'} RUN"
@@ -1,3 +1,3 @@
1
1
  module BatchManager
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-06 00:00:00.000000000 Z
11
+ date: 2013-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: 3.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: 3.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: log4r
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +94,22 @@ dependencies:
94
94
  - - ! '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: A rails plugin to manage batch scripts similar to migrations.
97
+ - !ruby/object:Gem::Dependency
98
+ name: resque
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Manage batches status. Support execute batches from web interface. Automatically
112
+ save the log to file.
98
113
  email:
99
114
  - cctiger36@gmail.com
100
115
  executables:
@@ -107,8 +122,10 @@ files:
107
122
  - app/assets/stylesheets/batch_manager/batches.css
108
123
  - app/controllers/batch_manager/application_controller.rb
109
124
  - app/controllers/batch_manager/batches_controller.rb
125
+ - app/views/batch_manager/batches/exec.html.erb
110
126
  - app/views/batch_manager/batches/index.html.erb
111
127
  - app/views/layouts/batch_manager/application.html.erb
128
+ - app/workers/batch_manager/exec_batch_worker.rb
112
129
  - config/routes.rb
113
130
  - bin/bm_exec
114
131
  - lib/batch_manager/batch_status.rb
@@ -127,9 +144,10 @@ files:
127
144
  - lib/tasks/batch_manager_tasks.rake
128
145
  - MIT-LICENSE
129
146
  - Rakefile
130
- - README.rdoc
147
+ - README.md
131
148
  homepage: https://github.com/cctiger36/batch_manager
132
- licenses: []
149
+ licenses:
150
+ - MIT
133
151
  metadata: {}
134
152
  post_install_message:
135
153
  rdoc_options: []
data/README.rdoc DELETED
@@ -1,89 +0,0 @@
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
-
3
- A rails plugin to manage batch scripts similar to migrations.
4
-
5
- == Installation
6
-
7
- Add to your Gemfile
8
-
9
- gem 'batch_manager'
10
-
11
- and bundle
12
-
13
- bundle
14
-
15
- initialize
16
-
17
- bundle exec rails generate batch_manager:migration
18
- bundle exec rake db:migrate
19
-
20
- This will create a table 'schema_batches' like 'schema_migrations' to save the status of batches in database.
21
-
22
- == Configuration
23
-
24
- config.batch_manager.batch_dir = "script/batch"
25
- config.batch_manager.save_log = true
26
- config.batch_manager.log_dir = "log/batch"
27
-
28
- You can change the default configuration in config/application.rb
29
-
30
- == Batch Generator
31
-
32
- bundle exec rails g batch test
33
-
34
- This will generate the file 'test.rb' in the configured batch_dir with default template.
35
-
36
- You can find the template in lib/generators/batch/templates, and modify it for yourself.
37
-
38
- == Batch Header
39
-
40
- The generated batch files will have the header like:
41
-
42
- # =Batch Manager=
43
- # =created_at: 2013-05-24 13:10:25
44
- # =times_limit: 1
45
- # =group_name: GroupName
46
-
47
- Add the "=Batch Manager=" to tell BatchManager to manage this batch file.
48
-
49
- You can also add these headers to the existing batch files.
50
-
51
- == Execute Batch
52
-
53
- bundle exec bm_exec [options] BATCH_FILE
54
-
55
- Please use this command instead of 'rails runner' to run batch scripts.
56
-
57
- option:
58
- -f, --force
59
- -w, --wet
60
-
61
- == Logger
62
-
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.
64
-
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
- Mount the web interface in the routes file.
76
-
77
- mount BatchManager::Engine, :at => "batch_manager"
78
-
79
- You can also use web interface to execute batches.
80
-
81
- == Rake Tasks
82
-
83
- show all batches
84
-
85
- bundle exec rake batch:list
86
-
87
- show the details of batches
88
-
89
- bundle exec rake batch:details