robin 0.0.4 → 0.1.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.
Files changed (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +372 -0
  3. data/COPYING +674 -0
  4. data/LICENSE +165 -0
  5. data/README.md +1490 -0
  6. data/assets/favicon.svg +31 -0
  7. data/assets/robin-logo.svg +37 -0
  8. data/assets/robin-mono.svg +12 -0
  9. data/assets/robin-wordmark.svg +4 -0
  10. data/assets/robin.svg +34 -0
  11. data/exe/robin +5 -0
  12. data/ext/robin_resp/extconf.rb +29 -0
  13. data/ext/robin_resp/robin_resp.c +645 -0
  14. data/lib/active_job/queue_adapters/robin_adapter.rb +3 -0
  15. data/lib/generators/robin/install/install_generator.rb +39 -0
  16. data/lib/generators/robin/install/templates/initializer.rb.tt +31 -0
  17. data/lib/generators/robin/install/templates/robin.yml.tt +25 -0
  18. data/lib/generators/robin/job/job_generator.rb +78 -0
  19. data/lib/generators/robin/job/templates/job.rb.tt +13 -0
  20. data/lib/generators/robin/job/templates/job_spec.rb.tt +21 -0
  21. data/lib/generators/robin/job/templates/job_test.rb.tt +21 -0
  22. data/lib/puma/plugin/robin.rb +131 -0
  23. data/lib/robin/active_job.rb +144 -0
  24. data/lib/robin/api.rb +777 -0
  25. data/lib/robin/batch.rb +700 -0
  26. data/lib/robin/cancellation.rb +62 -0
  27. data/lib/robin/cli.rb +325 -0
  28. data/lib/robin/client.rb +355 -21
  29. data/lib/robin/config.rb +454 -4
  30. data/lib/robin/cron.rb +325 -0
  31. data/lib/robin/debounce.rb +108 -0
  32. data/lib/robin/deploy.rb +65 -0
  33. data/lib/robin/embedded.rb +53 -0
  34. data/lib/robin/encryption.rb +147 -0
  35. data/lib/robin/events.rb +60 -0
  36. data/lib/robin/expiry.rb +41 -0
  37. data/lib/robin/fetcher.rb +441 -0
  38. data/lib/robin/gvl.rb +62 -0
  39. data/lib/robin/health.rb +76 -0
  40. data/lib/robin/iterable_job.rb +294 -0
  41. data/lib/robin/job.rb +258 -0
  42. data/lib/robin/job_class.rb +157 -0
  43. data/lib/robin/job_logger.rb +121 -0
  44. data/lib/robin/launcher.rb +658 -0
  45. data/lib/robin/leader.rb +86 -0
  46. data/lib/robin/limiter.rb +639 -0
  47. data/lib/robin/manager.rb +125 -0
  48. data/lib/robin/metrics.rb +303 -0
  49. data/lib/robin/middleware.rb +165 -0
  50. data/lib/robin/opentelemetry.rb +88 -0
  51. data/lib/robin/periodic.rb +327 -0
  52. data/lib/robin/processor.rb +317 -0
  53. data/lib/robin/profiler.rb +140 -0
  54. data/lib/robin/progress.rb +46 -0
  55. data/lib/robin/rails/app_loader.rb +23 -0
  56. data/lib/robin/rails/current_attributes.rb +88 -0
  57. data/lib/robin/rails/error_reporter.rb +31 -0
  58. data/lib/robin/rails/notifications.rb +22 -0
  59. data/lib/robin/rails/railtie.rb +87 -0
  60. data/lib/robin/rails/transactional_push.rb +49 -0
  61. data/lib/robin/rails.rb +23 -0
  62. data/lib/robin/recovery.rb +135 -0
  63. data/lib/robin/redis/connection.rb +639 -0
  64. data/lib/robin/redis/pool.rb +190 -0
  65. data/lib/robin/redis/resp.rb +77 -0
  66. data/lib/robin/reliable_push.rb +153 -0
  67. data/lib/robin/retrier.rb +284 -0
  68. data/lib/robin/scheduler.rb +102 -0
  69. data/lib/robin/scripts.rb +331 -0
  70. data/lib/robin/statsd.rb +135 -0
  71. data/lib/robin/swarm.rb +319 -0
  72. data/lib/robin/systemd.rb +47 -0
  73. data/lib/robin/testing.rb +158 -0
  74. data/lib/robin/top.rb +178 -0
  75. data/lib/robin/unique.rb +198 -0
  76. data/lib/robin/version.rb +5 -0
  77. data/lib/robin/warmup.rb +94 -0
  78. data/lib/robin/web/api.rb +580 -0
  79. data/lib/robin/web/batch_status.rb +44 -0
  80. data/lib/robin/web/dist/assets/ar-y7v9b75f.js +1 -0
  81. data/lib/robin/web/dist/assets/ar-y7v9b75f.js.gz +0 -0
  82. data/lib/robin/web/dist/assets/bricolage-0qmzcpnj.woff2 +0 -0
  83. data/lib/robin/web/dist/assets/cs-df88mq5e.js +1 -0
  84. data/lib/robin/web/dist/assets/cs-df88mq5e.js.gz +0 -0
  85. data/lib/robin/web/dist/assets/da-kemms74g.js +1 -0
  86. data/lib/robin/web/dist/assets/da-kemms74g.js.gz +0 -0
  87. data/lib/robin/web/dist/assets/de-z8kkqg65.js +1 -0
  88. data/lib/robin/web/dist/assets/de-z8kkqg65.js.gz +0 -0
  89. data/lib/robin/web/dist/assets/es-9ahrrn1s.js +1 -0
  90. data/lib/robin/web/dist/assets/es-9ahrrn1s.js.gz +0 -0
  91. data/lib/robin/web/dist/assets/fa-7r0x28nc.js +1 -0
  92. data/lib/robin/web/dist/assets/fa-7r0x28nc.js.gz +0 -0
  93. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg +31 -0
  94. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg.gz +0 -0
  95. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js +1 -0
  96. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js.gz +0 -0
  97. data/lib/robin/web/dist/assets/he-7t46b6rt.js +1 -0
  98. data/lib/robin/web/dist/assets/he-7t46b6rt.js.gz +0 -0
  99. data/lib/robin/web/dist/assets/hi-zj434tx4.js +1 -0
  100. data/lib/robin/web/dist/assets/hi-zj434tx4.js.gz +0 -0
  101. data/lib/robin/web/dist/assets/it-pqc2m7gm.js +1 -0
  102. data/lib/robin/web/dist/assets/it-pqc2m7gm.js.gz +0 -0
  103. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js +1 -0
  104. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js.gz +0 -0
  105. data/lib/robin/web/dist/assets/ko-bxersv91.js +1 -0
  106. data/lib/robin/web/dist/assets/ko-bxersv91.js.gz +0 -0
  107. data/lib/robin/web/dist/assets/main-anw3f6ba.css +1 -0
  108. data/lib/robin/web/dist/assets/main-anw3f6ba.css.gz +0 -0
  109. data/lib/robin/web/dist/assets/main-pdtvjejb.js +7 -0
  110. data/lib/robin/web/dist/assets/main-pdtvjejb.js.gz +0 -0
  111. data/lib/robin/web/dist/assets/main-zz8zzhwn.js +2 -0
  112. data/lib/robin/web/dist/assets/main-zz8zzhwn.js.gz +0 -0
  113. data/lib/robin/web/dist/assets/nb-fjnc5064.js +1 -0
  114. data/lib/robin/web/dist/assets/nb-fjnc5064.js.gz +0 -0
  115. data/lib/robin/web/dist/assets/nl-9p8b95b6.js +1 -0
  116. data/lib/robin/web/dist/assets/nl-9p8b95b6.js.gz +0 -0
  117. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js +1 -0
  118. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js.gz +0 -0
  119. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js +1 -0
  120. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js.gz +0 -0
  121. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg +34 -0
  122. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg.gz +0 -0
  123. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg +4 -0
  124. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg.gz +0 -0
  125. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js +1 -0
  126. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js.gz +0 -0
  127. data/lib/robin/web/dist/assets/sv-j9gqamr1.js +1 -0
  128. data/lib/robin/web/dist/assets/sv-j9gqamr1.js.gz +0 -0
  129. data/lib/robin/web/dist/assets/tr-y9z0q51g.js +1 -0
  130. data/lib/robin/web/dist/assets/tr-y9z0q51g.js.gz +0 -0
  131. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js +1 -0
  132. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js.gz +0 -0
  133. data/lib/robin/web/dist/assets/ur-h25nzq1s.js +1 -0
  134. data/lib/robin/web/dist/assets/ur-h25nzq1s.js.gz +0 -0
  135. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js +1 -0
  136. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js.gz +0 -0
  137. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js +1 -0
  138. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js.gz +0 -0
  139. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js +1 -0
  140. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js.gz +0 -0
  141. data/lib/robin/web/dist/index.html +19 -0
  142. data/lib/robin/web/extension.rb +193 -0
  143. data/lib/robin/web/permissions.rb +40 -0
  144. data/lib/robin/web/prometheus.rb +79 -0
  145. data/lib/robin/web/router.rb +150 -0
  146. data/lib/robin/web/static.rb +110 -0
  147. data/lib/robin/web.rb +196 -0
  148. data/lib/robin.rb +258 -8
  149. data/sig/manifest.yaml +9 -0
  150. data/sig/robin/api.rbs +442 -0
  151. data/sig/robin/batch.rbs +292 -0
  152. data/sig/robin/cancellation.rbs +25 -0
  153. data/sig/robin/cli.rbs +102 -0
  154. data/sig/robin/client.rbs +118 -0
  155. data/sig/robin/config.rbs +341 -0
  156. data/sig/robin/cron.rbs +149 -0
  157. data/sig/robin/debounce.rbs +36 -0
  158. data/sig/robin/deploy.rbs +24 -0
  159. data/sig/robin/embedded.rbs +25 -0
  160. data/sig/robin/encryption.rbs +49 -0
  161. data/sig/robin/events.rbs +10 -0
  162. data/sig/robin/expiry.rbs +17 -0
  163. data/sig/robin/fetcher.rbs +134 -0
  164. data/sig/robin/gvl.rbs +39 -0
  165. data/sig/robin/health.rbs +30 -0
  166. data/sig/robin/iterable_job.rbs +99 -0
  167. data/sig/robin/job.rbs +173 -0
  168. data/sig/robin/job_class.rbs +55 -0
  169. data/sig/robin/job_logger.rbs +56 -0
  170. data/sig/robin/launcher.rbs +191 -0
  171. data/sig/robin/leader.rbs +43 -0
  172. data/sig/robin/limiter.rbs +317 -0
  173. data/sig/robin/manager.rbs +54 -0
  174. data/sig/robin/metrics.rbs +122 -0
  175. data/sig/robin/middleware.rbs +106 -0
  176. data/sig/robin/opentelemetry.rbs +51 -0
  177. data/sig/robin/periodic.rbs +81 -0
  178. data/sig/robin/processor.rbs +107 -0
  179. data/sig/robin/profiler.rbs +60 -0
  180. data/sig/robin/progress.rbs +22 -0
  181. data/sig/robin/rails/app_loader.rbs +10 -0
  182. data/sig/robin/rails/current_attributes.rbs +44 -0
  183. data/sig/robin/rails/error_reporter.rbs +21 -0
  184. data/sig/robin/rails/notifications.rbs +9 -0
  185. data/sig/robin/rails/transactional_push.rbs +16 -0
  186. data/sig/robin/rails.rbs +8 -0
  187. data/sig/robin/recovery.rbs +29 -0
  188. data/sig/robin/redis/connection.rbs +224 -0
  189. data/sig/robin/redis/pool.rbs +89 -0
  190. data/sig/robin/redis/resp.rbs +29 -0
  191. data/sig/robin/reliable_push.rbs +77 -0
  192. data/sig/robin/retrier.rbs +72 -0
  193. data/sig/robin/scheduler.rbs +40 -0
  194. data/sig/robin/scripts.rbs +36 -0
  195. data/sig/robin/statsd.rbs +55 -0
  196. data/sig/robin/swarm.rbs +133 -0
  197. data/sig/robin/systemd.rbs +13 -0
  198. data/sig/robin/testing.rbs +80 -0
  199. data/sig/robin/top.rbs +79 -0
  200. data/sig/robin/unique.rbs +72 -0
  201. data/sig/robin/version.rbs +3 -0
  202. data/sig/robin/warmup.rbs +44 -0
  203. data/sig/robin/web/api.rbs +196 -0
  204. data/sig/robin/web/batch_status.rbs +25 -0
  205. data/sig/robin/web/extension.rbs +106 -0
  206. data/sig/robin/web/permissions.rbs +14 -0
  207. data/sig/robin/web/prometheus.rbs +33 -0
  208. data/sig/robin/web/router.rbs +53 -0
  209. data/sig/robin/web/static.rbs +42 -0
  210. data/sig/robin/web.rbs +140 -0
  211. data/sig/robin.rbs +116 -0
  212. data/sig/robin_resp.rbs +55 -0
  213. metadata +274 -44
  214. data/bin/robin +0 -5
  215. data/lib/robin/actions/authenticate.rb +0 -38
  216. data/lib/robin/actions/empty_tweet.rb +0 -7
  217. data/lib/robin/actions/followers.rb +0 -22
  218. data/lib/robin/actions/help.rb +0 -16
  219. data/lib/robin/actions/home_timeline.rb +0 -9
  220. data/lib/robin/actions/new_tweet.rb +0 -11
  221. data/lib/robin/actions/unknown_flag.rb +0 -11
  222. data/lib/robin/actions/user_timeline.rb +0 -19
  223. data/lib/robin/args_parser.rb +0 -70
  224. data/lib/robin/pin_authenticator.rb +0 -31
  225. data/lib/robin/user.rb +0 -36
@@ -0,0 +1,700 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ # A group of jobs tracked as a unit, with callbacks when the group is
5
+ # done.
6
+ #
7
+ # batch = Robin::Batch.new
8
+ # batch.description = "Import #{file.name}"
9
+ # batch.tags = ["imports", "customer:#{file.customer_id}"]
10
+ # batch.on(:success, ImportCallbacks, "file_id" => file.id)
11
+ # batch.on(:complete, "ImportCallbacks#cleanup")
12
+ # batch.jobs do
13
+ # file.rows.each_slice(500) { |rows| ImportRows.perform_async(rows) }
14
+ # end
15
+ #
16
+ # Events:
17
+ #
18
+ # complete:: every job has run at least once (some may have failed and
19
+ # be awaiting retry)
20
+ # success:: every job has succeeded
21
+ # death:: a job in the batch died (fires once per batch)
22
+ #
23
+ # Callback targets are a class (calls +on_<event>+) or "Class#method";
24
+ # they receive (Robin::Batch::Status, options) and run in a job on the
25
+ # batch's +callback_queue+.
26
+ #
27
+ # Creating a batch inside another batch's +jobs+ block, or inside a job
28
+ # that belongs to a batch, makes it a child: the parent isn't complete
29
+ # until the child is complete, nor successful until the child succeeds.
30
+ # Chaining children from success callbacks builds multi-step workflows.
31
+ #
32
+ # Tags label a batch for finding it later: Robin::Batch.list(tag: "imports")
33
+ # (and the web UI's batch filter) lists the batches with that tag.
34
+ #
35
+ # Cancelling a batch (Robin::Batch.new(bid).cancel!) cancels its child
36
+ # batches too. Their jobs that haven't started are skipped, and running
37
+ # ones see #cancelled? turn true (see Robin::Cancellation). The batch
38
+ # still completes once every job has run or been skipped, but never
39
+ # succeeds; callbacks can check status.cancelled?.
40
+ class Batch
41
+ class NoSuchBatch < Robin::Error; end
42
+
43
+ EVENTS = %w[complete success death].freeze
44
+ TTL = 30 * 86_400
45
+ CONTEXT = :robin_batch
46
+ RUNNING = :robin_running_bid
47
+ HOLD = :robin_batch_hold
48
+ FAILURE = :robin_batch_failure
49
+
50
+ # A batch job's failure as its ServerMiddleware saw it (+info+), held
51
+ # back while a processor runs the job (see Batch.holding_failure), to
52
+ # be recorded with the job's retry in one round trip.
53
+ HeldFailure = Struct.new(:payload, :info)
54
+
55
+ # What an atomic +jobs+ block holds back until it finishes: batch
56
+ # members to register ([batch, member]), pushes ([client, payloads]),
57
+ # the batches it created, and the +jobs+ blocks inside it to close.
58
+ Hold = Struct.new(:members, :pushes, :created, :closes)
59
+
60
+ class Hold
61
+ def self.empty = new([], [], [], [])
62
+
63
+ # Registers the members, sends the jobs (so none can finish before
64
+ # it's counted), then closes the inner blocks. When the jobs all go
65
+ # straight to the Redis the batches live in, that's one transaction.
66
+ def release
67
+ pool = shared_pool
68
+ if pool
69
+ pool.with do |conn|
70
+ conn.multi do |tx|
71
+ by_batch.each { |batch, list| batch.queue_register(tx, list) }
72
+ pushes.each { |client, payloads| client.queue_push(tx, payloads) }
73
+ end
74
+ end
75
+ else
76
+ by_batch.each { |batch, list| batch.register_all(list) }
77
+ pushes.each { |client, payloads| client.deliver_now(payloads) }
78
+ end
79
+ closes.each(&:close)
80
+ end
81
+
82
+ # Undoes the batches the block created (the held jobs are dropped)
83
+ # and closes the inner blocks of batches that already existed.
84
+ def discard
85
+ created.each(&:discard!)
86
+ (closes - created).each(&:close)
87
+ end
88
+
89
+ private
90
+
91
+ def by_batch = members.group_by(&:first).transform_values { |pairs| pairs.map(&:last) }
92
+
93
+ # The Redis pool every member and job goes to, when there are jobs and
94
+ # they're sent right away (see Client#direct?) to the one the batches
95
+ # use.
96
+ def shared_pool
97
+ return if pushes.all? { |_client, payloads| payloads.empty? }
98
+
99
+ pool = pushes.first.first.redis_pool
100
+ pool if pushes.all? { |client, payloads| client.redis_pool.equal?(pool) && client.direct?(payloads) } &&
101
+ members.all? { |batch, _member| batch.config.redis_pool.equal?(pool) }
102
+ end
103
+ end
104
+
105
+ # Updates counters for one member (a jid, or "batch:<bid>" for a child
106
+ # batch) and reports which events just became due: {complete,
107
+ # success}, and when either is, what their callbacks need too (the
108
+ # callback queue, created_at and tags). HSETNX on the *_at fields
109
+ # guarantees each event fires exactly once.
110
+ #
111
+ # KEYS: batch hash, pending members set, failed members hash
112
+ # ARGV: mode (success|failure|close), member, now (ms), failure info, ttl
113
+ UPDATE = Redis::Script.new(<<~LUA)
114
+ if redis.call('EXISTS', KEYS[1]) == 0 then return {0, 0} end
115
+ local mode = ARGV[1]
116
+ if mode == 'success' then
117
+ if redis.call('SREM', KEYS[2], ARGV[2]) == 0 then return {0, 0} end
118
+ redis.call('HDEL', KEYS[3], ARGV[2])
119
+ redis.call('HINCRBY', KEYS[1], 'pending', -1)
120
+ elseif mode == 'failure' then
121
+ if redis.call('SISMEMBER', KEYS[2], ARGV[2]) == 0 then return {0, 0} end
122
+ redis.call('HSET', KEYS[3], ARGV[2], ARGV[4])
123
+ redis.call('EXPIRE', KEYS[3], ARGV[5])
124
+ elseif mode == 'close' then
125
+ redis.call('HINCRBY', KEYS[1], 'open', -1)
126
+ end
127
+ local failures = redis.call('HLEN', KEYS[3])
128
+ redis.call('HSET', KEYS[1], 'failures', failures)
129
+ if tonumber(redis.call('HGET', KEYS[1], 'open') or '0') > 0 then return {0, 0} end
130
+ local pending = tonumber(redis.call('HGET', KEYS[1], 'pending') or '0')
131
+ local complete, success = 0, 0
132
+ if pending <= failures and redis.call('HSETNX', KEYS[1], 'complete_at', ARGV[3]) == 1 then complete = 1 end
133
+ if pending == 0 and redis.call('HEXISTS', KEYS[1], 'cancelled_at') == 0
134
+ and redis.call('HSETNX', KEYS[1], 'success_at', ARGV[3]) == 1 then success = 1 end
135
+ if complete + success == 0 then return {0, 0} end
136
+ local info = redis.call('HMGET', KEYS[1], 'callback_queue', 'created_at', 'tags')
137
+ return {complete, success, info[1], info[2], info[3]}
138
+ LUA
139
+
140
+ # Marks a batch and all its descendants cancelled (the first time;
141
+ # cancelled_at stays) and returns their bids, the batch's first.
142
+ # KEYS: none (they're built from ARGV). ARGV: batch hash key prefix
143
+ # ("<prefix>b:"), bid, now (ms).
144
+ CANCEL = Redis::Script.new(<<~LUA)
145
+ local bids = {ARGV[2]}
146
+ local i = 1
147
+ while bids[i] do
148
+ local hash = ARGV[1] .. bids[i]
149
+ redis.call('HSETNX', hash, 'cancelled_at', ARGV[3])
150
+ for _, child in ipairs(redis.call('SMEMBERS', hash .. ':children')) do bids[#bids + 1] = child end
151
+ i = i + 1
152
+ end
153
+ return bids
154
+ LUA
155
+
156
+ # Records when a batch first had a job die: {1, callback queue, tags}
157
+ # the first time, {0} after. A batch that doesn't
158
+ # exist (expired, or a job's "bid" from elsewhere) isn't created.
159
+ # KEYS: batch hash. ARGV: now (ms).
160
+ MARK_DEATH = Redis::Script.new(<<~LUA)
161
+ if redis.call('EXISTS', KEYS[1]) == 0 or redis.call('HSETNX', KEYS[1], 'death_at', ARGV[1]) == 0 then return {0} end
162
+ local info = redis.call('HMGET', KEYS[1], 'callback_queue', 'tags')
163
+ return {1, info[1], info[2]}
164
+ LUA
165
+
166
+ class << self
167
+ # The batch whose +jobs+ block is currently running in this fiber.
168
+ def current = Thread.current[CONTEXT]
169
+
170
+ # The atomic +jobs+ block's hold, if one is running in this thread.
171
+ def hold = Thread.current[HOLD]
172
+
173
+ # Inside a +jobs+ block, holds back the batch members the block
174
+ # registers and the jobs it pushes until it finishes, then registers
175
+ # each batch's members in one round trip before pushing: push_bulk
176
+ # counts its jobs together instead of one by one. If the block
177
+ # raises, nothing is registered or pushed. Inside an atomic +jobs+
178
+ # block, that block's hold already does this.
179
+ def holding
180
+ return yield if hold || !current
181
+
182
+ held = Thread.current[HOLD] = Hold.empty
183
+ begin
184
+ result = yield
185
+ ensure
186
+ Thread.current[HOLD] = nil
187
+ end
188
+ held.release
189
+ result
190
+ end
191
+
192
+ # The batch hash, pending members set and failed members hash.
193
+ def tracking_keys(config, bid)
194
+ base = config.key("b", bid)
195
+ [base, "#{base}:jids", "#{base}:failed"]
196
+ end
197
+
198
+ def keys(config, bid) = [*tracking_keys(config, bid), "#{config.key("b", bid)}:children"]
199
+
200
+ # The index of batches tagged +tag+, newest last.
201
+ def tag_key(config, tag) = config.key("batches", "tag", tag)
202
+
203
+ # mutant:disable -- a class and its name serialize identically to JSON
204
+ def error_info(exception) = {"error_class" => exception.class.name, "error_message" => exception.message[0, 1000]}
205
+
206
+ # Newest batches first (only those tagged +tag+, if given).
207
+ def list(config: Robin.config, limit: 100, tag: nil)
208
+ index = tag ? tag_key(config, tag) : config.key("batches")
209
+ _, bids = config.redis do |c|
210
+ c.pipelined do |p|
211
+ p.call("ZREMRANGEBYSCORE", index, "-inf", Robin.now_ms - TTL * 1000)
212
+ p.call("ZRANGE", index, 0, limit - 1, "REV")
213
+ end
214
+ end #: Array[untyped]
215
+ rows = config.redis { |c| c.pipelined { |p| bids.each { |b| p.call("HGETALL", config.key("b", b)) } } }
216
+ bids.zip(rows).filter_map do |bid, row|
217
+ Status.summarize(bid, row.each_slice(2).to_h) unless row.empty?
218
+ end
219
+ end
220
+
221
+ # -- tracking hooks, called by middleware and the retrier ---------------
222
+
223
+ def job_succeeded(config, bid, member)
224
+ update(config, bid, "success", member)
225
+ end
226
+
227
+ def job_failed(config, bid, member, info)
228
+ update(config, bid, "failure", member, Robin.dump_json(info))
229
+ end
230
+
231
+ # A batch job that raised: its ServerMiddleware records the failure,
232
+ # unless the processor running the job holds it back (see
233
+ # #holding_failure).
234
+ def failed(config, payload, info)
235
+ held = Thread.current[FAILURE]
236
+ return held.info = info if held&.payload.equal?(payload)
237
+
238
+ job_failed(config, payload.fetch("bid"), payload.fetch("jid"), info)
239
+ end
240
+
241
+ # Runs the block (a batch job) with its failure held back in +held+
242
+ # (a HeldFailure).
243
+ def holding_failure(held)
244
+ previous = Thread.current[FAILURE]
245
+ Thread.current[FAILURE] = held
246
+ begin
247
+ yield
248
+ ensure
249
+ Thread.current[FAILURE] = previous
250
+ end
251
+ end
252
+
253
+ # Queues a job's failure into +tx+ (a pipeline or transaction), for
254
+ # #updated to handle its reply.
255
+ def queue_failure(tx, config, payload, info)
256
+ eval_update(tx, config, payload.fetch("bid"), "failure", payload.fetch("jid"), Robin.dump_json(info))
257
+ end
258
+
259
+ # A job in a batch won't run again: records its failure and the
260
+ # batch's first death, in one round trip.
261
+ def job_died(config, payload, exception)
262
+ bid = payload["bid"]
263
+ return unless bid
264
+
265
+ replies = config.redis { |c| c.pipelined { |p| queue_death(p, config, payload, exception) } }
266
+ death_recorded(config, bid, replies)
267
+ end
268
+
269
+ # Queues what #job_died records into +tx+ (a pipeline or transaction),
270
+ # for #death_recorded to handle the two replies.
271
+ def queue_death(tx, config, payload, exception)
272
+ info = exception ? error_info(exception) : payload.slice("error_class", "error_message")
273
+ queue_failure(tx, config, payload, info)
274
+ eval_mark_death(tx, config, payload.fetch("bid"))
275
+ end
276
+
277
+ # Runs the callbacks the replies to #queue_death call for.
278
+ def death_recorded(config, bid, replies)
279
+ update, death = replies
280
+ updated(config, bid, update)
281
+ death_marked(config, bid, death)
282
+ end
283
+
284
+ def died(config, bid) = death_marked(config, bid, config.redis { |c| eval_mark_death(c, config, bid) })
285
+
286
+ # Runs MARK_DEATH on +redis+: a connection (returns the reply), or a
287
+ # pipeline or transaction to queue it in.
288
+ #
289
+ # mutant:disable -- Connection#evalsha splats ARGV, so passing the
290
+ # time bare or wrapped in an Array is identical.
291
+ def eval_mark_death(redis, config, bid) = redis.evalsha(MARK_DEATH, [config.key("b", bid)], [Robin.now_ms])
292
+
293
+ # The first death of a batch runs its death callbacks.
294
+ def death_marked(config, bid, reply)
295
+ first, queue, tags = reply
296
+ return unless first == 1
297
+
298
+ enqueue_callback(config, bid, "death", queue: queue)
299
+ report(config, ["death"], nil, tags)
300
+ end
301
+
302
+ def update(config, bid, mode, member = nil, info = nil)
303
+ updated(config, bid, config.redis { |c| eval_update(c, config, bid, mode, member, info) })
304
+ end
305
+
306
+ # Runs UPDATE on +redis+: a connection (returns the reply), or a
307
+ # pipeline or transaction to queue it in (#updated handles the reply).
308
+ def eval_update(redis, config, bid, mode, member, info)
309
+ redis.evalsha(UPDATE, tracking_keys(config, bid), [mode, member, Robin.now_ms, info, TTL])
310
+ end
311
+
312
+ # Runs the callbacks an UPDATE reply says just became due.
313
+ def updated(config, bid, reply)
314
+ complete, success, queue, created, tags = reply
315
+ # "complete" runs first and chains "success" when both are due.
316
+ events = [] #: Array[String]
317
+ events << "complete" if complete == 1
318
+ events << "success" if success == 1
319
+ return if events.empty?
320
+
321
+ # One or two events: the event, and the one it chains.
322
+ enqueue_callback(config, bid, events.fetch(0), events.at(1), queue: queue)
323
+ report(config, events, created, tags)
324
+ end
325
+
326
+ # Counts batch events, and times completed batches (+created+ is
327
+ # the batch's created_at, needed for "complete"; +tags+ its tags as
328
+ # JSON), in StatsD.
329
+ def report(config, events, created, tags)
330
+ statsd = config.statsd or return
331
+ # Plain StatsD would fold each tag into the metric's name.
332
+ tags = statsd.dogstatsd? ? {tag: Robin.load_json(tags || "[]")} : {} #: Statsd::tags
333
+ events.each { |event| statsd.increment("batch.#{event}", tags: tags) }
334
+ return unless events.include?("complete")
335
+
336
+ duration = Robin.now_ms - Integer(created)
337
+ statsd.timing("batch.duration", duration, tags: tags)
338
+ statsd.distribution("batch.duration_dist", duration, tags: tags) if statsd.distributions?
339
+ end
340
+
341
+ # Pushes the job running +bid+'s callbacks for +event+ (then
342
+ # +chain+'s) onto +queue+, the batch's callback queue.
343
+ def enqueue_callback(config, bid, event, chain = nil, queue:)
344
+ Client.new(config: config).push({"class" => Callback, "queue" => queue, "args" => [bid, event, chain].compact,
345
+ "batch_callback" => true})
346
+ end
347
+ end
348
+
349
+ attr_reader :bid, :config, :parent_bid, :tags
350
+ attr_accessor :description, :callback_queue
351
+
352
+ def initialize(bid = nil, config: Robin.config)
353
+ @config = config
354
+ @callbacks = {}
355
+ @cancelled = false
356
+ @tags = []
357
+ if bid
358
+ data = config.redis { |c| c.call("HGETALL", config.key("b", bid)) }.each_slice(2).to_h
359
+ raise NoSuchBatch, "no batch #{bid} (expired or deleted?)" if data.empty?
360
+
361
+ @bid = bid
362
+ @persisted = true
363
+ @description = data["description"]
364
+ @callback_queue = data["callback_queue"]
365
+ @parent_bid = data["parent_bid"]
366
+ @cancelled = data.key?("cancelled_at")
367
+ @callbacks = Robin.load_json(data["callbacks"] || "{}")
368
+ @tags = Robin.load_json(data["tags"] || "[]")
369
+ else
370
+ @bid = SecureRandom.hex(10)
371
+ end
372
+ end
373
+
374
+ # Registers a callback for :complete, :success or :death.
375
+ def on(event, target, options = {})
376
+ event = event.to_s
377
+ raise ArgumentError, "unknown batch event #{event.inspect}; expected #{EVENTS.join(", ")}" unless EVENTS.include?(event)
378
+
379
+ (@callbacks[event] ||= []) << [target.to_s, options.transform_keys(&:to_s)]
380
+ if @persisted
381
+ config.redis { |c| c.call("HSET", config.key("b", bid), "callbacks", Robin.dump_json(@callbacks)) }
382
+ end
383
+ self
384
+ end
385
+
386
+ def callbacks = @callbacks.dup
387
+
388
+ # Labels for finding the batch later; set before the first +jobs+.
389
+ def tags=(list)
390
+ raise ArgumentError, "a batch's tags are set before its first jobs" if @persisted
391
+
392
+ @tags = Array(list).map(&:to_s).reject(&:empty?).uniq
393
+ end
394
+
395
+ # Runs the block; every job pushed inside it belongs to this batch.
396
+ # May be called again later (e.g. from a job in the batch) to add more
397
+ # jobs. Returns the bid.
398
+ #
399
+ # Jobs are pushed as the block pushes them. With +atomic+, they're all
400
+ # pushed when the block finishes instead, and none at all if it raises
401
+ # (batches it created are then deleted). A unique job's lock, taken
402
+ # when it was pushed, stays until it expires.
403
+ def jobs(atomic: false)
404
+ hold = Thread.current[HOLD] = Hold.empty if atomic && !self.class.hold
405
+ # A new batch opens its first block as it's created.
406
+ @persisted ? config.redis { |c| c.call("HINCRBY", config.key("b", bid), "open", 1) } : persist!
407
+ previous = Thread.current[CONTEXT]
408
+ Thread.current[CONTEXT] = self
409
+ completed = false
410
+ begin
411
+ yield
412
+ completed = true
413
+ ensure
414
+ Thread.current[CONTEXT] = previous
415
+ if hold
416
+ Thread.current[HOLD] = nil
417
+ completed ? hold.release : hold.discard
418
+ close unless @discarded
419
+ elsif (outer = self.class.hold)
420
+ # (Once the outer block is done.)
421
+ outer.closes << self
422
+ else
423
+ close
424
+ end
425
+ end
426
+ bid
427
+ end
428
+
429
+ # Ends one +jobs+ block; with none left open, a batch whose jobs are
430
+ # all done completes.
431
+ def close = self.class.update(config, bid, "close")
432
+
433
+ def status = Status.new(bid, config: config)
434
+
435
+ # Cancels this batch and its child batches (see the class comment), in
436
+ # two round trips. Returns the bids cancelled.
437
+ def cancel!
438
+ bids = config.redis { |c| c.evalsha(CANCEL, [], ["#{config.key("b")}:", bid, Robin.now_ms]) }
439
+ Cancellation.cancel!(bids.map { |b| "batch:#{b}" }, ttl: TTL, config: config)
440
+ config.statsd&.increment("batch.cancelled", bids.size)
441
+ @cancelled = true
442
+ bids
443
+ end
444
+
445
+ # Was this batch (or one of its parents) cancelled?
446
+ def cancelled? = @cancelled
447
+
448
+ def parent = parent_bid && Batch.new(parent_bid, config: config)
449
+
450
+ # Adds +member+ to the pending set. Called before the job is pushed, so
451
+ # a fast job can never finish before it's counted. Inside an atomic
452
+ # +jobs+ block, that waits until the block finishes.
453
+ def register(member)
454
+ hold = self.class.hold
455
+ return hold.members << [self, member] if hold
456
+
457
+ register_all([member])
458
+ end
459
+
460
+ def register_all(members)
461
+ config.redis { |c| c.multi { |tx| queue_register(tx, members) } }
462
+ end
463
+
464
+ # Queues the commands registering +members+ into +tx+ (a transaction).
465
+ def queue_register(tx, members)
466
+ hash, jids = self.class.keys(config, bid)
467
+ tx.call("SADD", jids, *members)
468
+ tx.call("EXPIRE", jids, TTL)
469
+ tx.call("HINCRBY", hash, "pending", members.size)
470
+ tx.call("HINCRBY", hash, "total", members.size)
471
+ end
472
+
473
+ # Deletes a batch an atomic +jobs+ block created before it raised.
474
+ def discard!
475
+ Status.new(bid, config: config).delete!
476
+ config.redis { |c| c.call("SREM", self.class.keys(config, parent_bid).fetch(3), bid) } if parent_bid
477
+ @discarded = true
478
+ end
479
+
480
+ private
481
+
482
+ # The batch a new batch becomes a child of: the one whose +jobs+ block
483
+ # is running, else the one the running job belongs to.
484
+ def parent_for_new = Thread.current[CONTEXT]&.bid || Thread.current[RUNNING]
485
+
486
+ def persist!
487
+ parent = parent_for_new
488
+ @parent_bid = parent
489
+ hold = self.class.hold
490
+ hold.created << self if hold
491
+ now = Robin.now_ms
492
+ hash = config.key("b", bid)
493
+ parent_batch = parent && Batch.new(parent, config: config)
494
+ member = "batch:#{bid}"
495
+ # In an atomic +jobs+ block, the parent counts its new child once the
496
+ # block finishes, before the jobs are sent.
497
+ hold.members << [parent_batch, member] if parent_batch && hold
498
+ # pending/total start at zero implicitly (HINCRBY, or read as 0).
499
+ fields = {"created_at" => now, "callbacks" => Robin.dump_json(@callbacks)}
500
+ fields["description"] = description if description
501
+ fields["callback_queue"] = callback_queue if callback_queue
502
+ fields["parent_bid"] = parent if parent
503
+ fields["tags"] = Robin.dump_json(tags) unless tags.empty?
504
+ config.redis do |c|
505
+ c.multi do |tx|
506
+ if parent_batch
507
+ parent_batch.queue_register(tx, [member]) unless hold
508
+ children_key = self.class.keys(config, parent).fetch(3)
509
+ tx.call("SADD", children_key, bid)
510
+ tx.call("EXPIRE", children_key, TTL)
511
+ end
512
+ tx.call("HSET", hash, *fields.flatten)
513
+ tx.call("HINCRBY", hash, "open", 1) # the +jobs+ block creating it
514
+ tx.call("EXPIRE", hash, TTL)
515
+ tx.call("ZADD", config.key("batches"), now, bid)
516
+ tags.each do |tag|
517
+ tx.call("ZADD", self.class.tag_key(config, tag), now, bid)
518
+ tx.call("EXPIRE", self.class.tag_key(config, tag), TTL)
519
+ end
520
+ end
521
+ end
522
+ config.statsd&.increment("batch.created")
523
+ @persisted = true
524
+ cancel! if parent_batch&.cancelled?
525
+ end
526
+
527
+ # Read-only view of a batch, handed to callbacks and the web UI.
528
+ class Status
529
+ INTS = %w[total pending failures created_at complete_at success_at death_at cancelled_at].freeze
530
+
531
+ def self.summarize(bid, data)
532
+ h = {"bid" => bid, "description" => data["description"], "parent_bid" => data["parent_bid"],
533
+ "callbacks" => Robin.load_json(data["callbacks"] || "{}"), "tags" => Robin.load_json(data["tags"] || "[]")}
534
+ INTS.each { |k| h[k] = data[k]&.to_i }
535
+ %w[total pending failures].each { |k| h[k] ||= 0 }
536
+ h
537
+ end
538
+
539
+ attr_reader :bid
540
+
541
+ def initialize(bid, config: Robin.config)
542
+ @bid = bid
543
+ @config = config
544
+ @data = config.redis { |c| c.call("HGETALL", config.key("b", bid)) }.each_slice(2).to_h
545
+ end
546
+
547
+ def exists? = !@data.empty?
548
+
549
+ def total = @data["total"].to_i
550
+
551
+ def pending = @data["pending"].to_i
552
+
553
+ def failures = @data["failures"].to_i
554
+
555
+ def description = @data["description"]
556
+
557
+ def parent_bid = @data["parent_bid"]
558
+
559
+ def callback_queue = @data["callback_queue"]
560
+
561
+ def tags = Robin.load_json(@data["tags"] || "[]")
562
+
563
+ def created_at = @data["created_at"]&.to_i
564
+
565
+ def complete? = !@data["complete_at"].nil?
566
+
567
+ def success? = !@data["success_at"].nil?
568
+
569
+ def dead? = !@data["death_at"].nil?
570
+
571
+ def cancelled? = !@data["cancelled_at"].nil?
572
+
573
+ def callbacks = Robin.load_json(@data["callbacks"] || "{}")
574
+
575
+ def jids
576
+ @config.redis { |c| c.call("SMEMBERS", Batch.keys(@config, bid).fetch(1)) }.reject { |m| m.start_with?("batch:") }
577
+ end
578
+
579
+ def children
580
+ @config.redis { |c| c.call("SMEMBERS", Batch.keys(@config, bid).fetch(3)) }
581
+ end
582
+
583
+ def failed_jobs
584
+ @config.redis { |c| c.call("HGETALL", Batch.keys(@config, bid).fetch(2)) }.each_slice(2).map do |jid, json|
585
+ {"jid" => jid}.merge(Robin.load_json(json).slice("error_class", "error_message"))
586
+ end
587
+ end
588
+
589
+ # Counts and states only, as Web::BatchStatus shows them.
590
+ def progress
591
+ {"bid" => bid, "total" => total, "pending" => pending, "failures" => failures,
592
+ "complete" => complete?, "success" => success?, "dead" => dead?, "cancelled" => cancelled?}
593
+ end
594
+
595
+ def to_h
596
+ Status.summarize(bid, @data).merge({"failed_jobs" => failed_jobs, "children" => children, "jids" => jids})
597
+ end
598
+
599
+ def delete!
600
+ @config.redis do |c|
601
+ c.multi do |tx|
602
+ tx.call("UNLINK", *Batch.keys(@config, bid))
603
+ tx.call("ZREM", @config.key("batches"), bid)
604
+ tags.each { |tag| tx.call("ZREM", Batch.tag_key(@config, tag), bid) }
605
+ end
606
+ end
607
+ @data = {}
608
+ true
609
+ end
610
+ end
611
+
612
+ # Runs a batch's callbacks for one event, then propagates the outcome
613
+ # to the parent batch. Runs as a normal job, so failing callbacks are
614
+ # retried (callbacks for that event run again; make them idempotent).
615
+ class Callback
616
+ include Robin::Job
617
+
618
+ def perform(bid, event, chain = nil)
619
+ config = self.class.robin_config
620
+ status = begin
621
+ config.batch_status_class.new(bid, config: config)
622
+ rescue NoSuchBatch # a status class that refuses a missing batch
623
+ return
624
+ end
625
+ return unless status.exists?
626
+
627
+ Array(status.callbacks[event]).each do |target, options|
628
+ klass_name, method = target.split("#", 2) #: [String, String] | [String]
629
+ Object.const_get(klass_name).new.public_send(method || "on_#{event}", status, options || {})
630
+ end
631
+
632
+ Batch.enqueue_callback(config, bid, chain, queue: status.callback_queue) if chain
633
+ parent = status.parent_bid
634
+ return unless parent
635
+
636
+ member = "batch:#{bid}"
637
+ case event
638
+ when "success"
639
+ Batch.job_succeeded(config, parent, member)
640
+ when "complete"
641
+ unless status.success? || chain
642
+ message = status.cancelled? ? "child batch #{bid} was cancelled" : "child batch #{bid} has #{status.failures} failure(s)"
643
+ Batch.job_failed(config, parent, member, {"error_class" => "Robin::Batch::ChildFailed", "error_message" => message})
644
+ end
645
+ else # "death"
646
+ Batch.died(config, parent)
647
+ end
648
+ end
649
+ end
650
+
651
+ # Tags jobs pushed inside a +jobs+ block with the batch id.
652
+ class ClientMiddleware
653
+ include Middleware::ConfigAware
654
+ include Middleware::Stateless
655
+
656
+ # Only inside a batch's +jobs+ block.
657
+ def self.applies?(*) = !Batch.current.nil?
658
+
659
+ def call(_job_class, payload, _queue, _redis)
660
+ batch = Batch.current
661
+ if batch && !payload["bid"] && !payload["batch_callback"]
662
+ payload["bid"] = batch.bid
663
+ batch.register(payload.fetch("jid"))
664
+ end
665
+ yield
666
+ end
667
+ end
668
+
669
+ # Records the outcome of batch jobs.
670
+ class ServerMiddleware
671
+ include Middleware::ConfigAware
672
+ include Middleware::Stateless
673
+
674
+ def self.applies?(_job, payload, _queue) = payload.key?("bid")
675
+
676
+ def call(_job, payload, _queue)
677
+ bid = payload["bid"]
678
+ return yield unless bid
679
+
680
+ previous = Thread.current[RUNNING]
681
+ Thread.current[RUNNING] = bid
682
+ begin
683
+ result = yield
684
+ rescue Discard
685
+ Batch.job_succeeded(config, bid, payload.fetch("jid"))
686
+ raise
687
+ rescue Exception => e # rubocop:disable Lint/RescueException
688
+ Batch.failed(config, payload, Batch.error_info(e)) unless Robin.shutdown?(e) || e.is_a?(Limiter::OverLimit)
689
+ raise
690
+ ensure
691
+ Thread.current[RUNNING] = previous
692
+ end
693
+ # Iterable jobs that were interrupted re-enqueue themselves and are
694
+ # still pending.
695
+ Batch.job_succeeded(config, bid, payload.fetch("jid")) unless payload["iteration_interrupted"]
696
+ result
697
+ end
698
+ end
699
+ end
700
+ end