ndb 1.15.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 +135 -3
- data/exe/ndb +187 -0
- data/lib/ndb/client.rb +3 -0
- data/lib/ndb/connection.rb +19 -11
- data/lib/ndb/errors.rb +6 -0
- data/lib/ndb/redis_commands.rb +503 -0
- data/lib/ndb/stream.rb +106 -28
- data/lib/ndb/stream_worker.rb +393 -47
- data/lib/ndb/version.rb +1 -1
- data/lib/ndb.rb +1 -0
- metadata +6 -3
data/lib/ndb/stream_worker.rb
CHANGED
|
@@ -1,22 +1,157 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Ndb
|
|
4
|
-
# The error on_error gets for
|
|
5
|
-
#
|
|
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).
|
|
6
7
|
class PoisonMessage < Error
|
|
7
|
-
attr_reader :entry, :deliveries
|
|
8
|
+
attr_reader :entry, :deliveries, :reason
|
|
8
9
|
|
|
9
|
-
def initialize(message, entry:, deliveries:)
|
|
10
|
+
def initialize(message, entry:, deliveries:, reason: Stream::Tiers::MAX_DELIVERIES)
|
|
10
11
|
super(message, code: "poison_message")
|
|
11
12
|
|
|
12
13
|
@entry = entry
|
|
13
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)
|
|
14
146
|
end
|
|
15
147
|
end
|
|
16
148
|
|
|
17
149
|
# A worker of a group: it runs the block on each entry. A block that
|
|
18
|
-
# returns acks the entry
|
|
19
|
-
# worker of the group claims it again after claim_idle
|
|
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.
|
|
20
155
|
#
|
|
21
156
|
# worker = orders.group("billing").worker("w1", max_deliveries: 5) { |e| charge(e.doc) }
|
|
22
157
|
# thread = worker.start # or worker.run, which blocks
|
|
@@ -24,18 +159,45 @@ module Ndb
|
|
|
24
159
|
#
|
|
25
160
|
# Delivery is at least once: the work on an entry must be safe to repeat.
|
|
26
161
|
class Worker
|
|
27
|
-
|
|
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
|
|
28
166
|
|
|
29
167
|
# count: the most entries per read (10). block: how long a read waits
|
|
30
168
|
# for new entries, in seconds (5). claim_idle: entries pending this
|
|
31
169
|
# long, of any consumer, are claimed and tried again (60; nil or a
|
|
32
170
|
# negative value turns the claim off). claim_every: how often to look
|
|
33
|
-
# for them (claim_idle / 2, at least 1). max_deliveries: an entry
|
|
34
|
-
#
|
|
35
|
-
# acked; nil is no limit. on_error: a callable that
|
|
36
|
-
# do not stop the worker (a failed read, a raise in
|
|
37
|
-
# PoisonMessage).
|
|
38
|
-
|
|
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)
|
|
39
201
|
raise ArgumentError, "a worker needs a block" unless handler
|
|
40
202
|
|
|
41
203
|
@group = group
|
|
@@ -50,6 +212,11 @@ module Ndb
|
|
|
50
212
|
@dead_letter = dead_letter
|
|
51
213
|
@on_error = on_error
|
|
52
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"
|
|
53
220
|
@stopped = false
|
|
54
221
|
end
|
|
55
222
|
|
|
@@ -58,26 +225,33 @@ module Ndb
|
|
|
58
225
|
# 1. reads its own pending entries first (the ones it took before a
|
|
59
226
|
# restart and did not ack), and does the work on them;
|
|
60
227
|
# 2. reads new entries, waiting up to block for them;
|
|
61
|
-
# 3. acks the entries the block did without a raise, in one call
|
|
62
|
-
#
|
|
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
|
|
63
232
|
# sends the ones delivered max_deliveries times to dead_letter.
|
|
64
233
|
def run
|
|
65
234
|
work_pending
|
|
66
235
|
|
|
67
236
|
next_claim = now + @claim_every
|
|
237
|
+
next_due = @due_every ? now + @due_every : nil
|
|
68
238
|
backoff = 0
|
|
69
239
|
|
|
70
240
|
until stopped?
|
|
241
|
+
if next_due && now >= next_due
|
|
242
|
+
work_due
|
|
243
|
+
|
|
244
|
+
next_due = now + @due_every
|
|
245
|
+
end
|
|
246
|
+
|
|
71
247
|
if @claim_idle && now >= next_claim
|
|
72
248
|
work_idle
|
|
73
249
|
|
|
74
250
|
next_claim = now + @claim_every
|
|
75
251
|
end
|
|
76
252
|
|
|
77
|
-
wait = @claim_idle ? [@block, next_claim - now].min : @block
|
|
78
|
-
|
|
79
253
|
begin
|
|
80
|
-
entries = @group.read(@consumer, count: @count, block: [
|
|
254
|
+
entries = @group.read(@consumer, count: @count, block: [read_wait(next_claim, next_due), 0.001].max)
|
|
81
255
|
rescue Ndb::Error => e
|
|
82
256
|
report(e)
|
|
83
257
|
|
|
@@ -113,6 +287,15 @@ module Ndb
|
|
|
113
287
|
|
|
114
288
|
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
115
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
|
+
|
|
116
299
|
def pause(seconds)
|
|
117
300
|
deadline = now + seconds
|
|
118
301
|
|
|
@@ -123,67 +306,210 @@ module Ndb
|
|
|
123
306
|
@on_error&.call(error)
|
|
124
307
|
end
|
|
125
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
|
+
|
|
126
325
|
# The consumer's own pending entries, in ID order. An entry that fails
|
|
127
|
-
# stays pending; the loop moves past it.
|
|
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.
|
|
128
335
|
def work_pending
|
|
129
336
|
after = "0"
|
|
337
|
+
backoff = 0
|
|
130
338
|
|
|
131
339
|
until stopped?
|
|
132
|
-
|
|
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
|
|
133
353
|
|
|
134
354
|
return if entries.empty?
|
|
135
355
|
|
|
136
|
-
|
|
356
|
+
counts = pending_counts(entries)
|
|
357
|
+
|
|
358
|
+
handle(without_crashed(entries, counts), counts)
|
|
137
359
|
|
|
138
360
|
after = entries.last.id
|
|
139
361
|
end
|
|
140
362
|
end
|
|
141
363
|
|
|
142
|
-
#
|
|
143
|
-
# letter
|
|
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.
|
|
144
405
|
def work_idle
|
|
145
|
-
|
|
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)
|
|
146
413
|
|
|
147
|
-
|
|
414
|
+
handle(without_crashed(claimed.entries, counts), counts)
|
|
415
|
+
rescue Ndb::Error => e
|
|
416
|
+
report(e)
|
|
417
|
+
end
|
|
148
418
|
|
|
149
|
-
|
|
150
|
-
|
|
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)
|
|
151
425
|
|
|
152
|
-
|
|
153
|
-
|
|
426
|
+
wanted = entries.to_h { |entry| [entry.id, true] }
|
|
427
|
+
counts = {}
|
|
428
|
+
start = entries.first.id
|
|
429
|
+
page = [entries.size, COUNTS_PAGE].max
|
|
154
430
|
|
|
155
|
-
|
|
431
|
+
loop do
|
|
432
|
+
rows = @group.pending_list(start: start, stop: entries.last.id, count: page, consumer: @consumer)
|
|
156
433
|
|
|
157
|
-
|
|
434
|
+
rows.each { |p| counts[p["_id"]] = p["deliveries"] if wanted[p["_id"]] }
|
|
158
435
|
|
|
159
|
-
|
|
436
|
+
break if rows.size < page || counts.size == wanted.size
|
|
437
|
+
|
|
438
|
+
start = next_id(rows.last["_id"])
|
|
160
439
|
end
|
|
161
440
|
|
|
162
|
-
|
|
441
|
+
counts
|
|
163
442
|
rescue Ndb::Error => e
|
|
164
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}"
|
|
165
453
|
end
|
|
166
454
|
|
|
167
|
-
|
|
168
|
-
|
|
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)
|
|
169
459
|
stream = @group.stream
|
|
170
460
|
|
|
171
461
|
if @dead_letter
|
|
172
|
-
dead = { stream: stream.name, group: @group.name, id: entry.id, deliveries: deliveries, doc: entry.doc }
|
|
462
|
+
dead = { stream: stream.name, group: @group.name, id: entry.id, deliveries: deliveries, reason: reason, doc: entry.doc }
|
|
173
463
|
|
|
174
464
|
stream.client.stream(@dead_letter).append(dead)
|
|
175
465
|
end
|
|
176
466
|
|
|
177
467
|
@group.ack(entry.id)
|
|
178
468
|
|
|
179
|
-
report(PoisonMessage.new(
|
|
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)
|
|
180
503
|
rescue Ndb::Error => e
|
|
181
504
|
report(e)
|
|
182
505
|
end
|
|
183
506
|
|
|
184
507
|
# Runs the block on each entry and acks the ones that worked, in one
|
|
185
|
-
# call. A deleted entry is acked without the block.
|
|
186
|
-
|
|
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 = {})
|
|
187
513
|
done = []
|
|
188
514
|
|
|
189
515
|
entries.each do |entry|
|
|
@@ -195,22 +521,42 @@ module Ndb
|
|
|
195
521
|
next
|
|
196
522
|
end
|
|
197
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
|
+
|
|
198
532
|
begin
|
|
199
|
-
@handler.call(entry)
|
|
533
|
+
counts? ? @handler.call(entry, n) : @handler.call(entry)
|
|
200
534
|
|
|
201
535
|
done << entry.id
|
|
202
|
-
rescue
|
|
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
|
+
|
|
203
543
|
report(e)
|
|
544
|
+
|
|
545
|
+
failed(entry, n)
|
|
204
546
|
end
|
|
205
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
|
|
206
553
|
|
|
207
|
-
|
|
554
|
+
def ack_done(ids)
|
|
555
|
+
return if ids.nil? || ids.empty?
|
|
208
556
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
report(e)
|
|
213
|
-
end
|
|
557
|
+
@group.ack(ids)
|
|
558
|
+
rescue Ndb::Error => e
|
|
559
|
+
report(e)
|
|
214
560
|
end
|
|
215
561
|
end
|
|
216
562
|
|
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,7 @@ 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
|
|
28
31
|
- lib/ndb/stream.rb
|
|
29
32
|
- lib/ndb/stream_worker.rb
|
|
30
33
|
- lib/ndb/version.rb
|