kitcat 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kitcat/framework.rb +56 -77
- data/lib/kitcat/logging.rb +71 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f897bc2d5d9d7171d62f024338a59076cda9cd4e
|
4
|
+
data.tar.gz: b2a8c4e3a483b5e82dd46a5d45dad08feb28f3b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42ffa3a76b786a4c30e940f9fc397641d568f297df6398295e8696d66b18c245b84b102fe3e9bfd4b7abeeef4da67652c32e3c0947cdca503afc47b585837519
|
7
|
+
data.tar.gz: 41fad141fa2dbecfb79ebd1d17b8beacdba3dbf17d2632b79ee4742e368121f160d945640fe739b2d5d533bf42f0a3461dc7f28724ce723f8b49bf7c30cba079
|
data/lib/kitcat/framework.rb
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
require 'ruby-progressbar'
|
2
2
|
require 'active_model'
|
3
3
|
require 'active_support/core_ext'
|
4
|
+
require 'kitcat/logging'
|
4
5
|
|
5
6
|
module KitCat
|
6
7
|
class Framework
|
7
8
|
attr_reader :last_item_processed,
|
8
|
-
:migration_name,
|
9
9
|
:number_of_items_processed,
|
10
|
-
:migration_strategy
|
10
|
+
:migration_strategy,
|
11
|
+
:logging
|
12
|
+
|
13
|
+
delegate :log_file_path,
|
14
|
+
:start_logging, :end_logging,
|
15
|
+
:log_success, :log_failure,
|
16
|
+
:log_interrupt_callback_start,
|
17
|
+
:log_interrupt_callback_finish, to: :logging
|
11
18
|
|
12
19
|
# @params migration_strategy {Object}
|
13
20
|
# Instance implementing the methods of +KitCat::Callbacks+
|
@@ -34,42 +41,24 @@ module KitCat
|
|
34
41
|
progress_bar: true,
|
35
42
|
progress_bar_output: STDOUT)
|
36
43
|
@migration_strategy = migration_strategy
|
37
|
-
@migration_name = build_migration_name(migration_name)
|
38
44
|
@number_of_items_to_process = number_of_items_to_process
|
39
45
|
@last_item_processed = nil
|
40
46
|
@progress_bar = initialize_progress_bar(progress_bar, progress_bar_output)
|
47
|
+
@logging = KitCat::Logging.new(migration_strategy, migration_name)
|
41
48
|
end
|
42
49
|
|
43
50
|
def execute
|
44
|
-
|
45
|
-
|
46
|
-
Signal.trap('INT') { @interrupted = true }
|
51
|
+
trap_signals
|
52
|
+
|
47
53
|
start_logging
|
48
54
|
|
49
55
|
@number_of_items_processed = 0
|
50
56
|
|
51
57
|
items.each do |item|
|
52
|
-
|
53
|
-
|
54
|
-
log_success(item)
|
55
|
-
|
56
|
-
@number_of_items_processed += 1
|
57
|
-
|
58
|
-
increment_progress_bar
|
59
|
-
|
60
|
-
@last_item_processed = item
|
61
|
-
|
62
|
-
break unless process_more?
|
63
|
-
else
|
64
|
-
log_failure(item)
|
65
|
-
|
66
|
-
break
|
67
|
-
end
|
68
|
-
if @interrupted
|
69
|
-
handle_user_interrupt
|
70
|
-
break
|
71
|
-
end
|
58
|
+
break unless execute_for(item)
|
72
59
|
end
|
60
|
+
|
61
|
+
ensure
|
73
62
|
end_logging
|
74
63
|
end
|
75
64
|
|
@@ -77,10 +66,6 @@ module KitCat
|
|
77
66
|
@number_of_items_to_process ||= migration_strategy.criteria.count
|
78
67
|
end
|
79
68
|
|
80
|
-
def log_file_path
|
81
|
-
@log_file_path ||= File.join(log_dir, build_log_file_name)
|
82
|
-
end
|
83
|
-
|
84
69
|
def progress_bar?
|
85
70
|
!@progress_bar.nil?
|
86
71
|
end
|
@@ -92,31 +77,58 @@ module KitCat
|
|
92
77
|
|
93
78
|
private
|
94
79
|
|
95
|
-
def
|
96
|
-
|
80
|
+
def execute_for(item)
|
81
|
+
begin
|
82
|
+
if migration_strategy.process(item)
|
97
83
|
|
98
|
-
|
84
|
+
commit_success(item)
|
99
85
|
|
100
|
-
|
101
|
-
|
86
|
+
return false unless process_more?
|
87
|
+
else
|
88
|
+
commit_failure(item)
|
89
|
+
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
if @interrupted
|
93
|
+
handle_user_interrupt
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
rescue StandardError
|
97
|
+
commit_failure(item)
|
98
|
+
raise
|
102
99
|
end
|
100
|
+
|
101
|
+
true
|
103
102
|
end
|
104
103
|
|
105
|
-
def
|
106
|
-
|
104
|
+
def commit_success(item)
|
105
|
+
log_success(item)
|
106
|
+
|
107
|
+
@number_of_items_processed += 1
|
108
|
+
|
109
|
+
increment_progress_bar
|
110
|
+
|
111
|
+
@last_item_processed = item
|
107
112
|
end
|
108
113
|
|
109
|
-
def
|
110
|
-
|
114
|
+
def commit_failure(item)
|
115
|
+
log_failure(item)
|
111
116
|
end
|
112
117
|
|
113
|
-
def
|
114
|
-
|
118
|
+
def trap_signals
|
119
|
+
@interrupted = false
|
120
|
+
Signal.trap('TERM') { @interrupted = true }
|
121
|
+
Signal.trap('INT') { @interrupted = true }
|
115
122
|
end
|
116
123
|
|
117
|
-
def
|
118
|
-
|
119
|
-
|
124
|
+
def items
|
125
|
+
return enum_for(:items) unless block_given?
|
126
|
+
|
127
|
+
enum = migration_strategy.criteria.each
|
128
|
+
|
129
|
+
loop do
|
130
|
+
yield enum.next
|
131
|
+
end
|
120
132
|
end
|
121
133
|
|
122
134
|
def initialize_progress_bar(progress_bar_flag, output)
|
@@ -132,39 +144,6 @@ module KitCat
|
|
132
144
|
format: "%a %bᗧ%i %p%% %e")
|
133
145
|
end
|
134
146
|
|
135
|
-
def start_logging
|
136
|
-
logger.info 'Start Processing...'
|
137
|
-
end
|
138
|
-
|
139
|
-
def end_logging
|
140
|
-
logger.info '...end of processing'
|
141
|
-
end
|
142
|
-
|
143
|
-
def logger
|
144
|
-
@logger ||= Logger.new(log_file_path)
|
145
|
-
end
|
146
|
-
|
147
|
-
def log_success(item)
|
148
|
-
log_line(item) { |method| logger.info "...successfully processed item: #{item.try(method)}" }
|
149
|
-
end
|
150
|
-
|
151
|
-
def log_failure(item)
|
152
|
-
log_line(item) { |method| logger.error "...error while processing item: #{item.try(method)}" }
|
153
|
-
end
|
154
|
-
|
155
|
-
def log_line(item)
|
156
|
-
method = item.respond_to?(:to_log) ? :to_log : :to_s
|
157
|
-
yield method
|
158
|
-
end
|
159
|
-
|
160
|
-
def log_interrupt_callback_start
|
161
|
-
logger.info '...user interrupted, calling interrupt callback on migration strategy...'
|
162
|
-
end
|
163
|
-
|
164
|
-
def log_interrupt_callback_finish
|
165
|
-
logger.info '......end of interrupt callback after user interruption'
|
166
|
-
end
|
167
|
-
|
168
147
|
def process_more?
|
169
148
|
@number_of_items_to_process.nil? || @number_of_items_processed < @number_of_items_to_process
|
170
149
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module KitCat
|
2
|
+
# +KitCat::Logging+ is used to encapsulate the functionality of +Framework+ related to
|
3
|
+
# logging.
|
4
|
+
#
|
5
|
+
# It needs to be initialized with the +migration_name+ because this is used to build the
|
6
|
+
# relevant log filename.
|
7
|
+
#
|
8
|
+
class Logging
|
9
|
+
attr_reader :migration_name, :migration_strategy
|
10
|
+
|
11
|
+
def initialize(migration_strategy, migration_name)
|
12
|
+
@migration_name = build_migration_name(migration_name, migration_strategy)
|
13
|
+
end
|
14
|
+
|
15
|
+
def log_file_path
|
16
|
+
@log_file_path ||= File.join(log_dir, build_log_file_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def start_logging
|
20
|
+
logger.info 'Start Processing...'
|
21
|
+
end
|
22
|
+
|
23
|
+
def end_logging
|
24
|
+
logger.info '...end of processing'
|
25
|
+
end
|
26
|
+
|
27
|
+
def log_success(item)
|
28
|
+
log_line(item) { |method| logger.info "...successfully processed item: #{item.try(method)}" }
|
29
|
+
end
|
30
|
+
|
31
|
+
def log_failure(item)
|
32
|
+
log_line(item) { |method| logger.error "...error while processing item: #{item.try(method)}" }
|
33
|
+
end
|
34
|
+
|
35
|
+
def log_interrupt_callback_start
|
36
|
+
logger.info '...user interrupted, calling interrupt callback on migration strategy...'
|
37
|
+
end
|
38
|
+
|
39
|
+
def log_interrupt_callback_finish
|
40
|
+
logger.info '......end of interrupt callback after user interruption'
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def build_migration_name(migration_name, migration_strategy)
|
46
|
+
result = migration_name || migration_strategy.class.name.delete(':').underscore.upcase
|
47
|
+
result.gsub(/\W/, '').upcase
|
48
|
+
end
|
49
|
+
|
50
|
+
def log_dir
|
51
|
+
@log_dir ||= FileUtils.mkdir_p(File.join(Dir.pwd, 'log'))
|
52
|
+
end
|
53
|
+
|
54
|
+
def build_log_file_name
|
55
|
+
"migration-#{migration_name}-#{timestamp}.log"
|
56
|
+
end
|
57
|
+
|
58
|
+
def timestamp
|
59
|
+
Time.now.to_s(:number)
|
60
|
+
end
|
61
|
+
|
62
|
+
def logger
|
63
|
+
@logger ||= Logger.new(log_file_path)
|
64
|
+
end
|
65
|
+
|
66
|
+
def log_line(item)
|
67
|
+
method = item.respond_to?(:to_log) ? :to_log : :to_s
|
68
|
+
yield method
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kitcat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simply Business
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-progressbar
|
@@ -132,6 +132,7 @@ extra_rdoc_files: []
|
|
132
132
|
files:
|
133
133
|
- Rakefile
|
134
134
|
- lib/kitcat/framework.rb
|
135
|
+
- lib/kitcat/logging.rb
|
135
136
|
- lib/kitcat/terminal_width_calculator.rb
|
136
137
|
- lib/kitcat.rb
|
137
138
|
- LICENSE
|