esse-async_indexing 0.0.2

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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +35 -0
  3. data/CHANGELOG.md +14 -0
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +168 -0
  6. data/LICENSE +21 -0
  7. data/README.md +262 -0
  8. data/Rakefile +4 -0
  9. data/docker-compose.yml +11 -0
  10. data/lib/esse/async_indexing/actions/batch_delete.rb +15 -0
  11. data/lib/esse/async_indexing/actions/batch_import.rb +16 -0
  12. data/lib/esse/async_indexing/actions/batch_import_all.rb +11 -0
  13. data/lib/esse/async_indexing/actions/batch_update.rb +32 -0
  14. data/lib/esse/async_indexing/actions/bulk_update_lazy_document_attribute.rb +20 -0
  15. data/lib/esse/async_indexing/actions/coerce_index_repository.rb +28 -0
  16. data/lib/esse/async_indexing/actions/delete_document.rb +16 -0
  17. data/lib/esse/async_indexing/actions/import_batch_id.rb +21 -0
  18. data/lib/esse/async_indexing/actions/index_document.rb +20 -0
  19. data/lib/esse/async_indexing/actions/update_document.rb +20 -0
  20. data/lib/esse/async_indexing/actions/update_lazy_document_attribute.rb +14 -0
  21. data/lib/esse/async_indexing/actions/upsert_document.rb +25 -0
  22. data/lib/esse/async_indexing/actions.rb +21 -0
  23. data/lib/esse/async_indexing/active_record.rb +102 -0
  24. data/lib/esse/async_indexing/active_record_callbacks/callback.rb +27 -0
  25. data/lib/esse/async_indexing/active_record_callbacks/lazy_update_attribute.rb +26 -0
  26. data/lib/esse/async_indexing/active_record_callbacks/on_create.rb +15 -0
  27. data/lib/esse/async_indexing/active_record_callbacks/on_destroy.rb +15 -0
  28. data/lib/esse/async_indexing/active_record_callbacks/on_update.rb +27 -0
  29. data/lib/esse/async_indexing/adapters/adapter.rb +29 -0
  30. data/lib/esse/async_indexing/adapters/faktory.rb +114 -0
  31. data/lib/esse/async_indexing/adapters/sidekiq.rb +94 -0
  32. data/lib/esse/async_indexing/adapters.rb +12 -0
  33. data/lib/esse/async_indexing/cli/async_import.rb +58 -0
  34. data/lib/esse/async_indexing/cli.rb +32 -0
  35. data/lib/esse/async_indexing/config.rb +27 -0
  36. data/lib/esse/async_indexing/configuration/base.rb +65 -0
  37. data/lib/esse/async_indexing/configuration/faktory.rb +6 -0
  38. data/lib/esse/async_indexing/configuration/sidekiq.rb +12 -0
  39. data/lib/esse/async_indexing/configuration.rb +45 -0
  40. data/lib/esse/async_indexing/errors.rb +12 -0
  41. data/lib/esse/async_indexing/jobs/bulk_update_lazy_document_attribute_job.rb +7 -0
  42. data/lib/esse/async_indexing/jobs/document_delete_by_id_job.rb +7 -0
  43. data/lib/esse/async_indexing/jobs/document_index_by_id_job.rb +7 -0
  44. data/lib/esse/async_indexing/jobs/document_update_by_id_job.rb +7 -0
  45. data/lib/esse/async_indexing/jobs/document_upsert_by_id_job.rb +7 -0
  46. data/lib/esse/async_indexing/jobs/import_all_job.rb +7 -0
  47. data/lib/esse/async_indexing/jobs/import_batch_id_job.rb +34 -0
  48. data/lib/esse/async_indexing/jobs/update_lazy_document_attribute_job.rb +7 -0
  49. data/lib/esse/async_indexing/testing.rb +79 -0
  50. data/lib/esse/async_indexing/version.rb +7 -0
  51. data/lib/esse/async_indexing/worker.rb +85 -0
  52. data/lib/esse/async_indexing/workers/faktory.rb +28 -0
  53. data/lib/esse/async_indexing/workers/shared_class_methods.rb +26 -0
  54. data/lib/esse/async_indexing/workers/sidekiq.rb +28 -0
  55. data/lib/esse/async_indexing/workers.rb +48 -0
  56. data/lib/esse/async_indexing.rb +72 -0
  57. data/lib/esse/plugins/async_indexing.rb +106 -0
  58. data/lib/esse-async-indexing.rb +3 -0
  59. data/lib/esse-async_indexing.rb +3 -0
  60. metadata +244 -0
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Esse
4
+ module Plugins
5
+ module AsyncIndexing
6
+ module RepositoryClassMethods
7
+ DEFAULT_ASYNC_INDEXING_JOBS = {
8
+ import: ->(service:, repo:, operation:, ids:, **kwargs) {
9
+ unless (ids = Esse::ArrayUtils.wrap(ids)).empty?
10
+ batch_id = Esse::RedisStorage::Queue.for(repo: repo).enqueue(values: ids)
11
+ Esse::AsyncIndexing.worker("Esse::AsyncIndexing::Jobs::ImportBatchIdJob", service: service)
12
+ .with_args(repo.index.name, repo.repo_name, batch_id, Esse::HashUtils.deep_transform_keys(kwargs, &:to_s))
13
+ .push
14
+ end
15
+ },
16
+ index: ->(service:, repo:, operation:, id:, **kwargs) {
17
+ if id
18
+ Esse::AsyncIndexing.worker("Esse::AsyncIndexing::Jobs::DocumentIndexByIdJob", service: service)
19
+ .with_args(repo.index.name, repo.repo_name, id, Esse::HashUtils.deep_transform_keys(kwargs, &:to_s))
20
+ .push
21
+ end
22
+ },
23
+ update: ->(service:, repo:, operation:, id:, **kwargs) {
24
+ if id
25
+ Esse::AsyncIndexing.worker("Esse::AsyncIndexing::Jobs::DocumentUpdateByIdJob", service: service)
26
+ .with_args(repo.index.name, repo.repo_name, id, Esse::HashUtils.deep_transform_keys(kwargs, &:to_s))
27
+ .push
28
+ end
29
+ },
30
+ delete: ->(service:, repo:, operation:, id:, **kwargs) {
31
+ if id
32
+ Esse::AsyncIndexing.worker("Esse::AsyncIndexing::Jobs::DocumentDeleteByIdJob", service: service)
33
+ .with_args(repo.index.name, repo.repo_name, id, Esse::HashUtils.deep_transform_keys(kwargs, &:to_s))
34
+ .push
35
+ end
36
+ }
37
+ }.freeze
38
+
39
+ # This method is used to retrieve only the ids of the documents in the collection.
40
+ # It's used to asynchronously index the documents.
41
+ # The #each_batch_ids method is optional and should be implemented by the collection class.
42
+ #
43
+ # @return [Enumerator] The enumerator
44
+ def batch_ids(*args, **kwargs, &block)
45
+ if implement_batch_ids?
46
+ Enumerator.new do |yielder|
47
+ @collection_proc.new(*args, **kwargs).each_batch_ids do |batch|
48
+ yielder.yield(batch)
49
+ end
50
+ end
51
+ else
52
+ raise NotImplementedError, format("the %<t>p collection does not implement the #each_batch_ids method", t: @collection_proc)
53
+ end
54
+ end
55
+
56
+ # Check if the collection class implements the each_batch_ids method
57
+ #
58
+ # @return [Boolean] True if the collection class implements the each_batch_ids method
59
+ # @see #each_batch_ids
60
+ def implement_batch_ids?
61
+ @collection_proc.is_a?(Class) && @collection_proc.instance_methods.include?(:each_batch_ids)
62
+ end
63
+
64
+ # DSL to define custom job enqueueing
65
+ #
66
+ # async_indexing_job(:import) do |service:, repo:, operation:, ids:, **kwargs|
67
+ # MyCustomJob.perform_later(repo.index.name, ids, **kwargs)
68
+ # end
69
+ # async_indexing_job(:index, :update, :delete) do |service:, repo:, operation:, id, **kwargs|
70
+ # MyCustomJob.perform_later(repo.index.name, [id], **kwargs)
71
+ # end
72
+ def async_indexing_job(*operations, &block)
73
+ operations = AsyncIndexingJobValidator::OPERATIONS if operations.empty?
74
+ AsyncIndexingJobValidator.call(operations, block)
75
+ hash = operations.each_with_object({}) { |operation, h| h[operation] = block }
76
+ @async_indexing_jobs = async_indexing_jobs.dup.merge(hash)
77
+ ensure
78
+ @async_indexing_jobs.freeze
79
+ end
80
+
81
+ def async_indexing_jobs
82
+ @async_indexing_jobs || {}.freeze
83
+ end
84
+
85
+ def async_indexing_job_for(operation)
86
+ async_indexing_jobs[operation] || DEFAULT_ASYNC_INDEXING_JOBS[operation] || raise(ArgumentError, "The #{operation} operation is not implemented")
87
+ end
88
+
89
+ class AsyncIndexingJobValidator
90
+ OPERATIONS = %i[import index update delete].freeze
91
+
92
+ def self.call(operations, block)
93
+ unless block.is_a?(Proc)
94
+ raise ArgumentError, "The block of async_indexing_job must be a callable object"
95
+ end
96
+
97
+ operations.each do |operation|
98
+ next if OPERATIONS.include?(operation)
99
+ raise ArgumentError, format("Unrecognized operation: %<operation>p. Valid operations are: %<valid>p", operation: operation, valid: OPERATIONS)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "esse/async_indexing"
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "esse/async_indexing"
metadata ADDED
@@ -0,0 +1,244 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esse-async_indexing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Marcos G. Zimmermann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: esse
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.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.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: esse-redis_storage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: standard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-performance
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Extends Esse to allow async indexing using Faktory or Sidekiq.
154
+ email:
155
+ - mgzmaster@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".rubocop.yml"
161
+ - CHANGELOG.md
162
+ - Gemfile
163
+ - Gemfile.lock
164
+ - LICENSE
165
+ - README.md
166
+ - Rakefile
167
+ - docker-compose.yml
168
+ - lib/esse-async-indexing.rb
169
+ - lib/esse-async_indexing.rb
170
+ - lib/esse/async_indexing.rb
171
+ - lib/esse/async_indexing/actions.rb
172
+ - lib/esse/async_indexing/actions/batch_delete.rb
173
+ - lib/esse/async_indexing/actions/batch_import.rb
174
+ - lib/esse/async_indexing/actions/batch_import_all.rb
175
+ - lib/esse/async_indexing/actions/batch_update.rb
176
+ - lib/esse/async_indexing/actions/bulk_update_lazy_document_attribute.rb
177
+ - lib/esse/async_indexing/actions/coerce_index_repository.rb
178
+ - lib/esse/async_indexing/actions/delete_document.rb
179
+ - lib/esse/async_indexing/actions/import_batch_id.rb
180
+ - lib/esse/async_indexing/actions/index_document.rb
181
+ - lib/esse/async_indexing/actions/update_document.rb
182
+ - lib/esse/async_indexing/actions/update_lazy_document_attribute.rb
183
+ - lib/esse/async_indexing/actions/upsert_document.rb
184
+ - lib/esse/async_indexing/active_record.rb
185
+ - lib/esse/async_indexing/active_record_callbacks/callback.rb
186
+ - lib/esse/async_indexing/active_record_callbacks/lazy_update_attribute.rb
187
+ - lib/esse/async_indexing/active_record_callbacks/on_create.rb
188
+ - lib/esse/async_indexing/active_record_callbacks/on_destroy.rb
189
+ - lib/esse/async_indexing/active_record_callbacks/on_update.rb
190
+ - lib/esse/async_indexing/adapters.rb
191
+ - lib/esse/async_indexing/adapters/adapter.rb
192
+ - lib/esse/async_indexing/adapters/faktory.rb
193
+ - lib/esse/async_indexing/adapters/sidekiq.rb
194
+ - lib/esse/async_indexing/cli.rb
195
+ - lib/esse/async_indexing/cli/async_import.rb
196
+ - lib/esse/async_indexing/config.rb
197
+ - lib/esse/async_indexing/configuration.rb
198
+ - lib/esse/async_indexing/configuration/base.rb
199
+ - lib/esse/async_indexing/configuration/faktory.rb
200
+ - lib/esse/async_indexing/configuration/sidekiq.rb
201
+ - lib/esse/async_indexing/errors.rb
202
+ - lib/esse/async_indexing/jobs/bulk_update_lazy_document_attribute_job.rb
203
+ - lib/esse/async_indexing/jobs/document_delete_by_id_job.rb
204
+ - lib/esse/async_indexing/jobs/document_index_by_id_job.rb
205
+ - lib/esse/async_indexing/jobs/document_update_by_id_job.rb
206
+ - lib/esse/async_indexing/jobs/document_upsert_by_id_job.rb
207
+ - lib/esse/async_indexing/jobs/import_all_job.rb
208
+ - lib/esse/async_indexing/jobs/import_batch_id_job.rb
209
+ - lib/esse/async_indexing/jobs/update_lazy_document_attribute_job.rb
210
+ - lib/esse/async_indexing/testing.rb
211
+ - lib/esse/async_indexing/version.rb
212
+ - lib/esse/async_indexing/worker.rb
213
+ - lib/esse/async_indexing/workers.rb
214
+ - lib/esse/async_indexing/workers/faktory.rb
215
+ - lib/esse/async_indexing/workers/shared_class_methods.rb
216
+ - lib/esse/async_indexing/workers/sidekiq.rb
217
+ - lib/esse/plugins/async_indexing.rb
218
+ homepage: https://github.com/marcosgz/esse-async_indexing
219
+ licenses:
220
+ - MIT
221
+ metadata:
222
+ homepage_uri: https://github.com/marcosgz/esse-async_indexing
223
+ source_code_uri: https://github.com/marcosgz/esse-async_indexing
224
+ changelog_uri: https://github.com/marcosgz/esse-async_indexing/blob/main/CHANGELOG.md
225
+ post_install_message:
226
+ rdoc_options: []
227
+ require_paths:
228
+ - lib
229
+ required_ruby_version: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - ">="
232
+ - !ruby/object:Gem::Version
233
+ version: 2.7.0
234
+ required_rubygems_version: !ruby/object:Gem::Requirement
235
+ requirements:
236
+ - - ">="
237
+ - !ruby/object:Gem::Version
238
+ version: '0'
239
+ requirements: []
240
+ rubygems_version: 3.2.33
241
+ signing_key:
242
+ specification_version: 4
243
+ summary: Esse extension to allow async indexing using Faktory or Sidekiq.
244
+ test_files: []