sgx-jmp

created pr with sgx-jmp/notify-snikket-lauch-fail.1 on 2026-11-18T20:30:19Z · by 9e834e0d
commented on 2026-11-18T21:21:12Z · by 3c31bc8c
lgtm
cmds
checkout latest patchset:
ssh pr.pico.sh pull sgx-jmp:notify-snikket-lauch-fail | git am -3
checkout specific patchset revision:
ssh pr.pico.sh pull sgx-jmp:notify-snikket-lauch-fail [rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh sgx-jmp:notify-snikket-lauch-fail
add comment to patch request:
ssh pr.pico.sh comment sgx-jmp:notify-snikket-lauch-fail "lgtm!"

Patchset sgx-jmp/notify-snikket-lauch-fail.1 on 2026-11-18T20:30:19Z · commit 3d9d2e3

Notify admin if snikket launch fails
Stephen Paul Weber 2026-11-18T20:29:44Z
Or if it still hasn't suceeded within 5 minutes
Diff stat
lib/snikket.rb | 71 +++++++++++++++++++++++++-
test/test_registration.rb | 71 ++++++++++++++++++++++---
test/test_snikket.rb | 84 ++++++++++++++++++++++++++++++
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
879879 end
880880
881881 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|
885887 save_instance_and_wait(domain, launched)
886888 }.catch { |e|
887889 next EMPromise.reject(e) unless e.respond_to?(:text)
+70 -1 lib/snikket.rb #
......@@ -8,7 +8,52 @@ require "link-header-parser"
88 require "uri"
99 require "value_semantics/monkey_patched"
1010
11+require_relative "em"
12+require_relative "expiring_lock"
13+
1114 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+
1257 class Launch < Blather::Stanza::Iq
1358 register nil, "launch", "xmpp:snikket.org/hosting/v1"
1459
......@@ -331,7 +376,31 @@ module Snikket
331376 end
332377
333378 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+ }
335404 end
336405 end
337406
+8 -1 test/test_helper.rb #
......@@ -98,6 +98,7 @@ CONFIG = {
9898 }
9999 },
100100 notify_from: "notify_from@example.org",
101+ notify_admin: "notify_admin@example.org",
101102 activation_amount: 1,
102103 activation_amount_accept: 1,
103104 plans: [
......@@ -238,6 +239,10 @@ end
238239
239240 BLATHER = Class.new {
240241 def <<(*); end
242+
243+ def join(*); end
244+
245+ def say(*); end
241246 }.new.freeze
242247
243248 def execute_command(
......@@ -316,7 +321,9 @@ class FakeRedis
316321 @values = values
317322 end
318323
319- def set(key, value)
324+ def set(key, value, *args)
325+ return EMPromise.resolve(nil) if args.include?("NX") && @values.key?(key)
326+
320327 @values[key] = value
321328 EMPromise.resolve("OK")
322329 end
+62 -9 test/test_registration.rb #
......@@ -3,12 +3,15 @@
33 require "test_helper"
44 require "customer"
55 require "registration"
6+require "snikket"
67 require "bwmsgsv2_repo"
78 require "transaction"
89 require "sim_order"
910 require "statsd"
1011
1112 BandwidthTnReservationRepo::REDIS = FakeRedis.new
13+Snikket::REDIS = FakeRedis.new
14+Snikket::IQ_MANAGER = Minitest::Mock.new
1215
1316 class RegistrationTest < Minitest::Test
1417 def teardown
......@@ -2154,9 +2157,9 @@ class RegistrationTest < Minitest::Test
21542157
21552158 class SnikketTest < Minitest::Test
21562159 Command::COMMAND_MANAGER = Minitest::Mock.new
2157- Registration::FinishOnboarding::Snikket::IQ_MANAGER = Minitest::Mock.new
21582160
21592161 def setup
2162+ Snikket::REDIS.reset!
21602163 @sgx = Minitest::Mock.new(mksgx)
21612164 @snikket = Registration::FinishOnboarding::Snikket.new(
21622165 customer(sgx: @sgx),
......@@ -2216,7 +2219,7 @@ class RegistrationTest < Minitest::Test
22162219 end]
22172220 )
22182221
2219- Registration::FinishOnboarding::Snikket::IQ_MANAGER.expect(
2222+ Snikket::IQ_MANAGER.expect(
22202223 :write,
22212224 EMPromise.resolve(launched),
22222225 [Matching.new do |iq|
......@@ -2246,7 +2249,7 @@ class RegistrationTest < Minitest::Test
22462249 end
22472250
22482251 assert_mock Command::COMMAND_MANAGER
2249- assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER
2252+ assert_mock Snikket::IQ_MANAGER
22502253 assert_mock blather
22512254 end
22522255 em :test_write
......@@ -2287,7 +2290,7 @@ class RegistrationTest < Minitest::Test
22872290 end]
22882291 )
22892292
2290- Registration::FinishOnboarding::Snikket::IQ_MANAGER.expect(
2293+ Snikket::IQ_MANAGER.expect(
22912294 :write,
22922295 EMPromise.resolve(launched),
22932296 [Matching.new do |iq|
......@@ -2322,7 +2325,7 @@ class RegistrationTest < Minitest::Test
23222325
23232326 assert_equal :test_result, result
23242327 assert_mock Command::COMMAND_MANAGER
2325- assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER
2328+ assert_mock Snikket::IQ_MANAGER
23262329 end
23272330 em :test_write_not_yet
23282331
......@@ -2348,7 +2351,7 @@ class RegistrationTest < Minitest::Test
23482351 end]
23492352 )
23502353
2351- Registration::FinishOnboarding::Snikket::IQ_MANAGER.expect(
2354+ Snikket::IQ_MANAGER.expect(
23522355 :write,
23532356 EMPromise.reject(launched),
23542357 [Matching.new do |iq|
......@@ -2385,9 +2388,59 @@ class RegistrationTest < Minitest::Test
23852388
23862389 assert_equal :test_result, result
23872390 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
23892395 end
23902396 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
23912444 end
23922445
23932446 class SnikketCustomDomainTest < Minitest::Test
......@@ -2462,7 +2515,7 @@ class RegistrationTest < Minitest::Test
24622515 end]
24632516 )
24642517
2465- Registration::FinishOnboarding::Snikket::IQ_MANAGER.expect(
2518+ Snikket::IQ_MANAGER.expect(
24662519 :write,
24672520 EMPromise.resolve(launched),
24682521 [Matching.new do |iq|
......@@ -2505,7 +2558,7 @@ class RegistrationTest < Minitest::Test
25052558
25062559 assert_equal :test_result, result
25072560 assert_mock Command::COMMAND_MANAGER
2508- assert_mock Registration::FinishOnboarding::Snikket::IQ_MANAGER
2561+ assert_mock Snikket::IQ_MANAGER
25092562 end
25102563 em :test_write_needs_dns
25112564 end
+84 -0 test/test_snikket.rb #
......@@ -3,6 +3,8 @@
33 require "snikket"
44 require "openssl"
55
6+Snikket::REDIS = FakeRedis.new
7+
68 class TestSnikket < Minitest::Test
79 NS = "xmpp:snikket.org/hosting/v1"
810
......@@ -134,3 +136,85 @@ class TestSnikket < Minitest::Test
134136 end
135137 em :test_invite_repo_fetch_returns_xmpp_alternate_link
136138 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
Back to top