upstate-rails 0.2.0 → 0.4.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 +22 -0
- data/README.md +12 -1
- data/lib/tasks/upstate.rake +10 -0
- data/lib/upstate/rails/endpoint.rb +7 -1
- data/lib/upstate/rails/health.rb +12 -6
- data/lib/upstate/rails/railtie.rb +6 -1
- data/lib/upstate/rails/recurring/solid_queue_source.rb +139 -0
- data/lib/upstate/rails/recurring.rb +31 -0
- data/lib/upstate/rails/verify.rb +124 -0
- data/lib/upstate/rails/version.rb +1 -1
- data/lib/upstate/rails.rb +12 -0
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6fb7f4706bd20b617139ac0e7990e8bab0634a7d0a7eaf3d86f747fc5400e8b8
|
|
4
|
+
data.tar.gz: 797ee9dfdb3df2b67591c6ff397f7b6fae50f27c452fb8dd634ca12470eed52b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c083103ef1dce6647c1ccb5f5096a203b8468688fcff8ba1dabd512a47ea2919ca09ed13e609a620bc381f7c295b58900bd175dccc7930aed261af5df0371e00
|
|
7
|
+
data.tar.gz: a88b24544f7d934ff1299b2f875e21f7c8409a25b16e3200ff0cdfff6a4c4c4a7d902ae771a8f1dddf7fa2a79f49c88dbd226791b97d3552b74a2ae0674cef22
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
- Add `rake upstate:verify`, a read-only check that sends nothing to Upstate and writes nothing (it reads the app's database, as the endpoint does, and does not touch the cache, job systems, recurring-job tables or action rollups): disabled or configured, token presence (never the value), reporter and runtime versions, and endpoint authorization and response. Nothing runs on install, and it sends nothing to Upstate.
|
|
6
|
+
|
|
7
|
+
## 0.3.0
|
|
8
|
+
|
|
9
|
+
- Solid Queue: report the app's recurring jobs in a new `recurring` section so Upstate can
|
|
10
|
+
propose a check-in for each one:
|
|
11
|
+
- `tasks`: `{key, class_name, schedule, interval_seconds}` for each entry of
|
|
12
|
+
`config/recurring.yml` (or `SOLID_QUEUE_RECURRING_SCHEDULE`) under the current Rails
|
|
13
|
+
environment key. `interval_seconds` is the longest gap between the next runs, from Fugit
|
|
14
|
+
when it is loaded. Job arguments, `command:` and queue names are never read.
|
|
15
|
+
- `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,
|
|
16
|
+
started_at, finished_at, result}` with `result` of `success`, `fail` or `running`,
|
|
17
|
+
read from Solid Queue's own tables so any container reports them. `run_id` is
|
|
18
|
+
`sq-<job id>`. Solid Queue keeps no start time once a job finishes, so `started_at` is
|
|
19
|
+
only present while a run is going.
|
|
20
|
+
- The section is left out when there is no readable schedule or another job adapter is
|
|
21
|
+
in use (sidekiq-cron and GoodJob cron are not read yet; `Upstate::Rails::Recurring::SOURCES`
|
|
22
|
+
is the seam). The schema stays 1 and the section is additive: servers that predate it
|
|
23
|
+
ignore it, and this gem keeps working against them.
|
|
24
|
+
|
|
3
25
|
## 0.2.0
|
|
4
26
|
|
|
5
27
|
- 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
|
|
|
@@ -62,6 +63,16 @@ curl -i -H "Authorization: Bearer $UPSTATE_HEALTH_TOKEN" https://your-app.exampl
|
|
|
62
63
|
|
|
63
64
|
You should get `200` and JSON starting with `"schema": 1`. A `503` means the database is not answering (the JSON says so). Anything with a slow, missing or erroring section reports that section as `null` and still returns the rest.
|
|
64
65
|
|
|
66
|
+
### Local check
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
bin/rails upstate:verify
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Read-only. It sends nothing to Upstate and writes nothing (no cache probe, no job-system access, no action-rollup flush, no credential change), though it reads your database to build the response, as the endpoint does. It does not touch the cache, Sidekiq or other job systems, recurring-job tables or action rollups. It shows whether the reporter is on or disabled, that a token is present (its value is never printed), the reporter, Rails and Ruby versions, and that the endpoint rejects a wrong token, accepts the configured one and answers with schema 1 (the version it shows is your `APP_VERSION` or `KAMAL_VERSION`, not the gem's). It exits non-zero when something needs fixing and says what to do. It never runs on install, only when you invoke it.
|
|
73
|
+
|
|
74
|
+
A passing local check does not prove Upstate is receiving data. After a deploy, run `upstate doctor` to see Configured, Connected and Fresh evidence from Upstate itself.
|
|
75
|
+
|
|
65
76
|
## Good to know
|
|
66
77
|
|
|
67
78
|
- The route is added ahead of your routes, so a catch-all route cannot hide it.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
namespace :upstate do
|
|
4
|
+
desc "Check the Upstate app-health reporter locally (read-only; prints no token)"
|
|
5
|
+
task verify: :environment do
|
|
6
|
+
report = Upstate::Rails::Verify.call
|
|
7
|
+
puts Upstate::Rails::Verify.render(report)
|
|
8
|
+
exit(1) unless report[:ok]
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -6,12 +6,18 @@ module Upstate
|
|
|
6
6
|
class Endpoint
|
|
7
7
|
MAX_TOKEN_BYTES = 256
|
|
8
8
|
|
|
9
|
+
# `health` is any callable returning [status, body]; `rake upstate:verify` passes a
|
|
10
|
+
# read-only one.
|
|
11
|
+
def initialize(health: Health.method(:call))
|
|
12
|
+
@health = health
|
|
13
|
+
end
|
|
14
|
+
|
|
9
15
|
def call(env)
|
|
10
16
|
expected = ENV["UPSTATE_HEALTH_TOKEN"].to_s
|
|
11
17
|
return respond(404, "") if expected.empty?
|
|
12
18
|
return respond(401, "", "www-authenticate" => "Bearer") unless authorized?(env["HTTP_AUTHORIZATION"], expected)
|
|
13
19
|
|
|
14
|
-
status, body =
|
|
20
|
+
status, body = @health.call
|
|
15
21
|
respond(status, JSON.generate(body), "content-type" => "application/json")
|
|
16
22
|
end
|
|
17
23
|
|
data/lib/upstate/rails/health.rb
CHANGED
|
@@ -6,18 +6,22 @@ module Upstate
|
|
|
6
6
|
class Health
|
|
7
7
|
SCHEMA = 1
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
# `read_only` builds the response without any write or side effect: the cache probe,
|
|
10
|
+
# the job-system read (Sidekiq's process set prunes dead entries when loaded) and
|
|
11
|
+
# recurring-job tables (Solid Queue) and action rollups are skipped. Used by the local verify.
|
|
12
|
+
def self.call(now: Time.now, read_only: false)
|
|
13
|
+
new(now, read_only: read_only).call
|
|
11
14
|
end
|
|
12
15
|
|
|
13
|
-
def initialize(now)
|
|
16
|
+
def initialize(now, read_only: false)
|
|
14
17
|
@now = now
|
|
18
|
+
@read_only = read_only
|
|
15
19
|
end
|
|
16
20
|
|
|
17
21
|
# => [http_status, body_hash]
|
|
18
22
|
def call
|
|
19
23
|
database = section { Sections.database }
|
|
20
|
-
jobs, solid_queue = section { Jobs.call(now: @now) } || [nil, nil]
|
|
24
|
+
jobs, solid_queue = (@read_only ? nil : section { Jobs.call(now: @now) }) || [nil, nil]
|
|
21
25
|
|
|
22
26
|
body = {
|
|
23
27
|
schema: SCHEMA,
|
|
@@ -29,10 +33,12 @@ module Upstate
|
|
|
29
33
|
active_record_pool: section { Sections.active_record_pool },
|
|
30
34
|
puma: section { Sections.puma },
|
|
31
35
|
jobs: jobs,
|
|
32
|
-
cache: section { Sections.cache },
|
|
33
|
-
actions: section { actions }
|
|
36
|
+
cache: @read_only ? nil : section { Sections.cache },
|
|
37
|
+
actions: @read_only ? nil : section { actions }
|
|
34
38
|
}
|
|
35
39
|
body[:solid_queue] = solid_queue if solid_queue
|
|
40
|
+
recurring = @read_only ? nil : section { Recurring.call(now: @now) }
|
|
41
|
+
body[:recurring] = recurring if recurring
|
|
36
42
|
|
|
37
43
|
status = database && database[:ok] == false ? 503 : 200
|
|
38
44
|
[status, body]
|
|
@@ -34,7 +34,12 @@ module Upstate
|
|
|
34
34
|
next if ENV["UPSTATE_HEALTH_TOKEN"].to_s.empty?
|
|
35
35
|
|
|
36
36
|
Upstate::Rails.subscribe!
|
|
37
|
-
at_exit { Upstate::Rails.rollup.flush! }
|
|
37
|
+
at_exit { Upstate::Rails.rollup.flush! unless Upstate::Rails.flush_disabled? }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Defined only; nothing runs until someone invokes the task.
|
|
41
|
+
rake_tasks do
|
|
42
|
+
load File.expand_path("../../tasks/upstate.rake", __dir__)
|
|
38
43
|
end
|
|
39
44
|
|
|
40
45
|
generators do
|
|
@@ -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
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Upstate
|
|
4
|
+
module Rails
|
|
5
|
+
# Read-only local check for `rake upstate:verify`. It answers the questions that can
|
|
6
|
+
# be answered inside this process: is the reporter turned on, is a token present (never
|
|
7
|
+
# printed), and does the endpoint authorize and answer. It sends nothing to Upstate,
|
|
8
|
+
# writes nothing (no cache probe, no rollup flush) and never changes credentials. It
|
|
9
|
+
# does read from the app's own database to build the response. Whether Upstate has received a report
|
|
10
|
+
# is a separate fact that only Upstate can confirm (`upstate doctor`).
|
|
11
|
+
class Verify
|
|
12
|
+
Check = Struct.new(:name, :status, :detail, :action, keyword_init: true) do
|
|
13
|
+
def ok? = status == "ok"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.call(endpoint: nil)
|
|
17
|
+
new(endpoint).call
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(endpoint)
|
|
21
|
+
@endpoint = endpoint || Endpoint.new(health: -> { Health.call(read_only: true) })
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# => {reporter:, runtime:, environment:, token:, checks:, ok:}
|
|
25
|
+
def call
|
|
26
|
+
# The railtie registers an exit-time rollup flush when a token is set; a check must
|
|
27
|
+
# write nothing, so switch it off for this process.
|
|
28
|
+
Upstate::Rails.disable_flush!
|
|
29
|
+
token = ENV["UPSTATE_HEALTH_TOKEN"].to_s
|
|
30
|
+
checks = [configured_check(token)]
|
|
31
|
+
checks.concat(endpoint_checks(token)) unless token.empty?
|
|
32
|
+
{
|
|
33
|
+
reporter: {name: "upstate-rails", version: Upstate::Rails::VERSION},
|
|
34
|
+
runtime: {rails: ::Rails.version, ruby: RUBY_VERSION},
|
|
35
|
+
environment: ::Rails.env.to_s,
|
|
36
|
+
token: token_summary(token),
|
|
37
|
+
checks: checks.map(&:to_h),
|
|
38
|
+
ok: checks.all?(&:ok?),
|
|
39
|
+
next_step: "A passing local check is not proof that Upstate receives data. After a deploy, run `upstate doctor` and wait for fresh evidence."
|
|
40
|
+
}
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Human-readable lines. Never includes the token or any part of it.
|
|
44
|
+
def self.render(report)
|
|
45
|
+
lines = ["Upstate Rails reporter #{report[:reporter][:version]} (Rails #{report[:runtime][:rails]}, Ruby #{report[:runtime][:ruby]}, #{report[:environment]})"]
|
|
46
|
+
lines << "Token: #{report[:token][:present] ? "present (#{report[:token][:length]} characters, value not shown)" : "not set"}"
|
|
47
|
+
report[:checks].each do |check|
|
|
48
|
+
lines << "#{(check[:status] == "ok") ? "ok " : "FAIL"} #{check[:name]}: #{check[:detail]}"
|
|
49
|
+
lines << " next: #{check[:action]}" if check[:action]
|
|
50
|
+
end
|
|
51
|
+
lines << report[:next_step]
|
|
52
|
+
lines.join("\n")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def token_summary(token)
|
|
58
|
+
{present: !token.empty?, length: token.length}
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def configured_check(token)
|
|
62
|
+
if token.empty?
|
|
63
|
+
Check.new(name: "configured", status: "disabled",
|
|
64
|
+
detail: "UPSTATE_HEALTH_TOKEN is not set in this environment, so /upstate/health answers 404 and nothing is reported.",
|
|
65
|
+
action: "Generate a health token in Upstate, set UPSTATE_HEALTH_TOKEN for this environment (for example under env.secret in config/deploy.yml), then deploy.")
|
|
66
|
+
else
|
|
67
|
+
Check.new(name: "configured", status: "ok", detail: "UPSTATE_HEALTH_TOKEN is set; the endpoint is on.")
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def endpoint_checks(token)
|
|
72
|
+
if token.bytesize > Endpoint::MAX_TOKEN_BYTES
|
|
73
|
+
return [Check.new(name: "authorization", status: "failed",
|
|
74
|
+
detail: "UPSTATE_HEALTH_TOKEN is #{token.bytesize} bytes; the endpoint rejects any token over #{Endpoint::MAX_TOKEN_BYTES} bytes.",
|
|
75
|
+
action: "Generate a token in Upstate (they are well under the limit) and set it as UPSTATE_HEALTH_TOKEN, without extra characters.")]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# One request with the configured token serves both the authorization and the
|
|
79
|
+
# response check, so the health build (database, jobs, schedules) runs once.
|
|
80
|
+
status, _headers, body = call_endpoint(token)
|
|
81
|
+
[auth_check(token, status), health_check(status, body)]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# A wrong token that can never equal the configured one.
|
|
85
|
+
def probe_token(token)
|
|
86
|
+
loop do
|
|
87
|
+
candidate = "probe-#{SecureRandom.hex(16)}"
|
|
88
|
+
return candidate unless candidate == token
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def call_endpoint(bearer)
|
|
93
|
+
@endpoint.call({"HTTP_AUTHORIZATION" => "Bearer #{bearer}"})
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def auth_check(token, accepted)
|
|
97
|
+
rejected = call_endpoint(probe_token(token)).first
|
|
98
|
+
if rejected == 401 && accepted != 401
|
|
99
|
+
Check.new(name: "authorization", status: "ok", detail: "The endpoint rejects a wrong token and accepts the configured one.")
|
|
100
|
+
else
|
|
101
|
+
Check.new(name: "authorization", status: "failed",
|
|
102
|
+
detail: "The endpoint did not behave as expected (wrong token: #{rejected}, configured token: #{accepted}).",
|
|
103
|
+
action: "Check that UPSTATE_HEALTH_TOKEN has no stray characters or whitespace, and that this endpoint is the Upstate one, then redeploy.")
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def health_check(status, body)
|
|
108
|
+
parsed = JSON.parse(body.join)
|
|
109
|
+
if status == 200 && parsed["schema"] == Health::SCHEMA
|
|
110
|
+
Check.new(name: "health response", status: "ok", detail: "Schema #{Health::SCHEMA}, application version #{parsed["version"].inspect} (APP_VERSION or KAMAL_VERSION).")
|
|
111
|
+
elsif status == 503
|
|
112
|
+
Check.new(name: "health response", status: "failed", detail: "The database is not answering (503).",
|
|
113
|
+
action: "Fix database connectivity for this environment; the reporter will then answer 200.")
|
|
114
|
+
else
|
|
115
|
+
Check.new(name: "health response", status: "failed", detail: "Unexpected response #{status}.",
|
|
116
|
+
action: "Run the app and request /upstate/health with the token to see the response.")
|
|
117
|
+
end
|
|
118
|
+
rescue StandardError => e
|
|
119
|
+
Check.new(name: "health response", status: "failed", detail: "Could not read the health response (#{e.class}).",
|
|
120
|
+
action: "Run the app in this environment and check its logs for the error.")
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
data/lib/upstate/rails.rb
CHANGED
|
@@ -26,6 +26,15 @@ module Upstate
|
|
|
26
26
|
@rollup ||= ActionRollup.new
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# `rake upstate:verify` turns the exit-time flush off so the check writes nothing.
|
|
30
|
+
def disable_flush!
|
|
31
|
+
@flush_disabled = true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def flush_disabled?
|
|
35
|
+
!!@flush_disabled
|
|
36
|
+
end
|
|
37
|
+
|
|
29
38
|
# Subscribes once to process_action.action_controller.
|
|
30
39
|
def subscribe!
|
|
31
40
|
return if @subscriber
|
|
@@ -62,7 +71,10 @@ require_relative "rails/jobs/solid_queue"
|
|
|
62
71
|
require_relative "rails/jobs/sidekiq"
|
|
63
72
|
require_relative "rails/jobs/good_job"
|
|
64
73
|
require_relative "rails/jobs"
|
|
74
|
+
require_relative "rails/recurring/solid_queue_source"
|
|
75
|
+
require_relative "rails/recurring"
|
|
65
76
|
require_relative "rails/sections"
|
|
66
77
|
require_relative "rails/health"
|
|
67
78
|
require_relative "rails/endpoint"
|
|
79
|
+
require_relative "rails/verify"
|
|
68
80
|
require_relative "rails/railtie"
|
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.4.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-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: railties
|
|
@@ -64,6 +64,7 @@ files:
|
|
|
64
64
|
- LICENSE.txt
|
|
65
65
|
- README.md
|
|
66
66
|
- lib/generators/upstate/install/install_generator.rb
|
|
67
|
+
- lib/tasks/upstate.rake
|
|
67
68
|
- lib/upstate/rails.rb
|
|
68
69
|
- lib/upstate/rails/action_rollup.rb
|
|
69
70
|
- lib/upstate/rails/action_store.rb
|
|
@@ -74,7 +75,10 @@ files:
|
|
|
74
75
|
- lib/upstate/rails/jobs/sidekiq.rb
|
|
75
76
|
- lib/upstate/rails/jobs/solid_queue.rb
|
|
76
77
|
- lib/upstate/rails/railtie.rb
|
|
78
|
+
- lib/upstate/rails/recurring.rb
|
|
79
|
+
- lib/upstate/rails/recurring/solid_queue_source.rb
|
|
77
80
|
- lib/upstate/rails/sections.rb
|
|
81
|
+
- lib/upstate/rails/verify.rb
|
|
78
82
|
- lib/upstate/rails/version.rb
|
|
79
83
|
homepage: https://github.com/Upstate-run/upstate-rails
|
|
80
84
|
licenses:
|