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
@@ -7,12 +7,20 @@ module Cosmo
7
7
  module Json
8
8
  module_function
9
9
 
10
+ # @param value [String]
11
+ # @param default [Object] returned when +value+ is not valid JSON
12
+ # @param symbolize_names [Boolean]
13
+ # @param options [Hash] passed on to +JSON.parse+
14
+ # @return [Object]
10
15
  def parse(value, default: nil, symbolize_names: true, **options)
11
16
  JSON.parse(value, **options, symbolize_names:)
12
17
  rescue TypeError, JSON::ParserError
13
18
  default
14
19
  end
15
20
 
21
+ # @param value [Object]
22
+ # @param default [Object] returned when +value+ cannot be generated
23
+ # @return [String, Object]
16
24
  def dump(value, default: nil)
17
25
  ::JSON.generate(value)
18
26
  rescue TypeError, JSON::NestingError
@@ -3,19 +3,28 @@
3
3
  module Cosmo
4
4
  module Utils
5
5
  class Signal
6
+ # Traps the given signals, see {#initialize}.
7
+ #
8
+ # @return [Signal]
6
9
  def self.trap(...)
7
10
  new(...)
8
11
  end
9
12
 
13
+ # @param signals [Array<Symbol, String>] signals to trap
10
14
  def initialize(*signals)
11
15
  @queue = Queue.new
12
16
  signals.each { |s| ::Signal.trap(s) { push(s) } }
13
17
  end
14
18
 
19
+ # Blocks until a trapped signal arrives.
20
+ #
21
+ # @return [Symbol, String] the signal
15
22
  def wait
16
23
  @queue.pop # Wait indefinitely for a signal
17
24
  end
18
25
 
26
+ # @param signal [Symbol, String]
27
+ # @return [void]
19
28
  def push(signal)
20
29
  @queue.push(signal)
21
30
  end
@@ -17,6 +17,7 @@ module Cosmo
17
17
  (elapsed_millis / 1_000).round(2)
18
18
  end
19
19
 
20
+ # @return [void]
20
21
  def reset
21
22
  @started_at = clock_time
22
23
  end
@@ -5,6 +5,8 @@ module Cosmo
5
5
  module String
6
6
  module_function
7
7
 
8
+ # @param value [String, Symbol, Class] e.g. "Cosmo::SendEmailJob"
9
+ # @return [String] e.g. "cosmo-send_email_job"
8
10
  def underscore(value)
9
11
  value
10
12
  .to_s
@@ -14,6 +16,8 @@ module Cosmo
14
16
  .downcase
15
17
  end
16
18
 
19
+ # @param value [String] a constant name
20
+ # @return [Object, nil] the constant, or nil when it is not defined
17
21
  def safe_constantize(value)
18
22
  Object.const_get(value)
19
23
  rescue NameError
@@ -14,6 +14,7 @@ module Cosmo
14
14
 
15
15
  delegate %i[shutdown wait_for_termination] => :@pool
16
16
 
17
+ # @param concurrency [Integer] number of threads
17
18
  def initialize(concurrency)
18
19
  @mutex = Thread::Mutex.new
19
20
  @concurrency = concurrency
@@ -22,10 +23,17 @@ module Cosmo
22
23
  @pool = Concurrent::FixedThreadPool.new(concurrency)
23
24
  end
24
25
 
26
+ # Blocks until no task is running.
27
+ #
28
+ # @return [void]
25
29
  def wait_idle
26
30
  @mutex.synchronize { @cond.wait(@mutex) until @available == @concurrency }
27
31
  end
28
32
 
33
+ # Runs the block on a pool thread, blocking until one is free.
34
+ #
35
+ # @yield the task
36
+ # @return [void]
29
37
  def post
30
38
  @mutex.synchronize do
31
39
  @cond.wait(@mutex) while @available <= 0
@@ -7,17 +7,27 @@ module Cosmo
7
7
  @store = {}
8
8
  end
9
9
 
10
+ # @param key [Object]
11
+ # @param value [Object]
12
+ # @param ttl [Numeric, nil] seconds until the value expires, nil keeps it forever
13
+ # @return [Object] +value+
10
14
  def set(key, value, ttl: nil)
11
15
  @store[key] = [value, ttl ? Time.now + ttl : nil]
12
16
  value
13
17
  end
14
18
 
19
+ # @param key [Object]
20
+ # @return [Object, nil] the value, or nil when it is missing or expired
15
21
  def get(key)
16
22
  return unless key?(key)
17
23
 
18
24
  @store[key].first
19
25
  end
20
26
 
27
+ # @param key [Object]
28
+ # @param ttl [Numeric, nil] seconds until a computed value expires
29
+ # @yieldreturn [Object] the value, computed only when no fresh copy is cached
30
+ # @return [Object]
21
31
  def fetch(key, ttl: nil)
22
32
  return get(key) if key?(key)
23
33
 
data/lib/cosmo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cosmo
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
5
5
  end
@@ -76,6 +76,8 @@ pre{margin:0;white-space:pre-wrap;word-break:break-all}
76
76
  .seg a,.seg button{border:0;padding:5px 11px;border-radius:6px;font-family:var(--mono);font-size:11px;background:transparent;color:var(--mute)}
77
77
  .seg a:hover{text-decoration:none;color:var(--ink)}
78
78
  .seg .on{background:var(--panel2);color:var(--ink)}
79
+ .seg a span{margin-left:7px;color:var(--faint)}
80
+ .seg a span.has{color:var(--c2)}
79
81
  .live{display:flex;align-items:center;gap:7px;font-family:var(--mono);font-size:11px;color:var(--mute);white-space:nowrap}
80
82
  .live::before{content:"";width:6px;height:6px;border-radius:50%;background:var(--ok)}
81
83
  body.offline .live{color:var(--err)}
@@ -170,7 +172,7 @@ a.kpi:hover{text-decoration:none;background:var(--panel2)}
170
172
  .t-jobs.actions{grid-template-columns:110px 1.4fr 1.5fr .8fr 100px 110px 90px}
171
173
  .t-jobs.plain{grid-template-columns:110px 1.4fr 1.5fr .8fr 100px 150px}
172
174
  .t-cons{grid-template-columns:1.5fr .8fr .8fr .7fr .7fr 1.4fr}
173
- .t-crons{grid-template-columns:1.5fr 1.2fr .8fr 1.2fr 190px;gap:14px}
175
+ .t-crons{grid-template-columns:1.5fr 1.2fr 1.2fr 190px;gap:14px}
174
176
  .trow.t-crons{padding-top:13px;padding-bottom:13px}
175
177
  .t-procs{grid-template-columns:1.4fr 1.5fr 1.3fr 50px 80px 64px 56px 76px 64px 72px}
176
178
  .t-metrics{grid-template-columns:2fr .8fr .8fr 1fr 1fr}
@@ -181,8 +183,8 @@ a.kpi:hover{text-decoration:none;background:var(--panel2)}
181
183
  .qname .mono{font-size:13px}
182
184
  .dot{width:6px;height:6px;border-radius:50%;display:inline-block;flex:none}
183
185
  .paused-tag{font-size:10.5px;color:var(--warn)}
184
- .btn{display:inline-flex;align-items:center;gap:6px;padding:5px 10px;border:1px solid var(--line2);border-radius:6px;background:var(--panel);font-size:12px;color:var(--ink);white-space:nowrap}
185
- .btn:hover{border-color:var(--mute);text-decoration:none}
186
+ .btn{display:inline-flex;align-items:center;gap:6px;padding:5px 10px;border:1px solid var(--line2);border-radius:6px;background:var(--panel);font-size:12px;color:var(--ink);white-space:nowrap;cursor:pointer;user-select:none}
187
+ .btn:hover{border-color:var(--mute);background:var(--panel2);text-decoration:none}
186
188
  .btn.quiet{color:var(--mute)}
187
189
  .btn.danger{color:var(--err)}
188
190
  .btn.danger:hover{border-color:var(--err)}
@@ -9,10 +9,12 @@ module Cosmo
9
9
  TTL = 2
10
10
  LIMIT = 256
11
11
 
12
+ # @return [Cache]
12
13
  def self.instance
13
14
  @instance ||= new
14
15
  end
15
16
 
17
+ # @param ttl [Numeric] seconds a response is shared
16
18
  def initialize(ttl: ENV.fetch("COSMO_WEB_CACHE_TTL", TTL).to_f)
17
19
  @ttl = ttl
18
20
  @lock = Mutex.new
@@ -26,7 +26,7 @@ module Cosmo
26
26
  end
27
27
 
28
28
  # @return [String] the path of the run bars, one per day with runs
29
- def bars # rubocop:disable Metrics/AbcSize
29
+ def bars
30
30
  width = (slot * 0.6).clamp(2, BAR).round(1)
31
31
  @series.each_with_index.filter_map do |day, index|
32
32
  next if day[:count].zero?
@@ -73,7 +73,7 @@ module Cosmo
73
73
 
74
74
  # @return [Array<Hash>] per day the hover column +left+/+width+, its +center+, the execution time dot +dot+
75
75
  # (nil without one) and the bar top +top+, in percent, with the day's numbers
76
- def points # rubocop:disable Metrics/AbcSize
76
+ def points
77
77
  @series.each_with_index.map do |day, index|
78
78
  bar_top = HEIGHT - scale(day[:count], @count_top)
79
79
  day.merge(left: percent(slot * index), width: percent(slot), center: percent(center(index)),
@@ -10,6 +10,7 @@ module Cosmo
10
10
  ERRORS = [NATS::IO::Timeout, NATS::IO::ConnectError, NATS::IO::ConnectionClosedError,
11
11
  NATS::IO::StaleConnectionError].freeze
12
12
 
13
+ # @return [Circuit]
13
14
  def self.instance
14
15
  @instance ||= new
15
16
  end
@@ -7,15 +7,21 @@ module Cosmo
7
7
  class Context
8
8
  include Helpers::Application
9
9
 
10
+ # @param locals [Hash{Symbol => Object}] set as instance variables the view reads
11
+ # @param content_for [Hash{Symbol => String}, nil] the blocks the layout yields
10
12
  def initialize(locals, content_for = nil)
11
13
  @content_for = Hash(content_for)
12
14
  locals.each { |k, v| instance_variable_set("@#{k}", v) }
13
15
  end
14
16
 
17
+ # @param name [Symbol]
18
+ # @return [String, nil] the content captured under +name+
15
19
  def content_for(name)
16
20
  @content_for[name]
17
21
  end
18
22
 
23
+ # @param name [Symbol]
24
+ # @return [Boolean] whether content was captured under +name+
19
25
  def content_for?(name)
20
26
  @content_for.key?(name)
21
27
  end
@@ -6,15 +6,27 @@ module Cosmo
6
6
  class Application
7
7
  include Renderer
8
8
 
9
+ MAX_LIMIT = 100
10
+
11
+ # @param request [Rack::Request]
9
12
  def initialize(request)
10
13
  @request = request
11
14
  end
12
15
 
16
+ # Captures content the layout renders, such as the page title.
17
+ #
18
+ # @param name [Symbol]
19
+ # @param content [String]
20
+ # @return [String] the content
13
21
  def content_for(name, content)
14
22
  @content_for ||= {}
15
23
  @content_for[name] = content
16
24
  end
17
25
 
26
+ # @param template [String] the view, relative to Renderer::VIEWS_ROOT and without +.erb+
27
+ # @param locals [Hash{Symbol => Object}, nil] the view's instance variables
28
+ # @param layout [Boolean] whether to wrap the view in the page layout
29
+ # @return [String] the rendered HTML
18
30
  def render(template, locals = nil, layout: false)
19
31
  defaults = { request: @request }
20
32
  locals = Hash(locals).merge(defaults)
@@ -26,20 +38,31 @@ module Cosmo
26
38
  erb("layout", defaults, @content_for)
27
39
  end
28
40
 
41
+ # @return [Hash{String => Object}] the query and form parameters
29
42
  def params
30
43
  @request.params
31
44
  end
32
45
 
46
+ # @param default [Integer] the page size when the request names none
47
+ # @return [Integer] the requested page size, kept between 1 and MAX_LIMIT
48
+ def limit(default)
49
+ (params["limit"] || default).to_i.clamp(1, MAX_LIMIT)
50
+ end
51
+
52
+ # @return [String] the request path
33
53
  def path
34
54
  @request.path
35
55
  end
36
56
 
57
+ # @return [Boolean] whether htmx sent the request
37
58
  def hx_request?
38
59
  @request.get_header("HTTP_HX_REQUEST") == "true"
39
60
  end
40
61
 
41
62
  # 503 while NATS is unreachable: a bare body for an htmx request, which leaves the page as it is and backs
42
63
  # off, or a page that reloads itself.
64
+ #
65
+ # @return [Array] the Rack response
43
66
  def unavailable
44
67
  headers = { "content-type" => "text/html; charset=utf-8", "retry-after" => Circuit::OPEN_FOR.to_s }
45
68
  return respond_with(503, headers:, body: "NATS is unreachable") if hx_request?
@@ -50,6 +73,9 @@ module Cosmo
50
73
  end
51
74
 
52
75
  # An out-of-band swap of the page header's flash message, appended to an htmx response.
76
+ #
77
+ # @param message [String] escaped before rendering
78
+ # @return [String] the HTML
53
79
  def flash(message)
54
80
  %(<div id="flash" class="flash" hx-swap-oob="true">#{Rack::Utils.escape_html(message)}</div>)
55
81
  end
@@ -6,14 +6,20 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Batches < Application
9
+ # Renders the batches page.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  content_for :title, "Batches"
11
14
  content_for :subtitle, "Grouped jobs with a callback when the last one lands."
12
15
  ok render("batches/index", layout: true)
13
16
  end
14
17
 
18
+ # Renders the batches table.
19
+ #
20
+ # @return [Array] the Rack response
15
21
  def _table
16
- limit = (params["limit"] || API::Batch::LIMIT).to_i
22
+ limit = limit(API::Batch::LIMIT)
17
23
  ok render("batches/_table", { batches: API::Batch.all(limit:) })
18
24
  end
19
25
  end
@@ -6,6 +6,9 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Crons < Application
9
+ # Renders the crons page, or 404 while scheduling is disabled.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  return not_found unless Config.scheduled.enabled
11
14
 
@@ -14,14 +17,19 @@ module Cosmo
14
17
  ok render("crons/index", layout: true)
15
18
  end
16
19
 
20
+ # Renders the crons table.
21
+ #
22
+ # @return [Array] the Rack response
17
23
  def _table
18
24
  return not_found unless Config.scheduled.enabled
19
25
 
20
- ok render("crons/_table", { schedules: cron.all })
26
+ ok render("crons/_table", { groups: groups })
21
27
  end
22
28
 
23
29
  # Dispatch the job immediately, bypassing the schedule timer.
24
30
  # Expects params["subject"] = the schedule subject stored in NATS.
31
+ #
32
+ # @return [Array] the Rack response
25
33
  def run_now
26
34
  return not_found unless Config.scheduled.enabled
27
35
 
@@ -31,12 +39,14 @@ module Cosmo
31
39
  end
32
40
 
33
41
  # Purge the schedule from NATS so it stops firing.
42
+ #
43
+ # @return [Array] the Rack response
34
44
  def delete
35
45
  return not_found unless Config.scheduled.enabled
36
46
 
37
47
  subject = Rack::Utils.unescape(params["subject"].to_s)
38
48
  cron.delete!(subject)
39
- ok render("crons/_table", { schedules: cron.all }) + flash("#{label(subject)} deleted")
49
+ ok render("crons/_table", { groups: groups }) + flash("#{label(subject)} deleted")
40
50
  end
41
51
 
42
52
  private
@@ -45,6 +55,10 @@ module Cosmo
45
55
  @cron ||= API::Cron.instance
46
56
  end
47
57
 
58
+ def groups
59
+ cron.all.group_by { _1[:stream] }
60
+ end
61
+
48
62
  # "cosmo.cron.default.report_job.monthly" → "monthly", "cosmo.cron.default.report_job" → "report_job"
49
63
  def label(subject)
50
64
  parts = subject.split(".")
@@ -6,34 +6,55 @@ module Cosmo
6
6
  class Web
7
7
  module Controllers
8
8
  class Jobs < Application
9
+ # Renders the jobs page on the busy tab.
10
+ #
11
+ # @return [Array] the Rack response
9
12
  def index
10
13
  tab("busy")
11
14
  end
12
15
 
16
+ # Renders the busy tab.
17
+ #
18
+ # @return [Array] the Rack response
13
19
  def busy
14
20
  tab("busy")
15
21
  end
16
22
 
23
+ # Renders the enqueued tab.
24
+ #
25
+ # @return [Array] the Rack response
17
26
  def enqueued
18
27
  tab("enqueued")
19
28
  end
20
29
 
30
+ # Renders the scheduled tab, or 404 while scheduling is disabled.
31
+ #
32
+ # @return [Array] the Rack response
21
33
  def scheduled
22
34
  return not_found unless Config.scheduled.enabled
23
35
 
24
36
  tab("scheduled")
25
37
  end
26
38
 
39
+ # Renders the dead tab, or 404 while the dead stream is disabled.
40
+ #
41
+ # @return [Array] the Rack response
27
42
  def dead
28
43
  return not_found unless Config.dead.enabled
29
44
 
30
45
  tab("dead")
31
46
  end
32
47
 
48
+ # Renders the queues table.
49
+ #
50
+ # @return [Array] the Rack response
33
51
  def _queues
34
52
  ok render("jobs/_queues", { queues: API::Stats.queues, compact: params["compact"].to_s == "1" })
35
53
  end
36
54
 
55
+ # Sends a dead job back to its queue.
56
+ #
57
+ # @return [Array] the Rack response
37
58
  def retry
38
59
  return not_found unless Config.dead.enabled
39
60
 
@@ -42,6 +63,9 @@ module Cosmo
42
63
  ok flash("Dead job ##{seq} sent back to its queue")
43
64
  end
44
65
 
66
+ # Deletes a dead job.
67
+ #
68
+ # @return [Array] the Rack response
45
69
  def delete
46
70
  return not_found unless Config.dead.enabled
47
71
 
@@ -50,9 +74,12 @@ module Cosmo
50
74
  ok flash("Dead job ##{seq} deleted")
51
75
  end
52
76
 
77
+ # Removes an enqueued message from its stream and renders the enqueued tab.
78
+ #
79
+ # @return [Array] the Rack response
53
80
  def delete_enqueued
54
81
  seq = path.split("/").last.to_i
55
- stream_name, = streams
82
+ stream_name = params["stream_name"]
56
83
  API::Stream.new(stream_name).delete(seq)
57
84
  ok panel("enqueued") + flash("Message ##{seq} removed from #{stream_name}")
58
85
  end
@@ -79,23 +106,25 @@ module Cosmo
79
106
  end
80
107
 
81
108
  def busy_locals
82
- busy = API::Stats::Busy.instance
83
- total = busy.size
84
- page, limit, total_pages = paginate(total, API::Stats::Busy::LIMIT)
109
+ total = API::Stats.busy
110
+ page, limit, total_pages = paginate(total, API::Registry::LIMIT)
85
111
  polling = params["poll"].to_s != "0"
86
- { jobs: busy.list(page:, limit:), total:, page:, limit:, total_pages:, polling:, query: { limit:, poll: (0 unless polling) }.compact }
112
+ longest = params["order"] == "longest"
113
+ jobs = API::Stats.running_page(page:, limit:, longest:)
114
+ { jobs:, total:, page:, limit:, total_pages:, polling:,
115
+ query: { limit:, poll: (0 unless polling), order: ("longest" if longest) }.compact }
87
116
  end
88
117
 
89
118
  def enqueued_locals # rubocop:disable Metrics/AbcSize
90
- stream_name, stream_names = streams
91
- limit = (params["limit"] || API::Stream::LIMIT).to_i
119
+ stream_name, stream_sizes = streams
120
+ limit = limit(API::Stream::LIMIT)
92
121
  page = [params["page"].to_i, 1].max
93
- locals = { stream_name:, stream_names:, page:, limit:, total: 0, total_pages: 0, jobs: nil, polling: false,
122
+ locals = { stream_name:, stream_sizes:, page:, limit:, total: 0, total_pages: 0, jobs: nil, polling: false,
94
123
  query: { stream_name: Rack::Utils.escape(stream_name.to_s), limit: } }
95
124
  return locals if stream_name.to_s.empty?
96
125
 
97
126
  stream = API::Stream.new(stream_name)
98
- total = stream.total
127
+ total = stream_sizes.fetch(stream_name) { stream.total }
99
128
  total_pages = (total.to_f / limit).ceil
100
129
  page = page.clamp(1, [total_pages, 1].max)
101
130
  locals.merge(total:, total_pages:, page:, jobs: stream.messages(page:, limit:))
@@ -106,26 +135,26 @@ module Cosmo
106
135
  end
107
136
 
108
137
  def dead_locals
109
- stored_locals("dead").merge(polling: false)
138
+ stored_locals("dead", order: :desc).merge(polling: false)
110
139
  end
111
140
 
112
- def stored_locals(name)
141
+ def stored_locals(name, order: :asc)
113
142
  stream = API::Stream.new(name)
114
143
  total = stream.total
115
144
  page, limit, total_pages = paginate(total, API::Stream::LIMIT)
116
- { jobs: stream.messages(page:, limit:), total:, page:, limit:, total_pages:, query: { limit: } }
145
+ { jobs: stream.messages(page:, limit:, order:), total:, page:, limit:, total_pages:, query: { limit: } }
117
146
  end
118
147
 
119
148
  def paginate(total, default_limit)
120
- limit = (params["limit"] || default_limit).to_i
149
+ limit = limit(default_limit)
121
150
  total_pages = (total.to_f / limit).ceil
122
151
  [params["page"].to_i.clamp(1, [total_pages, 1].max), limit, total_pages]
123
152
  end
124
153
 
125
154
  def streams
126
- stream_names = API::Stream.jobs.map(&:name)
127
- stream_name = params.fetch("stream_name", stream_names.first)
128
- [stream_name, stream_names]
155
+ stream_sizes = API::Stream.jobs.to_h { [_1.name, _1.size] }
156
+ stream_name = params.fetch("stream_name") { stream_sizes.find { |_, size| size.positive? }&.first || stream_sizes.keys.first }
157
+ [stream_name, stream_sizes]
129
158
  end
130
159
  end
131
160
  end
@@ -9,6 +9,9 @@ module Cosmo
9
9
  class Metrics < Application
10
10
  RANGES = { 1 => "Today", 7 => "7 days", 30 => "30 days" }.freeze
11
11
 
12
+ # Renders the metrics page, or 404 while metrics are disabled.
13
+ #
14
+ # @return [Array] the Rack response
12
15
  def index
13
16
  return not_found unless Config.metrics.enabled
14
17
 
@@ -18,6 +21,8 @@ module Cosmo
18
21
  end
19
22
 
20
23
  # The chart and the table together, so the charted job stays highlighted across refreshes.
24
+ #
25
+ # @return [Array] the Rack response
21
26
  def _panel
22
27
  return not_found unless Config.metrics.enabled
23
28
 
@@ -8,20 +8,30 @@ module Cosmo
8
8
  module Controllers
9
9
  class Overview < Application
10
10
  RANGES = [7, 30].freeze
11
- CELLS = 256
11
+ CELLS = 64
12
12
  RUNNING = 9
13
- PROCESSES = 5
13
+ PROCESSES = 3
14
+ BATCHES = 100
14
15
 
16
+ # Renders the overview page.
17
+ #
18
+ # @return [Array] the Rack response
15
19
  def index
16
20
  content_for :title, "Overview"
17
21
  content_for :subtitle, "Everything beamed through NATS, live."
18
22
  ok render("overview/index", { metrics: Config.metrics.enabled }, layout: true)
19
23
  end
20
24
 
25
+ # Renders the overview's KPI cards.
26
+ #
27
+ # @return [Array] the Rack response
21
28
  def _kpis
22
29
  ok render("overview/_kpis", API::Stats.summary)
23
30
  end
24
31
 
32
+ # Renders the throughput chart for the requested range, or 404 while metrics are disabled.
33
+ #
34
+ # @return [Array] the Rack response
25
35
  def _throughput
26
36
  return not_found unless Config.metrics.enabled
27
37
 
@@ -30,35 +40,43 @@ module Cosmo
30
40
  ok render("overview/_throughput", { chart:, days:, ranges: RANGES })
31
41
  end
32
42
 
33
- def _workers # rubocop:disable Metrics/AbcSize
34
- processes = API::Stats::Processes.instance.list(limit: API::Stats::Processes.instance.size.clamp(1, nil))
35
- busy = API::Stats::Busy.instance.all.group_by { _1[:worker] }
36
- cells = processes.flat_map { worker_cells(_1, busy) }
37
- ok render("overview/_workers", { processes: processes.first(PROCESSES), total: processes.size,
38
- cells: cells.first(CELLS), hidden: [cells.size - CELLS, 0].max,
39
- busy: cells.count(&:itself), threads: cells.size })
43
+ # Renders the worker processes and a cell per thread, busy or idle.
44
+ #
45
+ # @return [Array] the Rack response
46
+ def _workers
47
+ registry = API::Stats::Processes.instance
48
+ threads = registry.threads
49
+ cells, busy = worker_cells(threads)
50
+ ok render("overview/_workers", { processes: registry.list(limit: PROCESSES), total: registry.size,
51
+ cells:, hidden: [threads - CELLS, 0].max, busy:, threads: })
40
52
  end
41
53
 
54
+ # Renders a sample of the running jobs.
55
+ #
56
+ # @return [Array] the Rack response
42
57
  def _running
43
- jobs = API::Stats::Busy.instance.all.sort_by { _1[:started_at].to_i }.first(RUNNING)
44
- jobs = jobs.map { _1.merge(data: Utils::Json.parse(_1[:data]) || {}) }
45
- ok render("overview/_running", { jobs:, total: API::Stats::Busy.instance.size })
58
+ ok render("overview/_running", { jobs: API::Stats.running(RUNNING), total: API::Stats.busy })
46
59
  end
47
60
 
61
+ # Renders the sidebar's count badges. Batches are only counted up to BATCHES; past that the badge
62
+ # reads "100+".
63
+ #
64
+ # @return [Array] the Rack response
48
65
  def _badges
49
66
  badges = { jobs: API::Stats.enqueued, streams: visible_streams.size }
50
67
  badges[:crons] = API::Cron.instance.all.size if Config.scheduled.enabled
51
- badges[:batches] = API::Batch.kv.keys("*.meta", limit: nil).size
68
+ batches = API::Batch.count(limit: BATCHES + 1)
69
+ badges[:batches] = batches > BATCHES ? "#{BATCHES}+" : batches
52
70
  ok render("overview/_badges", { badges: })
53
71
  end
54
72
 
55
73
  private
56
74
 
57
- # A process's threads, each the stream of the job it runs or nil while idle.
58
- def worker_cells(process, busy)
59
- streams = Array(busy[process[:identity]]).map { _1[:stream] }
60
- idle = [process[:concurrency].to_i - streams.size, 0].max
61
- streams + Array.new(idle)
75
+ def worker_cells(threads)
76
+ busy = API::Stats.busy_streams.select { _2.positive? }
77
+ cells = busy.flat_map { |stream, count| Array.new([count, CELLS].min, stream) }.first(CELLS)
78
+ cells += Array.new((threads - busy.values.sum).clamp(0, CELLS - cells.size))
79
+ [cells, busy.values.sum]
62
80
  end
63
81
 
64
82
  def visible_streams