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,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ # Cooperative job cancellation.
5
+ #
6
+ # jid = ExportJob.perform_async(account.id)
7
+ # Robin.cancel!(jid)
8
+ # Robin.cancel!(*jids) # several in one round trip
9
+ #
10
+ # Jobs not yet started are skipped when fetched. Running jobs see
11
+ # #cancelled? turn true and should return early. Servers refresh their
12
+ # local copy of the cancellation list on every heartbeat, so the
13
+ # fetch-time check costs no extra Redis round-trip.
14
+ #
15
+ # A cancelled batch (Robin::Batch#cancel!) is listed as "batch:<bid>",
16
+ # and cancels every job in it the same way.
17
+ module Cancellation
18
+ @cache = {}
19
+
20
+ class << self
21
+ def cancel!(*jids, ttl: 86_400, config: Robin.config)
22
+ jids = jids.flatten
23
+ return true if jids.empty?
24
+
25
+ expires = Robin.now_ms + ttl * 1000
26
+ config.redis do |c|
27
+ c.pipelined do |p|
28
+ p.call("ZADD", config.key("cancelled"), *jids.flat_map { |jid| [expires, jid] })
29
+ p.call("ZREMRANGEBYSCORE", config.key("cancelled"), "-inf", Robin.now_ms)
30
+ end
31
+ end
32
+ @cache[config.object_id]&.merge(jids)
33
+ true
34
+ end
35
+
36
+ # Live check against Redis: was the job, or its batch, cancelled?
37
+ def cancelled?(jid, bid: nil, config: Robin.config)
38
+ members = [jid, bid && "batch:#{bid}"].compact
39
+ return false if members.empty?
40
+
41
+ config.redis { |c| c.call("ZMSCORE", config.key("cancelled"), *members) }.any?
42
+ end
43
+
44
+ # Fast in-memory check used by processors.
45
+ def skip?(config, jid, bid = nil)
46
+ set = @cache[config.object_id]
47
+ !set.nil? && (set.include?(jid) || set.include?("batch:#{bid}"))
48
+ end
49
+
50
+ def refresh(config, conn) = store(config, conn.call(*listing(config)))
51
+
52
+ # The command that reads the live cancellation list, for a heartbeat
53
+ # to send along with others; pass its reply to #store.
54
+ def listing(config) = ["ZRANGE", config.key("cancelled"), Robin.now_ms, "+inf", "BYSCORE"]
55
+
56
+ # Replaces the local copy of the cancellation list.
57
+ def store(config, jids)
58
+ @cache[config.object_id] = Set.new(jids)
59
+ end
60
+ end
61
+ end
62
+ end
data/lib/robin/cli.rb ADDED
@@ -0,0 +1,325 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "yaml"
5
+ require "erb"
6
+ require "net/http"
7
+ require_relative "rails/app_loader"
8
+
9
+ module Robin
10
+ # The `robin` command.
11
+ #
12
+ # robin -r ./app.rb -c 10 -q critical,3 -q default
13
+ # robin -C config/robin.yml
14
+ # robin swarm -n 4 # see Robin::Swarm
15
+ # robin stats # print cluster stats as JSON
16
+ # robin deploy "v42" # mark a deploy (see Robin::Deploy)
17
+ #
18
+ # It boots Rails apps itself (see Robin::Rails::AppLoader). Gems extend
19
+ # the command with plugin files at robin/plugins/*.rb (found with
20
+ # Gem.find_files and loaded before options are parsed). A plugin can add
21
+ # an app loader, which boots an application directory given with -r, or
22
+ # an environment variable that names the application environment.
23
+ class CLI
24
+ # INFO (Ctrl-T in a terminal) exists on macOS and the BSDs only.
25
+ SIGNALS = %w[INT TERM TSTP TTIN INFO USR2].freeze
26
+ PLUGIN_GLOB = "robin/plugins/*.rb"
27
+ HEALTH_TIMEOUT = 2
28
+
29
+ @app_loaders = [Rails::AppLoader]
30
+ @environment_variables = %w[RAILS_ENV APP_ENV RACK_ENV]
31
+ @config_paths = %w[config/robin.yml config/robin.yml.erb]
32
+ @config_file_hooks = []
33
+
34
+ class << self
35
+ # Callables tried in order with an application directory; the first
36
+ # that returns truthy has loaded the app. Otherwise the directory's
37
+ # app.rb, if any, is required.
38
+ attr_reader :app_loaders
39
+
40
+ # Variables naming the application environment, by precedence. They
41
+ # pick the config file section, and -e sets all of them.
42
+ attr_reader :environment_variables
43
+
44
+ # Config files used (the first that exists) without -C.
45
+ attr_reader :config_paths
46
+
47
+ # Callables run with the config file's settings (a Hash with Symbol
48
+ # keys, the environment's section merged in) and the config, once
49
+ # the command's own options are applied: a plugin can apply settings
50
+ # of its own.
51
+ attr_reader :config_file_hooks
52
+
53
+ # The app directory's name, or, in a Capistrano-style release
54
+ # directory (/srv/app/releases/20260101120000), the app's.
55
+ def default_tag(dir)
56
+ parent = File.dirname(dir)
57
+ (File.basename(parent) == "releases") ? File.basename(File.dirname(parent)) : File.basename(dir)
58
+ end
59
+
60
+ def plugin_files = Gem.find_files(PLUGIN_GLOB)
61
+
62
+ def load_plugins
63
+ plugin_files.each { |file| require file }
64
+ end
65
+ end
66
+
67
+ attr_reader :config, :launcher
68
+
69
+ def initialize(argv = ARGV, config: Robin.config)
70
+ @argv = argv.dup
71
+ @config = config
72
+ @options = {}
73
+ @file_options = {}
74
+ end
75
+
76
+ def run
77
+ CLI.load_plugins
78
+ command = @argv.first unless @argv.first.to_s.start_with?("-")
79
+ case command
80
+ when "swarm"
81
+ parse
82
+ Swarm.new(self).run
83
+ when "stats"
84
+ parse
85
+ boot(server: false)
86
+ stats = Stats.new(config: config)
87
+ puts JSON.pretty_generate(stats.to_h.merge({"queues" => stats.queues}))
88
+ when "top"
89
+ parse
90
+ abort "robin top needs a terminal" unless $stdin.tty? && $stdout.tty?
91
+ boot(server: false)
92
+ Top.new(config: config).run
93
+ when "deploy"
94
+ parse
95
+ boot(server: false)
96
+ Deploy.mark!(@argv.drop(1).join(" ").then { |l| l unless l.empty? }, config: config)
97
+ when "health"
98
+ parse
99
+ exit(health_check)
100
+ when "version"
101
+ puts "Robin #{VERSION}"
102
+ when nil
103
+ parse
104
+ boot
105
+ prepare
106
+ serve
107
+ else
108
+ abort "robin: unknown command #{command.inspect} (try `robin --help`)"
109
+ end
110
+ end
111
+
112
+ def parse
113
+ parser = OptionParser.new do |o|
114
+ o.require_exact = true
115
+ o.banner = "Usage: robin [swarm|stats|top|deploy [LABEL]|health|version] [options]"
116
+ o.on("-c", "--concurrency N", Integer, "Worker threads (default 5)") { |v| @options[:concurrency] = v }
117
+ o.on("-q", "--queue NAME[,WEIGHT]", "Queue to process; repeatable") { |v| (@options[:queues] ||= []) << v }
118
+ o.on("-r", "--require PATH", "File or app directory to load") { |v| @options[:require] = v }
119
+ o.on("-e", "--environment ENV", "Application environment") { |v| @options[:environment] = v }
120
+ o.on("-t", "--timeout SECONDS", Integer, "Shutdown timeout (default 25)") { |v| @options[:timeout] = v }
121
+ o.on("-g", "--tag TAG", "Process tag shown in the web UI") { |v| @options[:tag] = v }
122
+ o.on("-C", "--config PATH", "YAML config file") { |v| @options[:config_file] = v }
123
+ o.on("-n", "--processes N", "swarm: child processes (a decimal: per CPU core)") { |v| @options[:processes] = v }
124
+ o.on("--health-port PORT", Integer, "Serve a health check on PORT") { |v| @options[:health_port] = v }
125
+ o.on("-v", "--verbose", "Debug logging") { @options[:verbose] = true }
126
+ o.on("--json-logs", "Log one JSON object per line") { @options[:json_logs] = true }
127
+ o.on("-V", "--version", "Print version") do
128
+ puts "Robin #{VERSION}"
129
+ exit
130
+ end
131
+ o.on("-h", "--help", "Show help") do
132
+ puts o
133
+ exit
134
+ end
135
+ end
136
+ parser.parse!(@argv)
137
+ load_config_file
138
+ @options
139
+ end
140
+
141
+ attr_reader :options
142
+
143
+ # Loads the application and applies options. Commands that only read
144
+ # or write Redis (stats, top, deploy) boot as clients: the app's
145
+ # configure_client blocks run, not its configure_server blocks.
146
+ def boot(server: true)
147
+ Robin.server! if server
148
+ if (env = @options[:environment])
149
+ CLI.environment_variables.each { |name| ENV[name] = env }
150
+ config.environment = env
151
+ end
152
+ load_application
153
+ apply_options
154
+ end
155
+
156
+ # Loads all of Robin and tidies the heap (a full GC, compaction) before
157
+ # a standalone server starts, rather than while it runs jobs. (A swarm
158
+ # does both before forking its children.)
159
+ def prepare
160
+ Robin.eager_load!
161
+ Process.warmup
162
+ end
163
+
164
+ # Runs a server until INT or TERM.
165
+ def serve
166
+ @launcher = build_launcher
167
+ reader, @signals = IO.pipe
168
+ @launcher.on_stop_request = -> { signal("TERM") }
169
+ previous = trap_signals(SIGNALS)
170
+ begin
171
+ print_banner
172
+ @launcher.run
173
+ wait_for_signals(reader)
174
+ config.fire_event(:exit)
175
+ ensure
176
+ previous.each { |sig, handler| trap(sig, handler) }
177
+ end
178
+ end
179
+
180
+ # Hands a signal to the serving thread. Signal handlers can't do much
181
+ # more than write to a pipe safely.
182
+ def signal(name)
183
+ @signals.write_nonblock("#{name}\n", exception: false)
184
+ end
185
+
186
+ private
187
+
188
+ def build_launcher = Launcher.new(config)
189
+
190
+ # Installs handlers and returns the previous ones. A handler the app (or
191
+ # a gem) had already installed still runs, after Robin's. Signals the
192
+ # platform doesn't support are skipped.
193
+ def trap_signals(names)
194
+ names.each_with_object({}) do |sig, previous|
195
+ previous[sig] = trap(sig) do |signo|
196
+ signal(sig)
197
+ chain(previous.fetch(sig), signo)
198
+ end
199
+ rescue ArgumentError
200
+ # not available on this platform
201
+ end
202
+ end
203
+
204
+ # Runs a previous signal handler, if it was a block (not "DEFAULT",
205
+ # "IGNORE" and such).
206
+ def chain(handler, signo)
207
+ handler.call(signo) if handler.respond_to?(:call)
208
+ rescue => e
209
+ config.handle_exception(e, {context: "previous #{Signal.signame(signo)} handler"})
210
+ end
211
+
212
+ def wait_for_signals(reader)
213
+ loop do
214
+ case reader.gets(chomp: true)
215
+ when "INT", "TERM" then return @launcher.stop
216
+ when "TSTP" then @launcher.quiet
217
+ when "TTIN", "INFO" then @launcher.dump_threads
218
+ when "USR2" then reopen_logs
219
+ end
220
+ end
221
+ end
222
+
223
+ def reopen_logs
224
+ config.logger.reopen if config.logger.respond_to?(:reopen)
225
+ end
226
+
227
+ # `robin health`: asks the health endpoint of the server on this host
228
+ # (see Robin::HealthServer) and prints its report. True when healthy.
229
+ def health_check
230
+ port = @options[:health_port] || ENV["ROBIN_HEALTH_PORT"]
231
+ abort "robin: no health port (use --health-port, health_port in robin.yml or ROBIN_HEALTH_PORT)" unless port
232
+
233
+ response = health_http(port).start { |http| http.get("/") }
234
+ puts response.body
235
+ response.code == "200"
236
+ rescue SystemCallError, IOError, Net::OpenTimeout, Net::ReadTimeout => e
237
+ warn "robin: health check failed: #{e.class}: #{e.message}"
238
+ false
239
+ end
240
+
241
+ # A client for the local health endpoint: quick to give up, and never
242
+ # retrying (Net::HTTP retries idempotent requests once by default).
243
+ def health_http(port)
244
+ Net::HTTP.new("127.0.0.1", Integer(port)).tap do |http|
245
+ http.open_timeout = HEALTH_TIMEOUT
246
+ http.read_timeout = HEALTH_TIMEOUT
247
+ http.max_retries = 0
248
+ end
249
+ end
250
+
251
+ def load_config_file
252
+ path = @options[:config_file]
253
+ # In the current directory, else in the app directory given with -r
254
+ # (when -r names a file, nothing can be found under it).
255
+ app = @options[:require]
256
+ roots = [".", *app]
257
+ path ||= roots.product(CLI.config_paths).map { |root, p| File.join(root, p) }.find { |p| File.exist?(p) }
258
+ return unless path
259
+
260
+ raw = YAML.safe_load(ERB.new(File.read(path), trim_mode: "-").result, permitted_classes: [Symbol], aliases: true) || {}
261
+ raw = raw.transform_keys(&:to_sym)
262
+ env = (@options[:environment] || ENV.values_at(*CLI.environment_variables).compact.first).to_s
263
+ section = raw[env.to_sym]
264
+ raw = raw.merge(section.transform_keys(&:to_sym)) if section.instance_of?(Hash)
265
+ @file_options = raw
266
+ @options = raw.slice(:concurrency, :queues, :require, :timeout, :tag, :environment, :processes, :health_port).merge(@options)
267
+ end
268
+
269
+ def apply_options
270
+ # Values from YAML may be explicitly null; those leave defaults alone.
271
+ if (concurrency = @options[:concurrency]) then config.concurrency = concurrency end
272
+ if (queues = @options[:queues]) then config.queues = queues end
273
+ if (timeout = @options[:timeout]) then config.timeout = timeout end
274
+ if (tag = @options[:tag]) then config.tag = tag end
275
+ if (port = @options[:health_port]) then config.health_port = port end
276
+ config.tag ||= CLI.default_tag(Dir.pwd)
277
+ config.logger.level = Logger::DEBUG if @options.key?(:verbose)
278
+ config.logger.formatter = JobLogger::JSONFormatter.new if @options.key?(:json_logs)
279
+ @file_options.fetch(:capsules, {}).each do |name, opts|
280
+ opts = opts.transform_keys(&:to_sym)
281
+ config.capsule(name) do |cap|
282
+ if (concurrency = opts[:concurrency]) then cap.concurrency = concurrency end
283
+ if (queues = opts[:queues]) then cap.queues = queues end
284
+ end
285
+ end
286
+ if config.capsules.values.any? { |c| c.queues.empty? }
287
+ abort "robin: every capsule needs at least one queue"
288
+ end
289
+ CLI.config_file_hooks.each { |hook| hook.call(@file_options, config) }
290
+ end
291
+
292
+ def load_application
293
+ path = @options[:require] || "."
294
+ if File.directory?(path)
295
+ load_directory(path)
296
+ elsif File.exist?(path)
297
+ require File.expand_path(path)
298
+ else
299
+ abort "robin: can't load #{path.inspect}"
300
+ end
301
+ end
302
+
303
+ def load_directory(path)
304
+ return if CLI.app_loaders.any? { |loader| loader.call(path) }
305
+
306
+ app = File.join(path, "app.rb")
307
+ if File.exist?(app)
308
+ require File.expand_path(app)
309
+ else
310
+ config.logger.warn { "robin: nothing to load in #{path} (use -r FILE)" }
311
+ end
312
+ end
313
+
314
+ def print_banner
315
+ return unless $stdout.tty?
316
+
317
+ puts <<~BANNER
318
+ \e[31m __
319
+ <(o )___ \e[1mRobin #{VERSION}\e[0;31m
320
+ ( ._> / #{config.total_concurrency} threads · queues: #{config.capsules.values.flat_map(&:queues).uniq.join(", ")}
321
+ `---' LGPL-3.0 · fully open source\e[0m
322
+ BANNER
323
+ end
324
+ end
325
+ end