Change summary
bin/cancel_expired_customers | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
Detailed changes
@@ -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