batch_manager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzZiMGY4MzliOTI3MTU4MGJlZjU0NDc1MTliMjJmMTE5ZTU1MzgxNA==
5
+ data.tar.gz: !binary |-
6
+ MmMzMjdlNzZhNTI5ZTE0MTA1ODY1NGYwMzc3NDljMThlZTJhMTFiOA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDY0ZTE3MmI4NWIyNzZkZmZiYTlkNmM5YmFjY2JjM2Y3N2VhNmE3MjAwNDcy
10
+ ZTE5MGMwZjdiNWU4YjQyMjMwYmM2ZjMyMGViM2M4YzMyN2FkYmNmY2VkNzNk
11
+ YmQyNTAzM2U0NDg3NGEyMWIxZWIyMmNlNTZhNmM4ZDcwMzBjZDQ=
12
+ data.tar.gz: !binary |-
13
+ NTA1ODk5YjczMTdjNDlmNWM5NzQ3NjVhM2RlNDI2YjRhZmVkMjcyNTA1M2Ey
14
+ ZDAwNTZkNjBkMDRiNzUwNDQ5ZGMwY2VmNWU5YWFmOWU0ZmQyZjkzODlmZDk4
15
+ ZjIxODE0ZmE5NjM3MzVlMWVhOGE1M2ZkNmY3MjFjZmYxOGVlNTQ=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = BatchManager
2
+
3
+ A rails plugin to manage batch scripts similar to migrations.
4
+
5
+ == Installation
6
+
7
+ bundle exec rails generate batch_manager:migration
8
+ bundle exec rake db:migrate
9
+
10
+ This will create a table 'schema_batches' like 'schema_migrations' to save the status of batches in database.
11
+
12
+ == Configuration
13
+
14
+ config.batch_manager.batch_dir = "script/batch"
15
+ # TODO: config.batch_manager.save_log = true
16
+ # TODO: config.batch_manager.log_dir = "log/batch"
17
+
18
+ You can change the default configuration in config/application.rb
19
+
20
+ == Batch Generator
21
+
22
+ bundle exec rails g batch test
23
+
24
+ This will generate the file 'test.rb' in the configured batch_dir with default template.
25
+
26
+ You can look up the template in lib/generators/batch/templates, and modify it for yourself.
27
+
28
+ == Batch Header
29
+
30
+ The generated batch files will have the header like:
31
+
32
+ # =Batch Manager=
33
+ # =created_at: <%= Time.now.strftime "%Y-%m-%d %H:%M:%S" %>
34
+ # =times_limit: 1
35
+ # =auto_run: false
36
+ # =group_name: GroupName
37
+
38
+ Add the "=Batch Manager=" to tell BatchManager to manage this batch file.
39
+
40
+ You can also add these headers to the existing batch files.
41
+
42
+ == Execute Batch
43
+
44
+ bundle exec bm_exec [options] BATCH_FILE
45
+
46
+ Please use this command instead of 'rails runner' to run batch scripts.
47
+
48
+ option:
49
+ -f, --force
50
+ -w, --wet
51
+
52
+ == Rake Tasks
53
+
54
+ # TODO: rake batch:list
55
+
56
+ == TODO
57
+
58
+ Save log automatically.
59
+ Rake tasks to check the status.
60
+ Admin interface.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ desc 'Default: run specs'
5
+ task :default => :spec
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = "spec/**/*_spec.rb"
10
+ end
11
+
12
+ Bundler::GemHelper.install_tasks
data/bin/bm_exec ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'optparse'
6
+ require 'batch_manager'
7
+
8
+ options = {}
9
+ batch_file = nil
10
+ ARGV.clone.options do |opts|
11
+ opts.banner = "Usage: batch [options] file_name"
12
+ opts.on("-f", "--force", "Force to run") { options[:force] = true }
13
+ opts.on("-w", "--wet", "Wet run") { options[:wet] = true }
14
+ opts.order! { |o| batch_file ||= o } rescue retry
15
+ end
16
+ ARGV.delete(batch_file)
17
+
18
+ APP_PATH = File.join('.', 'config', 'application')
19
+ ENV["RAILS_ENV"] = options[:environment] || "development"
20
+ require APP_PATH
21
+ Rails.application.require_environment!
22
+
23
+ BatchManager::Executor.exec(batch_file, options)
@@ -0,0 +1,62 @@
1
+ module BatchManager
2
+ class BatchStatus
3
+ attr_accessor :name, :created_at, :times_limit, :auto_run, :group_name, :path, :managed
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ @name = File.basename(path, ".rb")
8
+ File.open path do |f|
9
+ f.each_line do |line|
10
+ parse_batch_content line
11
+ end
12
+ end
13
+ end
14
+
15
+ def schema_batch
16
+ BatchManager::SchemaBatch.find_by_name(@name) if @name
17
+ end
18
+
19
+ def update_schema
20
+ if managed?
21
+ if schema_batch
22
+ schema_batch.increment!(:ran_times)
23
+ else
24
+ BatchManager::SchemaBatch.create! do |s|
25
+ s.name = @name
26
+ s.ran_times = 1
27
+ s.last_ran_at = Time.now
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def to_s
34
+ # TODO
35
+ end
36
+
37
+ def managed?
38
+ @managed
39
+ end
40
+
41
+ def can_run?
42
+ @times_limit.to_i <= 0 || @times_limit > schema_batch.try(:ran_times).to_i
43
+ end
44
+
45
+ def parse_batch_content(line)
46
+ if line.start_with?("#")
47
+ @managed = true if line.include?(BatchManager.signal)
48
+ if managed?
49
+ if line.include?("=times_limit:")
50
+ @times_limit = line.sub(/#\s*=times_limit:/, "").strip.to_i
51
+ elsif line.include?("=created_at:")
52
+ @created_at = Time.parse(line.sub(/#\s*=created_at:/, "").strip)
53
+ elsif line.include?("=auto_run:")
54
+ @auto_run = line.sub(/#\s*=auto_run:/, "").strip.downcase == "true"
55
+ elsif line.include?("=group_name:")
56
+ @group_name = line.sub(/#\s*=group_name:/, "").strip
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,21 @@
1
+ module BatchManager
2
+ class Executor
3
+ include BatchManager::Utils
4
+
5
+ def self.exec(batch_file, options)
6
+ batch_file_path = batch_path(batch_file)
7
+ if File.exist?(batch_file_path)
8
+ batch_status = BatchManager::BatchStatus.new(batch_file_path)
9
+ if options[:force] || batch_status.can_run?
10
+ @wet = options[:wet]
11
+ eval File.read(batch_file_path)
12
+ batch_status.update_schema
13
+ else
14
+ raise "Cannot run this batch."
15
+ end
16
+ else
17
+ raise "File not exist."
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module BatchManager
2
+ class Monitor
3
+ include BatchManager::Utils
4
+
5
+ class << self
6
+ def list
7
+ # TODO
8
+ end
9
+
10
+ def status(file_name)
11
+ # TODO
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module BatchManager
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
7
+ end
8
+
9
+ config.batch_manager = ActiveSupport::OrderedOptions.new
10
+ config.batch_manager.batch_dir = "script/batch"
11
+ config.batch_manager.save_log = true
12
+ config.batch_manager.log_dir = "log/batch"
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module BatchManager
2
+ class SchemaBatch < ::ActiveRecord::Base
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ module BatchManager
2
+ module Utils
3
+ def self.included(base)
4
+ base.send :include, OverallMethods
5
+ base.extend OverallMethods
6
+ end
7
+
8
+ module OverallMethods
9
+ def batch_path(file)
10
+ if file.start_with?(::BatchManager.batch_dir)
11
+ file + ".rb"
12
+ else
13
+ File.join(::BatchManager.batch_dir, file) + ".rb"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module BatchManager
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_record'
2
+ require 'batch_manager/railtie'
3
+
4
+ module BatchManager
5
+ class << self
6
+ def batch_dir
7
+ Rails.application.config.batch_manager.batch_dir
8
+ end
9
+
10
+ def signal
11
+ "=Batch Manager="
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'batch_manager/utils'
17
+ require 'batch_manager/schema_batch'
18
+ require 'batch_manager/batch_status'
19
+ require 'batch_manager/executor'
20
+ require 'batch_manager/monitor'
@@ -0,0 +1,20 @@
1
+ module Rails
2
+ module Generators
3
+ class BatchGenerator < NamedBase
4
+
5
+ desc "Generates batch file"
6
+
7
+ def self.orm
8
+ Rails::Generators.options[:rails][:orm]
9
+ end
10
+
11
+ def self.source_root
12
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
13
+ end
14
+
15
+ def create_batch_file
16
+ template 'batch.rb', "#{BatchManager.batch_dir}/#{file_name}.rb"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ # =Batch Manager=
3
+ # =created_at: <%= Time.now.strftime "%Y-%m-%d %H:%M:%S" %>
4
+ # =times_limit: 1
5
+ # =auto_run: false
6
+
7
+ wet_run = (ARGV[0] == "wet" || @wet)
8
+
9
+ ActiveRecord::Base.connection.with_standby do
10
+
11
+ ActiveRecord::Base.transaction do
12
+
13
+ if wet_run
14
+ p "Wet run completed!"
15
+ else
16
+ p "Rolling back."
17
+ raise ActiveRecord::Rollback
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,38 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module BatchManager
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc "Generates migration for SchemaBatch models"
9
+
10
+ def self.orm
11
+ Rails::Generators.options[:rails][:orm]
12
+ end
13
+
14
+ def self.source_root
15
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
16
+ end
17
+
18
+ def self.orm_has_migration?
19
+ [:active_record].include? orm
20
+ end
21
+
22
+ def self.next_migration_number(dirname)
23
+ if ActiveRecord::Base.timestamped_migrations
24
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
25
+ migration_number += 1
26
+ migration_number.to_s
27
+ else
28
+ "%.3d" % (current_migration_number(dirname) + 1)
29
+ end
30
+ end
31
+
32
+ def create_migration_file
33
+ if self.class.orm_has_migration?
34
+ migration_template 'migration.rb', 'db/migrate/batch_manager_migration'
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ class BatchManagerMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :schema_batches do |t|
4
+ t.string :name
5
+ t.integer :ran_times, :default => 0
6
+ t.string :last_ran_at
7
+ end
8
+ add_index :schema_batches, :name
9
+ end
10
+
11
+ def self.down
12
+ drop_table :schema_batches
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ desc "Execute batch"
2
+ namespace :batch do
3
+ task :list do
4
+ # TODO
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batch_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Weihu Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.13
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_girl_rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ammeter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A rails plugin to manage batch scripts similar to migrations.
84
+ email:
85
+ - cctiger36@gmail.com
86
+ executables:
87
+ - bm_exec
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/bm_exec
92
+ - lib/batch_manager/batch_status.rb
93
+ - lib/batch_manager/executor.rb
94
+ - lib/batch_manager/monitor.rb
95
+ - lib/batch_manager/railtie.rb
96
+ - lib/batch_manager/schema_batch.rb
97
+ - lib/batch_manager/utils.rb
98
+ - lib/batch_manager/version.rb
99
+ - lib/batch_manager.rb
100
+ - lib/generators/batch/batch_generator.rb
101
+ - lib/generators/batch/templates/active_record/batch.rb
102
+ - lib/generators/batch_manager/migration/migration_generator.rb
103
+ - lib/generators/batch_manager/migration/templates/active_record/migration.rb
104
+ - lib/tasks/batch_manager_tasks.rake
105
+ - MIT-LICENSE
106
+ - Rakefile
107
+ - README.rdoc
108
+ homepage: https://github.com/cctiger36/batch_manager
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A rails plugin to manage batch scripts similar to migrations.
131
+ test_files: []