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.
- checksums.yaml +4 -4
- data/README.md +54 -13
- data/lib/cosmo/active_job/adapter.rb +2 -0
- data/lib/cosmo/active_job/executor.rb +7 -0
- data/lib/cosmo/active_job/options.rb +6 -0
- data/lib/cosmo/api/batch.rb +26 -0
- data/lib/cosmo/api/counter.rb +51 -3
- data/lib/cosmo/api/cron/entry.rb +24 -9
- data/lib/cosmo/api/cron.rb +21 -7
- data/lib/cosmo/api/job.rb +14 -0
- data/lib/cosmo/api/kv.rb +138 -9
- data/lib/cosmo/api/registry.rb +47 -6
- data/lib/cosmo/api/stats/jobs.rb +84 -0
- data/lib/cosmo/api/stats/metrics.rb +18 -7
- data/lib/cosmo/api/stats/processes.rb +27 -11
- data/lib/cosmo/api/stats/totals.rb +1 -0
- data/lib/cosmo/api/stats.rb +47 -11
- data/lib/cosmo/api/stream.rb +66 -22
- data/lib/cosmo/batch/callback.rb +5 -0
- data/lib/cosmo/batch/dispatcher.rb +4 -4
- data/lib/cosmo/batch.rb +34 -11
- data/lib/cosmo/cli.rb +6 -0
- data/lib/cosmo/client.rb +208 -11
- data/lib/cosmo/config/cosmo.yml +1 -0
- data/lib/cosmo/config/settings.rb +4 -0
- data/lib/cosmo/config.rb +58 -10
- data/lib/cosmo/engine.rb +28 -1
- data/lib/cosmo/heartbeat.rb +12 -3
- data/lib/cosmo/http_server/handler.rb +5 -0
- data/lib/cosmo/http_server/health.rb +2 -0
- data/lib/cosmo/http_server/ping.rb +2 -0
- data/lib/cosmo/http_server.rb +8 -0
- data/lib/cosmo/job/data.rb +11 -0
- data/lib/cosmo/job/enqueuer.rb +100 -1
- data/lib/cosmo/job/limit.rb +5 -1
- data/lib/cosmo/job/processor.rb +51 -15
- data/lib/cosmo/job/setter.rb +53 -0
- data/lib/cosmo/job.rb +71 -2
- data/lib/cosmo/logger.rb +37 -0
- data/lib/cosmo/middleware/chain.rb +12 -0
- data/lib/cosmo/middleware/limit.rb +12 -3
- data/lib/cosmo/middleware/metrics.rb +6 -4
- data/lib/cosmo/middleware/sentry.rb +6 -4
- data/lib/cosmo/middleware/totals.rb +5 -11
- data/lib/cosmo/middleware.rb +0 -1
- data/lib/cosmo/processor.rb +60 -3
- data/lib/cosmo/publisher.rb +8 -0
- data/lib/cosmo/services.rb +7 -2
- data/lib/cosmo/stream/message.rb +5 -0
- data/lib/cosmo/stream/processor.rb +7 -0
- data/lib/cosmo/stream/serializer.rb +4 -0
- data/lib/cosmo/stream.rb +35 -1
- data/lib/cosmo/utils/duration.rb +3 -0
- data/lib/cosmo/utils/hash.rb +12 -0
- data/lib/cosmo/utils/json.rb +8 -0
- data/lib/cosmo/utils/signal.rb +9 -0
- data/lib/cosmo/utils/stopwatch.rb +1 -0
- data/lib/cosmo/utils/string.rb +4 -0
- data/lib/cosmo/utils/thread_pool.rb +8 -0
- data/lib/cosmo/utils/ttl_cache.rb +10 -0
- data/lib/cosmo/version.rb +1 -1
- data/lib/cosmo/web/assets/app.css +5 -3
- data/lib/cosmo/web/cache.rb +2 -0
- data/lib/cosmo/web/chart.rb +2 -2
- data/lib/cosmo/web/circuit.rb +1 -0
- data/lib/cosmo/web/context.rb +6 -0
- data/lib/cosmo/web/controllers/application.rb +26 -0
- data/lib/cosmo/web/controllers/batches.rb +7 -1
- data/lib/cosmo/web/controllers/crons.rb +16 -2
- data/lib/cosmo/web/controllers/jobs.rb +45 -16
- data/lib/cosmo/web/controllers/metrics.rb +5 -0
- data/lib/cosmo/web/controllers/overview.rb +36 -18
- data/lib/cosmo/web/controllers/processes.rb +8 -5
- data/lib/cosmo/web/controllers/streams.rb +12 -0
- data/lib/cosmo/web/helpers/application.rb +44 -0
- data/lib/cosmo/web/renderer.rb +17 -0
- data/lib/cosmo/web/throughput.rb +1 -1
- data/lib/cosmo/web/views/crons/_table.erb +29 -27
- data/lib/cosmo/web/views/jobs/_args.erb +2 -2
- data/lib/cosmo/web/views/jobs/_busy.erb +4 -4
- data/lib/cosmo/web/views/jobs/_enqueued.erb +2 -2
- data/lib/cosmo/web/views/jobs/_panel.erb +6 -1
- data/lib/cosmo/web/views/layout.erb +1 -1
- data/lib/cosmo/web/views/overview/_badges.erb +1 -1
- data/lib/cosmo/web/views/overview/_running.erb +3 -3
- data/lib/cosmo/web/views/overview/index.erb +1 -1
- data/lib/cosmo/web/views/streams/_info.erb +1 -0
- data/lib/cosmo/web/views/streams/_stream.erb +1 -0
- data/lib/cosmo/web.rb +8 -1
- data/lib/cosmo.rb +33 -0
- data/sig/cosmo/active_job/adapter.rbs +3 -3
- data/sig/cosmo/active_job/options.rbs +4 -2
- data/sig/cosmo/api/batch.rbs +5 -3
- data/sig/cosmo/api/counter.rbs +16 -6
- data/sig/cosmo/api/cron/entry.rbs +4 -2
- data/sig/cosmo/api/cron.rbs +10 -4
- data/sig/cosmo/api/kv.rbs +21 -4
- data/sig/cosmo/api/registry.rbs +10 -0
- data/sig/cosmo/api/stats/jobs.rbs +34 -0
- data/sig/cosmo/api/stats/metrics.rbs +3 -0
- data/sig/cosmo/api/stats/processes.rbs +9 -1
- data/sig/cosmo/api/stats.rbs +8 -2
- data/sig/cosmo/api/stream.rbs +10 -5
- data/sig/cosmo/batch/callback.rbs +1 -1
- data/sig/cosmo/batch/dispatcher.rbs +1 -1
- data/sig/cosmo/batch.rbs +5 -5
- data/sig/cosmo/cli.rbs +2 -0
- data/sig/cosmo/client.rbs +23 -4
- data/sig/cosmo/config/settings.rbs +6 -0
- data/sig/cosmo/config.rbs +5 -5
- data/sig/cosmo/engine.rbs +5 -0
- data/sig/cosmo/heartbeat.rbs +2 -0
- data/sig/cosmo/job/data.rbs +3 -1
- data/sig/cosmo/job/enqueuer.rbs +12 -0
- data/sig/cosmo/job/processor.rbs +18 -3
- data/sig/cosmo/job/setter.rbs +20 -0
- data/sig/cosmo/job.rbs +10 -4
- data/sig/cosmo/logger.rbs +5 -3
- data/sig/cosmo/middleware.rbs +0 -8
- data/sig/cosmo/processor.rbs +14 -2
- data/sig/cosmo/publisher.rbs +1 -0
- data/sig/cosmo/stream/processor.rbs +4 -0
- data/sig/cosmo/stream.rbs +4 -3
- data/sig/cosmo/utils/hash.rbs +2 -1
- data/sig/cosmo/utils/signal.rbs +5 -5
- data/sig/cosmo/utils/string.rbs +2 -2
- data/sig/cosmo/utils/thread_pool.rbs +1 -1
- data/sig/cosmo/web/cache.rbs +35 -0
- data/sig/cosmo/web/chart.rbs +48 -0
- data/sig/cosmo/web/circuit.rbs +29 -0
- data/sig/cosmo/web/context.rbs +15 -0
- data/sig/cosmo/web/controllers/application.rbs +32 -0
- data/sig/cosmo/web/controllers/batches.rbs +11 -0
- data/sig/cosmo/web/controllers/crons.rbs +23 -0
- data/sig/cosmo/web/controllers/jobs.rbs +47 -0
- data/sig/cosmo/web/controllers/metrics.rbs +21 -0
- data/sig/cosmo/web/controllers/overview.rbs +31 -0
- data/sig/cosmo/web/controllers/processes.rbs +13 -0
- data/sig/cosmo/web/controllers/streams.rbs +27 -0
- data/sig/cosmo/web/helpers/application.rbs +61 -0
- data/sig/cosmo/web/renderer.rbs +33 -0
- data/sig/cosmo/web/throughput.rbs +45 -0
- data/sig/cosmo/web.rbs +25 -0
- data/sig/cosmo.rbs +19 -0
- metadata +23 -6
- data/lib/cosmo/api/stats/busy.rb +0 -68
- data/lib/cosmo/middleware/busy.rb +0 -15
- data/sig/cosmo/api/stats/busy.rbs +0 -33
data/lib/cosmo/utils/json.rb
CHANGED
|
@@ -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
|
data/lib/cosmo/utils/signal.rb
CHANGED
|
@@ -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
|
data/lib/cosmo/utils/string.rb
CHANGED
|
@@ -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
|
@@ -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
|
|
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)}
|
data/lib/cosmo/web/cache.rb
CHANGED
|
@@ -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
|
data/lib/cosmo/web/chart.rb
CHANGED
|
@@ -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
|
|
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
|
|
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)),
|
data/lib/cosmo/web/circuit.rb
CHANGED
data/lib/cosmo/web/context.rb
CHANGED
|
@@ -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 = (
|
|
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", {
|
|
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", {
|
|
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
|
|
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
|
-
|
|
83
|
-
|
|
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
|
-
|
|
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,
|
|
91
|
-
limit = (
|
|
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:,
|
|
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 = (
|
|
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
|
-
|
|
127
|
-
stream_name = params.fetch("stream_name",
|
|
128
|
-
[stream_name,
|
|
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 =
|
|
11
|
+
CELLS = 64
|
|
12
12
|
RUNNING = 9
|
|
13
|
-
PROCESSES =
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|