worker_tools 1.2 → 1.3.1
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 +4 -4
- data/.tool-versions +1 -0
- data/README.md +38 -0
- data/lib/worker_tools/basics.rb +3 -1
- data/lib/worker_tools/csv_input.rb +10 -0
- data/lib/worker_tools/errors.rb +6 -0
- data/lib/worker_tools/memory_usage.rb +28 -0
- data/lib/worker_tools/version.rb +1 -1
- data/lib/worker_tools/xlsx_input.rb +6 -0
- data/worker_tools.gemspec +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 36547805fb8cfbbf750b84c31fb325a918c54501830bbdfd68f8f2c8f5b1267e
|
|
4
|
+
data.tar.gz: 47361b96ca6e8964b688b0ea50e426c2786edc9b0415984fc9dd4c5247c9cc1d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d0fbe8c88c4aa16ea93840bae76823d43aa98539110ba3f347607ca809ddf0ae5687a84608d86b73ae3b771816ad746d9e2006c681192fca3c1bec33c04a9af6
|
|
7
|
+
data.tar.gz: c0bbd1d018d2d77a61fd538c337423d6380ed1a351e8844eb930e6fd416068461df06fa8732527cadd5949348be44f3ec70bf6ac97dcfdb1b7eec02825dfd0e2
|
data/.tool-versions
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby 2.7.5
|
data/README.md
CHANGED
|
@@ -84,6 +84,7 @@ class Import < ApplicationRecord
|
|
|
84
84
|
complete_with_warnings
|
|
85
85
|
failed
|
|
86
86
|
running
|
|
87
|
+
empty
|
|
87
88
|
].map { |e| [e, e] }.to_h
|
|
88
89
|
|
|
89
90
|
enum kind: { foo: 0, bar: 1 }
|
|
@@ -99,6 +100,8 @@ The state `complete` and `failed` are used by the modules. Both `state` and `kin
|
|
|
99
100
|
|
|
100
101
|
The state `complete_with_warnings` indicates that the model contains notes that did not lead to a failure but should get some attention. By default those levels are `warning` and `errors` and can be customized.
|
|
101
102
|
|
|
103
|
+
The state `empty` is automatically set by the CSV and XLSX input modules when the input file does not exist or is empty (0 bytes).
|
|
104
|
+
|
|
102
105
|
In this case the migration would be something like this:
|
|
103
106
|
|
|
104
107
|
```ruby
|
|
@@ -199,6 +202,22 @@ See all methods in [slack_error_notifier](/lib/worker_tools/slack_error_notifier
|
|
|
199
202
|
|
|
200
203
|
See all methods in [csv_input](/lib/worker_tools/csv_input.rb)
|
|
201
204
|
|
|
205
|
+
### Skipping File Presence Check
|
|
206
|
+
|
|
207
|
+
By default, the CSV Input module checks if the file exists and is not empty. If you need to skip this validation (for example, if an empty file is valid for your process), you can override the `csv_input_file_presence_check` method:
|
|
208
|
+
|
|
209
|
+
```ruby
|
|
210
|
+
class CsvInputExample
|
|
211
|
+
include WorkerTools::CsvInput
|
|
212
|
+
|
|
213
|
+
def csv_input_file_presence_check
|
|
214
|
+
true
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# ... rest of your implementation
|
|
218
|
+
end
|
|
219
|
+
```
|
|
220
|
+
|
|
202
221
|
## Module CSV Output
|
|
203
222
|
|
|
204
223
|
See all methods in [csv_output](/lib/worker_tools/csv_output.rb)
|
|
@@ -349,6 +368,25 @@ class MyImporter
|
|
|
349
368
|
end
|
|
350
369
|
```
|
|
351
370
|
|
|
371
|
+
## MemoryUsage
|
|
372
|
+
|
|
373
|
+
There is a memory usage wrapper that you can use to record the memory consumption of your worker tasks. The memory usage is recorded in megabytes and stored in `model.meta['memory_usage']`. The module uses the `get_process_mem` gem to measure memory usage before and after the task execution.
|
|
374
|
+
|
|
375
|
+
```ruby
|
|
376
|
+
class MyImporter
|
|
377
|
+
include WorkerTools::MemoryUsage
|
|
378
|
+
wrappers :memory_usage
|
|
379
|
+
|
|
380
|
+
def run
|
|
381
|
+
# do stuff that consumes memory
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# ..
|
|
385
|
+
end
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
The memory usage will be automatically recorded in the model's meta attribute as `memory_usage` with the value in megabytes (rounded to 2 decimal places). The measurement represents the difference in memory usage before and after the task execution.
|
|
389
|
+
|
|
352
390
|
## Module 'Notes'
|
|
353
391
|
|
|
354
392
|
If you use ActiveRecord you may need to modify the serializer as well as deserializer from the note attribute. After that you can easily serialize hashes and array of hashes with indifferent access. For that purpose the gem provides two utility methods. (HashWithIndifferentAccessType, SerializedArrayType). There is an example of how you can use it.
|
data/lib/worker_tools/basics.rb
CHANGED
|
@@ -57,7 +57,9 @@ module WorkerTools
|
|
|
57
57
|
# rubocop:disable Lint/RescueException
|
|
58
58
|
rescue Exception => e
|
|
59
59
|
# rubocop:enable Lint/RescueException
|
|
60
|
-
|
|
60
|
+
state = e.is_a?(WorkerTools::Errors::EmptyFile) ? 'empty' : 'failed'
|
|
61
|
+
save_state_without_validate(state)
|
|
62
|
+
|
|
61
63
|
raise unless silent_error?(e)
|
|
62
64
|
end
|
|
63
65
|
|
|
@@ -59,6 +59,8 @@ module WorkerTools
|
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
def csv_input_columns_array_check(csv_rows_enum)
|
|
62
|
+
return if csv_rows_enum.first.nil?
|
|
63
|
+
|
|
62
64
|
expected_columns_length = csv_input_columns.length
|
|
63
65
|
actual_columns_length = csv_rows_enum.first.length
|
|
64
66
|
return if expected_columns_length == actual_columns_length
|
|
@@ -68,6 +70,8 @@ module WorkerTools
|
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
def csv_input_columns_hash_check(csv_rows_enum)
|
|
73
|
+
raise Errors::MissingColumns, 'The headers are missing' if csv_rows_enum.first.nil?
|
|
74
|
+
|
|
71
75
|
expected_names = csv_input_columns.values
|
|
72
76
|
filtered_actual_names = csv_rows_enum.first.map { |n| csv_input_header_normalized(n) }
|
|
73
77
|
csv_input_columns_hash_check_duplicates(filtered_actual_names)
|
|
@@ -145,8 +149,14 @@ module WorkerTools
|
|
|
145
149
|
true
|
|
146
150
|
end
|
|
147
151
|
|
|
152
|
+
def csv_input_file_presence_check
|
|
153
|
+
raise Errors::EmptyFile, 'The file does not exist' unless File.exist?(csv_input_file_path)
|
|
154
|
+
raise Errors::EmptyFile, 'The file is empty' if File.zero?(csv_input_file_path)
|
|
155
|
+
end
|
|
156
|
+
|
|
148
157
|
def csv_input_foreach
|
|
149
158
|
@csv_input_foreach ||= begin
|
|
159
|
+
csv_input_file_presence_check
|
|
150
160
|
csv_input_columns_check(csv_rows_enum)
|
|
151
161
|
|
|
152
162
|
CsvInputForeach.new(
|
data/lib/worker_tools/errors.rb
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'get_process_mem'
|
|
2
|
+
|
|
3
|
+
module WorkerTools
|
|
4
|
+
module MemoryUsage
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
# rubocop:disable Metrics/MethodLength
|
|
9
|
+
def with_wrapper_memory_usage(&block)
|
|
10
|
+
memory_error = nil
|
|
11
|
+
start_memory = GetProcessMem.new.mb
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
block.call
|
|
15
|
+
rescue StandardError => e
|
|
16
|
+
memory_error = e
|
|
17
|
+
ensure
|
|
18
|
+
end_memory = GetProcessMem.new.mb
|
|
19
|
+
memory_used = (end_memory - start_memory).round(2)
|
|
20
|
+
model.meta['memory_usage'] = memory_used if model.respond_to?(:meta)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
raise memory_error if memory_error
|
|
24
|
+
end
|
|
25
|
+
# rubocop:enable Metrics/MethodLength
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/worker_tools/version.rb
CHANGED
|
@@ -142,8 +142,14 @@ module WorkerTools
|
|
|
142
142
|
true
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
+
def xlsx_input_file_presence_check
|
|
146
|
+
raise Errors::EmptyFile, 'The file does not exist' unless File.exist?(xlsx_input_file_path)
|
|
147
|
+
raise Errors::EmptyFile, 'The file is empty' if File.zero?(xlsx_input_file_path)
|
|
148
|
+
end
|
|
149
|
+
|
|
145
150
|
def xlsx_input_foreach
|
|
146
151
|
@xlsx_input_foreach ||= begin
|
|
152
|
+
xlsx_input_file_presence_check
|
|
147
153
|
xlsx_input_columns_check(xlsx_rows_enum)
|
|
148
154
|
|
|
149
155
|
XlsxInputForeach.new(
|
data/worker_tools.gemspec
CHANGED
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:
|
|
4
|
+
version: 1.3.1
|
|
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: 2026-01-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -24,6 +24,20 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: get_process_mem
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
27
41
|
- !ruby/object:Gem::Dependency
|
|
28
42
|
name: roo
|
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -229,6 +243,7 @@ extra_rdoc_files: []
|
|
|
229
243
|
files:
|
|
230
244
|
- ".gitignore"
|
|
231
245
|
- ".rubocop.yml"
|
|
246
|
+
- ".tool-versions"
|
|
232
247
|
- ".travis.yml"
|
|
233
248
|
- CHANGELOG.md
|
|
234
249
|
- Gemfile
|
|
@@ -244,6 +259,7 @@ files:
|
|
|
244
259
|
- lib/worker_tools/csv_input.rb
|
|
245
260
|
- lib/worker_tools/csv_output.rb
|
|
246
261
|
- lib/worker_tools/errors.rb
|
|
262
|
+
- lib/worker_tools/memory_usage.rb
|
|
247
263
|
- lib/worker_tools/recorder.rb
|
|
248
264
|
- lib/worker_tools/slack_error_notifier.rb
|
|
249
265
|
- lib/worker_tools/utils/hash_with_indifferent_access_type.rb
|