sgx-jmp
created pr with
sgx-jmp/notify-snikket-lauch-fail.1
commented
lgtm
cmds
checkout latest patchset:
ssh pr.pico.sh pull sgx-jmp:notify-snikket-lauch-fail | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull sgx-jmp:notify-snikket-lauch-fail [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh sgx-jmp:notify-snikket-lauch-failadd comment to patch request:
ssh pr.pico.sh comment sgx-jmp:notify-snikket-lauch-fail "lgtm!"
Patchset
sgx-jmp/notify-snikket-lauch-fail.1
Notify admin if snikket launch fails
Stephen Paul Weber
2026-11-18T20:29:44ZOr if it still hasn't suceeded within 5 minutes
Diff stat
5 files changed, 229 insertions(+), 14 deletions(-)
Semantic diff summary
0 added,
0 modified,
0 signature changed,
0 removed
across 0 analyzed files
(5 files skipped: unsupported file type)
+5
-3
lib/registration.rb
#
| ... | ... | @@ -879,9 +879,11 @@ class Registration | |
| 879 | 879 | end | |
| 880 | 880 | ||
| 881 | 881 | def launch(domain) | |
| 882 | - | IQ_MANAGER.write(::Snikket::Launch.new( | |
| 883 | - | nil, CONFIG[:snikket_hosting_api], domain: domain | |
| 884 | - | )).then { |launched| | |
| 882 | + | ::Snikket.launch_instance( | |
| 883 | + | domain, | |
| 884 | + | customer: @customer, | |
| 885 | + | tel: @tel | |
| 886 | + | ).then { |launched| | |
| 885 | 887 | save_instance_and_wait(domain, launched) | |
| 886 | 888 | }.catch { |e| | |
| 887 | 889 | next EMPromise.reject(e) unless e.respond_to?(:text) |
+70
-1
lib/snikket.rb
#
| ... | ... | @@ -8,7 +8,52 @@ require "link-header-parser" | |
| 8 | 8 | require "uri" | |
| 9 | 9 | require "value_semantics/monkey_patched" | |
| 10 | 10 | ||
| 11 | + | require_relative "em" | |
| 12 | + | require_relative "expiring_lock" | |
| 13 | + | ||
| 11 | 14 | module Snikket | |
| 15 | + | LAUNCH_TIMEOUT = 60 * 5 | |
| 16 | + | ||
| 17 | + | def self.notify_admin(message, key:) | |
| 18 | + | ExpiringLock.new( | |
| 19 | + | "jmp_snikket_alert-#{key}", | |
| 20 | + | expiry: 60 * 60 * 24, | |
| 21 | + | redis: REDIS | |
| 22 | + | ).with do | |
| 23 | + | BLATHER.join(CONFIG[:notify_admin], "sgx-jmp") | |
| 24 | + | BLATHER.say(CONFIG[:notify_admin], message, :groupchat) | |
| 25 | + | end | |
| 26 | + | end | |
| 27 | + | ||
| 28 | + | def self.launch_instance(domain, customer:, tel:) | |
| 29 | + | IQ_MANAGER.write(Launch.new( | |
| 30 | + | nil, CONFIG[:snikket_hosting_api], domain: domain | |
| 31 | + | )) | |
| 32 | + | .catch { |e| launch_failed(domain, customer, tel, e) } | |
| 33 | + | .then { |launched| watch_new_instance(launched, domain, customer) } | |
| 34 | + | end | |
| 35 | + | ||
| 36 | + | def self.launch_failed(domain, customer, tel, e) | |
| 37 | + | error = e.respond_to?(:text) ? e.text : e.message | |
| 38 | + | notify_admin( | |
| 39 | + | "Snikket instance #{domain} for #{customer} (#{tel}) " \ | |
| 40 | + | "failed to launch: #{error}", | |
| 41 | + | key: domain | |
| 42 | + | ) | |
| 43 | + | EMPromise.reject(e) | |
| 44 | + | end | |
| 45 | + | private_class_method :launch_failed | |
| 46 | + | ||
| 47 | + | def self.watch_new_instance(launched, domain, customer) | |
| 48 | + | return launched if launched.status == :needs_dns | |
| 49 | + | ||
| 50 | + | instance = CustomerInstance.for(customer, domain, launched) | |
| 51 | + | instance.launch_started! | |
| 52 | + | EM.promise_timer(LAUNCH_TIMEOUT).then { instance.fetch_invite } | |
| 53 | + | launched | |
| 54 | + | end | |
| 55 | + | private_class_method :watch_new_instance | |
| 56 | + | ||
| 12 | 57 | class Launch < Blather::Stanza::Iq | |
| 13 | 58 | register nil, "launch", "xmpp:snikket.org/hosting/v1" | |
| 14 | 59 |
| ... | ... | @@ -331,7 +376,31 @@ module Snikket | |
| 331 | 376 | end | |
| 332 | 377 | ||
| 333 | 378 | def fetch_invite | |
| 334 | - | InviteRepo.new(bootstrap_uri: bootstrap_uri).fetch | |
| 379 | + | InviteRepo.new(bootstrap_uri: bootstrap_uri).fetch.then do |xmpp_uri| | |
| 380 | + | next xmpp_uri if xmpp_uri | |
| 381 | + | ||
| 382 | + | alert_if_launch_timed_out.then { nil } | |
| 383 | + | end | |
| 384 | + | end | |
| 385 | + | ||
| 386 | + | def launch_started! | |
| 387 | + | REDIS.set( | |
| 388 | + | "jmp_snikket_launch-#{instance_id}", | |
| 389 | + | Time.now.to_i, | |
| 390 | + | "EX", LAUNCH_TIMEOUT | |
| 391 | + | ) | |
| 392 | + | end | |
| 393 | + | ||
| 394 | + | def alert_if_launch_timed_out | |
| 395 | + | REDIS.get("jmp_snikket_launch-#{instance_id}").then { |started_at| | |
| 396 | + | next if started_at | |
| 397 | + | ||
| 398 | + | Snikket.notify_admin( | |
| 399 | + | "Snikket instance #{domain} (#{instance_id}) for " \ | |
| 400 | + | "#{customer_id} is still not launched", | |
| 401 | + | key: instance_id | |
| 402 | + | ) | |
| 403 | + | } | |
| 335 | 404 | end | |
| 336 | 405 | end | |
| 337 | 406 |
+62
-9
test/test_registration.rb
#
| ... | ... | @@ -3,12 +3,15 @@ | |
| 3 | 3 | require "test_helper" | |
| 4 | 4 | require "customer" | |
| 5 | 5 | require "registration" | |
| 6 | + | require "snikket" | |
| 6 | 7 | require "bwmsgsv2_repo" | |
| 7 | 8 | require "transaction" | |
| 8 | 9 | require "sim_order" | |
| 9 | 10 | require "statsd" | |
| 10 | 11 | ||
| 11 | 12 | BandwidthTnReservationRepo::REDIS = FakeRedis.new | |
| 13 | + | Snikket::REDIS = FakeRedis.new | |
| 14 | + | Snikket::IQ_MANAGER = Minitest::Mock.new | |
| 12 | 15 | ||
| 13 | 16 | class RegistrationTest < Minitest::Test | |
| 14 | 17 | def teardown |
| ... | ... | @@ -2154,9 +2157,9 @@ class RegistrationTest < Minitest::Test | |
| 2154 | 2157 | ||
| 2155 | 2158 | class SnikketTest < Minitest::Test | |
| 2156 | 2159 | Command::COMMAND_MANAGER = Minitest::Mock.new | |
| 2157 | - | Registration::FinishOnboarding::Snikket::IQ_MANAGER = Minitest::Mock.new | |
| 2158 | 2160 | ||
| 2159 | 2161 | def setup | |
| 2162 | + | Snikket::REDIS.reset! | |
| 2160 | 2163 | @sgx = Minitest::Mock.new(mksgx) | |
| 2161 | 2164 | @snikket = Registration::FinishOnboarding::Snikket.new( | |
| 2162 | 2165 | customer(sgx: @sgx), |
| ... | ... | @@ -2246,7 +2249,7 @@ class RegistrationTest < Minitest::Test | |
| 2246 | 2249 | end | |
| 2247 | 2250 | ||
| 2248 | 2251 | assert_mock Command::COMMAND_MANAGER | |
| 2249 | - | assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER | |
| 2252 | + | assert_mock Snikket::IQ_MANAGER | |
| 2250 | 2253 | assert_mock blather | |
| 2251 | 2254 | end | |
| 2252 | 2255 | em :test_write |
| ... | ... | @@ -2322,7 +2325,7 @@ class RegistrationTest < Minitest::Test | |
| 2322 | 2325 | ||
| 2323 | 2326 | assert_equal :test_result, result | |
| 2324 | 2327 | assert_mock Command::COMMAND_MANAGER | |
| 2325 | - | assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER | |
| 2328 | + | assert_mock Snikket::IQ_MANAGER | |
| 2326 | 2329 | end | |
| 2327 | 2330 | em :test_write_not_yet | |
| 2328 | 2331 |
| ... | ... | @@ -2385,9 +2388,59 @@ class RegistrationTest < Minitest::Test | |
| 2385 | 2388 | ||
| 2386 | 2389 | assert_equal :test_result, result | |
| 2387 | 2390 | assert_mock Command::COMMAND_MANAGER | |
| 2388 | - | assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER | |
| 2391 | + | assert_mock Snikket::IQ_MANAGER | |
| 2392 | + | assert Snikket::REDIS.get( | |
| 2393 | + | "jmp_snikket_alert-test.snikket.chat" | |
| 2394 | + | ).sync | |
| 2389 | 2395 | end | |
| 2390 | 2396 | em :test_write_already_taken | |
| 2397 | + | ||
| 2398 | + | def test_fetch_invite_alerts_when_launch_timed_out | |
| 2399 | + | instance = Snikket::CustomerInstance.new( | |
| 2400 | + | instance_id: "si-watch", | |
| 2401 | + | bootstrap_token: "SI_SECRET_TOKEN_WATCH", | |
| 2402 | + | customer_id: "watch-test", | |
| 2403 | + | domain: "watch.snikket.chat" | |
| 2404 | + | ) | |
| 2405 | + | # The launch marker has expired, so the instance has timed out. | |
| 2406 | + | stub_request( | |
| 2407 | + | :head, | |
| 2408 | + | "https://watch.snikket.chat/invites_bootstrap" \ | |
| 2409 | + | "?token=SI_SECRET_TOKEN_WATCH" | |
| 2410 | + | ).to_timeout | |
| 2411 | + | ||
| 2412 | + | instance.fetch_invite.sync | |
| 2413 | + | ||
| 2414 | + | assert Snikket::REDIS.get("jmp_snikket_alert-si-watch").sync | |
| 2415 | + | end | |
| 2416 | + | em :test_fetch_invite_alerts_when_launch_timed_out | |
| 2417 | + | ||
| 2418 | + | def test_fetch_invite_does_not_alert_when_ready | |
| 2419 | + | instance = Snikket::CustomerInstance.new( | |
| 2420 | + | instance_id: "si-watch", | |
| 2421 | + | bootstrap_token: "SI_SECRET_TOKEN_WATCH", | |
| 2422 | + | customer_id: "watch-test", | |
| 2423 | + | domain: "watch.snikket.chat" | |
| 2424 | + | ) | |
| 2425 | + | stub_request( | |
| 2426 | + | :head, | |
| 2427 | + | "https://watch.snikket.chat/invites_bootstrap" \ | |
| 2428 | + | "?token=SI_SECRET_TOKEN_WATCH" | |
| 2429 | + | ).to_return( | |
| 2430 | + | status: 200, | |
| 2431 | + | headers: { | |
| 2432 | + | "link" => "<xmpp:watch@example.com?join>; rel=\"alternate\"" | |
| 2433 | + | } | |
| 2434 | + | ) | |
| 2435 | + | ||
| 2436 | + | assert_equal( | |
| 2437 | + | "xmpp:watch@example.com?join", | |
| 2438 | + | instance.fetch_invite.sync | |
| 2439 | + | ) | |
| 2440 | + | ||
| 2441 | + | assert_nil Snikket::REDIS.get("jmp_snikket_alert-si-watch").sync | |
| 2442 | + | end | |
| 2443 | + | em :test_fetch_invite_does_not_alert_when_ready | |
| 2391 | 2444 | end | |
| 2392 | 2445 | ||
| 2393 | 2446 | class SnikketCustomDomainTest < Minitest::Test |
| ... | ... | @@ -2505,7 +2558,7 @@ class RegistrationTest < Minitest::Test | |
| 2505 | 2558 | ||
| 2506 | 2559 | assert_equal :test_result, result | |
| 2507 | 2560 | assert_mock Command::COMMAND_MANAGER | |
| 2508 | - | assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER | |
| 2561 | + | assert_mock Snikket::IQ_MANAGER | |
| 2509 | 2562 | end | |
| 2510 | 2563 | em :test_write_needs_dns | |
| 2511 | 2564 | end |
+84
-0
test/test_snikket.rb
#
| ... | ... | @@ -134,3 +136,85 @@ class TestSnikket < Minitest::Test | |
| 134 | 136 | end | |
| 135 | 137 | em :test_invite_repo_fetch_returns_xmpp_alternate_link | |
| 136 | 138 | end | |
| 139 | + | ||
| 140 | + | class TestSnikketAlerts < Minitest::Test | |
| 141 | + | def setup | |
| 142 | + | Snikket::REDIS.reset! | |
| 143 | + | end | |
| 144 | + | ||
| 145 | + | def with_blather(mock) | |
| 146 | + | had_blather = Snikket.const_defined?(:BLATHER, false) | |
| 147 | + | old_blather = Snikket::BLATHER if had_blather | |
| 148 | + | Snikket.const_set(:BLATHER, mock) | |
| 149 | + | yield | |
| 150 | + | ensure | |
| 151 | + | if had_blather | |
| 152 | + | Snikket.const_set(:BLATHER, old_blather) | |
| 153 | + | else | |
| 154 | + | Snikket.send(:remove_const, :BLATHER) | |
| 155 | + | end | |
| 156 | + | end | |
| 157 | + | ||
| 158 | + | def instance | |
| 159 | + | Snikket::CustomerInstance.new( | |
| 160 | + | instance_id: "si-alerts", | |
| 161 | + | bootstrap_token: "SI_SECRET_TOKEN_ALERTS", | |
| 162 | + | customer_id: "alerts-test", | |
| 163 | + | domain: "alerts.example.com" | |
| 164 | + | ) | |
| 165 | + | end | |
| 166 | + | ||
| 167 | + | def test_notify_admin_sends_once_per_lock_key | |
| 168 | + | blather = Minitest::Mock.new | |
| 169 | + | blather.expect(:join, nil, [CONFIG[:notify_admin], "sgx-jmp"]) | |
| 170 | + | blather.expect( | |
| 171 | + | :say, | |
| 172 | + | nil, | |
| 173 | + | [ | |
| 174 | + | CONFIG[:notify_admin], | |
| 175 | + | Matching.new { |m| m.include?("test alert") }, | |
| 176 | + | :groupchat | |
| 177 | + | ] | |
| 178 | + | ) | |
| 179 | + | ||
| 180 | + | with_blather(blather) do | |
| 181 | + | Snikket.notify_admin("test alert", key: "test").sync | |
| 182 | + | Snikket.notify_admin("test alert", key: "test").sync | |
| 183 | + | end | |
| 184 | + | ||
| 185 | + | assert_mock blather | |
| 186 | + | end | |
| 187 | + | em :test_notify_admin_sends_once_per_lock_key | |
| 188 | + | ||
| 189 | + | def test_alert_if_launch_timed_out | |
| 190 | + | # The launch marker expires after LAUNCH_TIMEOUT, so its absence means | |
| 191 | + | # the instance has timed out. | |
| 192 | + | blather = Minitest::Mock.new | |
| 193 | + | blather.expect(:join, nil, [CONFIG[:notify_admin], "sgx-jmp"]) | |
| 194 | + | blather.expect( | |
| 195 | + | :say, | |
| 196 | + | nil, | |
| 197 | + | [ | |
| 198 | + | CONFIG[:notify_admin], | |
| 199 | + | Matching.new { |m| m.include?("still not launched") }, | |
| 200 | + | :groupchat | |
| 201 | + | ] | |
| 202 | + | ) | |
| 203 | + | ||
| 204 | + | with_blather(blather) { instance.alert_if_launch_timed_out.sync } | |
| 205 | + | ||
| 206 | + | assert_mock blather | |
| 207 | + | end | |
| 208 | + | em :test_alert_if_launch_timed_out | |
| 209 | + | ||
| 210 | + | def test_no_alert_if_launch_not_timed_out | |
| 211 | + | Snikket::REDIS.set( | |
| 212 | + | "jmp_snikket_launch-si-alerts", Time.now.to_i | |
| 213 | + | ).sync | |
| 214 | + | ||
| 215 | + | with_blather(Minitest::Mock.new) do | |
| 216 | + | instance.alert_if_launch_timed_out.sync | |
| 217 | + | end | |
| 218 | + | end | |
| 219 | + | em :test_no_alert_if_launch_not_timed_out | |
| 220 | + | end |