upstate-rails 0.2.0 → 0.3.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/CHANGELOG.md +18 -0
- data/README.md +2 -1
- data/lib/upstate/rails/health.rb +2 -0
- data/lib/upstate/rails/recurring/solid_queue_source.rb +139 -0
- data/lib/upstate/rails/recurring.rb +31 -0
- data/lib/upstate/rails/version.rb +1 -1
- data/lib/upstate/rails.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9daa36dc978c28d7dc1bba397cc07566184c20dc682ee8c00b28e8a3836858d3
|
|
4
|
+
data.tar.gz: 047d1db6c3e0b8b586abed9c69fda37a137e7babcca644df64e8eac42d20c273
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6b863838c88ded9db1364173f7d778de273c7dc56fe8d536f14ff5de33da35a1788bf61d600cd9c26d9ede9864a920db3ee0d2610cabf1ecc0a09e0c134987cf
|
|
7
|
+
data.tar.gz: f475fd351d17f8eaba474f84bc59fc9c2aab10a54b07493bee2011815cba12dc0266ac7b606bdf86ace9fde16787beebd2f0fdc6ae6332bbbcd1d92d8a631b92
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
- Solid Queue: report the app's recurring jobs in a new `recurring` section so Upstate can
|
|
6
|
+
propose a check-in for each one:
|
|
7
|
+
- `tasks`: `{key, class_name, schedule, interval_seconds}` for each entry of
|
|
8
|
+
`config/recurring.yml` (or `SOLID_QUEUE_RECURRING_SCHEDULE`) under the current Rails
|
|
9
|
+
environment key. `interval_seconds` is the longest gap between the next runs, from Fugit
|
|
10
|
+
when it is loaded. Job arguments, `command:` and queue names are never read.
|
|
11
|
+
- `runs`: for those jobs, the last 24 hours (newest 10 finished runs per job, plus every run still going at any age; running jobs are read independently of the row limits; finished and failed runs are the newest by end time) as `{key, run_id,
|
|
12
|
+
started_at, finished_at, result}` with `result` of `success`, `fail` or `running`,
|
|
13
|
+
read from Solid Queue's own tables so any container reports them. `run_id` is
|
|
14
|
+
`sq-<job id>`. Solid Queue keeps no start time once a job finishes, so `started_at` is
|
|
15
|
+
only present while a run is going.
|
|
16
|
+
- The section is left out when there is no readable schedule or another job adapter is
|
|
17
|
+
in use (sidekiq-cron and GoodJob cron are not read yet; `Upstate::Rails::Recurring::SOURCES`
|
|
18
|
+
is the seam). The schema stays 1 and the section is additive: servers that predate it
|
|
19
|
+
ignore it, and this gem keeps working against them.
|
|
20
|
+
|
|
3
21
|
## 0.2.0
|
|
4
22
|
|
|
5
23
|
- Report database health in the `database` section, for the primary connection on
|
data/README.md
CHANGED
|
@@ -12,12 +12,13 @@ The gem adds one endpoint, `GET /upstate/health`. Upstate's host observer reads
|
|
|
12
12
|
- Active Record pool size, busy and waiting connections.
|
|
13
13
|
- Puma workers, threads, backlog and spare capacity.
|
|
14
14
|
- Job numbers for Solid Queue, Sidekiq or GoodJob: queue depth, oldest queued age, worker count, jobs processed and failed in the last 5 minutes, and when something last ran.
|
|
15
|
+
- Solid Queue only: your recurring jobs from `config/recurring.yml` (or `SOLID_QUEUE_RECURRING_SCHEDULE`), under the current Rails environment. For each, the key, the class name and the schedule text, and for its recent runs (last 24 hours, newest 10 finished runs per job, plus any still running) an id, when it started or finished and whether it succeeded, failed or is running. Upstate turns these into check-in suggestions; nothing is monitored until you accept. A job's arguments and `command:` are never read.
|
|
15
16
|
- Whether your cache (Solid Cache or Redis) answers.
|
|
16
17
|
- For the last 15 minutes, the 10 slowest `Controller#action` names per minute, with request count, error count, a 12-bucket timing histogram, and total database time.
|
|
17
18
|
|
|
18
19
|
## What is never sent
|
|
19
20
|
|
|
20
|
-
No params, URLs or paths, SQL, row data, IP addresses, user agents, headers, cookies, job arguments, exception classes or messages, logs, or traces. An error is only a count (a 5xx response or an unhandled exception).
|
|
21
|
+
No params, URLs or paths, SQL, row data, IP addresses, user agents, headers, cookies, job arguments, exception classes or messages, logs, or traces. An error is only a count (a 5xx response or an unhandled exception). Queue names are not read. Job class names are read only for the recurring jobs listed in `config/recurring.yml`, and never for other jobs.
|
|
21
22
|
|
|
22
23
|
## Install
|
|
23
24
|
|
data/lib/upstate/rails/health.rb
CHANGED
|
@@ -33,6 +33,8 @@ module Upstate
|
|
|
33
33
|
actions: section { actions }
|
|
34
34
|
}
|
|
35
35
|
body[:solid_queue] = solid_queue if solid_queue
|
|
36
|
+
recurring = section { Recurring.call(now: @now) }
|
|
37
|
+
body[:recurring] = recurring if recurring
|
|
36
38
|
|
|
37
39
|
status = database && database[:ok] == false ? 503 : 200
|
|
38
40
|
[status, body]
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
require "erb"
|
|
5
|
+
require "pathname"
|
|
6
|
+
|
|
7
|
+
module Upstate
|
|
8
|
+
module Rails
|
|
9
|
+
module Recurring
|
|
10
|
+
# Solid Queue: tasks come from config/recurring.yml (or SOLID_QUEUE_RECURRING_SCHEDULE),
|
|
11
|
+
# under the current Rails environment key when the file has one, the same way Solid Queue
|
|
12
|
+
# reads it. Runs come from solid_queue_recurring_executions and the job rows they point
|
|
13
|
+
# at, so they are visible from any container, not only the worker's.
|
|
14
|
+
module SolidQueueSource
|
|
15
|
+
WINDOW = 24 * 3600
|
|
16
|
+
RUNS_PER_TASK = 10
|
|
17
|
+
MAX_RUNNING = 100
|
|
18
|
+
MAX_TASKS = 100
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def call(now: Time.now)
|
|
23
|
+
list = tasks
|
|
24
|
+
return {adapter: "solid_queue", tasks: nil, runs: []} if list.nil?
|
|
25
|
+
|
|
26
|
+
{adapter: "solid_queue", tasks: list, runs: runs(list.map { |t| t[:key] }, now)}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def schedule_path
|
|
30
|
+
configured = ENV["SOLID_QUEUE_RECURRING_SCHEDULE"].to_s.strip
|
|
31
|
+
root = ::Rails.root || Pathname(Dir.pwd)
|
|
32
|
+
root.join(configured.empty? ? "config/recurring.yml" : configured)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# => [{key:, class_name:, schedule:, interval_seconds:}] or nil when unreadable.
|
|
36
|
+
def tasks(path: schedule_path, env: ::Rails.env.to_s)
|
|
37
|
+
return nil unless File.file?(path)
|
|
38
|
+
|
|
39
|
+
data = YAML.safe_load(ERB.new(File.read(path)).result, aliases: true, permitted_classes: [Symbol])
|
|
40
|
+
return nil unless data.is_a?(Hash)
|
|
41
|
+
|
|
42
|
+
scoped = data[env].is_a?(Hash) ? data[env] : nil
|
|
43
|
+
entries = scoped || data
|
|
44
|
+
list = entries.filter_map { |key, entry| task(key, entry) }.first(MAX_TASKS)
|
|
45
|
+
# A file with only other environments' sections is not a schedule for this one.
|
|
46
|
+
return nil if scoped.nil? && list.empty? && !entries.empty?
|
|
47
|
+
|
|
48
|
+
list
|
|
49
|
+
rescue StandardError, Psych::Exception, ScriptError
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Only key, class and schedule are read from an entry; args and command never are.
|
|
54
|
+
def task(key, entry)
|
|
55
|
+
return unless entry.is_a?(Hash)
|
|
56
|
+
|
|
57
|
+
schedule = entry["schedule"]
|
|
58
|
+
return unless schedule.is_a?(String) && !schedule.strip.empty?
|
|
59
|
+
|
|
60
|
+
class_name = entry["class"]
|
|
61
|
+
{
|
|
62
|
+
key: key.to_s,
|
|
63
|
+
class_name: class_name.is_a?(String) ? class_name : nil,
|
|
64
|
+
schedule: schedule.strip,
|
|
65
|
+
interval_seconds: interval_seconds(schedule.strip)
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Longest gap over the next runs, via Fugit (a Solid Queue dependency) when present.
|
|
70
|
+
def interval_seconds(schedule, from: Time.now)
|
|
71
|
+
return unless defined?(::Fugit)
|
|
72
|
+
|
|
73
|
+
parsed = ::Fugit.parse(schedule)
|
|
74
|
+
if parsed.respond_to?(:next_time)
|
|
75
|
+
times = [from]
|
|
76
|
+
12.times { times << parsed.next_time(times.last).to_t }
|
|
77
|
+
times.each_cons(2).map { |a, b| (b - a).to_i }.max
|
|
78
|
+
elsif parsed.respond_to?(:to_sec)
|
|
79
|
+
parsed.to_sec.to_i
|
|
80
|
+
end
|
|
81
|
+
rescue StandardError
|
|
82
|
+
nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Recent runs of the given recurring tasks. Per task, three independent bounded reads,
|
|
86
|
+
# none of which depends on the order executions were enqueued in:
|
|
87
|
+
# * runs still going: every claimed execution of the task (bounded by what is
|
|
88
|
+
# actually executing, capped at MAX_RUNNING), at any age;
|
|
89
|
+
# * finished jobs, newest RUNS_PER_TASK by finish time inside WINDOW;
|
|
90
|
+
# * failed jobs, newest RUNS_PER_TASK by failure time inside WINDOW.
|
|
91
|
+
# The finished and failed reads are merged and cut to RUNS_PER_TASK by end time.
|
|
92
|
+
def runs(keys, now)
|
|
93
|
+
return [] if keys.empty? || !defined?(::SolidQueue::RecurringExecution)
|
|
94
|
+
|
|
95
|
+
since = now - WINDOW
|
|
96
|
+
keys.flat_map { |key| task_runs(key, since) }
|
|
97
|
+
rescue StandardError
|
|
98
|
+
[]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def task_runs(key, since)
|
|
102
|
+
job_ids = ::SolidQueue::RecurringExecution.where(task_key: key).select(:job_id)
|
|
103
|
+
|
|
104
|
+
running = ::SolidQueue::ClaimedExecution.where(job_id: job_ids)
|
|
105
|
+
.order(created_at: :desc).limit(MAX_RUNNING).pluck(:job_id, :created_at)
|
|
106
|
+
done = ::SolidQueue::Job.where(id: job_ids).where(finished_at: since..)
|
|
107
|
+
.order(finished_at: :desc).limit(RUNS_PER_TASK).pluck(:id, :finished_at)
|
|
108
|
+
failed = ::SolidQueue::FailedExecution.where(job_id: job_ids).where(created_at: since..)
|
|
109
|
+
.order(created_at: :desc).limit(RUNS_PER_TASK).pluck(:job_id, :created_at)
|
|
110
|
+
|
|
111
|
+
ended = failed.map { |id, at| run(key, id, nil, at, nil) } +
|
|
112
|
+
done.map { |id, at| run(key, id, at, nil, nil) }
|
|
113
|
+
ended = ended.compact.uniq { |r| r[:run_id] }.sort_by { |r| r[:finished_at] }.reverse
|
|
114
|
+
going = running.filter_map { |id, at| run(key, id, nil, nil, at) }
|
|
115
|
+
|
|
116
|
+
going + ended.reject { |r| going.any? { |g| g[:run_id] == r[:run_id] } }.first(RUNS_PER_TASK)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# failed -> fail (at the failure), finished -> success, claimed -> running (since the
|
|
120
|
+
# claim). A job still waiting to be claimed has not started and is not reported.
|
|
121
|
+
# Solid Queue keeps no start time once a job finishes, so only a running job has one.
|
|
122
|
+
def run(key, job_id, finished_at, failed_at, claimed_at)
|
|
123
|
+
base = {key: key, run_id: "sq-#{job_id}", started_at: nil, finished_at: nil}
|
|
124
|
+
if failed_at
|
|
125
|
+
base.merge(finished_at: iso(failed_at), result: "fail")
|
|
126
|
+
elsif finished_at
|
|
127
|
+
base.merge(finished_at: iso(finished_at), result: "success")
|
|
128
|
+
elsif claimed_at
|
|
129
|
+
base.merge(started_at: iso(claimed_at), result: "running")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def iso(time)
|
|
134
|
+
time.utc.iso8601
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Upstate
|
|
4
|
+
module Rails
|
|
5
|
+
# The `recurring` section: the app's own recurring-job schedule and recent runs of those
|
|
6
|
+
# jobs, so Upstate can propose a check-in for each one. Sent: job key, class name,
|
|
7
|
+
# schedule text and, per run, an id, start, end and result. Never job arguments, never a
|
|
8
|
+
# `command:` string, never queue names or error text.
|
|
9
|
+
#
|
|
10
|
+
# Each source answers `call(now:)` with
|
|
11
|
+
# {adapter: "...", tasks: [{key:, class_name:, schedule:, interval_seconds:}] | nil,
|
|
12
|
+
# runs: [{key:, run_id:, started_at:, finished_at:, result:}]}
|
|
13
|
+
# `tasks: nil` means the schedule could not be read (the server then changes nothing).
|
|
14
|
+
# To add sidekiq-cron or GoodJob cron, add a source here that returns the same shape and
|
|
15
|
+
# register it under the adapter name; the server allowlist gains the adapter name too.
|
|
16
|
+
module Recurring
|
|
17
|
+
SOURCES = {"solid_queue" => :SolidQueueSource}.freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# => section hash, or nil when the adapter has no source or finds no schedule file.
|
|
22
|
+
def call(now: Time.now)
|
|
23
|
+
source = SOURCES[Jobs.adapter_name]
|
|
24
|
+
return unless source
|
|
25
|
+
|
|
26
|
+
section = const_get(source).call(now: now)
|
|
27
|
+
section if section && !section[:tasks].nil?
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/upstate/rails.rb
CHANGED
|
@@ -62,6 +62,8 @@ require_relative "rails/jobs/solid_queue"
|
|
|
62
62
|
require_relative "rails/jobs/sidekiq"
|
|
63
63
|
require_relative "rails/jobs/good_job"
|
|
64
64
|
require_relative "rails/jobs"
|
|
65
|
+
require_relative "rails/recurring/solid_queue_source"
|
|
66
|
+
require_relative "rails/recurring"
|
|
65
67
|
require_relative "rails/sections"
|
|
66
68
|
require_relative "rails/health"
|
|
67
69
|
require_relative "rails/endpoint"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: upstate-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Upstate
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: railties
|
|
@@ -74,6 +74,8 @@ files:
|
|
|
74
74
|
- lib/upstate/rails/jobs/sidekiq.rb
|
|
75
75
|
- lib/upstate/rails/jobs/solid_queue.rb
|
|
76
76
|
- lib/upstate/rails/railtie.rb
|
|
77
|
+
- lib/upstate/rails/recurring.rb
|
|
78
|
+
- lib/upstate/rails/recurring/solid_queue_source.rb
|
|
77
79
|
- lib/upstate/rails/sections.rb
|
|
78
80
|
- lib/upstate/rails/version.rb
|
|
79
81
|
homepage: https://github.com/Upstate-run/upstate-rails
|