ndb 1.14.0 → 1.16.0
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/README.md +201 -0
- data/exe/ndb +187 -0
- data/lib/ndb/client.rb +32 -4
- data/lib/ndb/connection.rb +38 -13
- data/lib/ndb/errors.rb +23 -1
- data/lib/ndb/redis_commands.rb +503 -0
- data/lib/ndb/stream.rb +363 -0
- data/lib/ndb/stream_worker.rb +591 -0
- data/lib/ndb/version.rb +1 -1
- data/lib/ndb.rb +3 -0
- metadata +8 -3
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ndb
|
|
4
|
+
# The error on_error gets for an entry that the worker sent to the dead
|
|
5
|
+
# letter and acked. reason is "max_deliveries" (it was delivered
|
|
6
|
+
# max_deliveries times) or "max_age" (it was older than max_age).
|
|
7
|
+
class PoisonMessage < Error
|
|
8
|
+
attr_reader :entry, :deliveries, :reason
|
|
9
|
+
|
|
10
|
+
def initialize(message, entry:, deliveries:, reason: Stream::Tiers::MAX_DELIVERIES)
|
|
11
|
+
super(message, code: "poison_message")
|
|
12
|
+
|
|
13
|
+
@entry = entry
|
|
14
|
+
@deliveries = deliveries
|
|
15
|
+
@reason = reason
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Stream
|
|
20
|
+
# A retry schedule: the wait (seconds) before the next try of an entry
|
|
21
|
+
# that failed on its delivery n. Make one with Stream.tiers. It is a
|
|
22
|
+
# callable, as the retry: option of a worker takes.
|
|
23
|
+
class Tiers
|
|
24
|
+
MAX_DELIVERIES = "max_deliveries"
|
|
25
|
+
MAX_AGE = "max_age"
|
|
26
|
+
|
|
27
|
+
# The longest wait of a nack (30 days).
|
|
28
|
+
MAX_WAIT = 30 * 86_400
|
|
29
|
+
|
|
30
|
+
# [[wait, up_to], ...]: the wait of a try up to up_to (nil: no end).
|
|
31
|
+
attr_reader :steps
|
|
32
|
+
|
|
33
|
+
# waits: seconds, one for each try; the last one repeats. Or steps:
|
|
34
|
+
# Hashes { wait:, up_to: }; a step is the wait of the tries up to
|
|
35
|
+
# up_to (the tries after the step before it), and the last one
|
|
36
|
+
# repeats.
|
|
37
|
+
def initialize(*waits)
|
|
38
|
+
waits = waits.flatten
|
|
39
|
+
|
|
40
|
+
raise ArgumentError, "tiers needs at least one wait" if waits.empty?
|
|
41
|
+
|
|
42
|
+
@steps = waits.all?(Hash) ? from_steps(waits) : from_waits(waits)
|
|
43
|
+
@steps.freeze
|
|
44
|
+
|
|
45
|
+
freeze
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The wait (seconds) after a failure on the delivery n (1 is the
|
|
49
|
+
# first delivery).
|
|
50
|
+
def call(deliveries)
|
|
51
|
+
step = @steps.find { |_, up_to| up_to.nil? || deliveries <= up_to }
|
|
52
|
+
|
|
53
|
+
(step || @steps.last).first
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def to_proc = method(:call).to_proc
|
|
57
|
+
|
|
58
|
+
# "15s, 1m, 10m, 1h, then 6h", or "15s x1, 1m x4, then 6h".
|
|
59
|
+
def to_s
|
|
60
|
+
plain = @steps.each_with_index.all? { |(_, up_to), index| up_to.nil? || up_to == index + 1 }
|
|
61
|
+
last = @steps.last.first
|
|
62
|
+
previous = 0
|
|
63
|
+
|
|
64
|
+
head = @steps[0...-1].map do |wait, up_to|
|
|
65
|
+
tries = up_to - previous
|
|
66
|
+
previous = up_to
|
|
67
|
+
|
|
68
|
+
plain ? Tiers.duration(wait) : "#{Tiers.duration(wait)} x#{tries}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
[*head, "then #{Tiers.duration(last)}"].join(", ").delete_prefix("then ")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
alias inspect to_s
|
|
75
|
+
|
|
76
|
+
# "15s", "1m", "6h", "15d"; "1.5s" for a part of a second.
|
|
77
|
+
def self.duration(seconds)
|
|
78
|
+
seconds = seconds.to_f
|
|
79
|
+
|
|
80
|
+
return "#{format_number(seconds / 86_400)}d" if seconds >= 86_400 && (seconds % 86_400).zero?
|
|
81
|
+
return "#{format_number(seconds / 3600)}h" if seconds >= 3600 && (seconds % 3600).zero?
|
|
82
|
+
return "#{format_number(seconds / 60)}m" if seconds >= 60 && (seconds % 60).zero?
|
|
83
|
+
|
|
84
|
+
"#{format_number(seconds)}s"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The first tries of any retry callable, for a log: "15s, 1m, ...".
|
|
88
|
+
def self.describe(retry_schedule, tries: 5)
|
|
89
|
+
return retry_schedule.to_s if retry_schedule.is_a?(Tiers)
|
|
90
|
+
|
|
91
|
+
waits = (1..tries).map { |n| duration(retry_schedule.call(n)) }
|
|
92
|
+
|
|
93
|
+
"#{waits.join(', ')}, ..."
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.format_number(value)
|
|
97
|
+
rounded = value.round(3)
|
|
98
|
+
|
|
99
|
+
rounded == rounded.to_i ? rounded.to_i.to_s : rounded.to_s
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def from_waits(waits)
|
|
105
|
+
waits.each_with_index.map do |wait, index|
|
|
106
|
+
[seconds(wait), index == waits.size - 1 ? nil : index + 1]
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def from_steps(steps)
|
|
111
|
+
previous = 0
|
|
112
|
+
|
|
113
|
+
steps.each_with_index.map do |step, index|
|
|
114
|
+
unknown = step.keys - %i[wait up_to]
|
|
115
|
+
|
|
116
|
+
raise ArgumentError, "a tier step has wait: and up_to:, not #{unknown.inspect}" unless unknown.empty?
|
|
117
|
+
|
|
118
|
+
up_to = step[:up_to]
|
|
119
|
+
last = index == steps.size - 1
|
|
120
|
+
|
|
121
|
+
unless (up_to.nil? && last) || (up_to.is_a?(Integer) && up_to > previous)
|
|
122
|
+
raise ArgumentError, "up_to of a tier step is an integer greater than the one before it (only the last step can have none), not #{up_to.inspect}"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
previous = up_to if up_to
|
|
126
|
+
|
|
127
|
+
[seconds(step.fetch(:wait)), up_to]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def seconds(wait)
|
|
132
|
+
unless wait.respond_to?(:to_f) && !wait.is_a?(String) && wait.to_f >= 0 && wait.to_f <= MAX_WAIT
|
|
133
|
+
raise ArgumentError, "a tier wait is a number of seconds from 0 to 30 days, not #{wait.inspect}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
wait.to_f
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A retry schedule for the retry: option of a worker:
|
|
141
|
+
#
|
|
142
|
+
# Ndb::Stream.tiers(15, 60, 600, 3600, 21_600) # try 1: 15 s, try 2: 1 min, ... then 6 h
|
|
143
|
+
# Ndb::Stream.tiers({ wait: 15, up_to: 1 }, { wait: 60, up_to: 5 }, { wait: 21_600 })
|
|
144
|
+
def self.tiers(*waits)
|
|
145
|
+
Tiers.new(*waits)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# A worker of a group: it runs the block on each entry. A block that
|
|
150
|
+
# returns acks the entry. A block that raises leaves it pending, and a
|
|
151
|
+
# worker of the group claims it again after claim_idle; with retry:, the
|
|
152
|
+
# entry comes back after the wait of the schedule instead. Any Exception
|
|
153
|
+
# is a failed entry (also NotImplementedError or SystemStackError), but a
|
|
154
|
+
# SignalException or SystemExit goes on to the caller.
|
|
155
|
+
#
|
|
156
|
+
# worker = orders.group("billing").worker("w1", max_deliveries: 5) { |e| charge(e.doc) }
|
|
157
|
+
# thread = worker.start # or worker.run, which blocks
|
|
158
|
+
# worker.stop # ends the loop after the current read
|
|
159
|
+
#
|
|
160
|
+
# Delivery is at least once: the work on an entry must be safe to repeat.
|
|
161
|
+
class Worker
|
|
162
|
+
# The most pending rows of one request for the delivery counts.
|
|
163
|
+
COUNTS_PAGE = 100
|
|
164
|
+
|
|
165
|
+
attr_reader :group, :consumer, :retry_schedule, :max_age
|
|
166
|
+
|
|
167
|
+
# count: the most entries per read (10). block: how long a read waits
|
|
168
|
+
# for new entries, in seconds (5). claim_idle: entries pending this
|
|
169
|
+
# long, of any consumer, are claimed and tried again (60; nil or a
|
|
170
|
+
# negative value turns the claim off). claim_every: how often to look
|
|
171
|
+
# for them (claim_idle / 2, at least 1). max_deliveries: an entry that
|
|
172
|
+
# fails on its max_deliveries-th delivery goes to dead_letter (a stream
|
|
173
|
+
# name) and is acked at once; nil is no limit. on_error: a callable that
|
|
174
|
+
# gets the errors that do not stop the worker (a failed read, a raise in
|
|
175
|
+
# the block, a PoisonMessage). A failed read, also of a group or stream
|
|
176
|
+
# that does not exist yet, is reported and tried again with a backoff
|
|
177
|
+
# (0.1 s, doubled up to 5 s): a worker that starts before its group
|
|
178
|
+
# waits for it.
|
|
179
|
+
#
|
|
180
|
+
# Delayed retry (ndb only):
|
|
181
|
+
#
|
|
182
|
+
# retry: the wait before the next try of a failed entry. A callable
|
|
183
|
+
# ->(deliveries) { seconds } (deliveries is 1 after the first failure),
|
|
184
|
+
# or an Array of waits in seconds, [15, 60, 600, 3600, 21_600] (the
|
|
185
|
+
# last one repeats; see Stream.tiers). The worker nacks the entry with
|
|
186
|
+
# that wait: it has no owner while it waits, and any worker of the
|
|
187
|
+
# group takes it when it is due. nil keeps the entry pending (claim_idle).
|
|
188
|
+
# max_age: seconds. An entry older than this (by the time of its ID)
|
|
189
|
+
# goes to dead_letter and is acked, without the block and with no retry.
|
|
190
|
+
# An entry that fails when it is older than this goes there too, at
|
|
191
|
+
# once (it is not nacked).
|
|
192
|
+
# due_every: how often to claim the due entries, in seconds (1 when
|
|
193
|
+
# retry: is set; else off, and claim_idle also takes them).
|
|
194
|
+
#
|
|
195
|
+
# The dead letter entry is a Hash with "stream", "group", "id",
|
|
196
|
+
# "deliveries", "reason" ("max_deliveries" or "max_age") and "doc".
|
|
197
|
+
#
|
|
198
|
+
# The block gets the entry. A block with two parameters also gets the
|
|
199
|
+
# delivery count of the entry (1 on the first delivery).
|
|
200
|
+
def initialize(group, consumer, count: 10, block: 5, claim_idle: 60, claim_every: nil, max_deliveries: nil, dead_letter: nil, on_error: nil, retry: nil, max_age: nil, due_every: nil, &handler)
|
|
201
|
+
raise ArgumentError, "a worker needs a block" unless handler
|
|
202
|
+
|
|
203
|
+
@group = group
|
|
204
|
+
@consumer = consumer
|
|
205
|
+
@handler = handler
|
|
206
|
+
|
|
207
|
+
@count = count
|
|
208
|
+
@block = block
|
|
209
|
+
@claim_idle = claim_idle && claim_idle.positive? ? claim_idle : nil
|
|
210
|
+
@claim_every = claim_every || [(@claim_idle || 0) / 2.0, 1].max
|
|
211
|
+
@max_deliveries = max_deliveries
|
|
212
|
+
@dead_letter = dead_letter
|
|
213
|
+
@on_error = on_error
|
|
214
|
+
|
|
215
|
+
@retry_schedule = retry_callable(binding.local_variable_get(:retry))
|
|
216
|
+
@max_age = positive_seconds(:max_age, max_age)
|
|
217
|
+
@due_every = due_every.nil? && @retry_schedule ? 1 : positive_seconds(:due_every, due_every)
|
|
218
|
+
|
|
219
|
+
@idle_cursor = "0-0"
|
|
220
|
+
@stopped = false
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Runs the loop in this thread until #stop. It:
|
|
224
|
+
#
|
|
225
|
+
# 1. reads its own pending entries first (the ones it took before a
|
|
226
|
+
# restart and did not ack), and does the work on them;
|
|
227
|
+
# 2. reads new entries, waiting up to block for them;
|
|
228
|
+
# 3. acks the entries the block did without a raise, in one call, and
|
|
229
|
+
# nacks the failed ones with the wait of retry:;
|
|
230
|
+
# 4. every due_every, claims the nacked entries whose wait passed;
|
|
231
|
+
# 5. every claim_every, claims the entries idle for claim_idle, and
|
|
232
|
+
# sends the ones delivered max_deliveries times to dead_letter.
|
|
233
|
+
def run
|
|
234
|
+
work_pending
|
|
235
|
+
|
|
236
|
+
next_claim = now + @claim_every
|
|
237
|
+
next_due = @due_every ? now + @due_every : nil
|
|
238
|
+
backoff = 0
|
|
239
|
+
|
|
240
|
+
until stopped?
|
|
241
|
+
if next_due && now >= next_due
|
|
242
|
+
work_due
|
|
243
|
+
|
|
244
|
+
next_due = now + @due_every
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
if @claim_idle && now >= next_claim
|
|
248
|
+
work_idle
|
|
249
|
+
|
|
250
|
+
next_claim = now + @claim_every
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
begin
|
|
254
|
+
entries = @group.read(@consumer, count: @count, block: [read_wait(next_claim, next_due), 0.001].max)
|
|
255
|
+
rescue Ndb::Error => e
|
|
256
|
+
report(e)
|
|
257
|
+
|
|
258
|
+
backoff = [[backoff * 2, 0.1].max, 5].min
|
|
259
|
+
|
|
260
|
+
pause(backoff)
|
|
261
|
+
|
|
262
|
+
next
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
backoff = 0
|
|
266
|
+
|
|
267
|
+
handle(entries)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
nil
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Runs the loop in a new Thread and returns it.
|
|
274
|
+
def start
|
|
275
|
+
Thread.new { run }
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Asks the loop to end. It ends after the current read (at most block
|
|
279
|
+
# seconds) and the work on the entries of that read.
|
|
280
|
+
def stop
|
|
281
|
+
@stopped = true
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def stopped? = @stopped
|
|
285
|
+
|
|
286
|
+
private
|
|
287
|
+
|
|
288
|
+
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
289
|
+
|
|
290
|
+
# The read waits for new entries until the next claim.
|
|
291
|
+
def read_wait(next_claim, next_due)
|
|
292
|
+
waits = [@block]
|
|
293
|
+
waits << next_claim - now if @claim_idle
|
|
294
|
+
waits << next_due - now if next_due
|
|
295
|
+
|
|
296
|
+
waits.min
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def pause(seconds)
|
|
300
|
+
deadline = now + seconds
|
|
301
|
+
|
|
302
|
+
sleep 0.01 while !stopped? && now < deadline
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def report(error)
|
|
306
|
+
@on_error&.call(error)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# An Array is a Stream.tiers; a callable is used as it is.
|
|
310
|
+
def retry_callable(value)
|
|
311
|
+
return nil if value.nil?
|
|
312
|
+
return Stream.tiers(value) if value.is_a?(Array)
|
|
313
|
+
return value if value.respond_to?(:call)
|
|
314
|
+
|
|
315
|
+
raise ArgumentError, "retry is an Array of waits or a callable ->(deliveries) { seconds }, not #{value.inspect}"
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def positive_seconds(name, value)
|
|
319
|
+
return nil if value.nil?
|
|
320
|
+
return value.to_f if value.respond_to?(:to_f) && !value.is_a?(String) && value.to_f.positive?
|
|
321
|
+
|
|
322
|
+
raise ArgumentError, "#{name} is a number of seconds greater than 0, not #{value.inspect}"
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# The consumer's own pending entries, in ID order. An entry that fails
|
|
326
|
+
# stays pending (or is nacked, with retry:); the loop moves past it. A
|
|
327
|
+
# failed read goes to on_error and is tried again after a backoff, also
|
|
328
|
+
# a group (or stream) that does not exist yet: the worker waits for it.
|
|
329
|
+
# A nacked entry has no owner, so this read does not give it.
|
|
330
|
+
#
|
|
331
|
+
# The pending read counts one more delivery. With max_deliveries, an
|
|
332
|
+
# entry above the limit had max_deliveries deliveries without an ack:
|
|
333
|
+
# the process stopped in the block (a crash). It goes to the dead letter
|
|
334
|
+
# without the block.
|
|
335
|
+
def work_pending
|
|
336
|
+
after = "0"
|
|
337
|
+
backoff = 0
|
|
338
|
+
|
|
339
|
+
until stopped?
|
|
340
|
+
begin
|
|
341
|
+
entries = @group.read(@consumer, count: @count, pending: true, after: after)
|
|
342
|
+
rescue Ndb::Error => e
|
|
343
|
+
report(e)
|
|
344
|
+
|
|
345
|
+
backoff = [[backoff * 2, 0.1].max, 5].min
|
|
346
|
+
|
|
347
|
+
pause(backoff)
|
|
348
|
+
|
|
349
|
+
next
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
backoff = 0
|
|
353
|
+
|
|
354
|
+
return if entries.empty?
|
|
355
|
+
|
|
356
|
+
counts = pending_counts(entries)
|
|
357
|
+
|
|
358
|
+
handle(without_crashed(entries, counts), counts)
|
|
359
|
+
|
|
360
|
+
after = entries.last.id
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# The entries to run: the ones above max_deliveries go to the dead
|
|
365
|
+
# letter. Their count has the last delivery, so they ran count - 1
|
|
366
|
+
# times.
|
|
367
|
+
def without_crashed(entries, counts)
|
|
368
|
+
return entries unless @max_deliveries
|
|
369
|
+
|
|
370
|
+
entries.reject do |entry|
|
|
371
|
+
n = counts.fetch(entry.id, 1)
|
|
372
|
+
|
|
373
|
+
next false if entry.deleted? || n <= @max_deliveries
|
|
374
|
+
|
|
375
|
+
bury(entry, n - 1, Stream::Tiers::MAX_DELIVERIES)
|
|
376
|
+
|
|
377
|
+
true
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Claims the nacked entries whose wait passed, batch by batch, and runs
|
|
382
|
+
# them. The claim counts one more delivery.
|
|
383
|
+
def work_due
|
|
384
|
+
cursor = "0-0"
|
|
385
|
+
|
|
386
|
+
until stopped?
|
|
387
|
+
claimed = @group.claim_due(@consumer, cursor: cursor, count: @count)
|
|
388
|
+
counts = pending_counts(claimed.entries)
|
|
389
|
+
|
|
390
|
+
handle(without_crashed(claimed.entries, counts), counts)
|
|
391
|
+
|
|
392
|
+
cursor = claimed.cursor
|
|
393
|
+
|
|
394
|
+
break if cursor.nil? || cursor == "0-0"
|
|
395
|
+
end
|
|
396
|
+
rescue Ndb::Error => e
|
|
397
|
+
report(e)
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# Claims the idle entries of the group with a scan, one batch each
|
|
401
|
+
# time, from where the last one ended. The scan skips the nacked
|
|
402
|
+
# entries that still wait. An entry above the limit is one that a
|
|
403
|
+
# stopped process did not bury: it goes to the dead letter. The others
|
|
404
|
+
# run again.
|
|
405
|
+
def work_idle
|
|
406
|
+
claimed = @group.auto_claim(@consumer, min_idle: @claim_idle, cursor: @idle_cursor, count: @count)
|
|
407
|
+
|
|
408
|
+
@idle_cursor = claimed.cursor || "0-0"
|
|
409
|
+
|
|
410
|
+
return if claimed.entries.empty?
|
|
411
|
+
|
|
412
|
+
counts = pending_counts(claimed.entries)
|
|
413
|
+
|
|
414
|
+
handle(without_crashed(claimed.entries, counts), counts)
|
|
415
|
+
rescue Ndb::Error => e
|
|
416
|
+
report(e)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# The delivery counts of the consumer's pending entries, by ID. Only a
|
|
420
|
+
# block that takes the count, a max_deliveries or a retry: needs them.
|
|
421
|
+
# It reads the pending list of the consumer from the first ID to the
|
|
422
|
+
# last, page by page, until it has every ID.
|
|
423
|
+
def pending_counts(entries)
|
|
424
|
+
return {} if entries.empty? || !(counts? || @max_deliveries || @retry_schedule)
|
|
425
|
+
|
|
426
|
+
wanted = entries.to_h { |entry| [entry.id, true] }
|
|
427
|
+
counts = {}
|
|
428
|
+
start = entries.first.id
|
|
429
|
+
page = [entries.size, COUNTS_PAGE].max
|
|
430
|
+
|
|
431
|
+
loop do
|
|
432
|
+
rows = @group.pending_list(start: start, stop: entries.last.id, count: page, consumer: @consumer)
|
|
433
|
+
|
|
434
|
+
rows.each { |p| counts[p["_id"]] = p["deliveries"] if wanted[p["_id"]] }
|
|
435
|
+
|
|
436
|
+
break if rows.size < page || counts.size == wanted.size
|
|
437
|
+
|
|
438
|
+
start = next_id(rows.last["_id"])
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
counts
|
|
442
|
+
rescue Ndb::Error => e
|
|
443
|
+
report(e)
|
|
444
|
+
|
|
445
|
+
{}
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# The ID just after id.
|
|
449
|
+
def next_id(id)
|
|
450
|
+
ms, _, seq = id.partition("-")
|
|
451
|
+
|
|
452
|
+
"#{ms}-#{seq.to_i + 1}"
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
def counts? = @handler.arity == 2
|
|
456
|
+
|
|
457
|
+
# Sends an entry to the dead letter, acks it, then reports it.
|
|
458
|
+
def bury(entry, deliveries, reason)
|
|
459
|
+
stream = @group.stream
|
|
460
|
+
|
|
461
|
+
if @dead_letter
|
|
462
|
+
dead = { stream: stream.name, group: @group.name, id: entry.id, deliveries: deliveries, reason: reason, doc: entry.doc }
|
|
463
|
+
|
|
464
|
+
stream.client.stream(@dead_letter).append(dead)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
@group.ack(entry.id)
|
|
468
|
+
|
|
469
|
+
report(PoisonMessage.new(poison_text(entry, deliveries, reason), entry: entry, deliveries: deliveries, reason: reason))
|
|
470
|
+
rescue Ndb::Error => e
|
|
471
|
+
report(e)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
def poison_text(entry, deliveries, reason)
|
|
475
|
+
where = "#{@group.stream.name}/#{@group.name} #{entry.id}"
|
|
476
|
+
|
|
477
|
+
return "poison message: #{where} after #{deliveries} deliveries" if reason == Stream::Tiers::MAX_DELIVERIES
|
|
478
|
+
|
|
479
|
+
"expired message: #{where} is older than max_age #{Stream::Tiers.duration(@max_age)} (#{deliveries} deliveries)"
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# True when the entry is older than max_age, by the time of its ID.
|
|
483
|
+
def expired?(entry)
|
|
484
|
+
@max_age && entry.time && Time.now - entry.time > @max_age
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
# A failed entry: on its max_deliveries-th delivery, or when it is now
|
|
488
|
+
# older than max_age, it goes to the dead letter; with retry: it is
|
|
489
|
+
# nacked with the wait of the schedule; else it stays pending.
|
|
490
|
+
def failed(entry, deliveries)
|
|
491
|
+
return bury(entry, deliveries, Stream::Tiers::MAX_DELIVERIES) if @max_deliveries && deliveries >= @max_deliveries
|
|
492
|
+
return bury(entry, deliveries, Stream::Tiers::MAX_AGE) if expired?(entry)
|
|
493
|
+
|
|
494
|
+
nack(entry, deliveries) if @retry_schedule
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
# Gives the entry back to the group after the wait of the schedule. A
|
|
498
|
+
# failed nack leaves it pending: claim_idle takes it.
|
|
499
|
+
def nack(entry, deliveries)
|
|
500
|
+
wait = @retry_schedule.call(deliveries).to_f.clamp(0, Stream::Tiers::MAX_WAIT)
|
|
501
|
+
|
|
502
|
+
@group.nack(@consumer, entry.id, delay: wait)
|
|
503
|
+
rescue Ndb::Error => e
|
|
504
|
+
report(e)
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
# Runs the block on each entry and acks the ones that worked, in one
|
|
508
|
+
# call. A deleted entry is acked without the block. An entry older
|
|
509
|
+
# than max_age goes to the dead letter without the block. counts has
|
|
510
|
+
# the delivery count of the entries that were delivered before. A stop
|
|
511
|
+
# leaves the entries not yet run pending.
|
|
512
|
+
def handle(entries, counts = {})
|
|
513
|
+
done = []
|
|
514
|
+
|
|
515
|
+
entries.each do |entry|
|
|
516
|
+
break if stopped?
|
|
517
|
+
|
|
518
|
+
if entry.deleted?
|
|
519
|
+
done << entry.id
|
|
520
|
+
|
|
521
|
+
next
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
n = counts.fetch(entry.id, 1)
|
|
525
|
+
|
|
526
|
+
if expired?(entry)
|
|
527
|
+
bury(entry, n, Stream::Tiers::MAX_AGE)
|
|
528
|
+
|
|
529
|
+
next
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
begin
|
|
533
|
+
counts? ? @handler.call(entry, n) : @handler.call(entry)
|
|
534
|
+
|
|
535
|
+
done << entry.id
|
|
536
|
+
rescue SignalException, SystemExit
|
|
537
|
+
raise
|
|
538
|
+
rescue Exception => e
|
|
539
|
+
# A raise after a stop is not a failure: the entry stays pending
|
|
540
|
+
# for the next run.
|
|
541
|
+
break if stopped?
|
|
542
|
+
|
|
543
|
+
report(e)
|
|
544
|
+
|
|
545
|
+
failed(entry, n)
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
ensure
|
|
549
|
+
# Also after a signal or Thread#raise in the block: the entries done
|
|
550
|
+
# before it are acked.
|
|
551
|
+
ack_done(done)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
def ack_done(ids)
|
|
555
|
+
return if ids.nil? || ids.empty?
|
|
556
|
+
|
|
557
|
+
@group.ack(ids)
|
|
558
|
+
rescue Ndb::Error => e
|
|
559
|
+
report(e)
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
class Group
|
|
564
|
+
# A Worker of this group as consumer, with the block as the handler. See
|
|
565
|
+
# Worker#initialize for the options.
|
|
566
|
+
def worker(consumer, **options, &handler)
|
|
567
|
+
Worker.new(self, consumer, **options, &handler)
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
# Runs a worker in this thread until it is stopped. Pass stop: a callable
|
|
571
|
+
# that returns true to end the loop (it is checked after each read), or
|
|
572
|
+
# build one with #worker and call Worker#stop from another thread.
|
|
573
|
+
def work(consumer, stop: nil, **options, &handler)
|
|
574
|
+
worker = Worker.new(self, consumer, **options, &handler)
|
|
575
|
+
|
|
576
|
+
return worker.run unless stop
|
|
577
|
+
|
|
578
|
+
watcher = Thread.new do
|
|
579
|
+
sleep 0.05 until stop.call || worker.stopped?
|
|
580
|
+
|
|
581
|
+
worker.stop
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
begin
|
|
585
|
+
worker.run
|
|
586
|
+
ensure
|
|
587
|
+
watcher.kill
|
|
588
|
+
end
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
end
|
data/lib/ndb/version.rb
CHANGED
data/lib/ndb.rb
CHANGED
metadata
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ndb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.16.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thadeu
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
description: 'Ndb::Client talks HTTP to ndb over a unix socket or TCP: collections,
|
|
13
13
|
documents with versions, queries, aggregates, watch streams and blobs. Stdlib only,
|
|
14
14
|
no runtime dependencies. The daemon decides every query semantic.'
|
|
15
|
-
executables:
|
|
15
|
+
executables:
|
|
16
|
+
- ndb
|
|
16
17
|
extensions: []
|
|
17
18
|
extra_rdoc_files: []
|
|
18
19
|
files:
|
|
19
20
|
- LICENSE
|
|
20
21
|
- README.md
|
|
22
|
+
- exe/ndb
|
|
21
23
|
- lib/ndb.rb
|
|
22
24
|
- lib/ndb/client.rb
|
|
23
25
|
- lib/ndb/collection.rb
|
|
@@ -25,6 +27,9 @@ files:
|
|
|
25
27
|
- lib/ndb/connection.rb
|
|
26
28
|
- lib/ndb/errors.rb
|
|
27
29
|
- lib/ndb/id.rb
|
|
30
|
+
- lib/ndb/redis_commands.rb
|
|
31
|
+
- lib/ndb/stream.rb
|
|
32
|
+
- lib/ndb/stream_worker.rb
|
|
28
33
|
- lib/ndb/version.rb
|
|
29
34
|
homepage: https://ndb.clowk.in
|
|
30
35
|
licenses:
|