pj migration: handle signal interrupts

This commit is contained in:
Pierre de La Morinerie 2019-05-29 13:06:36 +02:00
parent 1a256b37f8
commit 6cb02f2927
2 changed files with 18 additions and 4 deletions

View file

@ -64,7 +64,8 @@ class PieceJustificativeToChampPieceJointeMigrationService
yield if block_given?
end
end
rescue
rescue StandardError, SignalException
# If anything goes wrong, we roll back the migration by destroying the newly created
# types de champ, champs blobs and attachments.
types_de_champ_pj.each do |type_champ|
@ -95,7 +96,7 @@ class PieceJustificativeToChampPieceJointeMigrationService
# that one exists now. We do this so that, if we need to roll back and destroy the champ,
# the blob, the attachment and the actual file on OpenStack also get deleted.
champ.reload
rescue
rescue StandardError, SignalException
# Destroy partially attached object that the more general rescue in `populate_champs_pjs!`
# might not be able to handle.

View file

@ -182,6 +182,7 @@ describe PieceJustificativeToChampPieceJointeMigrationService do
context 'cleanup when conversion fails' do
let(:pjs) { make_pjs }
let(:exception) { 'LOL no!' }
let!(:failing_dossier) do
create(
@ -197,14 +198,14 @@ describe PieceJustificativeToChampPieceJointeMigrationService do
expect(storage_service).to receive(:copy_from_carrierwave_to_active_storage!)
expect(storage_service).to receive(:copy_from_carrierwave_to_active_storage!)
.and_raise('LOL no!')
.and_raise(exception)
expect(storage_service).to receive(:delete_from_active_storage!)
end
def try_convert(procedure)
service.convert_procedure_pjs_to_champ_pjs(procedure)
rescue => e
rescue StandardError, SignalException => e
e
end
@ -242,5 +243,17 @@ describe PieceJustificativeToChampPieceJointeMigrationService do
expect { try_convert(procedure) }
.not_to change { ActiveStorage::Attachment.count }
end
context 'when receiving a Signal interruption (like Ctrl+C)' do
let(:exception) { Interrupt }
it 'handles the exception as well' do
expect { service.convert_procedure_pjs_to_champ_pjs(procedure) }.to raise_error { Interrupt }
end
it 'does not create champs' do
expect { try_convert(procedure) }.not_to change { dossier.champs.count }
end
end
end
end