bin_script 0.1.2 → 0.1.3
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.
- data/Gemfile.lock +1 -1
- data/README.markdown +29 -5
- data/lib/bin_script/bin_script.rb +17 -23
- data/lib/bin_script/rails_stub.rb +22 -0
- data/lib/bin_script/version.rb +1 -1
- data/lib/bin_script/xlogger.rb +18 -13
- data/lib/generators/bin/templates/script_class.rb +5 -6
- metadata +6 -5
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
|
@@ -21,10 +21,11 @@ gem 'bin_script'
|
|
|
21
21
|
|
|
22
22
|
Call like:
|
|
23
23
|
|
|
24
|
-
$ cd project && ./bin/bla.rb -e production -
|
|
24
|
+
$ cd project && ./bin/bla.rb -e production -t -d "2012-04-07" -u "asdf"
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
Features by default:
|
|
27
27
|
|
|
28
|
+
$ ./bin/bla.rb -h
|
|
28
29
|
$ ./bin/bla.rb -e production
|
|
29
30
|
$ ./bin/bla.rb -e production -L ./locks/bla.lock
|
|
30
31
|
$ ./bin/bla.rb -e production -l ./log/bla.log
|
|
@@ -34,10 +35,10 @@ Examples (default features):
|
|
|
34
35
|
|
|
35
36
|
Example Bin
|
|
36
37
|
-----------
|
|
37
|
-
app/models/bin/
|
|
38
|
+
app/models/bin/bla_script.rb
|
|
38
39
|
|
|
39
40
|
``` ruby
|
|
40
|
-
class
|
|
41
|
+
class BlaScript < BinScript
|
|
41
42
|
optional :u, "Update string"
|
|
42
43
|
required :d, "Date in format YYYY-MM-DD or YYYY-MM"
|
|
43
44
|
noarg :t, "Test run"
|
|
@@ -65,4 +66,27 @@ class BinScript
|
|
|
65
66
|
end
|
|
66
67
|
end
|
|
67
68
|
```
|
|
68
|
-
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
### Create bin script without loading Rails Environment
|
|
72
|
+
add into file ./bin/bla.rb
|
|
73
|
+
|
|
74
|
+
NO_RAILS=true
|
|
75
|
+
|
|
76
|
+
example:
|
|
77
|
+
|
|
78
|
+
``` ruby
|
|
79
|
+
NO_RAILS = true
|
|
80
|
+
load Gem.bin_path('bin_script', 'bin_helper')
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Disable lock or log
|
|
85
|
+
|
|
86
|
+
``` ruby
|
|
87
|
+
class Bla < BinScript
|
|
88
|
+
self.log_level = Logger::DEBUG
|
|
89
|
+
self.enable_locking = false
|
|
90
|
+
self.enable_logging = false
|
|
91
|
+
end
|
|
92
|
+
```
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
2
|
require 'getoptlong'
|
|
3
3
|
require 'pathname'
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
require File.dirname(__FILE__) + '/rails_stub'
|
|
5
6
|
require File.dirname(__FILE__) + '/lock_file'
|
|
6
7
|
require File.dirname(__FILE__) + '/xlogger'
|
|
7
8
|
require File.dirname(__FILE__) + '/class_inheritable_attributes'
|
|
@@ -19,14 +20,14 @@ class BinScript
|
|
|
19
20
|
{:key => :L, :type => :optional, :description => "Path to lock file (default \#{Rails.root}/lock/[script_name].lock"}
|
|
20
21
|
]
|
|
21
22
|
|
|
22
|
-
# Default log level INFO or DEBUG for test env
|
|
23
|
-
@log_level = (Rails.env == 'development' ? XLogger::DEBUG : XLogger::INFO)
|
|
24
|
-
|
|
25
23
|
# Enable locking by default
|
|
26
24
|
@enable_locking = true
|
|
27
25
|
|
|
28
26
|
# Enable logging by default
|
|
29
27
|
@enable_logging = true
|
|
28
|
+
|
|
29
|
+
# Default log level INFO or DEBUG for test env
|
|
30
|
+
@log_level = (RailsStub.env == 'development' ? XLogger::DEBUG : XLogger::INFO)
|
|
30
31
|
|
|
31
32
|
# По умолчанию мы при каждом запуске скрипта продолжаем писать в основной лог. Но можно записать сюда формат, который
|
|
32
33
|
# можно скормить time.strftime(format) и результат подставится в конец имени файла лога. Таким образом можно делать
|
|
@@ -60,8 +61,8 @@ class BinScript
|
|
|
60
61
|
# Define class level helper method
|
|
61
62
|
singleton.class_eval do
|
|
62
63
|
define_method :info do |message|
|
|
63
|
-
return unless
|
|
64
|
-
|
|
64
|
+
return unless RailsStub.logger
|
|
65
|
+
RailsStub.logger.send(method,message)
|
|
65
66
|
end
|
|
66
67
|
end
|
|
67
68
|
|
|
@@ -71,7 +72,7 @@ class BinScript
|
|
|
71
72
|
@logger.send(method,message)
|
|
72
73
|
end
|
|
73
74
|
end
|
|
74
|
-
|
|
75
|
+
|
|
75
76
|
class << self
|
|
76
77
|
# Get parameter by key
|
|
77
78
|
def get_parameter(key)
|
|
@@ -131,7 +132,7 @@ class BinScript
|
|
|
131
132
|
# Run script detected by the filename of source script file
|
|
132
133
|
def run_script(filename = $0)
|
|
133
134
|
cfg = parse_script_file_name(Pathname.new(filename).realpath.to_s)
|
|
134
|
-
cfg[:files].each { |f| require File.join(
|
|
135
|
+
cfg[:files].each { |f| require File.join(RailsStub.root, f) }
|
|
135
136
|
|
|
136
137
|
# Create instance and call run! for script class
|
|
137
138
|
klass = cfg[:class].constantize
|
|
@@ -165,14 +166,7 @@ class BinScript
|
|
|
165
166
|
@parameters.each { |p| new << p if p[:key] != key }
|
|
166
167
|
@parameters = new
|
|
167
168
|
end
|
|
168
|
-
|
|
169
|
-
# RAILS_ROOT is not availible if rails environment was not loaded. So detect application root in this case from current file path
|
|
170
|
-
def rails_root
|
|
171
|
-
path = Rails.root
|
|
172
|
-
path ||= defined?(APP_ROOT) ? APP_ROOT : '.'
|
|
173
|
-
Pathname.new(path).realpath.to_s
|
|
174
|
-
end
|
|
175
|
-
|
|
169
|
+
|
|
176
170
|
# Prepare ARGV parameters as hash
|
|
177
171
|
def get_argv_values
|
|
178
172
|
values = {}
|
|
@@ -215,7 +209,7 @@ class BinScript
|
|
|
215
209
|
end
|
|
216
210
|
|
|
217
211
|
def puts(*arg)
|
|
218
|
-
return if self.class.disable_puts_for_tests &&
|
|
212
|
+
return if self.class.disable_puts_for_tests && RailsStub.env == 'test'
|
|
219
213
|
Kernel.puts(*arg)
|
|
220
214
|
end
|
|
221
215
|
|
|
@@ -224,11 +218,11 @@ class BinScript
|
|
|
224
218
|
begin
|
|
225
219
|
@source_argv = ARGV.dup
|
|
226
220
|
@overrided_parameters = {}
|
|
227
|
-
@params_values = (
|
|
221
|
+
@params_values = (RailsStub.env == 'test' ? {} : self.class.get_argv_values)
|
|
228
222
|
|
|
229
223
|
# Create logger if logging enabled
|
|
230
224
|
if self.class.enable_logging
|
|
231
|
-
@logger = XLogger.new(:file => log_filename, :dont_touch_rails_logger => (
|
|
225
|
+
@logger = XLogger.new(:file => log_filename, :dont_touch_rails_logger => (RailsStub.env == 'test'))
|
|
232
226
|
@logger.level = self.class.log_level
|
|
233
227
|
end
|
|
234
228
|
|
|
@@ -257,7 +251,7 @@ class BinScript
|
|
|
257
251
|
begin
|
|
258
252
|
# Log important info and call script job
|
|
259
253
|
info "Log level = #{@logger.level}" if self.class.enable_logging
|
|
260
|
-
info "Parameters: #{@
|
|
254
|
+
info "Parameters: #{@params_values.inspect}"
|
|
261
255
|
info "Starting script #{self.class.script_name}..."
|
|
262
256
|
start = Time.now
|
|
263
257
|
|
|
@@ -277,7 +271,7 @@ class BinScript
|
|
|
277
271
|
log_benchmarker_data
|
|
278
272
|
rescue Exception => e
|
|
279
273
|
# Print error info if it's not test env or exit
|
|
280
|
-
if
|
|
274
|
+
if RailsStub.env != 'test' && e.class != SystemExit && e.class != Interrupt
|
|
281
275
|
msg = self.class.prepare_exception_message(e)
|
|
282
276
|
puts "\n" + msg
|
|
283
277
|
fatal msg
|
|
@@ -334,12 +328,12 @@ class BinScript
|
|
|
334
328
|
|
|
335
329
|
# Prepare filename of log file
|
|
336
330
|
def lock_filename
|
|
337
|
-
params(:L).blank? ? File.join(
|
|
331
|
+
params(:L).blank? ? File.join(RailsStub.root, 'locks', "#{self.class.script_name}.lock") : params(:L)
|
|
338
332
|
end
|
|
339
333
|
|
|
340
334
|
# Prepare filename of log file
|
|
341
335
|
def log_filename
|
|
342
|
-
params(:l).blank? ? File.join(
|
|
336
|
+
params(:l).blank? ? File.join(RailsStub.root, 'log', "#{self.class.script_name}#{log_filename_time_part}.log") : params(:l)
|
|
343
337
|
end
|
|
344
338
|
|
|
345
339
|
private
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'rails' unless defined?(Rails)
|
|
3
|
+
rescue LoadError => e
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
module RailsStub
|
|
7
|
+
|
|
8
|
+
def self.env
|
|
9
|
+
defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.logger
|
|
13
|
+
defined?(Rails) ? Rails.logger : nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.root
|
|
17
|
+
path = defined?(Rails) ? Rails.root : nil
|
|
18
|
+
path ||= defined?(APP_ROOT) ? APP_ROOT : '.'
|
|
19
|
+
Pathname.new(path).realpath.to_s
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
data/lib/bin_script/version.rb
CHANGED
data/lib/bin_script/xlogger.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
|
|
3
2
|
require 'logger'
|
|
4
3
|
|
|
5
4
|
class XLogger < Logger
|
|
@@ -18,14 +17,14 @@ class XLogger < Logger
|
|
|
18
17
|
# Don't change default logger if asked
|
|
19
18
|
if hint[:dont_touch_rails_logger].blank? && defined?(ActiveRecord::Base)
|
|
20
19
|
ActiveRecord::Base.logger = self
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
|
|
21
|
+
def Rails.logger
|
|
22
|
+
ActiveRecord::Base.logger
|
|
23
|
+
end
|
|
25
24
|
end
|
|
26
25
|
|
|
27
26
|
self.level = hint[:log_level] || rails_env_log_level
|
|
28
|
-
log_sql if hint[:log_sql] || !
|
|
27
|
+
log_sql if hint[:log_sql] || !prod_env?
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
class Formatter < Logger::Formatter
|
|
@@ -45,16 +44,22 @@ class XLogger < Logger
|
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
def rails_env_log_level
|
|
48
|
-
|
|
47
|
+
prod_env? ? Logger::INFO : Logger::DEBUG
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def prod_env?
|
|
51
|
+
defined?(Rails) && Rails.env.production?
|
|
49
52
|
end
|
|
50
53
|
end
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
module
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
if defined?(ActiveRecord)
|
|
56
|
+
module ActiveRecord
|
|
57
|
+
module ConnectionAdapters
|
|
58
|
+
class AbstractAdapter
|
|
59
|
+
def logger=(val)
|
|
60
|
+
@logger = val
|
|
61
|
+
end
|
|
57
62
|
end
|
|
58
63
|
end
|
|
59
64
|
end
|
|
60
|
-
end
|
|
65
|
+
end
|
|
@@ -28,15 +28,14 @@ class <%= class_name %>Script < BinScript
|
|
|
28
28
|
# FATAL = 4
|
|
29
29
|
# UNKNOWN = 5
|
|
30
30
|
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
# Например "_%Y-%m-%d_%H-%M-%S" - каждый запуск новый лог
|
|
35
|
-
# "_%Y-%m-%d" - каждый день новый лог
|
|
31
|
+
# By default, each bin logging into main log. Possible to specify log name for date.
|
|
32
|
+
# Examples: "_%Y-%m-%d_%H-%M-%S" - each execute, new log
|
|
33
|
+
# "_%Y-%m-%d" - each day, new log
|
|
36
34
|
# self.date_log_postfix = "_%Y-%m-%d"
|
|
37
35
|
|
|
38
|
-
#
|
|
36
|
+
# Execute
|
|
39
37
|
def do!
|
|
40
38
|
logger.info "Script <%= class_name %> works!"
|
|
41
39
|
end
|
|
40
|
+
|
|
42
41
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bin_script
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -14,7 +14,7 @@ date: 2012-04-07 00:00:00.000000000 Z
|
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: activesupport
|
|
17
|
-
requirement: &
|
|
17
|
+
requirement: &75985330 !ruby/object:Gem::Requirement
|
|
18
18
|
none: false
|
|
19
19
|
requirements:
|
|
20
20
|
- - ! '>='
|
|
@@ -22,10 +22,10 @@ dependencies:
|
|
|
22
22
|
version: 2.3.2
|
|
23
23
|
type: :runtime
|
|
24
24
|
prerelease: false
|
|
25
|
-
version_requirements: *
|
|
25
|
+
version_requirements: *75985330
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: rspec
|
|
28
|
-
requirement: &
|
|
28
|
+
requirement: &75985130 !ruby/object:Gem::Requirement
|
|
29
29
|
none: false
|
|
30
30
|
requirements:
|
|
31
31
|
- - ! '>='
|
|
@@ -33,7 +33,7 @@ dependencies:
|
|
|
33
33
|
version: '0'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
|
-
version_requirements: *
|
|
36
|
+
version_requirements: *75985130
|
|
37
37
|
description: Easy writing and executing bins (espesually for crontab or god) in Rails
|
|
38
38
|
project
|
|
39
39
|
email: kostya27@gmail.com
|
|
@@ -55,6 +55,7 @@ files:
|
|
|
55
55
|
- lib/bin_script/bin_script.rb
|
|
56
56
|
- lib/bin_script/class_inheritable_attributes.rb
|
|
57
57
|
- lib/bin_script/lock_file.rb
|
|
58
|
+
- lib/bin_script/rails_stub.rb
|
|
58
59
|
- lib/bin_script/version.rb
|
|
59
60
|
- lib/bin_script/xlogger.rb
|
|
60
61
|
- lib/generators/bin/bin_generator.rb
|