worker_tools 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +32 -2
- data/lib/worker_tools/basics.rb +30 -8
- data/lib/worker_tools/counters.rb +1 -1
- data/lib/worker_tools/csv_input.rb +1 -13
- data/lib/worker_tools/errors.rb +4 -4
- data/lib/worker_tools/version.rb +1 -1
- data/lib/worker_tools/xlsx_input.rb +9 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 762658656dbb6922693f4a5c53c1dcbadf5ef2c9ab1e6059078aef2b14ff9a70
|
4
|
+
data.tar.gz: 7e0cb67a63bc63e478f6d741d5ecd1d400489a6cc00bbefc66831687e0b968ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33c69281ec2a6577bb9be3ce2ac1c02937d10fa032a143f0ceed0afbe11b2f5ecbf5082862f06df68c41368b085b4f08c2757f9a45d3f472508e57f4eb09c1e3
|
7
|
+
data.tar.gz: 556c5fa62fae0c8e51b34bfa0561dac1b05c14d4602687520505a04ee317ea989d64514760de931e7926f127656166e44897f9b078782cc08643de18627db369
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [1.1.0] - 2023-07-13
|
4
|
+
|
5
|
+
### New
|
6
|
+
|
7
|
+
- Custom run_modes
|
8
|
+
- XlsxInput without headers
|
9
|
+
- Custom reset hook
|
10
|
+
- Increment counters by a given amount
|
11
|
+
|
12
|
+
### BREAKING CHANGES
|
13
|
+
|
14
|
+
`WorkerTools::Errors::Invalid` has being replaced with `WorkerTools::Errors::Silent`
|
15
|
+
|
16
|
+
The method `non_failure_error?` has being replaced with `silent_error?`
|
17
|
+
|
3
18
|
## [1.0.0] - 2022-05-20
|
4
19
|
|
5
20
|
Compared to 0.2.1
|
data/README.md
CHANGED
@@ -145,7 +145,7 @@ end
|
|
145
145
|
|
146
146
|
The basics module contains a `perform` method, which is the usual entry point for ApplicationJob and Sidekiq. It can receive the id of the model, the model instance, or nothing, in which case it will attempt to create this model on its own.
|
147
147
|
|
148
|
-
By default errors subclassed from WorkerTools::Errors::
|
148
|
+
By default errors subclassed from WorkerTools::Errors::Silent (such as those related to wrong headers in the input modules) will mark the model as failed but not raise. The method `silent_error?` lets you modifiy this behaviour.
|
149
149
|
|
150
150
|
## Module 'Recorder'
|
151
151
|
|
@@ -269,6 +269,35 @@ def perform(model_id)
|
|
269
269
|
end
|
270
270
|
```
|
271
271
|
|
272
|
+
## Run Modes
|
273
|
+
|
274
|
+
It is possible to run the same task in different modes by providing the key `run_mode` inside `options`. For example, by setting it to `:destroy`, the method `run_in_destroy_mode` will get called instead of the usual `run`
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
# options[:run_mode] = :destroy
|
278
|
+
|
279
|
+
def run_in_destroy_mode
|
280
|
+
# add_some note
|
281
|
+
# delete plenty of stuff, take your time
|
282
|
+
end
|
283
|
+
```
|
284
|
+
|
285
|
+
As a convention, use `options[:run_mode_option]` to provide more context:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
# options[:run_mode] = :destroy
|
289
|
+
# options[:run_mode_option] = :all / :only_foos / :only_bars
|
290
|
+
def run_in_destroy_mode
|
291
|
+
case run_mode_option
|
292
|
+
when :all then kaboom
|
293
|
+
when :only_foos then delete_foos
|
294
|
+
when :only_bars then delete_bars
|
295
|
+
end
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
299
|
+
If the corresponding run method is not available an exeception will be raised. A special case is the run_mode `:repeat` which will try to use the method `:run_in_repeat_mode` and fallback to `run` if not present.
|
300
|
+
|
272
301
|
## Counter
|
273
302
|
|
274
303
|
There is a counter wrapper that you can use to add custom counters to the meta attribute. To do this, you need to complete the following tasks:
|
@@ -290,7 +319,8 @@ class MyImporter
|
|
290
319
|
|
291
320
|
def example_foo_counter_methods
|
292
321
|
# you can use the increment helper
|
293
|
-
10.times { increment_foo }
|
322
|
+
10.times { increment_foo } # +1
|
323
|
+
increment_foo(5) # +5
|
294
324
|
|
295
325
|
# the counter works like a regular accessor, you can read it and modify it
|
296
326
|
# directly
|
data/lib/worker_tools/basics.rb
CHANGED
@@ -33,8 +33,9 @@ module WorkerTools
|
|
33
33
|
def perform(model_id = nil)
|
34
34
|
@model_id = model_id
|
35
35
|
|
36
|
+
default_reset
|
36
37
|
with_wrappers(wrapper_methods) do
|
37
|
-
|
38
|
+
send(run_method)
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
@@ -48,18 +49,16 @@ module WorkerTools
|
|
48
49
|
end
|
49
50
|
|
50
51
|
def with_wrapper_basics(&block)
|
51
|
-
|
52
|
+
custom_reset if respond_to?(:custom_reset, true)
|
52
53
|
block.yield
|
53
54
|
finalize
|
54
55
|
# this time we do want to catch Exception to attempt to handle some of the
|
55
56
|
# critical errors.
|
56
57
|
# rubocop:disable Lint/RescueException
|
57
58
|
rescue Exception => e
|
58
|
-
return finalize if non_failure_error?(e)
|
59
|
-
|
60
59
|
# rubocop:enable Lint/RescueException
|
61
60
|
save_state_without_validate('failed')
|
62
|
-
raise
|
61
|
+
raise unless silent_error?(e)
|
63
62
|
end
|
64
63
|
|
65
64
|
def finalize
|
@@ -85,14 +84,37 @@ module WorkerTools
|
|
85
84
|
send(current_wrapper_symbol) { with_wrappers(wrapper_symbols, &block) }
|
86
85
|
end
|
87
86
|
|
88
|
-
def
|
89
|
-
error.is_a?(WorkerTools::Errors::
|
87
|
+
def silent_error?(error)
|
88
|
+
error.is_a?(WorkerTools::Errors::Silent)
|
90
89
|
# or add your list
|
91
|
-
# [WorkerTools::Errors::
|
90
|
+
# [WorkerTools::Errors::Silent, SomeOtherError].any? { |k| e.is_a?(k) }
|
92
91
|
end
|
93
92
|
|
94
93
|
private
|
95
94
|
|
95
|
+
def run_mode
|
96
|
+
model.try(:options).try(:[], 'run_mode').try(:to_sym)
|
97
|
+
end
|
98
|
+
|
99
|
+
def run_mode_option
|
100
|
+
model.try(:options).try(:[], 'run_mode_option').try(:to_sym)
|
101
|
+
end
|
102
|
+
|
103
|
+
def run_method
|
104
|
+
return :run unless run_mode.present?
|
105
|
+
|
106
|
+
method_name = "run_in_#{run_mode}_mode"
|
107
|
+
return method_name.to_sym if respond_to?(method_name, true)
|
108
|
+
return :run if run_mode == :repeat # common fallback
|
109
|
+
|
110
|
+
raise "Missing method #{method_name}"
|
111
|
+
end
|
112
|
+
|
113
|
+
def default_reset
|
114
|
+
model.attributes = { notes: [], meta: {}, state: 'running' }
|
115
|
+
model.save!(validate: false)
|
116
|
+
end
|
117
|
+
|
96
118
|
def save_state_without_validate(state)
|
97
119
|
model.state = state
|
98
120
|
model.save!(validate: false)
|
@@ -21,7 +21,7 @@ module WorkerTools
|
|
21
21
|
define_method("#{name}=") { |value| model.meta[name] = value }
|
22
22
|
|
23
23
|
# ex `increment_inserts`
|
24
|
-
define_method("increment_#{name}") { model.meta[name] +=
|
24
|
+
define_method("increment_#{name}") { |inc = 1| model.meta[name] += inc }
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -151,7 +151,6 @@ module WorkerTools
|
|
151
151
|
|
152
152
|
CsvInputForeach.new(
|
153
153
|
rows_enum: csv_rows_enum,
|
154
|
-
input_columns: csv_input_columns,
|
155
154
|
mapping_order: csv_input_mapping_order(csv_rows_enum.first),
|
156
155
|
cleanup_method: method(:cvs_input_value_cleanup),
|
157
156
|
headers_present: csv_input_headers_present
|
@@ -162,9 +161,8 @@ module WorkerTools
|
|
162
161
|
class CsvInputForeach
|
163
162
|
include Enumerable
|
164
163
|
|
165
|
-
def initialize(rows_enum:,
|
164
|
+
def initialize(rows_enum:, mapping_order:, cleanup_method:, headers_present:)
|
166
165
|
@rows_enum = rows_enum
|
167
|
-
@input_columns = input_columns
|
168
166
|
@mapping_order = mapping_order
|
169
167
|
@cleanup_method = cleanup_method
|
170
168
|
@headers_present = headers_present
|
@@ -181,20 +179,10 @@ module WorkerTools
|
|
181
179
|
end
|
182
180
|
|
183
181
|
def values_to_row(values)
|
184
|
-
return values_to_row_according_to_mapping(values) if @mapping_order
|
185
|
-
|
186
|
-
values_to_row_according_to_position(values)
|
187
|
-
end
|
188
|
-
|
189
|
-
def values_to_row_according_to_mapping(values)
|
190
182
|
@mapping_order.each_with_object(HashWithIndifferentAccess.new) do |(k, v), h|
|
191
183
|
h[k] = @cleanup_method.call(values[v])
|
192
184
|
end
|
193
185
|
end
|
194
|
-
|
195
|
-
def values_to_row_according_to_position(values)
|
196
|
-
@input_columns.map.with_index { |c, i| [c, @cleanup_method.call(values[i])] }.to_h.with_indifferent_access
|
197
|
-
end
|
198
186
|
end
|
199
187
|
end
|
200
188
|
end
|
data/lib/worker_tools/errors.rb
CHANGED
@@ -2,9 +2,9 @@ module WorkerTools
|
|
2
2
|
Error = Class.new(StandardError)
|
3
3
|
|
4
4
|
module Errors
|
5
|
-
|
6
|
-
WrongNumberOfColumns = Class.new(
|
7
|
-
DuplicatedColumns = Class.new(
|
8
|
-
MissingColumns = Class.new(
|
5
|
+
Silent = Class.new(Error)
|
6
|
+
WrongNumberOfColumns = Class.new(Silent)
|
7
|
+
DuplicatedColumns = Class.new(Silent)
|
8
|
+
MissingColumns = Class.new(Silent)
|
9
9
|
end
|
10
10
|
end
|
data/lib/worker_tools/version.rb
CHANGED
@@ -138,6 +138,10 @@ module WorkerTools
|
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
|
+
def xlsx_input_headers_present
|
142
|
+
true
|
143
|
+
end
|
144
|
+
|
141
145
|
def xlsx_input_foreach
|
142
146
|
@xlsx_input_foreach ||= begin
|
143
147
|
xlsx_input_columns_check(xlsx_rows_enum)
|
@@ -145,7 +149,8 @@ module WorkerTools
|
|
145
149
|
XlsxInputForeach.new(
|
146
150
|
rows_enum: xlsx_rows_enum,
|
147
151
|
mapping_order: xlsx_input_mapping_order(xlsx_rows_enum.first),
|
148
|
-
cleanup_method: method(:xlsx_input_value_cleanup)
|
152
|
+
cleanup_method: method(:xlsx_input_value_cleanup),
|
153
|
+
headers_present: xlsx_input_headers_present
|
149
154
|
)
|
150
155
|
end
|
151
156
|
end
|
@@ -153,17 +158,18 @@ module WorkerTools
|
|
153
158
|
class XlsxInputForeach
|
154
159
|
include Enumerable
|
155
160
|
|
156
|
-
def initialize(rows_enum:, mapping_order:, cleanup_method:)
|
161
|
+
def initialize(rows_enum:, mapping_order:, cleanup_method:, headers_present:)
|
157
162
|
@rows_enum = rows_enum
|
158
163
|
@mapping_order = mapping_order
|
159
164
|
@cleanup_method = cleanup_method
|
165
|
+
@headers_present = headers_present
|
160
166
|
end
|
161
167
|
|
162
168
|
def each
|
163
169
|
return enum_for(:each) unless block_given?
|
164
170
|
|
165
171
|
@rows_enum.with_index.each do |values, index|
|
166
|
-
next if index.zero?
|
172
|
+
next if index.zero? && @headers_present
|
167
173
|
|
168
174
|
yield values_to_row(values)
|
169
175
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worker_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fsainz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|