diff --git a/app/services/reservation_manager.rb b/app/services/reservation_manager.rb index 3528a0388..a6f3be821 100644 --- a/app/services/reservation_manager.rb +++ b/app/services/reservation_manager.rb @@ -10,8 +10,13 @@ def initialize(reservation) end def start_reservation - reservation.reservation_statuses.create!(status: 'Starting') - manage_reservation(:start) + if previous_reservation_ended_fully? + reservation.reservation_statuses.create!(status: 'Starting') + manage_reservation(:start) + else + reservation.update_attribute(:start_instantly, false) + reservation.reservation_statuses.create!(status: 'Waiting for other reservation on server to end fully') + end end def end_reservation @@ -28,4 +33,10 @@ def update_reservation def manage_reservation(action) ReservationWorker.perform_async(reservation.id, action.to_s) end + + private + + def previous_reservation_ended_fully? + Reservation.where.not(id: reservation.id).where(server_id: reservation.server_id, ended: false).where('reservations.ends_at > ?', 15.minutes.ago).none? + end end diff --git a/spec/models/reservation_spec.rb b/spec/models/reservation_spec.rb index b676267a7..83ac04b22 100644 --- a/spec/models/reservation_spec.rb +++ b/spec/models/reservation_spec.rb @@ -222,6 +222,16 @@ ReservationWorker.should_receive(:perform_async).with(reservation.id, 'start') reservation.start_reservation end + + it 'should wait if a previous reservation is not fully ended' do + previous_reservation = create(:reservation) + previous_reservation.update_attribute(:server_id, reservation.server_id) + previous_reservation.update_attribute(:ends_at, 1.minute.ago) + + reservation.start_reservation + + expect(reservation.reservation_statuses.map(&:status)).to include 'Waiting for other reservation on server to end fully' + end end describe '#update_reservation' do