anway 0.4.0-aarch64-linux-gnu → 0.5.0-aarch64-linux-gnu

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3256b906ca160fd38f7bef88324e54fe5758716ea1cf0f839dcb6ebe552f4513
4
- data.tar.gz: 259b306e73018b8935e68652113ad54eae5db0bb9ed457f85c4cb127862e09db
3
+ metadata.gz: a2becf6866b6061f3147f57b6297de5286103a77082c9e1d13ce9c6dc4c73cfb
4
+ data.tar.gz: 590370fce81f67766fa12e499213af7020825b89c30aa7c15a2702ad62bb7444
5
5
  SHA512:
6
- metadata.gz: 1955e6210abf3704a636fa46eb23da30b7c8d9c63951e82645e0f00be67b546de8a71a5a231d520983a10106d861ff8bba5fcdd60b4d3775e91c8b2e0362f95e
7
- data.tar.gz: 91508df20225cb275beb69969a763f259fafc2665bcf918d505e688d1d382eccf08a8fcd655fbbf3a6feb0b84b6fc7f8d26cd877b0d58dfd277d6497d35985b3
6
+ metadata.gz: c8e15c3a326397d7dd68c699c3919047cf1530b6454d8673ceb97ae14bbcca67446694b255a503630a435d1a01b806cbba563c6a37aa26f5c11186b1e637388b
7
+ data.tar.gz: ba8ddb723267e1df464e8138f4bfab53960e53b81860ee0e77fae9608d3443a1743eadffbe2e444af0a6569b6f802669c870b0d178bbbbde4478e14de393463d
data/README.md CHANGED
@@ -155,6 +155,29 @@ Anway.push('image.resize', [42], queue: 'media', expires_in: 600)
155
155
  Anway::Job.new('email.welcome', [1], unique_for: 60, queue: 'low')
156
156
  ```
157
157
 
158
+ ## Bulk push
159
+
160
+ A bulk push sends many jobs with one `PUSHB` command, and the server puts them in with one write. Use it to push a large list of jobs fast. Bulk push needs server v0.3.2 or later.
161
+
162
+ ```ruby
163
+ WelcomeJob.perform_bulk([[1], [2], [3]])
164
+ # => {}
165
+
166
+ WelcomeJob.set(queue: 'low', at: Time.now + 300).perform_bulk(users.map { |user| [user.id] })
167
+
168
+ Anway.push_bulk([Anway::Job.new('image.resize', [1]), Anway::Job.new('image.resize', [2])])
169
+ ```
170
+
171
+ - Each item of `perform_bulk` is the args of one job. The jobs get the options of the class and of `set`, as `perform_async` does.
172
+ - The result is a hash with the reason for each job that the server refused, by jid. When all jobs are in, it is `{}`.
173
+ - A refused job, such as a duplicate jid or a job that a unique lock blocks, does not stop the other jobs.
174
+ - One `PUSHB` takes at most 1,000 jobs. `perform_bulk` and `Anway.push_bulk` split a longer list into several `PUSHB` commands, and merge the failures.
175
+ - The gem builds all jobs before the first send. An item that is not valid raises `ArgumentError`, and no job goes to the server.
176
+ - In a database transaction, the push waits for the commit, and the result is `nil`. A rollback cancels the push.
177
+ - A list of more than 1,000 jobs uses more than one `PUSHB`. If a connection error occurs after the first `PUSHB`, the jobs of the earlier commands stay in, and the gem does not send them again.
178
+
179
+ `client.push_bulk(jobs)` sends one `PUSHB`. It takes at most 1,000 jobs, and more raise `ArgumentError` before the send.
180
+
158
181
  ## Topics
159
182
 
160
183
  A topic sends one event to many queues. Each subscription of the topic gets one copy of the event, as a normal job in its own queue. Topics need server v0.3.0 or later.
@@ -200,6 +223,7 @@ config.active_job.queue_adapter = :anway
200
223
  - In a database transaction, a push waits for the commit, and a rollback cancels it. This also applies to `perform_async`.
201
224
  - A job with `retry_on` keeps ActiveJob attempts, and its final error goes to the dead set.
202
225
  - A job with no `retry_on` uses server retries.
226
+ - `ActiveJob.perform_all_later(jobs)` sends one `PUSHB` for each 1,000 jobs, for jobs of all classes and queues. A job with `set(wait:)` or `set(wait_until:)` keeps its time. Each job gets `successfully_enqueued`, and a job that the server refused gets an `ActiveJob::EnqueueError` in `enqueue_error`.
203
227
 
204
228
  Tasks in `recurring.yml` and jobs that wait in Solid Queue do not move automatically. Move recurring tasks to the server cron, and drain the old queue first.
205
229
 
@@ -305,6 +329,8 @@ Anway::Testing.inline!
305
329
 
306
330
  In inline mode, `Anway.publish` runs one copy in each job class of the app that subscribes to the topic.
307
331
 
332
+ `perform_bulk` and `Anway.push_bulk` work as single pushes do: fake mode adds each job to `Anway::Testing.jobs`, and inline mode runs each job. The result is `{}`.
333
+
308
334
  ## Client
309
335
 
310
336
  `Anway::Client` has the operations of [anway-go](https://github.com/thadeu/anway-go), in snake_case. It is safe for threads and for forks.
@@ -312,6 +338,7 @@ In inline mode, `Anway.publish` runs one copy in each job class of the app that
312
338
  ```ruby
313
339
  client = Anway::Client.new('tcp://localhost:7419')
314
340
  client.push(Anway::Job.new('email.welcome', [1]))
341
+ client.push_bulk([Anway::Job.new('email.welcome', [2]), Anway::Job.new('email.welcome', [3])])
315
342
  ```
316
343
 
317
344
  The error kinds are `Anway::NotUnique`, `Anway::Closed`, `Anway::ServerError`, and `Anway::ConnectionError`. A failed call does not resend.
@@ -46,23 +46,56 @@ module ActiveJob
46
46
  push(job, { at: Time.at(timestamp) })
47
47
  end
48
48
 
49
+ # ActiveJob.perform_all_later calls this. Jobs of all classes and queues
50
+ # go together, with one PUSHB for each 1,000 jobs, and a scheduled job
51
+ # gets its at. Sets successfully_enqueued on each job, and enqueue_error
52
+ # on a refused job. Returns the number of jobs in. Inside a database
53
+ # transaction, the push waits for the commit, and each job counts as in,
54
+ # as perform_later does.
55
+ def enqueue_all(jobs)
56
+ pushed = jobs.map { |job| [job, build(job, scheduled(job))] }
57
+ failures = Anway.push_bulk(pushed.map(&:last)) || {}
58
+
59
+ pushed.each do |job, wire|
60
+ reason = failures[wire.jid]
61
+
62
+ job.provider_job_id = wire.jid
63
+ job.successfully_enqueued = reason.nil?
64
+ job.enqueue_error = reason && ActiveJob::EnqueueError.new(reason)
65
+ end
66
+
67
+ jobs.count(&:successfully_enqueued?)
68
+ end
69
+
49
70
  private
50
71
 
72
+ def scheduled(job)
73
+ job.scheduled_at ? { at: Time.at(job.scheduled_at.to_f) } : {}
74
+ end
75
+
51
76
  # Each push has a new id. A retry_on attempt keeps its job_id while the
52
77
  # first attempt is still reserved, and the server rejects a repeated id.
53
78
  # custom.wrapped names the job class, so that the console shows it, as
54
79
  # Sidekiq does. The options are the push keys of the input contract.
55
80
  def push(job, options)
81
+ pushed = build(job, options)
82
+
83
+ job.provider_job_id = pushed.jid
84
+
85
+ Anway.enqueue(pushed)
86
+ end
87
+
88
+ def build(job, options)
56
89
  options = options.merge(queue: job.queue_name, backtrace: BACKTRACE, custom: { 'wrapped' => job.class.name })
57
90
 
58
91
  # retry_on owns the attempts, so its final error goes to the dead set.
59
92
  options[:retry] = DEAD_SET if job.class.anway_retry_on
60
93
 
61
- pushed = Anway::Job.new(Anway::ActiveJobWrapper::EVENT, [job.serialize.as_json], **options)
62
-
63
- job.provider_job_id = pushed.jid
64
-
65
- Anway.enqueue(pushed)
94
+ Anway::Job.new(
95
+ Anway::ActiveJobWrapper::EVENT,
96
+ [job.serialize.as_json],
97
+ **options
98
+ )
66
99
  end
67
100
  end
68
101
  end
data/lib/anway/client.rb CHANGED
@@ -10,6 +10,9 @@ module Anway
10
10
  DEFAULT_PORT = 7419
11
11
  FETCH_WAIT = 2
12
12
 
13
+ # The most jobs that one PUSHB takes.
14
+ MAX_BULK = 1000
15
+
13
16
  Endpoint = Data.define(:host, :port, :password, :tls)
14
17
 
15
18
  # The BEAT reply: the state (nil, "quiet", or "terminate"), and the thread
@@ -42,6 +45,20 @@ module Anway
42
45
  job.jid
43
46
  end
44
47
 
48
+ # Sends up to MAX_BULK jobs with one PUSHB. A refused job, such as a
49
+ # duplicate jid or a unique lock, does not stop the others. Returns the
50
+ # reason for each refused job by jid, or {} when all jobs are in. More
51
+ # than MAX_BULK jobs raise ArgumentError before the send.
52
+ def push_bulk(jobs)
53
+ jobs = jobs.to_a
54
+
55
+ raise ArgumentError, "a bulk push has at most #{MAX_BULK} jobs, not #{jobs.size}" if jobs.size > MAX_BULK
56
+
57
+ return {} if jobs.empty?
58
+
59
+ JSON.parse(call('PUSHB', jobs.map(&:to_h)))
60
+ end
61
+
45
62
  # Returns the next job, or nil when no job arrives in about 2 seconds.
46
63
  def fetch(*queues)
47
64
  reply = call('FETCH', queues.join(' '), wait: FETCH_WAIT)
data/lib/anway/testing.rb CHANGED
@@ -42,6 +42,14 @@ module Anway
42
42
  job.jid
43
43
  end
44
44
 
45
+ # Keeps or runs each job as a single push does. No job is refused, so
46
+ # the result is {}.
47
+ def push_bulk(jobs)
48
+ jobs.each { |job| enqueue(job) }
49
+
50
+ {}
51
+ end
52
+
45
53
  # Keeps the event as the PUBLISH payload, with string keys. jobs is the
46
54
  # number of subscriptions in the job classes of the app. There is no
47
55
  # duplicate check.
data/lib/anway/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anway
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
 
6
6
  # The anway server that the platform gems carry, and that CI tests against.
7
- SERVER_VERSION = '0.3.1'
7
+ SERVER_VERSION = '0.3.2'
8
8
  end
data/lib/anway/worker.rb CHANGED
@@ -14,6 +14,7 @@ module Anway
14
14
  #
15
15
  # WelcomeJob.perform_async(42)
16
16
  # WelcomeJob.set(queue: 'low', unique_for: 60).perform_async(42)
17
+ # WelcomeJob.perform_bulk([[1], [2], [3]])
17
18
  #
18
19
  # The keys are the input contract (Anway::Options). The event is the job
19
20
  # type on the wire, and the default is the class name. Each topic makes one
@@ -158,17 +159,48 @@ module Anway
158
159
  Setter.new(self, options)
159
160
  end
160
161
 
161
- # The options of the class, then the options of the push, merge with
162
- # the same rules as two anway_options calls.
162
+ # Pushes one job for each item, and each item is the args of one job:
163
+ # WelcomeJob.perform_bulk([[1], [2], [3]]). All jobs are built before
164
+ # the first send, so a bad item sends no job. Each Client::MAX_BULK jobs
165
+ # go in one PUSHB. Returns the reason for each refused job by jid, or {}
166
+ # when all jobs are in. Inside a database transaction, the push waits
167
+ # for the commit and returns nil.
168
+ def perform_bulk(args_list)
169
+ bulk_with(args_list, {})
170
+ end
171
+
163
172
  def enqueue_with(args, options)
164
- declared = declared_options
165
- push = Options.merge({ backtrace: DEFAULT_BACKTRACE }, declared.except(*OWN_KEYS), options)
173
+ event, push = push_options(options)
174
+
175
+ Anway.enqueue(Job.new(event, args, **push))
176
+ end
177
+
178
+ def bulk_with(args_list, options)
179
+ raise ArgumentError, "perform_bulk needs an array of args: #{args_list.inspect}" unless args_list.is_a?(Array)
180
+
181
+ event, push = push_options(options)
182
+
183
+ jobs = args_list.map do |args|
184
+ Job.validate_args!(args)
166
185
 
167
- Anway.enqueue(Job.new(declared[:event], args, **push))
186
+ Job.new(event, args, **push)
187
+ end
188
+
189
+ Anway.push_bulk(jobs)
168
190
  end
169
191
 
170
192
  private
171
193
 
194
+ # The event, and the push keys: the options of the class, then the
195
+ # options of the push, merge with the same rules as two anway_options
196
+ # calls.
197
+ def push_options(options)
198
+ declared = declared_options
199
+ push = Options.merge({ backtrace: DEFAULT_BACKTRACE }, declared.except(*OWN_KEYS), options)
200
+
201
+ [declared[:event], push]
202
+ end
203
+
172
204
  def declared_options
173
205
  parent = superclass.respond_to?(:anway_options) ? superclass.anway_options.except(*OWN_KEYS) : {}
174
206
 
@@ -212,6 +244,10 @@ module Anway
212
244
  def perform_in(seconds, *args)
213
245
  @klass.enqueue_with(args, @options.merge(at: Time.now + seconds.to_f))
214
246
  end
247
+
248
+ def perform_bulk(args_list)
249
+ @klass.bulk_with(args_list, @options)
250
+ end
215
251
  end
216
252
  end
217
253
  end
data/lib/anway.rb CHANGED
@@ -57,6 +57,25 @@ module Anway
57
57
  job.jid
58
58
  end
59
59
 
60
+ # Sends the jobs with one PUSHB for each Client::MAX_BULK jobs. Returns
61
+ # the reason for each refused job by jid, from all the sends, or {} when
62
+ # all jobs are in. Inside a database transaction, the push waits for the
63
+ # commit and returns nil.
64
+ def push_bulk(jobs)
65
+ jobs = jobs.to_a
66
+ testing = const_defined?(:Testing) ? Testing : nil
67
+
68
+ return testing.push_bulk(jobs) if testing&.mode
69
+
70
+ failures = nil
71
+
72
+ config.push_wrapper.call do
73
+ failures = jobs.each_slice(Client::MAX_BULK).reduce({}) { |merged, slice| merged.merge(client.push_bulk(slice)) }
74
+ end
75
+
76
+ failures
77
+ end
78
+
60
79
  # Sends one event to each subscription of the topic. Returns a
61
80
  # Client::Published. Inside a database transaction, the publish waits for
62
81
  # the commit and returns nil.
data/libexec/anway-server CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: aarch64-linux-gnu
6
6
  authors:
7
7
  - Thadeu