robin 0.1.0-java

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 (217) 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/java/robin_resp/Blocking.java +27 -0
  13. data/ext/java/robin_resp/NativeIO.java +229 -0
  14. data/ext/java/robin_resp/NativeRESP.java +279 -0
  15. data/ext/java/robin_resp/NativeSlots.java +133 -0
  16. data/ext/java/robin_resp/RobinRespService.java +32 -0
  17. data/lib/active_job/queue_adapters/robin_adapter.rb +3 -0
  18. data/lib/generators/robin/install/install_generator.rb +39 -0
  19. data/lib/generators/robin/install/templates/initializer.rb.tt +31 -0
  20. data/lib/generators/robin/install/templates/robin.yml.tt +25 -0
  21. data/lib/generators/robin/job/job_generator.rb +78 -0
  22. data/lib/generators/robin/job/templates/job.rb.tt +13 -0
  23. data/lib/generators/robin/job/templates/job_spec.rb.tt +21 -0
  24. data/lib/generators/robin/job/templates/job_test.rb.tt +21 -0
  25. data/lib/puma/plugin/robin.rb +131 -0
  26. data/lib/robin/active_job.rb +144 -0
  27. data/lib/robin/api.rb +777 -0
  28. data/lib/robin/batch.rb +700 -0
  29. data/lib/robin/cancellation.rb +62 -0
  30. data/lib/robin/cli.rb +325 -0
  31. data/lib/robin/client.rb +372 -0
  32. data/lib/robin/config.rb +461 -0
  33. data/lib/robin/cron.rb +325 -0
  34. data/lib/robin/debounce.rb +108 -0
  35. data/lib/robin/deploy.rb +65 -0
  36. data/lib/robin/embedded.rb +53 -0
  37. data/lib/robin/encryption.rb +147 -0
  38. data/lib/robin/events.rb +60 -0
  39. data/lib/robin/expiry.rb +41 -0
  40. data/lib/robin/fetcher.rb +441 -0
  41. data/lib/robin/gvl.rb +62 -0
  42. data/lib/robin/health.rb +76 -0
  43. data/lib/robin/iterable_job.rb +294 -0
  44. data/lib/robin/job.rb +258 -0
  45. data/lib/robin/job_class.rb +157 -0
  46. data/lib/robin/job_logger.rb +121 -0
  47. data/lib/robin/launcher.rb +658 -0
  48. data/lib/robin/leader.rb +86 -0
  49. data/lib/robin/limiter.rb +639 -0
  50. data/lib/robin/manager.rb +125 -0
  51. data/lib/robin/metrics.rb +303 -0
  52. data/lib/robin/middleware.rb +165 -0
  53. data/lib/robin/opentelemetry.rb +88 -0
  54. data/lib/robin/periodic.rb +327 -0
  55. data/lib/robin/processor.rb +317 -0
  56. data/lib/robin/profiler.rb +140 -0
  57. data/lib/robin/progress.rb +46 -0
  58. data/lib/robin/rails/app_loader.rb +23 -0
  59. data/lib/robin/rails/current_attributes.rb +88 -0
  60. data/lib/robin/rails/error_reporter.rb +31 -0
  61. data/lib/robin/rails/notifications.rb +22 -0
  62. data/lib/robin/rails/railtie.rb +87 -0
  63. data/lib/robin/rails/transactional_push.rb +49 -0
  64. data/lib/robin/rails.rb +23 -0
  65. data/lib/robin/recovery.rb +135 -0
  66. data/lib/robin/redis/connection.rb +639 -0
  67. data/lib/robin/redis/pool.rb +190 -0
  68. data/lib/robin/redis/resp.rb +77 -0
  69. data/lib/robin/reliable_push.rb +153 -0
  70. data/lib/robin/retrier.rb +284 -0
  71. data/lib/robin/scheduler.rb +102 -0
  72. data/lib/robin/scripts.rb +331 -0
  73. data/lib/robin/statsd.rb +135 -0
  74. data/lib/robin/swarm.rb +319 -0
  75. data/lib/robin/systemd.rb +47 -0
  76. data/lib/robin/testing.rb +158 -0
  77. data/lib/robin/top.rb +178 -0
  78. data/lib/robin/unique.rb +198 -0
  79. data/lib/robin/version.rb +5 -0
  80. data/lib/robin/warmup.rb +94 -0
  81. data/lib/robin/web/api.rb +580 -0
  82. data/lib/robin/web/batch_status.rb +44 -0
  83. data/lib/robin/web/dist/assets/ar-y7v9b75f.js +1 -0
  84. data/lib/robin/web/dist/assets/ar-y7v9b75f.js.gz +0 -0
  85. data/lib/robin/web/dist/assets/bricolage-0qmzcpnj.woff2 +0 -0
  86. data/lib/robin/web/dist/assets/cs-df88mq5e.js +1 -0
  87. data/lib/robin/web/dist/assets/cs-df88mq5e.js.gz +0 -0
  88. data/lib/robin/web/dist/assets/da-kemms74g.js +1 -0
  89. data/lib/robin/web/dist/assets/da-kemms74g.js.gz +0 -0
  90. data/lib/robin/web/dist/assets/de-z8kkqg65.js +1 -0
  91. data/lib/robin/web/dist/assets/de-z8kkqg65.js.gz +0 -0
  92. data/lib/robin/web/dist/assets/es-9ahrrn1s.js +1 -0
  93. data/lib/robin/web/dist/assets/es-9ahrrn1s.js.gz +0 -0
  94. data/lib/robin/web/dist/assets/fa-7r0x28nc.js +1 -0
  95. data/lib/robin/web/dist/assets/fa-7r0x28nc.js.gz +0 -0
  96. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg +31 -0
  97. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg.gz +0 -0
  98. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js +1 -0
  99. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js.gz +0 -0
  100. data/lib/robin/web/dist/assets/he-7t46b6rt.js +1 -0
  101. data/lib/robin/web/dist/assets/he-7t46b6rt.js.gz +0 -0
  102. data/lib/robin/web/dist/assets/hi-zj434tx4.js +1 -0
  103. data/lib/robin/web/dist/assets/hi-zj434tx4.js.gz +0 -0
  104. data/lib/robin/web/dist/assets/it-pqc2m7gm.js +1 -0
  105. data/lib/robin/web/dist/assets/it-pqc2m7gm.js.gz +0 -0
  106. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js +1 -0
  107. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js.gz +0 -0
  108. data/lib/robin/web/dist/assets/ko-bxersv91.js +1 -0
  109. data/lib/robin/web/dist/assets/ko-bxersv91.js.gz +0 -0
  110. data/lib/robin/web/dist/assets/main-anw3f6ba.css +1 -0
  111. data/lib/robin/web/dist/assets/main-anw3f6ba.css.gz +0 -0
  112. data/lib/robin/web/dist/assets/main-pdtvjejb.js +7 -0
  113. data/lib/robin/web/dist/assets/main-pdtvjejb.js.gz +0 -0
  114. data/lib/robin/web/dist/assets/main-zz8zzhwn.js +2 -0
  115. data/lib/robin/web/dist/assets/main-zz8zzhwn.js.gz +0 -0
  116. data/lib/robin/web/dist/assets/nb-fjnc5064.js +1 -0
  117. data/lib/robin/web/dist/assets/nb-fjnc5064.js.gz +0 -0
  118. data/lib/robin/web/dist/assets/nl-9p8b95b6.js +1 -0
  119. data/lib/robin/web/dist/assets/nl-9p8b95b6.js.gz +0 -0
  120. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js +1 -0
  121. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js.gz +0 -0
  122. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js +1 -0
  123. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js.gz +0 -0
  124. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg +34 -0
  125. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg.gz +0 -0
  126. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg +4 -0
  127. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg.gz +0 -0
  128. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js +1 -0
  129. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js.gz +0 -0
  130. data/lib/robin/web/dist/assets/sv-j9gqamr1.js +1 -0
  131. data/lib/robin/web/dist/assets/sv-j9gqamr1.js.gz +0 -0
  132. data/lib/robin/web/dist/assets/tr-y9z0q51g.js +1 -0
  133. data/lib/robin/web/dist/assets/tr-y9z0q51g.js.gz +0 -0
  134. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js +1 -0
  135. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js.gz +0 -0
  136. data/lib/robin/web/dist/assets/ur-h25nzq1s.js +1 -0
  137. data/lib/robin/web/dist/assets/ur-h25nzq1s.js.gz +0 -0
  138. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js +1 -0
  139. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js.gz +0 -0
  140. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js +1 -0
  141. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js.gz +0 -0
  142. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js +1 -0
  143. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js.gz +0 -0
  144. data/lib/robin/web/dist/index.html +19 -0
  145. data/lib/robin/web/extension.rb +193 -0
  146. data/lib/robin/web/permissions.rb +40 -0
  147. data/lib/robin/web/prometheus.rb +79 -0
  148. data/lib/robin/web/router.rb +150 -0
  149. data/lib/robin/web/static.rb +110 -0
  150. data/lib/robin/web.rb +196 -0
  151. data/lib/robin.rb +261 -0
  152. data/lib/robin_resp/robin_resp.jar +0 -0
  153. data/sig/manifest.yaml +9 -0
  154. data/sig/robin/api.rbs +442 -0
  155. data/sig/robin/batch.rbs +292 -0
  156. data/sig/robin/cancellation.rbs +25 -0
  157. data/sig/robin/cli.rbs +102 -0
  158. data/sig/robin/client.rbs +118 -0
  159. data/sig/robin/config.rbs +341 -0
  160. data/sig/robin/cron.rbs +149 -0
  161. data/sig/robin/debounce.rbs +36 -0
  162. data/sig/robin/deploy.rbs +24 -0
  163. data/sig/robin/embedded.rbs +25 -0
  164. data/sig/robin/encryption.rbs +49 -0
  165. data/sig/robin/events.rbs +10 -0
  166. data/sig/robin/expiry.rbs +17 -0
  167. data/sig/robin/fetcher.rbs +134 -0
  168. data/sig/robin/gvl.rbs +39 -0
  169. data/sig/robin/health.rbs +30 -0
  170. data/sig/robin/iterable_job.rbs +99 -0
  171. data/sig/robin/job.rbs +173 -0
  172. data/sig/robin/job_class.rbs +55 -0
  173. data/sig/robin/job_logger.rbs +56 -0
  174. data/sig/robin/launcher.rbs +191 -0
  175. data/sig/robin/leader.rbs +43 -0
  176. data/sig/robin/limiter.rbs +317 -0
  177. data/sig/robin/manager.rbs +54 -0
  178. data/sig/robin/metrics.rbs +122 -0
  179. data/sig/robin/middleware.rbs +106 -0
  180. data/sig/robin/opentelemetry.rbs +51 -0
  181. data/sig/robin/periodic.rbs +81 -0
  182. data/sig/robin/processor.rbs +107 -0
  183. data/sig/robin/profiler.rbs +60 -0
  184. data/sig/robin/progress.rbs +22 -0
  185. data/sig/robin/rails/app_loader.rbs +10 -0
  186. data/sig/robin/rails/current_attributes.rbs +44 -0
  187. data/sig/robin/rails/error_reporter.rbs +21 -0
  188. data/sig/robin/rails/notifications.rbs +9 -0
  189. data/sig/robin/rails/transactional_push.rbs +16 -0
  190. data/sig/robin/rails.rbs +8 -0
  191. data/sig/robin/recovery.rbs +29 -0
  192. data/sig/robin/redis/connection.rbs +224 -0
  193. data/sig/robin/redis/pool.rbs +89 -0
  194. data/sig/robin/redis/resp.rbs +29 -0
  195. data/sig/robin/reliable_push.rbs +77 -0
  196. data/sig/robin/retrier.rbs +72 -0
  197. data/sig/robin/scheduler.rbs +40 -0
  198. data/sig/robin/scripts.rbs +36 -0
  199. data/sig/robin/statsd.rbs +55 -0
  200. data/sig/robin/swarm.rbs +133 -0
  201. data/sig/robin/systemd.rbs +13 -0
  202. data/sig/robin/testing.rbs +80 -0
  203. data/sig/robin/top.rbs +79 -0
  204. data/sig/robin/unique.rbs +72 -0
  205. data/sig/robin/version.rbs +3 -0
  206. data/sig/robin/warmup.rbs +44 -0
  207. data/sig/robin/web/api.rbs +196 -0
  208. data/sig/robin/web/batch_status.rbs +25 -0
  209. data/sig/robin/web/extension.rbs +106 -0
  210. data/sig/robin/web/permissions.rbs +14 -0
  211. data/sig/robin/web/prometheus.rbs +33 -0
  212. data/sig/robin/web/router.rbs +53 -0
  213. data/sig/robin/web/static.rbs +42 -0
  214. data/sig/robin/web.rbs +140 -0
  215. data/sig/robin.rbs +116 -0
  216. data/sig/robin_resp.rbs +55 -0
  217. metadata +294 -0
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Robin
6
+ class Web
7
+ # A page of your own in the dashboard. Describe it with a few kinds of
8
+ # blocks (no frontend code), and attach actions to its buttons:
9
+ #
10
+ # class BillingPage < Robin::Web::Extension
11
+ # slug "billing" # in its URL: /ext/billing
12
+ # title "Billing" # the tab
13
+ #
14
+ # def blocks(request)
15
+ # queued = Robin::Queue.new("invoices", config: request.config).size
16
+ # [
17
+ # {type: "stats", items: [{label: request.t("Invoices waiting"), value: queued}]},
18
+ # {type: "table", columns: ["Customer", "Total"], rows: Invoice.pending.map { [_1.customer, _1.total] }},
19
+ # {type: "text", text: "Invoices go out at 6am."},
20
+ # {type: "actions", items: [{label: "Send now", action: "send", confirm: "Send every invoice?"}]}
21
+ # ]
22
+ # end
23
+ #
24
+ # action "send" do |request|
25
+ # SendInvoices.perform_async
26
+ # "Sending invoices" # shown to the user
27
+ # end
28
+ # end
29
+ #
30
+ # Robin::Web.register(BillingPage)
31
+ #
32
+ # For anything blocks can't show, a page can bring its own frontend
33
+ # code: a JavaScript module (and stylesheet) from a directory of
34
+ # assets, which the dashboard loads into the page, plus JSON data
35
+ # endpoints for it to call:
36
+ #
37
+ # class ReportsPage < Robin::Web::Extension
38
+ # slug "reports"
39
+ # assets File.expand_path("reports", __dir__) # served at <mount>/extensions/reports/
40
+ # script "reports.js"
41
+ # stylesheet "reports.css"
42
+ #
43
+ # data "summary" do |request| # GET api/extensions/reports/data/summary
44
+ # {"late" => Report.late.count, "since" => request.params["since"]}
45
+ # end
46
+ # end
47
+ #
48
+ # The module's default export is called with the element to render
49
+ # into and a context object (see the README), and may return a
50
+ # function to call when the page goes away.
51
+ #
52
+ # Translations: with `translations dir`, dir/<language>.yml files map
53
+ # English text to that language (e.g. de.yml: {"Billing": "Abrechnung"}).
54
+ # The title is translated in the navigation, request.t translates text
55
+ # in blocks and actions, and scripts get the messages as context.t.
56
+ #
57
+ # Viewing the page, its assets and data needs the :read permission;
58
+ # running an action needs :extensions (see Robin::Web.authorize; the
59
+ # resource is "extension:<slug>/<action>"). Blocks are JSON: strings,
60
+ # numbers and booleans render as text, never as HTML.
61
+ class Extension
62
+ SLUG = /\A[a-z0-9][a-z0-9_-]*\z/
63
+
64
+ # What blocks, actions and data blocks get: the dashboard request.
65
+ # +params+ are the query parameters, +body+ an action's JSON body
66
+ # (a Hash), +locale+ the dashboard's language.
67
+ Request = Struct.new(:config, :params, :env, :locale, :body, :extension)
68
+
69
+ class Request
70
+ # +text+ in the dashboard's language (when the extension has a
71
+ # translation for it), with {name} placeholders filled from +values+.
72
+ def t(text, **values)
73
+ translated = extension&.messages(locale)&.fetch(text, nil) || text
74
+ translated.gsub(/\{(\w+)\}/) do |placeholder|
75
+ # (The pattern's group always matches.)
76
+ name = Regexp.last_match(1) #: String
77
+ values.fetch(name.to_sym) { placeholder }
78
+ end
79
+ end
80
+ end
81
+
82
+ class << self
83
+ def slug(value = nil)
84
+ return @slug || raise(ArgumentError, "#{self} needs a slug") if value.nil?
85
+
86
+ value = value.to_s
87
+ raise ArgumentError, "extension slugs are lowercase letters, digits, - and _: #{value.inspect}" unless SLUG.match?(value)
88
+
89
+ @slug = value
90
+ end
91
+
92
+ def title(value = nil)
93
+ return @title || slug.capitalize if value.nil?
94
+
95
+ @title = value.to_s
96
+ end
97
+
98
+ def action(name, &block)
99
+ actions[name.to_s] = block
100
+ end
101
+
102
+ def actions = (@actions ||= {})
103
+
104
+ # A JSON endpoint for the page's script; the block's value is the
105
+ # response.
106
+ def data(name, &block)
107
+ data_sources[name.to_s] = block
108
+ end
109
+
110
+ def data_sources = (@data_sources ||= {})
111
+
112
+ # The directory the page's script, stylesheet and other files are
113
+ # served from.
114
+ def assets(dir = nil)
115
+ return @assets if dir.nil?
116
+
117
+ @assets = File.expand_path(dir)
118
+ end
119
+
120
+ # The page's JavaScript module and stylesheet, relative to +assets+.
121
+ def script(path = nil) = asset_setting(:@script, path)
122
+
123
+ def stylesheet(path = nil) = asset_setting(:@stylesheet, path)
124
+
125
+ # The directory of <language>.yml translation files, read once here.
126
+ def translations(dir = nil)
127
+ return @translations if dir.nil?
128
+
129
+ @translations = File.expand_path(dir)
130
+ @messages = Dir.glob("*.yml", base: @translations).to_h do |file|
131
+ [File.basename(file, ".yml"), load_messages(File.join(@translations, file))]
132
+ end
133
+ end
134
+
135
+ # English text => translation for +locale+ ("pt-BR" falls back to
136
+ # pt.yml), or nil without translations for it.
137
+ def messages(locale)
138
+ all = @messages or return
139
+ all[locale] || all[locale.to_s.split("-").first]
140
+ end
141
+
142
+ # The full path of an asset, or nil when there's no such file
143
+ # inside the assets directory.
144
+ def asset_file(path)
145
+ return unless @assets
146
+
147
+ file = File.expand_path(path, @assets)
148
+ file if file.start_with?("#{@assets}/") && File.file?(file)
149
+ end
150
+
151
+ def to_h
152
+ {
153
+ "slug" => slug, "title" => title, "titles" => (titles if @messages),
154
+ "script" => @script && "extensions/#{slug}/#{@script}",
155
+ "stylesheet" => @stylesheet && "extensions/#{slug}/#{@stylesheet}"
156
+ }.compact
157
+ end
158
+
159
+ private
160
+
161
+ def asset_setting(name, path)
162
+ return instance_variable_get(name) if path.nil?
163
+ raise ArgumentError, "#{self} needs `assets dir` before its script or stylesheet" unless @assets
164
+
165
+ instance_variable_set(name, path.to_s.delete_prefix("/"))
166
+ end
167
+
168
+ # The title in every language it's translated to.
169
+ def titles = @messages.transform_values { |messages| messages[title] }.compact
170
+
171
+ def load_messages(file)
172
+ messages = YAML.safe_load_file(file)
173
+ raise ArgumentError, "#{file} must map English text to translations" unless messages.instance_of?(Hash)
174
+
175
+ messages.to_h { |english, translated| [english.to_s, translated.to_s] }
176
+ end
177
+ end
178
+
179
+ # The page's blocks; override this.
180
+ def blocks(_request) = []
181
+
182
+ # Runs an action; returns the message to show.
183
+ def run(name, request)
184
+ block = self.class.actions.fetch(name)
185
+ result = instance_exec(request, &block)
186
+ result.instance_of?(String) ? result : request.t("Done")
187
+ end
188
+
189
+ # Runs a data block; returns its value.
190
+ def fetch(name, request) = instance_exec(request, &self.class.data_sources.fetch(name))
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ class Web
5
+ # Fine-grained actions passed to Robin::Web.authorize. Everything
6
+ # except :read changes state.
7
+ module Permissions
8
+ ACTIONS = {
9
+ read: "View the dashboard",
10
+ retry: "Retry or enqueue jobs now",
11
+ edit: "Edit job arguments",
12
+ delete: "Delete jobs and clear queues",
13
+ kill: "Move jobs to the dead set",
14
+ pause: "Pause and resume queues, job classes and cron jobs",
15
+ cancel: "Cancel jobs",
16
+ process_control: "Quiet, stop or resize processes",
17
+ enqueue: "Run cron jobs now",
18
+ limiters: "Reset and delete rate limiters",
19
+ batches: "Delete batches",
20
+ profiles: "Delete job profiles",
21
+ extensions: "Use actions on extension pages"
22
+ }.freeze
23
+
24
+ class << self
25
+ def allowed?(env, action, resource = nil)
26
+ return false if !action.eql?(:read) && Web.read_only
27
+
28
+ auth = Web.authorizer
29
+ return true unless auth
30
+
31
+ !!auth.call(env, action, resource)
32
+ end
33
+
34
+ def for(env)
35
+ ACTIONS.to_h { |action, _| [action.to_s, allowed?(env, action)] }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ class Web
5
+ # Prometheus text exposition at GET /metrics, one sample set per
6
+ # configured cluster (labelled cluster="...").
7
+ class Prometheus
8
+ # name, type, help, Robin::Stats reader
9
+ STATS = [
10
+ ["robin_jobs_processed_total", "counter", "Jobs processed since stats were last reset", :processed],
11
+ ["robin_jobs_failed_total", "counter", "Jobs that raised since stats were last reset", :failed],
12
+ ["robin_jobs_enqueued", "gauge", "Jobs waiting in all queues", :enqueued],
13
+ ["robin_scheduled_jobs", "gauge", "Jobs in the schedule set", :scheduled_size],
14
+ ["robin_retry_jobs", "gauge", "Jobs waiting to be retried", :retry_size],
15
+ ["robin_dead_jobs", "gauge", "Jobs in the dead set", :dead_size],
16
+ ["robin_processes", "gauge", "Live Robin server processes", :processes_size],
17
+ ["robin_busy_workers", "gauge", "Worker threads currently running a job", :workers_size]
18
+ ].freeze #: Array[[String, String, String, Symbol]]
19
+
20
+ QUEUE_SIZE = ["robin_queue_size", "gauge", "Jobs waiting in a queue"].freeze #: [String, String, String]
21
+ QUEUE_LATENCY = ["robin_queue_latency_seconds", "gauge", "Age of the oldest job in a queue"].freeze #: [String, String, String]
22
+ GVL = ["robin_process_gvl_contention_percent", "gauge",
23
+ "Share of the time a process's threads could wait for the GVL that they did"].freeze #: [String, String, String]
24
+
25
+ def initialize(env)
26
+ @env = env
27
+ end
28
+
29
+ def call
30
+ return [403, {"content-type" => "text/plain"}, ["Forbidden"]] unless Permissions.allowed?(@env, :read, "/metrics")
31
+
32
+ clusters = Web.clusters.map { |name, config| [name, config, Stats.new(config: config)] } #: Array[[String, Config, Stats]]
33
+ out = +""
34
+ STATS.each do |name, type, help, reader|
35
+ header(out, name, type, help)
36
+ clusters.each { |cluster, _, stats| sample(out, name, {"cluster" => cluster}, stats.public_send(reader)) }
37
+ end
38
+ queues = clusters.flat_map { |cluster, config, stats| queue_samples(cluster, config, stats) }
39
+ # (Steep can't splat a tuple into arguments, here and below.)
40
+ header(out, *QUEUE_SIZE) # steep:ignore ArgumentTypeMismatch
41
+ queues.each { |labels, size, _| sample(out, QUEUE_SIZE.fetch(0), labels, size) }
42
+ header(out, *QUEUE_LATENCY) # steep:ignore ArgumentTypeMismatch
43
+ queues.each { |labels, _, latency| sample(out, QUEUE_LATENCY.fetch(0), labels, latency) }
44
+ header(out, *GVL) # steep:ignore ArgumentTypeMismatch
45
+ clusters.each do |cluster, config, _|
46
+ ProcessSet.new(config: config).each do |process|
47
+ next unless process.gvl_contention
48
+
49
+ sample(out, GVL.fetch(0), {"cluster" => cluster, "process" => process.identity}, process.gvl_contention)
50
+ end
51
+ end
52
+ [200, {"content-type" => "text/plain; version=0.0.4; charset=utf-8", "cache-control" => "no-store"}, [out]]
53
+ end
54
+
55
+ private
56
+
57
+ # [labels, size, latency] for every queue in one cluster.
58
+ def queue_samples(cluster, config, stats)
59
+ names = stats.queues.keys
60
+ oldest = config.redis { |c| c.pipelined { |p| names.each { |n| p.call("LINDEX", config.key("queue", n), -1) } } }
61
+ names.zip(oldest).map do |queue, raw|
62
+ [{"cluster" => cluster, "queue" => queue}, stats.queues.fetch(queue), Queue.latency_of(raw)]
63
+ end
64
+ end
65
+
66
+ def header(out, name, type, help)
67
+ out << "# HELP #{name} #{help}\n# TYPE #{name} #{type}\n"
68
+ end
69
+
70
+ def sample(out, name, labels, value)
71
+ out << "#{name}{#{labels.map { |k, v| "#{k}=\"#{escape(v)}\"" }.join(",")}} #{value}\n"
72
+ end
73
+
74
+ def escape(value)
75
+ value.gsub("\\", "\\\\\\\\").gsub("\n", "\\n").gsub('"', '\\"')
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module Robin
6
+ class Web
7
+ # Dispatches /api requests to Robin::Web::API, enforcing CSRF and
8
+ # authorization rules.
9
+ class Router
10
+ Route = Struct.new(:verb, :pattern, :keys, :handler, :action)
11
+
12
+ class HttpError < StandardError
13
+ attr_reader :status
14
+
15
+ def initialize(status, message)
16
+ @status = status
17
+ super(message)
18
+ end
19
+
20
+ def body = {"error" => message}
21
+ end
22
+
23
+ # verb, path, API method, permission needed (:read for viewing;
24
+ # :write handlers check their own fine-grained permission).
25
+ ROUTES = [
26
+ ["GET", "/api/session", :session, :read],
27
+ ["GET", "/api/dashboard", :dashboard, :read],
28
+ ["GET", "/api/stats", :stats, :read],
29
+ ["GET", "/api/stream", :stream, :read],
30
+ ["GET", "/api/info", :info, :read],
31
+ ["GET", "/api/queues", :queues, :read],
32
+ ["GET", "/api/queues/:name", :queue, :read],
33
+ ["POST", "/api/queues/:name/pause", :pause_queue, :pause],
34
+ ["POST", "/api/queues/:name/unpause", :unpause_queue, :pause],
35
+ ["POST", "/api/queues/:name/delete_jobs", :delete_queue_jobs, :delete],
36
+ ["POST", "/api/queues/:name/delete_class", :delete_queue_class, :delete],
37
+ ["DELETE", "/api/queues/:name", :clear_queue, :delete],
38
+ ["POST", "/api/classes/:name/pause", :pause_class, :pause],
39
+ ["POST", "/api/classes/:name/unpause", :unpause_class, :pause],
40
+ ["GET", "/api/busy", :busy, :read],
41
+ ["POST", "/api/processes/:identity/resize", :resize_process, :process_control],
42
+ ["POST", "/api/processes/:identity/:signal", :signal_process, :process_control],
43
+ ["POST", "/api/processes/:signal", :signal_all_processes, :process_control],
44
+ ["GET", "/api/sets/:set", :set, :read],
45
+ ["GET", "/api/sets/:set/:id", :set_entry, :read],
46
+ ["POST", "/api/sets/:set/bulk", :set_bulk, :write],
47
+ ["POST", "/api/sets/:set/all", :set_all, :write],
48
+ ["POST", "/api/sets/:set/:id/edit", :edit_entry, :edit],
49
+ ["GET", "/api/jobs/:jid", :find_job, :read],
50
+ ["POST", "/api/jobs/:jid/cancel", :cancel_job, :cancel],
51
+ ["GET", "/api/metrics", :metrics, :read],
52
+ ["GET", "/api/metrics/history", :metrics_history, :read],
53
+ ["GET", "/api/metrics/queues", :metrics_queues, :read],
54
+ ["GET", "/api/metrics/jobs/:klass", :metrics_job, :read],
55
+ ["GET", "/api/batches", :batches, :read],
56
+ ["GET", "/api/batches/:bid", :batch, :read],
57
+ ["POST", "/api/batches/:bid/cancel", :cancel_batch, :cancel],
58
+ ["DELETE", "/api/batches/:bid", :delete_batch, :batches],
59
+ ["GET", "/api/limiters", :limiters, :read],
60
+ ["POST", "/api/limiters/:name/reset", :reset_limiter, :limiters],
61
+ ["DELETE", "/api/limiters/:name", :delete_limiter, :limiters],
62
+ ["GET", "/api/cron", :cron, :read],
63
+ ["GET", "/api/cron/:id", :cron_entry, :read],
64
+ ["POST", "/api/cron/:id/enqueue", :cron_enqueue, :enqueue],
65
+ ["POST", "/api/cron/:id/pause", :cron_pause, :pause],
66
+ ["POST", "/api/cron/:id/unpause", :cron_unpause, :pause],
67
+ ["GET", "/api/profiles", :profiles, :read],
68
+ ["GET", "/api/profiles/:jid", :profile_data, :read],
69
+ ["DELETE", "/api/profiles/:jid", :delete_profile, :profiles],
70
+ ["GET", "/api/extensions/:slug", :extension_page, :read],
71
+ ["GET", "/api/extensions/:slug/data/:name", :extension_data, :read],
72
+ ["POST", "/api/extensions/:slug/actions/:action", :extension_action, :write]
73
+ ].freeze #: Array[[String, String, Symbol, Symbol]]
74
+
75
+ # Actions checked by the handler itself, or not at all.
76
+ SELF_CHECKED = %i[read write].freeze
77
+
78
+ attr_reader :routes
79
+
80
+ def initialize
81
+ @routes = ROUTES.map { |verb, path, handler, action| compile(verb, path, handler, action) }
82
+ end
83
+
84
+ def call(env)
85
+ request = Rack::Request.new(env)
86
+ verb = request.head? ? "GET" : request.request_method
87
+ path = request.path_info
88
+ matched = match(verb, path)
89
+ return API.json(404, {"error" => "No API endpoint for #{verb} #{path}"}) unless matched
90
+
91
+ route, captures = matched
92
+ return API.json(403, {"error" => "Cross-site request rejected"}) unless verb == "GET" || same_origin?(request)
93
+
94
+ params = request.GET.merge(captures)
95
+ cluster = cluster_name(params["cluster"])
96
+ return API.json(404, {"error" => "Unknown cluster #{cluster.inspect}"}) unless Web.clusters.key?(cluster)
97
+ return API.json(403, {"error" => "You don't have access to this dashboard"}) unless Permissions.allowed?(env, :read, path)
98
+
99
+ api = API.new(env, request, cluster, params)
100
+ api.authorize!(route.action, path) unless SELF_CHECKED.include?(route.action)
101
+ api.public_send(route.handler)
102
+ rescue HttpError => e
103
+ API.json(e.status, e.body)
104
+ rescue JSON::ParserError
105
+ API.json(400, {"error" => "Request body must be valid JSON"})
106
+ end
107
+
108
+ private
109
+
110
+ def compile(verb, path, handler, action)
111
+ keys = path.scan(/:(\w+)/).flatten
112
+ Route.new(verb, /\A#{path.gsub(/:\w+/, "([^/]+)")}\z/, keys, handler, action)
113
+ end
114
+
115
+ # Returns the route and its decoded path parameters.
116
+ def match(verb, path)
117
+ @routes.each do |route|
118
+ next unless route.verb == verb
119
+
120
+ m = route.pattern.match(path) or next
121
+ # (Every group in a route's pattern matches something, so no capture is nil.)
122
+ return [route, route.keys.zip(m.captures.map { |c| URI.decode_www_form_component(c) }).to_h] # steep:ignore ArgumentTypeMismatch
123
+ end
124
+ nil
125
+ end
126
+
127
+ # The requested cluster, or the first one when none is given.
128
+ def cluster_name(name)
129
+ name.to_s.empty? ? Web.clusters.keys.first : name
130
+ end
131
+
132
+ # Fetch metadata values a browser sends for requests from the page
133
+ # itself (or typed in by the user).
134
+ SAME_SITE_FETCHES = ["same-origin", "none", ""].freeze
135
+
136
+ # Non-GET requests must carry our custom header (which browsers
137
+ # won't send cross-site without CORS approval), a Sec-Fetch-Site
138
+ # saying the browser didn't send it on another site's behalf, and an
139
+ # Origin matching the host. The last two are checked when present
140
+ # (older browsers and non-browser clients leave them out).
141
+ def same_origin?(request)
142
+ return false unless request.get_header("HTTP_X_ROBIN_REQUEST") == "1"
143
+ return false unless SAME_SITE_FETCHES.include?(request.get_header("HTTP_SEC_FETCH_SITE").to_s)
144
+
145
+ origin = request.get_header("HTTP_ORIGIN").to_s
146
+ origin.empty? || origin == request.base_url
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ class Web
5
+ # Serves the compiled single-page app from lib/robin/web/dist.
6
+ #
7
+ # Hashed files under /assets are cached forever; every other GET gets
8
+ # index.html (with the mount path filled in) so client-side routes
9
+ # work on reload.
10
+ class Static
11
+ TYPES = {
12
+ ".js" => "text/javascript; charset=utf-8", ".mjs" => "text/javascript; charset=utf-8", ".css" => "text/css; charset=utf-8",
13
+ ".svg" => "image/svg+xml", ".woff2" => "font/woff2", ".png" => "image/png",
14
+ ".ico" => "image/x-icon", ".json" => "application/json", ".map" => "application/json"
15
+ }.freeze
16
+
17
+ CSP = [
18
+ "default-src 'self'", "script-src 'self'", "style-src 'self'", "img-src 'self' data:",
19
+ "font-src 'self'", "connect-src 'self'", "frame-ancestors 'self'", "base-uri 'self'", "form-action 'self'"
20
+ ].join("; ").freeze
21
+
22
+ SECURITY_HEADERS = {
23
+ "content-security-policy" => CSP, "x-content-type-options" => "nosniff",
24
+ "referrer-policy" => "same-origin", "x-frame-options" => "SAMEORIGIN"
25
+ }.freeze
26
+
27
+ ASSET_CACHE = "public, max-age=31536000, immutable"
28
+
29
+ UNBUILT = "Robin's dashboard assets aren't built. Run `bun install && bun run build` in the web/ directory."
30
+
31
+ def initialize(root)
32
+ @root = root
33
+ end
34
+
35
+ def call(env)
36
+ respond(env) do
37
+ path = env.fetch("PATH_INFO")
38
+ next index(env) unless path.start_with?("/assets/")
39
+
40
+ file = File.join(@root, path)
41
+ (path.include?("..") || !File.file?(file)) ? not_found : file(env, file, ASSET_CACHE)
42
+ end
43
+ end
44
+
45
+ # Serves +file+ (a full path, or nil for a 404), e.g. from an
46
+ # extension's assets. They aren't fingerprinted, so browsers check
47
+ # back each time.
48
+ def call_file(env, file)
49
+ respond(env) { file ? file(env, file, "no-cache") : not_found }
50
+ end
51
+
52
+ private
53
+
54
+ def not_found = [404, {"content-type" => "text/plain"}, ["Not found"]]
55
+
56
+ def respond(env)
57
+ method = env.fetch("REQUEST_METHOD")
58
+ return [405, {"content-type" => "text/plain"}, ["Method not allowed"]] unless method == "GET" || method == "HEAD"
59
+
60
+ status, headers, body = yield
61
+ [status, headers, (method == "HEAD") ? [] : body]
62
+ end
63
+
64
+ def file(env, file, cache)
65
+ headers = {
66
+ "content-type" => TYPES.fetch(File.extname(file), "application/octet-stream"),
67
+ "cache-control" => cache, "vary" => "accept-encoding", "x-content-type-options" => "nosniff"
68
+ }
69
+ if env["HTTP_ACCEPT_ENCODING"].to_s.include?("gzip") && File.file?("#{file}.gz")
70
+ headers["content-encoding"] = "gzip"
71
+ file = "#{file}.gz"
72
+ end
73
+ data = File.binread(file)
74
+ headers["content-length"] = data.bytesize.to_s
75
+ [200, headers, [data]]
76
+ end
77
+
78
+ def index(env)
79
+ template = index_template
80
+ return [503, {"content-type" => "text/plain; charset=utf-8"}, [UNBUILT]] unless template
81
+
82
+ base = "#{env.fetch("SCRIPT_NAME")}/".squeeze("/")
83
+ html = template
84
+ .gsub("%ROBIN_BASE%") { Rack::Utils.escape_html(base) }
85
+ .gsub("%ROBIN_TITLE%") { Rack::Utils.escape_html(Web.title) }
86
+ html = html.gsub('"assets/') { %("#{Rack::Utils.escape_html(Web.assets_path)}) } if Web.assets_path
87
+ [200, {"content-type" => "text/html; charset=utf-8", "cache-control" => "no-store"}.merge(security_headers), [html]]
88
+ end
89
+
90
+ # With assets on another host (Web.assets_path), the policy lets
91
+ # scripts, styles, images and fonts come from it.
92
+ def security_headers
93
+ origin = Web.assets_path&.[](%r{\Ahttps?://[^/]+}i) or return SECURITY_HEADERS
94
+
95
+ SECURITY_HEADERS.merge({"content-security-policy" => CSP.gsub(/(?:script|style|img|font)-src 'self'/) { |src| "#{src} #{origin}" }})
96
+ end
97
+
98
+ # index.html, re-read whenever it changes on disk (e.g. `bun run dev`).
99
+ def index_template
100
+ path = File.join(@root, "index.html")
101
+ return unless File.file?(path)
102
+
103
+ mtime = File.mtime(path)
104
+ @index = File.read(path) unless mtime == @index_mtime
105
+ @index_mtime = mtime
106
+ @index
107
+ end
108
+ end
109
+ end
110
+ end