batch_manager 0.3.9 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f126c36d799f8c8ecf3e7f5d6d2f516039a6ef9
4
- data.tar.gz: 24ec977d830ca47e3ba5b742dfd7d03c1110f1b3
3
+ metadata.gz: 6415687d75bbd24e14c61db0eb8458876583c8cd
4
+ data.tar.gz: 9bdbb1e0d415f6f04c479db2ef9ba03e8695ace3
5
5
  SHA512:
6
- metadata.gz: d6ed61ecfe20421b6c14dc831fc55cdc9c9c812336afc89e2ef301f573fe34406a1c241209fe0c5c55f1da0d4b49533e4fc8553f4b920dddbfc0d72258046c92
7
- data.tar.gz: 2b1e126e9575b5965bc713b635cfded66c6f03dfdf2c0cbce067f1726fda35a52ad91b50cdf9c2acbde3f183f7c5e8fa32448ce1aa4dc4c89cff84ca55f6ef48
6
+ metadata.gz: 48b4a7f60176ac7f2fb2b37ed6394a00e82e05ea3281ef4f4e4d084bf97fdef2ad051934abc8f9acfdf968cd3caffb63f6b8880a214c83e2cbd3ab7ce987934b
7
+ data.tar.gz: e02cd3b2a8f0bf9748d690e9481d4ccef257a6d692c68de207d49a4aa64c666f0c187c2a069ca0e38fbe87eb75e6f63e6f1b471fb6712d926f97187b357441f0
data/README.md CHANGED
@@ -57,9 +57,20 @@ You can also add these headers to the existing batch files.
57
57
 
58
58
  Please use this command instead of 'rails runner' to run batch scripts.
59
59
 
60
- option:
61
- -f, --force
62
- -w, --wet
60
+ options:
61
+ -f, --force Force to run
62
+ -w, --wet Wet run
63
+ -d, --daemon COMMAND Daemon mode
64
+
65
+ ### Daemon mode
66
+
67
+ Use `-d` option to run batch as daemon.
68
+
69
+ # start daemon
70
+ bundle exec bm_exec -d start BATCH_FILE
71
+
72
+ # stop daemon
73
+ bundle exec bm_exec -d stop BATCH_FILE
63
74
 
64
75
  ## Logger
65
76
 
data/bin/bm_exec CHANGED
@@ -8,16 +8,20 @@ require 'batch_manager'
8
8
  options = {}
9
9
  batch_file = nil
10
10
  ARGV.clone.options do |opts|
11
- opts.banner = "Usage: batch [options] BATCH_FILE"
11
+ opts.banner = "Usage: bm_exec [options] BATCH_FILE"
12
12
  opts.on("-f", "--force", "Force to run") { options[:force] = true }
13
13
  opts.on("-w", "--wet", "Wet run") { options[:wet] = true }
14
+ opts.on("-d", "--daemon COMMAND", "Daemon mode") { |cmd| options[:daemon] = cmd }
14
15
  opts.order! { |o| batch_file ||= o } rescue retry
15
16
  end
16
17
  ARGV.delete(batch_file)
17
18
 
18
19
  APP_PATH = File.join('.', 'config', 'application')
19
20
  require APP_PATH
20
- #ENV["RAILS_ENV"] = options[:environment] || "development"
21
21
  Rails.application.require_environment!
22
22
 
23
- BatchManager::Executor.exec(batch_file, options)
23
+ if options[:daemon]
24
+ BatchManager::Daemon.spawn(options[:daemon], batch_file, options)
25
+ else
26
+ BatchManager::Executor.exec(batch_file, options)
27
+ end
@@ -0,0 +1,34 @@
1
+ require "daemon_spawn"
2
+
3
+ module BatchManager
4
+ class Daemon
5
+ include BatchManager::Utils
6
+
7
+ class << self
8
+ def spawn(command, batch_file, options = {})
9
+ daemon_options = {
10
+ working_dir: Rails.root.to_s,
11
+ pid_file: pid_file_path(batch_name(batch_file), options[:wet]),
12
+ log_file: BatchManager::Logger.log_file_path(batch_name(batch_file), options[:wet]),
13
+ sync_log: true
14
+ }
15
+ FileUtils.mkdir_p(File.dirname(daemon_options[:pid_file]), :mode => 0775)
16
+ Spawn.spawn!(daemon_options, [command, batch_file, options])
17
+ end
18
+
19
+ def pid_file_path(batch_name, is_wet = false)
20
+ pid_file_name = is_wet ? "#{batch_name}_wet" : batch_name
21
+ File.join(BatchManager.log_dir, pid_file_name) + ".pid"
22
+ end
23
+ end
24
+
25
+ class Spawn < ::DaemonSpawn::Base
26
+ def start(args)
27
+ BatchManager::Executor.exec(args[0], args[1])
28
+ end
29
+
30
+ def stop
31
+ end
32
+ end
33
+ end
34
+ end
@@ -8,8 +8,9 @@ module BatchManager
8
8
  if File.exist?(@batch_file_path)
9
9
  @batch_status = BatchManager::BatchStatus.new(@batch_file_path)
10
10
  @wet = options[:wet]
11
+ @disable_stdout = options[:daemon]
11
12
  if options[:force] || !@wet || @batch_status.can_run?
12
- logging_run_duration { exec_batch_script }
13
+ log_run_duration { exec_batch_script }
13
14
  else
14
15
  raise "Cannot run this batch."
15
16
  end
@@ -20,8 +21,8 @@ module BatchManager
20
21
 
21
22
  protected
22
23
 
23
- def logging_run_duration
24
- @logger = BatchManager::Logger.new(@batch_status.name, @wet)
24
+ def log_run_duration
25
+ @logger = BatchManager::Logger.new(@batch_status.name, @wet, @disable_stdout)
25
26
  start_at = Time.now
26
27
  yield
27
28
  end_at = Time.now
@@ -13,9 +13,9 @@ module BatchManager
13
13
  end
14
14
  end
15
15
 
16
- def initialize(batch_name, is_wet)
16
+ def initialize(batch_name, is_wet, disable_stdout = false)
17
17
  @logger = Log4r::Logger.new(batch_name)
18
- @logger.outputters << Log4r::Outputter.stdout
18
+ @logger.outputters << Log4r::Outputter.stdout unless disable_stdout
19
19
  if BatchManager.save_log?
20
20
  @log_file = prepare_log_file(batch_name, is_wet)
21
21
  @logger.outputters << Log4r::FileOutputter.new(File.basename(@log_file, ".log"), :filename => @log_file)
@@ -1,3 +1,3 @@
1
1
  module BatchManager
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/batch_manager.rb CHANGED
@@ -37,3 +37,4 @@ require 'batch_manager/batch_status'
37
37
  require 'batch_manager/executor'
38
38
  require 'batch_manager/monitor'
39
39
  require "batch_manager/logger"
40
+ require "batch_manager/daemon"
@@ -3,13 +3,12 @@
3
3
  # =created_at: <%= Time.now.strftime "%Y-%m-%d %H:%M:%S" %>
4
4
  # =times_limit: 1
5
5
 
6
- wet_run = (ARGV[0] == "wet" || @wet)
7
-
8
6
  ActiveRecord::Base.transaction do
9
7
 
10
8
  # Code at here will rollback when dry run.
11
9
 
12
- if wet_run
10
+ # @wet will be true when -f option passed.
11
+ if @wet
13
12
  BatchManager.logger.info "Wet run completed!"
14
13
  else
15
14
  BatchManager.logger.warn "Rolling back."
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.3.9
4
+ version: 0.4.0
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-12-06 00:00:00.000000000 Z
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: daemon-spawn
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.2
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: sqlite3
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -140,6 +154,7 @@ files:
140
154
  - config/routes.rb
141
155
  - bin/bm_exec
142
156
  - lib/batch_manager/batch_status.rb
157
+ - lib/batch_manager/daemon.rb
143
158
  - lib/batch_manager/engine.rb
144
159
  - lib/batch_manager/executor.rb
145
160
  - lib/batch_manager/logger.rb