worker_tools 1.1.1 → 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.
- checksums.yaml +4 -4
- data/.tool-versions +1 -0
- data/README.md +19 -0
- data/lib/worker_tools/memory_usage.rb +28 -0
- data/lib/worker_tools/version.rb +1 -1
- data/lib/worker_tools/xlsx_output.rb +43 -4
- data/worker_tools.gemspec +2 -1
- metadata +21 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a69e014d2d2959641972ad163b5567e957693c90b54ae9e7aec4a723f1f39c6
|
4
|
+
data.tar.gz: 3cc3536cf26ab79054761570671361d23bbcb2c708e4974ebfd07ea8671180f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4d622417f3afd6057ae548f2b0e688ce1d8087d1e75a176ff4fe85caef5b1a1469e3ad07db2e21da2be6a365e97ba0bae18fbdd818f2eaea47bc7bb4e2fd0b7
|
7
|
+
data.tar.gz: e479438605973cf104b4c9d60cf3a7f821d09c053c7e569370afa7a1bbcc5e52006f65863a8a2085caa31f878df987c85d8daa7461c41f229aaa71094f9a6c2b
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.7.5
|
data/README.md
CHANGED
@@ -349,6 +349,25 @@ class MyImporter
|
|
349
349
|
end
|
350
350
|
```
|
351
351
|
|
352
|
+
## MemoryUsage
|
353
|
+
|
354
|
+
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.
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
class MyImporter
|
358
|
+
include WorkerTools::MemoryUsage
|
359
|
+
wrappers :memory_usage
|
360
|
+
|
361
|
+
def run
|
362
|
+
# do stuff that consumes memory
|
363
|
+
end
|
364
|
+
|
365
|
+
# ..
|
366
|
+
end
|
367
|
+
```
|
368
|
+
|
369
|
+
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.
|
370
|
+
|
352
371
|
## Module 'Notes'
|
353
372
|
|
354
373
|
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.
|
@@ -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
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'rubyXL'
|
2
|
+
require 'rubyXL/convenience_methods'
|
3
|
+
|
2
4
|
module WorkerTools
|
5
|
+
# rubocop:disable Metrics/ModuleLength
|
3
6
|
module XlsxOutput
|
4
7
|
def xlsx_output_entries
|
5
8
|
raise "xlsx_output_entries has to be defined in #{self}"
|
@@ -25,7 +28,8 @@ module WorkerTools
|
|
25
28
|
label: 'Sheet 1',
|
26
29
|
headers: xlsx_output_column_headers,
|
27
30
|
rows: xlsx_output_entries.lazy.map { |entry| xlsx_output_row_values(entry) },
|
28
|
-
column_style: xlsx_output_column_format
|
31
|
+
column_style: xlsx_output_column_format,
|
32
|
+
number_format: xlsx_output_number_format
|
29
33
|
}
|
30
34
|
}
|
31
35
|
end
|
@@ -48,6 +52,21 @@ module WorkerTools
|
|
48
52
|
{}
|
49
53
|
end
|
50
54
|
|
55
|
+
def xlsx_output_number_format
|
56
|
+
# set the cell number format for a given column
|
57
|
+
# number format also applies to dates, see the excel documentation:
|
58
|
+
# https://support.microsoft.com/en-us/office/number-format-codes-in-excel-for-mac-5026bbd6-04bc-48cd-bf33-80f18b4eae68
|
59
|
+
# https://www.rubydoc.info/gems/rubyXL/3.3.21/RubyXL/NumberFormats
|
60
|
+
#
|
61
|
+
# Ex:
|
62
|
+
# @xlsx_output_number_format ||= {
|
63
|
+
# foo: :auto,
|
64
|
+
# bar: '0.00',
|
65
|
+
# # nothing for baz, it will go through `.to_s`
|
66
|
+
# }
|
67
|
+
{}
|
68
|
+
end
|
69
|
+
|
51
70
|
def xlsx_output_insert_headers(spreadsheet, headers)
|
52
71
|
return unless headers
|
53
72
|
|
@@ -62,13 +81,30 @@ module WorkerTools
|
|
62
81
|
end
|
63
82
|
end
|
64
83
|
|
65
|
-
|
84
|
+
# rubocop:disable Metrics/AbcSize
|
85
|
+
# rubocop:disable Metrics/MethodLength
|
86
|
+
def xlsx_output_insert_rows(spreadsheet, rows, headers, number_formats)
|
87
|
+
number_format_by_col_index = Hash(number_formats).each_with_object({}) do |(col, number_format), hash|
|
88
|
+
hash[headers.keys.index(col)] = number_format
|
89
|
+
end
|
90
|
+
|
66
91
|
rows.each_with_index do |row, row_index|
|
67
92
|
xlsx_output_iterators(row, headers).each_with_index do |value, col_index|
|
68
|
-
|
93
|
+
number_format = number_format_by_col_index[col_index]
|
94
|
+
|
95
|
+
case number_format
|
96
|
+
when :auto, 'auto'
|
97
|
+
spreadsheet.add_cell(row_index + 1, col_index, value)
|
98
|
+
when nil
|
99
|
+
spreadsheet.add_cell(row_index + 1, col_index, value.to_s)
|
100
|
+
else
|
101
|
+
spreadsheet.add_cell(row_index + 1, col_index, value).set_number_format(number_format)
|
102
|
+
end
|
69
103
|
end
|
70
104
|
end
|
71
105
|
end
|
106
|
+
# rubocop:enable Metrics/AbcSize
|
107
|
+
# rubocop:enable Metrics/MethodLength
|
72
108
|
|
73
109
|
def xlsx_output_iterators(iterable, compare_hash = nil)
|
74
110
|
if iterable.is_a? Hash
|
@@ -108,6 +144,7 @@ module WorkerTools
|
|
108
144
|
)
|
109
145
|
end
|
110
146
|
|
147
|
+
# rubocop:disable Metrics/AbcSize
|
111
148
|
def xlsx_output_write_sheet(workbook, sheet_content, index)
|
112
149
|
sheet = workbook.worksheets[index]
|
113
150
|
sheet = workbook.add_worksheet(sheet_content[:label]) if sheet.nil?
|
@@ -115,8 +152,9 @@ module WorkerTools
|
|
115
152
|
sheet.sheet_name = sheet_content[:label]
|
116
153
|
xlsx_output_style_columns(sheet, sheet_content[:column_style], sheet_content[:headers])
|
117
154
|
xlsx_output_insert_headers(sheet, sheet_content[:headers])
|
118
|
-
xlsx_output_insert_rows(sheet, sheet_content[:rows], sheet_content[:headers])
|
155
|
+
xlsx_output_insert_rows(sheet, sheet_content[:rows], sheet_content[:headers], sheet_content[:number_format])
|
119
156
|
end
|
157
|
+
# rubocop:enable Metrics/AbcSize
|
120
158
|
|
121
159
|
def xlsx_output_write_file
|
122
160
|
book = RubyXL::Workbook.new
|
@@ -129,4 +167,5 @@ module WorkerTools
|
|
129
167
|
xlsx_output_add_attachment
|
130
168
|
end
|
131
169
|
end
|
170
|
+
# rubocop:enable Metrics/ModuleLength
|
132
171
|
end
|
data/worker_tools.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.require_paths = ['lib']
|
28
28
|
|
29
29
|
spec.add_dependency 'activesupport'
|
30
|
+
spec.add_dependency 'get_process_mem'
|
30
31
|
spec.add_dependency 'roo'
|
31
32
|
spec.add_dependency 'rubyXL'
|
32
33
|
spec.add_dependency 'slack-notifier'
|
@@ -39,7 +40,7 @@ Gem::Specification.new do |spec|
|
|
39
40
|
spec.add_development_dependency 'mocha'
|
40
41
|
spec.add_development_dependency 'pry'
|
41
42
|
spec.add_development_dependency 'rake'
|
42
|
-
spec.add_development_dependency 'rubocop', '0.
|
43
|
+
spec.add_development_dependency 'rubocop', '0.72.0'
|
43
44
|
spec.add_development_dependency 'simplecov'
|
44
45
|
spec.add_development_dependency 'sqlite3'
|
45
46
|
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.
|
4
|
+
version: '1.3'
|
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: 2025-09-24 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
|
@@ -184,14 +198,14 @@ dependencies:
|
|
184
198
|
requirements:
|
185
199
|
- - '='
|
186
200
|
- !ruby/object:Gem::Version
|
187
|
-
version: 0.
|
201
|
+
version: 0.72.0
|
188
202
|
type: :development
|
189
203
|
prerelease: false
|
190
204
|
version_requirements: !ruby/object:Gem::Requirement
|
191
205
|
requirements:
|
192
206
|
- - '='
|
193
207
|
- !ruby/object:Gem::Version
|
194
|
-
version: 0.
|
208
|
+
version: 0.72.0
|
195
209
|
- !ruby/object:Gem::Dependency
|
196
210
|
name: simplecov
|
197
211
|
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
|
@@ -272,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
272
288
|
- !ruby/object:Gem::Version
|
273
289
|
version: '0'
|
274
290
|
requirements: []
|
275
|
-
rubygems_version: 3.1.
|
291
|
+
rubygems_version: 3.1.6
|
276
292
|
signing_key:
|
277
293
|
specification_version: 4
|
278
294
|
summary: A collection of modules to help writing common worker tasks)
|