module StackCommands extend self def self.result_codes { stack_rolled_back: 1, unkown_status: 2, timeout: 3, error: 5 } end def self.result_code(code) result_codes.fetch(code) end def sync_stack_proc lambda do |out, stack, mongo| # 5 tries each 5 seconds, then 200 tries each 10 seconds sleep_times = [5]*5 + [10]*200 begin out << "Syncing stack '#{stack.id}'...\n" sleep_times.each do |sleep_time| sleep sleep_time stack.sync_details! out.puts stack.events.inspect case stack.stack_status when 'CREATE_IN_PROGRESS' out << "." out.flush when 'CREATE_COMPLETE' mongo.stack_update(stack) out << "\nStack '#{stack.id}' status is now #{stack.stack_status}\n" out.flush return 0 when 'ROLLBACK_COMPLETE' out << "\nStack '#{stack.id}' status is rolled back\n" return StackCommands.result_code(:stack_rolled_back) else out.puts "\nUnknown stack status: '#{stack.stack_status}'" return StackCommands.result_code(:unkown_status) end end out.puts "Stack hasn't synced in #{sleep_times.inject(&:+)} seconds." return StackCommands.result_code(:timeout) rescue StandardError => e logger.error e.message out << "Error: #{e.message}\n" return StackCommands.result_code(:error) end end end end