From 0b705c142646091618927e0499cfd35594bb3cc6 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Tue, 7 Oct 2025 16:57:12 -0400 Subject: [PATCH] stop snikket instances on when cancelling cust --- bin/cancel_expired_customers | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/bin/cancel_expired_customers b/bin/cancel_expired_customers index a044d6b2cf9717e61f1521875c561d26c717401a..7b130545bcc15d55ccd4e295d821a74e072c8002 100755 --- a/bin/cancel_expired_customers +++ b/bin/cancel_expired_customers @@ -128,13 +128,31 @@ class ExpiringCustomer end end +module SnikketInstanceManager + def self.stop_instances(instance_ids) + EMPromise.all(instance_ids.map { |id| stop_instance(id) }) + end + + def self.stop_instance(instance_id) + BlatherNotify.execute( + "stop snikket", + { instance_id: instance_id }.to_form(:submit) + ) + end +end + one = Queue.new ported_in_promise.then { |ported_in| EM::Iterator.new(db.exec( <<-SQL - SELECT customer_id, expires_at FROM customer_plans - WHERE expires_at < LOCALTIMESTAMP - INTERVAL '1 month' + SELECT customer_plans.customer_id, + customer_plans.expires_at, + array_agg(snikket_instances.instance_id) as instance_ids + FROM customer_plans + LEFT JOIN snikket_instances ON customer_plans.customer_id = snikket_instances.customer_id + WHERE customer_plans.expires_at < LOCALTIMESTAMP - INTERVAL '1 month' + GROUP BY customer_plans.customer_id, customer_plans.expires_at SQL ), 3).each(nil, -> { one << :done }) do |row, iter| customer = ExpiringCustomer.new(row["customer_id"]) @@ -150,6 +168,10 @@ ported_in_promise.then { |ported_in| customer.cancel_account }.then { |result| puts format(result) + + SnikketInstanceManager.stop_instances(row["instance_ids"]) + }.then { |stopped_instances| + puts "Stopped #{stopped_instances.length} Snikket instance(s)" iter.next }.catch do |err| next iter.next if err == :skip