From befab460a506468778a80232eef7942944b3546a Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 24 Jul 2015 12:08:54 +0200 Subject: [PATCH] Made article customer user creation thread safe. --- lib/import/otrs.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index c1960334f..bf91a64d4 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -1300,17 +1300,27 @@ module Import::OTRS display_name = Mail::Field.new( 'X-From', display_name ).to_s roles = Role.lookup( name: 'Customer' ) - user = User.create( - login: email, - firstname: display_name, - lastname: '', - email: email, - password: '', - active: true, - role_ids: [roles.id], - updated_by_id: 1, - created_by_id: 1, - ) + begin + user = User.create( + login: email, + firstname: display_name, + lastname: '', + email: email, + password: '', + active: true, + role_ids: [roles.id], + updated_by_id: 1, + created_by_id: 1, + ) + rescue ActiveRecord::RecordNotUnique + log "User #{email} was handled by another thread, taking this." + user = User.find_by( login: email ) + if !user + log "User #{email} wasn't created sleep and retry." + sleep rand 3 + retry + end + end end article['created_by_id'] = user.id