cosmonats 0.9.0 → 0.10.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +54 -13
  3. data/lib/cosmo/active_job/adapter.rb +2 -0
  4. data/lib/cosmo/active_job/executor.rb +7 -0
  5. data/lib/cosmo/active_job/options.rb +6 -0
  6. data/lib/cosmo/api/batch.rb +26 -0
  7. data/lib/cosmo/api/counter.rb +51 -3
  8. data/lib/cosmo/api/cron/entry.rb +24 -9
  9. data/lib/cosmo/api/cron.rb +21 -7
  10. data/lib/cosmo/api/job.rb +14 -0
  11. data/lib/cosmo/api/kv.rb +138 -9
  12. data/lib/cosmo/api/registry.rb +47 -6
  13. data/lib/cosmo/api/stats/jobs.rb +84 -0
  14. data/lib/cosmo/api/stats/metrics.rb +18 -7
  15. data/lib/cosmo/api/stats/processes.rb +27 -11
  16. data/lib/cosmo/api/stats/totals.rb +1 -0
  17. data/lib/cosmo/api/stats.rb +47 -11
  18. data/lib/cosmo/api/stream.rb +66 -22
  19. data/lib/cosmo/batch/callback.rb +5 -0
  20. data/lib/cosmo/batch/dispatcher.rb +4 -4
  21. data/lib/cosmo/batch.rb +34 -11
  22. data/lib/cosmo/cli.rb +6 -0
  23. data/lib/cosmo/client.rb +208 -11
  24. data/lib/cosmo/config/cosmo.yml +1 -0
  25. data/lib/cosmo/config/settings.rb +4 -0
  26. data/lib/cosmo/config.rb +58 -10
  27. data/lib/cosmo/engine.rb +28 -1
  28. data/lib/cosmo/heartbeat.rb +12 -3
  29. data/lib/cosmo/http_server/handler.rb +5 -0
  30. data/lib/cosmo/http_server/health.rb +2 -0
  31. data/lib/cosmo/http_server/ping.rb +2 -0
  32. data/lib/cosmo/http_server.rb +8 -0
  33. data/lib/cosmo/job/data.rb +11 -0
  34. data/lib/cosmo/job/enqueuer.rb +100 -1
  35. data/lib/cosmo/job/limit.rb +5 -1
  36. data/lib/cosmo/job/processor.rb +51 -15
  37. data/lib/cosmo/job/setter.rb +53 -0
  38. data/lib/cosmo/job.rb +71 -2
  39. data/lib/cosmo/logger.rb +37 -0
  40. data/lib/cosmo/middleware/chain.rb +12 -0
  41. data/lib/cosmo/middleware/limit.rb +12 -3
  42. data/lib/cosmo/middleware/metrics.rb +6 -4
  43. data/lib/cosmo/middleware/sentry.rb +6 -4
  44. data/lib/cosmo/middleware/totals.rb +5 -11
  45. data/lib/cosmo/middleware.rb +0 -1
  46. data/lib/cosmo/processor.rb +60 -3
  47. data/lib/cosmo/publisher.rb +8 -0
  48. data/lib/cosmo/services.rb +7 -2
  49. data/lib/cosmo/stream/message.rb +5 -0
  50. data/lib/cosmo/stream/processor.rb +7 -0
  51. data/lib/cosmo/stream/serializer.rb +4 -0
  52. data/lib/cosmo/stream.rb +35 -1
  53. data/lib/cosmo/utils/duration.rb +3 -0
  54. data/lib/cosmo/utils/hash.rb +12 -0
  55. data/lib/cosmo/utils/json.rb +8 -0
  56. data/lib/cosmo/utils/signal.rb +9 -0
  57. data/lib/cosmo/utils/stopwatch.rb +1 -0
  58. data/lib/cosmo/utils/string.rb +4 -0
  59. data/lib/cosmo/utils/thread_pool.rb +8 -0
  60. data/lib/cosmo/utils/ttl_cache.rb +10 -0
  61. data/lib/cosmo/version.rb +1 -1
  62. data/lib/cosmo/web/assets/app.css +5 -3
  63. data/lib/cosmo/web/cache.rb +2 -0
  64. data/lib/cosmo/web/chart.rb +2 -2
  65. data/lib/cosmo/web/circuit.rb +1 -0
  66. data/lib/cosmo/web/context.rb +6 -0
  67. data/lib/cosmo/web/controllers/application.rb +26 -0
  68. data/lib/cosmo/web/controllers/batches.rb +7 -1
  69. data/lib/cosmo/web/controllers/crons.rb +16 -2
  70. data/lib/cosmo/web/controllers/jobs.rb +45 -16
  71. data/lib/cosmo/web/controllers/metrics.rb +5 -0
  72. data/lib/cosmo/web/controllers/overview.rb +36 -18
  73. data/lib/cosmo/web/controllers/processes.rb +8 -5
  74. data/lib/cosmo/web/controllers/streams.rb +12 -0
  75. data/lib/cosmo/web/helpers/application.rb +44 -0
  76. data/lib/cosmo/web/renderer.rb +17 -0
  77. data/lib/cosmo/web/throughput.rb +1 -1
  78. data/lib/cosmo/web/views/crons/_table.erb +29 -27
  79. data/lib/cosmo/web/views/jobs/_args.erb +2 -2
  80. data/lib/cosmo/web/views/jobs/_busy.erb +4 -4
  81. data/lib/cosmo/web/views/jobs/_enqueued.erb +2 -2
  82. data/lib/cosmo/web/views/jobs/_panel.erb +6 -1
  83. data/lib/cosmo/web/views/layout.erb +1 -1
  84. data/lib/cosmo/web/views/overview/_badges.erb +1 -1
  85. data/lib/cosmo/web/views/overview/_running.erb +3 -3
  86. data/lib/cosmo/web/views/overview/index.erb +1 -1
  87. data/lib/cosmo/web/views/streams/_info.erb +1 -0
  88. data/lib/cosmo/web/views/streams/_stream.erb +1 -0
  89. data/lib/cosmo/web.rb +8 -1
  90. data/lib/cosmo.rb +33 -0
  91. data/sig/cosmo/active_job/adapter.rbs +3 -3
  92. data/sig/cosmo/active_job/options.rbs +4 -2
  93. data/sig/cosmo/api/batch.rbs +5 -3
  94. data/sig/cosmo/api/counter.rbs +16 -6
  95. data/sig/cosmo/api/cron/entry.rbs +4 -2
  96. data/sig/cosmo/api/cron.rbs +10 -4
  97. data/sig/cosmo/api/kv.rbs +21 -4
  98. data/sig/cosmo/api/registry.rbs +10 -0
  99. data/sig/cosmo/api/stats/jobs.rbs +34 -0
  100. data/sig/cosmo/api/stats/metrics.rbs +3 -0
  101. data/sig/cosmo/api/stats/processes.rbs +9 -1
  102. data/sig/cosmo/api/stats.rbs +8 -2
  103. data/sig/cosmo/api/stream.rbs +10 -5
  104. data/sig/cosmo/batch/callback.rbs +1 -1
  105. data/sig/cosmo/batch/dispatcher.rbs +1 -1
  106. data/sig/cosmo/batch.rbs +5 -5
  107. data/sig/cosmo/cli.rbs +2 -0
  108. data/sig/cosmo/client.rbs +23 -4
  109. data/sig/cosmo/config/settings.rbs +6 -0
  110. data/sig/cosmo/config.rbs +5 -5
  111. data/sig/cosmo/engine.rbs +5 -0
  112. data/sig/cosmo/heartbeat.rbs +2 -0
  113. data/sig/cosmo/job/data.rbs +3 -1
  114. data/sig/cosmo/job/enqueuer.rbs +12 -0
  115. data/sig/cosmo/job/processor.rbs +18 -3
  116. data/sig/cosmo/job/setter.rbs +20 -0
  117. data/sig/cosmo/job.rbs +10 -4
  118. data/sig/cosmo/logger.rbs +5 -3
  119. data/sig/cosmo/middleware.rbs +0 -8
  120. data/sig/cosmo/processor.rbs +14 -2
  121. data/sig/cosmo/publisher.rbs +1 -0
  122. data/sig/cosmo/stream/processor.rbs +4 -0
  123. data/sig/cosmo/stream.rbs +4 -3
  124. data/sig/cosmo/utils/hash.rbs +2 -1
  125. data/sig/cosmo/utils/signal.rbs +5 -5
  126. data/sig/cosmo/utils/string.rbs +2 -2
  127. data/sig/cosmo/utils/thread_pool.rbs +1 -1
  128. data/sig/cosmo/web/cache.rbs +35 -0
  129. data/sig/cosmo/web/chart.rbs +48 -0
  130. data/sig/cosmo/web/circuit.rbs +29 -0
  131. data/sig/cosmo/web/context.rbs +15 -0
  132. data/sig/cosmo/web/controllers/application.rbs +32 -0
  133. data/sig/cosmo/web/controllers/batches.rbs +11 -0
  134. data/sig/cosmo/web/controllers/crons.rbs +23 -0
  135. data/sig/cosmo/web/controllers/jobs.rbs +47 -0
  136. data/sig/cosmo/web/controllers/metrics.rbs +21 -0
  137. data/sig/cosmo/web/controllers/overview.rbs +31 -0
  138. data/sig/cosmo/web/controllers/processes.rbs +13 -0
  139. data/sig/cosmo/web/controllers/streams.rbs +27 -0
  140. data/sig/cosmo/web/helpers/application.rbs +61 -0
  141. data/sig/cosmo/web/renderer.rbs +33 -0
  142. data/sig/cosmo/web/throughput.rbs +45 -0
  143. data/sig/cosmo/web.rbs +25 -0
  144. data/sig/cosmo.rbs +19 -0
  145. metadata +23 -6
  146. data/lib/cosmo/api/stats/busy.rb +0 -68
  147. data/lib/cosmo/middleware/busy.rb +0 -15
  148. data/sig/cosmo/api/stats/busy.rbs +0 -33
@@ -6,6 +6,9 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Processes < Application
9
+ # Renders the processes page, or only its table for htmx.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  return ok(table) if hx_request?
11
14
 
@@ -16,14 +19,14 @@ module Cosmo
16
19
 
17
20
  private
18
21
 
19
- def table # rubocop:disable Metrics/AbcSize
20
- processes = API::Stats::Processes.instance
21
- total = processes.size
22
- limit = (params["limit"] || API::Stats::Processes::LIMIT).to_i
22
+ def table
23
+ registry = API::Stats::Processes.instance
24
+ total = registry.size
25
+ limit = limit(API::Stats::Processes::LIMIT)
23
26
  total_pages = (total.to_f / limit).ceil
24
27
  page = params["page"].to_i.clamp(1, [total_pages, 1].max)
25
28
  polling = params["poll"].to_s != "0"
26
- groups = processes.list(page:, limit:).chunk { API::Stats::Processes.kind(_1) }.to_a
29
+ groups = registry.list(page:, limit:).chunk { API::Stats::Processes.kind(_1) }.to_a
27
30
  render("processes/_table", { groups:, total:, page:, limit:, total_pages:, polling:,
28
31
  query: { limit:, poll: (0 unless polling) }.compact })
29
32
  end
@@ -6,6 +6,9 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Streams < Application
9
+ # Renders the streams page, or only the stream list for htmx.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  return ok(list) if hx_request?
11
14
 
@@ -14,6 +17,9 @@ module Cosmo
14
17
  ok render("streams/index", layout: true)
15
18
  end
16
19
 
20
+ # Renders a stream's configuration.
21
+ #
22
+ # @return [Array] the Rack response
17
23
  def info
18
24
  return ok(render("streams/_info", info_locals(API::Stream.new(name)))) if hx_request?
19
25
 
@@ -22,12 +28,18 @@ module Cosmo
22
28
  ok render("streams/info", { name: }, layout: true)
23
29
  end
24
30
 
31
+ # Pauses a stream, so workers stop fetching from it.
32
+ #
33
+ # @return [Array] the Rack response
25
34
  def pause
26
35
  stream = API::Stream.new(name)
27
36
  stream.pause!
28
37
  toggled(stream, "paused")
29
38
  end
30
39
 
40
+ # Resumes a paused stream.
41
+ #
42
+ # @return [Array] the Rack response
31
43
  def unpause
32
44
  stream = API::Stream.new(name)
33
45
  stream.unpause!
@@ -11,16 +11,24 @@ module Cosmo
11
11
  # Seconds between htmx auto-refreshes. Override with COSMO_WEB_POLL_INTERVAL.
12
12
  POLL_INTERVAL = 5
13
13
 
14
+ # @return [Integer] seconds between htmx auto-refreshes
14
15
  def poll_interval
15
16
  @poll_interval ||= ENV.fetch("COSMO_WEB_POLL_INTERVAL", POLL_INTERVAL).to_i
16
17
  end
17
18
 
19
+ # Renders a partial from within a view.
20
+ #
21
+ # @param template [String] the view, relative to Renderer::VIEWS_ROOT and without +.erb+
22
+ # @param locals [Hash{Symbol => Object}, nil] the partial's instance variables
23
+ # @return [String] the rendered HTML
18
24
  def render(template, locals = nil)
19
25
  defaults = { request: @request }
20
26
  locals = Hash(locals).merge(defaults)
21
27
  erb(template, locals)
22
28
  end
23
29
 
30
+ # @param bytes [Integer, String, nil]
31
+ # @return [String] e.g. "0 B", "1.5 KB", "2.25 GB"
24
32
  def format_bytes(bytes)
25
33
  b = bytes.to_i
26
34
  return "0 B" if b.zero?
@@ -30,10 +38,14 @@ module Cosmo
30
38
  "#{(b / (1024.0**i)).round(2)} #{sizes[i]}"
31
39
  end
32
40
 
41
+ # @param num [Integer, String]
42
+ # @return [String] e.g. "1,234,567"
33
43
  def format_numbers(num)
34
44
  num.to_s.reverse.gsub(/(\d{3})(?=\d)/, "\\1,").reverse
35
45
  end
36
46
 
47
+ # @param value [Numeric, String, nil] Unix seconds
48
+ # @return [String] e.g. "2026-10-16 13:22:16", or "N/A" without a value
37
49
  def format_timestamp(value)
38
50
  return "N/A" unless value
39
51
 
@@ -52,6 +64,8 @@ module Cosmo
52
64
  "#{(millis / 60_000).floor}m #{((millis % 60_000) / 1_000).round}s"
53
65
  end
54
66
 
67
+ # @param value [Time, Integer, String] the start, as a Time or Unix seconds
68
+ # @return [String] the time since then, e.g. "45s", "3m 10s", "2h 5m", "1d 4h"
55
69
  def elapsed(value)
56
70
  elapsed = Time.now.to_i - value.to_i
57
71
 
@@ -66,6 +80,17 @@ module Cosmo
66
80
  end
67
81
  end
68
82
 
83
+ # @param state [NATS::JetStream::API::StreamState]
84
+ # @return [String] e.g. "3m 10s ago", or "never" for a stream that has not received a message
85
+ def last_message(state)
86
+ time = Time.parse(state.last_ts.to_s)
87
+ time.year > 1 ? "#{elapsed(time)} ago" : "never"
88
+ rescue ArgumentError
89
+ "never"
90
+ end
91
+
92
+ # @param value [Numeric, String, nil] Unix seconds
93
+ # @return [String] the time left, e.g. "45s", "3m", "2h", "1d", "Ready" once passed, or "N/A" without a value
69
94
  def time_until(value)
70
95
  return "N/A" unless value
71
96
 
@@ -78,6 +103,7 @@ module Cosmo
78
103
  "#{(diff / 86_400).to_i}d"
79
104
  end
80
105
 
106
+ # @param num [Integer, String, nil]
81
107
  # @return [String] e.g. "940", "18.9k", "1.28M"
82
108
  def compact_number(num)
83
109
  n = num.to_i
@@ -99,6 +125,9 @@ module Cosmo
99
125
 
100
126
  # A stable chart color per name, one of the palette's five hues: job streams take them in name order, so the
101
127
  # first five never share one, anything else by a hash of its name.
128
+ #
129
+ # @param name [String, Symbol]
130
+ # @return [String] a CSS color, e.g. "var(--c2)"
102
131
  def hue(name)
103
132
  @hues ||= API::Stream.jobs.map(&:name).sort.each_with_index.to_h
104
133
  index = @hues.fetch(name.to_s) { Zlib.crc32(name.to_s) }
@@ -135,10 +164,14 @@ module Cosmo
135
164
  {}
136
165
  end
137
166
 
167
+ # @param value [Object]
168
+ # @return [String] the value, HTML-escaped
138
169
  def h(value)
139
170
  Rack::Utils.escape_html(value.to_s)
140
171
  end
141
172
 
173
+ # @param value [Object]
174
+ # @return [String] the value, URL-escaped
142
175
  def u(value)
143
176
  Rack::Utils.escape(value.to_s)
144
177
  end
@@ -146,6 +179,11 @@ module Cosmo
146
179
  # Build the list of page numbers to render around the current page, with
147
180
  # `:gap` markers where numbers are skipped.
148
181
  # pages(5, 20) # => [1, :gap, 3, 4, 5, 6, 7, :gap, 20]
182
+ #
183
+ # @param page [Integer] the current page
184
+ # @param total_pages [Integer]
185
+ # @param window [Integer] pages shown on each side of the current one
186
+ # @return [Array<Integer, Symbol>] empty for a single page
149
187
  def pages(page, total_pages, window: 2)
150
188
  return [] if total_pages <= 1
151
189
 
@@ -158,16 +196,22 @@ module Cosmo
158
196
  end
159
197
  end
160
198
 
199
+ # @param path [String] a mount-relative route
200
+ # @return [Boolean] whether the request is for exactly this route
161
201
  def current_page?(path)
162
202
  request_path = @request.path_info
163
203
  request_path = "/" if request_path.empty?
164
204
  request_path == path
165
205
  end
166
206
 
207
+ # @param values [Array<String>] mount-relative route prefixes
208
+ # @return [Boolean] whether the request path starts with any of them
167
209
  def path_prefix?(*values)
168
210
  values.any? { |v| @request.path_info.start_with?(v) }
169
211
  end
170
212
 
213
+ # @param path [String] a mount-relative route
214
+ # @return [Boolean] whether the request's referrer is this route
171
215
  def referrer?(path)
172
216
  referrer_uri = URI(@request.referrer)
173
217
  referrer_path = referrer_uri.path
@@ -22,10 +22,19 @@ module Cosmo
22
22
  end
23
23
  end
24
24
 
25
+ # @param url [String] an internal route
26
+ # @param status [Integer]
27
+ # @return [Array] the Rack response
25
28
  def redirect_to(url, status = 302)
26
29
  [status, { "location" => url_for(url) }, []]
27
30
  end
28
31
 
32
+ # Sends a file from ASSETS_ROOT, or 404 when it is missing.
33
+ #
34
+ # @param filename [String] relative to ASSETS_ROOT
35
+ # @param content_type [String]
36
+ # @param headers [Hash{String => String}, nil] extra response headers
37
+ # @return [Array] the Rack response
29
38
  def serve(filename, content_type, headers = nil)
30
39
  path = File.join(ASSETS_ROOT, filename)
31
40
  return not_found unless File.exist?(path)
@@ -33,15 +42,19 @@ module Cosmo
33
42
  respond_with 200, body: File.binread(path), headers: { "content-type" => content_type }.merge(headers || {})
34
43
  end
35
44
 
45
+ # @param body [String] the HTML to send
46
+ # @return [Array] the Rack response
36
47
  def ok(body = "")
37
48
  headers = { "content-type" => "text/html; charset=utf-8" }
38
49
  respond_with 200, body:, headers:
39
50
  end
40
51
 
52
+ # @return [Array] the Rack response
41
53
  def no_content
42
54
  respond_with 204, body: ""
43
55
  end
44
56
 
57
+ # @return [Array] the Rack response
45
58
  def not_found
46
59
  body = "<div class=\"empty\"><b>404</b>Not found</div>"
47
60
  headers = { "content-type" => "text/html; charset=utf-8" }
@@ -51,6 +64,10 @@ module Cosmo
51
64
  # Prepend the mount prefix to an internal route.
52
65
  # url_for("/jobs") # => "/admin/cosmo/jobs" (mounted)
53
66
  # url_for("/jobs") # => "/jobs" (standalone)
67
+ #
68
+ # @param path [String]
69
+ # @param params [Hash, nil] appended as the query string
70
+ # @return [String]
54
71
  def url_for(path, params = nil)
55
72
  url = "#{@request.script_name}#{path}"
56
73
  url = "#{url}?#{params.to_a.map { _1.join("=") }.join("&")}" if params
@@ -65,7 +65,7 @@ module Cosmo
65
65
  end
66
66
 
67
67
  # @return [Array<Hash>] per day the hover column +left+/+width+ and the dot +top+, in percent, with the numbers
68
- def points # rubocop:disable Metrics/AbcSize
68
+ def points
69
69
  slot = 100.0 / [@series.size, 1].max
70
70
  @series.each_with_index.map do |day, index|
71
71
  center = @series.size == 1 ? 50.0 : x(index) / WIDTH * 100
@@ -1,37 +1,39 @@
1
- <% if @schedules.empty? -%>
1
+ <% if @groups.empty? -%>
2
2
  <div class="empty">
3
3
  <b>No cron schedules deployed in NATS</b>
4
4
  <span>Add a <code>cron:</code> section to <code>cosmo.yml</code> and run <code>cosmo --setup</code> to publish them.</span>
5
5
  </div>
6
6
  <% else -%>
7
7
  <div class="scroll"><div style="min-width:900px">
8
- <div class="thead t-crons"><span>Schedule</span><span>Cron</span><span>Stream</span><span>Args</span><span></span></div>
9
- <% @schedules.each do |s| -%>
10
- <% subject = s[:schedule_subject].to_s -%>
11
- <div class="trow hover t-crons">
12
- <div class="stack">
13
- <span class="strong ellip"><%= h(s[:name] || s[:class]) %></span>
14
- <% if s[:name] -%><span class="small-mono ellip"><%= h(s[:class]) %></span><% end -%>
15
- <span class="small-mono ellip" title="<%= h(subject) %>"><%= h(subject) %></span>
8
+ <div class="thead t-crons"><span>Schedule</span><span>Cron</span><span>Args</span><span></span></div>
9
+ <% @groups.each do |stream, schedules| -%>
10
+ <div class="trow group t-crons"><span class="full qname"><span class="dot" style="background:<%= hue(stream) %>"></span><%= h(stream) %> (<%= schedules.size %>)</span></div>
11
+ <% schedules.each do |s| -%>
12
+ <% subject = s[:schedule_subject].to_s -%>
13
+ <div class="trow hover t-crons">
14
+ <div class="stack">
15
+ <span class="strong ellip"><%= h(s[:name] || s[:class]) %></span>
16
+ <% if s[:name] -%><span class="small-mono ellip"><%= h(s[:class]) %></span><% end -%>
17
+ <span class="small-mono ellip" title="<%= h(subject) %>"><%= h(subject) %></span>
18
+ </div>
19
+ <div class="stack">
20
+ <span class="mono" style="font-size:12.5px"><%= h(s[:schedule]) %></span>
21
+ <span class="mute" style="font-size:11.5px"><%= h(s[:timezone] || 'UTC') %></span>
22
+ </div>
23
+ <% if s[:args]&.any? -%>
24
+ <span class="mono mute ellip" style="font-size:11.5px" title="<%= h(s[:args].inspect) %>"><%= h(s[:args].inspect) %></span>
25
+ <% else -%>
26
+ <span class="faint">—</span>
27
+ <% end -%>
28
+ <div class="btns">
29
+ <button type="button" class="btn" title="Dispatch this job now, bypassing the schedule timer"
30
+ hx-post="<%= url_for('/crons/run', { subject: u(subject) }) %>" hx-swap="none" hx-indicator="#global-spinner">► Run now</button>
31
+ <button type="button" class="btn danger" title="Purge schedule from NATS"
32
+ hx-delete="<%= url_for('/crons/delete', { subject: u(subject) }) %>" hx-target="#crons-table" hx-swap="innerHTML"
33
+ hx-indicator="#global-spinner" hx-confirm="Delete '<%= h(subject) %>'? NATS will stop firing it.">Delete</button>
34
+ </div>
16
35
  </div>
17
- <div class="stack">
18
- <span class="mono" style="font-size:12.5px"><%= h(s[:schedule]) %></span>
19
- <span class="mute" style="font-size:11.5px"><%= h(s[:timezone] || 'UTC') %></span>
20
- </div>
21
- <span class="qname"><span class="dot" style="background:<%= hue(s[:stream]) %>"></span><span class="mono ellip"><%= h(s[:stream]) %></span></span>
22
- <% if s[:args]&.any? -%>
23
- <span class="mono mute ellip" style="font-size:11.5px" title="<%= h(s[:args].inspect) %>"><%= h(s[:args].inspect) %></span>
24
- <% else -%>
25
- <span class="faint">—</span>
26
- <% end -%>
27
- <div class="btns">
28
- <button type="button" class="btn" title="Dispatch this job now, bypassing the schedule timer"
29
- hx-post="<%= url_for('/crons/run', { subject: u(subject) }) %>" hx-swap="none" hx-indicator="#global-spinner">► Run now</button>
30
- <button type="button" class="btn danger" title="Purge schedule from NATS"
31
- hx-delete="<%= url_for('/crons/delete', { subject: u(subject) }) %>" hx-target="#crons-table" hx-swap="innerHTML"
32
- hx-indicator="#global-spinner" hx-confirm="Delete '<%= h(subject) %>'? NATS will stop firing it.">Delete</button>
33
- </div>
34
- </div>
36
+ <% end -%>
35
37
  <% end -%>
36
38
  </div></div>
37
39
  <% end -%>
@@ -1,5 +1,5 @@
1
- <% json = (JSON.generate(@args) rescue @args.to_s) -%>
1
+ <% json = @args.is_a?(String) ? @args : (JSON.generate(@args) rescue @args.to_s) -%>
2
2
  <details class="stack">
3
3
  <summary class="mono mute ellip" style="font-size:11.5px" title="<%= h(json) %>"><%= h(json) %></summary>
4
- <pre class="code-block"><%= h((JSON.pretty_generate(@args) rescue @args.to_s)) %></pre>
4
+ <pre class="code-block"><%= h(@args.is_a?(String) ? @args : (JSON.pretty_generate(@args) rescue @args.to_s)) %></pre>
5
5
  </details>
@@ -5,14 +5,14 @@
5
5
  <div class="thead t-jobs"><span>JID</span><span>Class</span><span>Args</span><span>Queue</span><span>Status</span><span class="r">Try</span><span class="r">Running for</span></div>
6
6
  <% @jobs.each do |job| -%>
7
7
  <% delivery = job[:delivery].to_i -%>
8
- <% retries = job.dig(:data, :retry) -%>
8
+ <% retries = job[:retry] -%>
9
9
  <div class="trow hover t-jobs">
10
- <span class="mono mute ellip" style="font-size:11.5px" title="<%= h(job.dig(:data, :jid)) %>"><%= h(job.dig(:data, :jid)) %></span>
10
+ <span class="mono mute ellip" style="font-size:11.5px" title="<%= h(job[:jid]) %>"><%= h(job[:jid]) %></span>
11
11
  <div class="stack">
12
- <span class="strong ellip"><%= h(job.dig(:data, :class)) %></span>
12
+ <span class="strong ellip"><%= h(job[:class]) %></span>
13
13
  <span class="small-mono ellip" title="<%= h(job[:worker]) %>"><%= h(job[:worker]) %></span>
14
14
  </div>
15
- <%= render('jobs/_args', { args: job.dig(:data, :args) }) %>
15
+ <%= render('jobs/_args', { args: job[:args] }) %>
16
16
  <span class="mono ellip" style="font-size:12px"><%= h(job[:stream]) %></span>
17
17
  <span class="pill" style="--tone:var(--acc)">running</span>
18
18
  <span class="mono r" style="font-size:12px;color:<%= delivery >= 5 ? 'var(--err)' : delivery > 1 ? 'var(--warn)' : 'var(--mute)' %>">
@@ -1,9 +1,9 @@
1
1
  <div class="subbar">
2
2
  <div class="seg">
3
- <% @stream_names.each do |name| -%>
3
+ <% @stream_sizes.each do |name, size| -%>
4
4
  <a class="<%= 'on' if @stream_name.to_s == name.to_s %>" href="<%= url_for('/jobs/enqueued', stream_name: u(name)) %>"
5
5
  hx-get="<%= url_for('/jobs/enqueued', stream_name: u(name)) %>" hx-target="#jobs-panel" hx-swap="outerHTML"
6
- hx-push-url="true" hx-indicator="#global-spinner"><%= h(name) %></a>
6
+ hx-push-url="true" hx-indicator="#global-spinner"><%= h(name) %><span class="<%= 'has' if size.positive? %>"><%= compact_number(size) %></span></a>
7
7
  <% end -%>
8
8
  </div>
9
9
  <span class="spacer"></span>
@@ -12,6 +12,11 @@
12
12
  <% end -%>
13
13
  <div class="tools">
14
14
  <% if @tab == 'busy' -%>
15
+ <% longest = @query[:order] == 'longest' -%>
16
+ <button type="button" class="btn<%= ' quiet' unless longest %>"
17
+ hx-get="<%= url_for(tab_path, @query.merge(page: 1).tap { |q| longest ? q.delete(:order) : q[:order] = 'longest' }) %>"
18
+ hx-target="#jobs-panel" hx-swap="outerHTML" hx-indicator="#global-spinner"
19
+ title="Reads every running job, so it is slower with many"><%= longest ? '✓ Longest first' : 'Order by longest' %></button>
15
20
  <% if @polling -%>
16
21
  <button type="button" class="btn quiet" hx-get="<%= url_for(tab_path, @query.merge(page: @page, poll: 0)) %>"
17
22
  hx-target="#jobs-panel" hx-swap="outerHTML" hx-indicator="#global-spinner">⏸ Stop polling</button>
@@ -23,7 +28,7 @@
23
28
  </div>
24
29
  </div>
25
30
 
26
- <%= render("jobs/_#{@tab}", { jobs: @jobs, total: @total, page: @page, limit: @limit, stream_name: @stream_name, stream_names: @stream_names }) %>
31
+ <%= render("jobs/_#{@tab}", { jobs: @jobs, total: @total, page: @page, limit: @limit, stream_name: @stream_name, stream_sizes: @stream_sizes }) %>
27
32
 
28
33
  <%= render('shared/_pager', { path: tab_path, query: @query, page: @page, total_pages: @total_pages, target: '#jobs-panel' }) %>
29
34
  </section>
@@ -39,7 +39,7 @@
39
39
  <div class="conn-title"><span class="conn-dot<%= ' down' if server.empty? %>"></span><%= server.empty? ? 'NATS unreachable' : 'NATS connected' %></div>
40
40
  <div class="conn-meta">
41
41
  <%= h(server[:url] || "unknown server") %><br>
42
- <% if server[:name] -%><%= h(server[:name]) %> · <% end -%><% if server[:version] -%>v<%= h(server[:version]) %> · <% end -%>R<%= Cosmo::Config.replicas %>
42
+ <%= h([server[:name], ("v#{server[:version]}" if server[:version])].compact.join(' · ')) %>
43
43
  </div>
44
44
  </div>
45
45
  <div class="theme-toggle" role="group" aria-label="Theme">
@@ -1,3 +1,3 @@
1
1
  <% @badges.each do |key, count| -%>
2
- <span class="badge" id="badge-<%= key %>" hx-swap-oob="true"><%= compact_number(count) %></span>
2
+ <span class="badge" id="badge-<%= key %>" hx-swap-oob="true"><%= count.is_a?(String) ? h(count) : compact_number(count) %></span>
3
3
  <% end -%>
@@ -1,5 +1,5 @@
1
1
  <div class="card-head bar ruled">
2
- <div class="card-title">Running now</div>
2
+ <div class="card-title">Running sample</div>
3
3
  <a class="small-mono" href="<%= url_for('/jobs/busy') %>"><%= @total %> busy &rarr;</a>
4
4
  </div>
5
5
  <% if @jobs.empty? -%>
@@ -7,10 +7,10 @@
7
7
  <% else -%>
8
8
  <div class="feed">
9
9
  <% @jobs.each do |job| -%>
10
- <div class="feed-row" title="<%= h(job.dig(:data, :jid)) %> · <%= h(job[:worker]) %>">
10
+ <div class="feed-row" title="<%= h(job[:jid]) %> · <%= h(job[:worker]) %>">
11
11
  <span class="mono faint"><%= Time.at(job[:started_at].to_i).strftime('%H:%M:%S') %></span>
12
12
  <span class="dot" style="background:<%= hue(job[:stream]) %>"></span>
13
- <span class="ellip"><%= h(job.dig(:data, :class)) %></span>
13
+ <span class="ellip"><%= h(job[:class]) %></span>
14
14
  <span class="mono mute"><%= h(job[:stream]) %> · <%= elapsed(job[:started_at]) %></span>
15
15
  </div>
16
16
  <% end -%>
@@ -22,7 +22,7 @@
22
22
  <div class="loading">Loading queues&hellip;</div>
23
23
  </section>
24
24
  <section class="card grow1" hx-get="<%= url_for('/_running') %>" hx-trigger="load, cosmo:poll" data-poll="<%= poll_interval %>" hx-swap="innerHTML">
25
- <div class="card-head bar ruled"><div class="card-title">Running now</div></div>
25
+ <div class="card-head bar ruled"><div class="card-title">Running sample</div></div>
26
26
  <div class="loading">Loading jobs&hellip;</div>
27
27
  </section>
28
28
  </div>
@@ -7,6 +7,7 @@
7
7
  <div class="kpi" style="--hue:var(--c2)"><div class="kpi-label"><i></i>Size</div><div class="kpi-val"><b><%= format_bytes(@state.bytes) %></b></div></div>
8
8
  <div class="kpi" style="--hue:var(--c3)"><div class="kpi-label"><i></i>Consumers</div><div class="kpi-val"><b><%= @state.consumer_count %></b></div></div>
9
9
  <div class="kpi" style="--hue:var(--c4)"><div class="kpi-label"><i></i>Sequence range</div><div class="kpi-val"><b style="font-size:22px"><%= format_numbers(@state.first_seq) %> → <%= format_numbers(@state.last_seq) %></b></div></div>
10
+ <div class="kpi" style="--hue:var(--c5)"><div class="kpi-label"><i></i>Last message</div><div class="kpi-val"><b title="<%= h(@state.last_ts) %>"><%= last_message(@state) %></b></div></div>
10
11
  </div>
11
12
 
12
13
  <%= render('streams/_pause_banner', { name: @name, paused: @paused }) %>
@@ -12,6 +12,7 @@
12
12
  <div class="stat"><span class="eyebrow">Messages</span><b><%= compact_number(state.messages) %></b></div>
13
13
  <div class="stat"><span class="eyebrow">Size</span><b><%= format_bytes(state.bytes) %></b></div>
14
14
  <div class="stat"><span class="eyebrow">Retention</span><b><%= h(config.retention) %><%= " · #{format_span(max_age / 1_000_000_000)}" if max_age.positive? %></b></div>
15
+ <div class="stat"><span class="eyebrow">Last message</span><b title="<%= h(state.last_ts) %>"><%= last_message(state) %></b></div>
15
16
  <div class="stat"><span class="eyebrow">Replicas</span><b>R<%= h(config.num_replicas || 1) %></b></div>
16
17
  <div class="stat"><span class="eyebrow">Sequence</span><b class="ellip"><%= compact_number(state.first_seq) %> → <%= compact_number(state.last_seq) %></b></div>
17
18
  </div>
data/lib/cosmo/web.rb CHANGED
@@ -23,6 +23,10 @@ module Cosmo
23
23
 
24
24
  FONT_HEADERS = { "cache-control" => "public, max-age=31536000, immutable" }.freeze
25
25
 
26
+ # Serves a request with a fresh instance, so the class itself is a Rack app.
27
+ #
28
+ # @param env [Hash] the Rack environment
29
+ # @return [Array] the Rack response
26
30
  def self.call(env)
27
31
  new.call(env)
28
32
  end
@@ -30,7 +34,10 @@ module Cosmo
30
34
  # Serves the UI. An htmx refresh is shared through Cache with concurrent and recent identical ones; any other
31
35
  # method than GET changes something, so it empties the cache. While Circuit is open, anything but an asset
32
36
  # answers 503 at once instead of waiting on NATS.
33
- def call(env) # rubocop:disable Metrics/AbcSize
37
+ #
38
+ # @param env [Hash] the Rack environment
39
+ # @return [Array] the Rack response
40
+ def call(env)
34
41
  @request = Rack::Request.new(env)
35
42
  return route if @request.path_info.start_with?("/assets/", "/favicon.ico")
36
43
  return guarded.tap { Cache.instance.clear } unless @request.get?
data/lib/cosmo.rb CHANGED
@@ -78,6 +78,39 @@ module Cosmo
78
78
  end
79
79
  end
80
80
 
81
+ class BulkEnqueueError < Error
82
+ # @return [Array<String, nil>] each job's jid, nil for one that was not enqueued
83
+ attr_reader :jids
84
+ # @return [Hash{Integer => StandardError}] why each failed job was not enqueued, by its index
85
+ attr_reader :errors
86
+
87
+ # @param jids [Array<String, nil>]
88
+ # @param errors [Hash{Integer => StandardError}]
89
+ def initialize(jids, errors)
90
+ @jids = jids
91
+ @errors = errors
92
+ super("#{errors.size} of #{jids.size} jobs were not enqueued: #{errors.values.first.message}")
93
+ end
94
+ end
95
+
96
+ class JobClassNotFoundError < Error
97
+ def initialize(class_name)
98
+ super("#{class_name} class not found")
99
+ end
100
+ end
101
+
102
+ class JobInterruptedError < Error
103
+ def initialize
104
+ super("Job was still running when the worker shutdown timed out")
105
+ end
106
+ end
107
+
108
+ class JobTimeoutError < Error
109
+ def initialize(seconds)
110
+ super("Job exceeded its #{seconds}s duration limit")
111
+ end
112
+ end
113
+
81
114
  class UnknownJobStreamError < Error
82
115
  def initialize(names, configured)
83
116
  super("Unknown job stream#{"s" if names.size > 1} #{names.map { "`#{_1}`" }.join(", ")}; " \
@@ -1,12 +1,12 @@
1
1
  module Cosmo
2
2
  module ActiveJobAdapter
3
3
  class Adapter
4
- def enqueue: (untyped job) -> String
5
- def enqueue_at: (untyped job, Numeric timestamp) -> String
4
+ def enqueue: (untyped job) -> String?
5
+ def enqueue_at: (untyped job, Numeric timestamp) -> String?
6
6
 
7
7
  private
8
8
 
9
- def publish: (untyped job, Numeric? timestamp) -> String
9
+ def publish: (untyped job, Numeric? timestamp) -> String?
10
10
  def job_cosmo_options: (untyped job) -> Hash[Symbol, untyped]
11
11
  end
12
12
  end
@@ -1,11 +1,13 @@
1
1
  module Cosmo
2
2
  module ActiveJobAdapter
3
- VALID_OPTIONS: ::Array[::Symbol]
4
-
5
3
  module Options
4
+ VALID_OPTIONS: ::Array[::Symbol]
5
+
6
6
  def self.included: (::Module base) -> void
7
7
 
8
8
  module ClassMethods
9
+ @cosmo_options: ::Hash[::Symbol, untyped]?
10
+
9
11
  def cosmo_options: (**untyped opts) -> ::Hash[::Symbol, untyped]
10
12
  def get_cosmo_options: () -> ::Hash[::Symbol, untyped]
11
13
  end
@@ -3,11 +3,13 @@ module Cosmo
3
3
  class Batch
4
4
  LIMIT: ::Integer
5
5
 
6
- def self.all: (?limit: ::Integer) -> Array[Batch]
6
+ def self.all: (?limit: ::Integer?) -> Array[Batch]
7
+
8
+ def self.count: (limit: ::Integer) -> ::Integer
7
9
 
8
10
  def self.kv: () -> KV
9
11
 
10
- def self.counter: () -> Counter
12
+ def self.counter: () -> Cosmo::Batch::Counters
11
13
 
12
14
  @bid: ::String
13
15
 
@@ -37,7 +39,7 @@ module Cosmo
37
39
 
38
40
  def kv: () -> KV
39
41
 
40
- def counter: () -> Counter
42
+ def counter: () -> Cosmo::Batch::Counters
41
43
  end
42
44
  end
43
45
  end
@@ -2,6 +2,7 @@ module Cosmo
2
2
  module API
3
3
  class Counter
4
4
  @namespace: ::String
5
+ @client: Client
5
6
 
6
7
  def self.setup!: () -> void
7
8
 
@@ -9,21 +10,30 @@ module Cosmo
9
10
 
10
11
  def initialize: (::String namespace) -> void
11
12
 
12
- def increment: (Symbol key, ?by: ::Integer) -> ::Integer
13
+ def increment!: (Symbol | ::String key, ?by: ::Integer, ?msg_id: ::String?) -> ::Integer?
14
+ alias incr! increment!
15
+
16
+ def increment: (Symbol | ::String key, ?by: ::Integer, ?msg_id: ::String?) -> ::Integer?
13
17
  alias incr increment
14
18
 
15
- def decrement: (Symbol key, ?by: ::Integer) -> ::Integer
19
+ def decrement!: (Symbol | ::String key, ?by: ::Integer, ?msg_id: ::String?) -> ::Integer?
20
+ alias decr! decrement!
21
+
22
+ def decrement: (Symbol | ::String key, ?by: ::Integer, ?msg_id: ::String?) -> ::Integer?
16
23
  alias decr decrement
17
24
 
18
- def reset: (Symbol key) -> untyped
25
+ def reset: (Symbol | ::String key) -> ::Integer?
26
+ alias purge reset
19
27
 
20
- def get: (Symbol key) -> ::Integer
28
+ def get: (Symbol | ::String key) -> ::Integer
21
29
 
22
30
  private
23
31
 
24
- def publish: (Symbol key, ::String value) -> ::Integer
32
+ def safely: [T] (Symbol | ::String key) { () -> T } -> T?
33
+
34
+ def publish: (Symbol | ::String key, ::String value, ?msg_id: ::String?) -> ::Integer?
25
35
 
26
- def subject: (Symbol key) -> ::String
36
+ def subject: (Symbol | ::String key) -> ::String
27
37
 
28
38
  def client: () -> Client
29
39
  end