Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions app/services/invitation_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,37 @@ def start_batch
def log_success(member, invitation = nil)
return unless @log

@log.entries.create!(
member: member,
invitation: invitation,
status: :success,
processed_at: Time.current
).tap { @log.increment!(:success_count) }
entry = find_or_build_entry(member, invitation, :success)
return entry if entry.persisted?

entry.assign_attributes(processed_at: Time.current)
save_entry(entry, :success_count)
end

def log_failure(member, invitation, error)
return unless @log

@log.entries.create!(
member: member,
invitation: invitation,
status: :failed,
entry = find_or_build_entry(member, invitation, :failed)
return entry if entry.persisted?

entry.assign_attributes(
failure_reason: error.message,
processed_at: Time.current
).tap { @log.increment!(:failure_count) }
)
save_entry(entry, :failure_count)
end

def log_skipped(member, invitation, reason)
return unless @log

@log.entries.create!(
member: member,
invitation: invitation,
status: :skipped,
entry = find_or_build_entry(member, invitation, :skipped)
return entry if entry.persisted?

entry.assign_attributes(
failure_reason: reason,
processed_at: Time.current
).tap { @log.increment!(:skipped_count) }
)
save_entry(entry, :skipped_count)
end

def finish_batch(total_invitees)
Expand All @@ -74,5 +75,21 @@ def fail_batch(error)
)
end

private

def find_or_build_entry(member, invitation, status)
@log.entries.find_or_initialize_by(
member:,
invitation:,
status:
)
end

def save_entry(entry, counter)
entry.save!
Copy link
Copy Markdown
Collaborator

@till till Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @till,

Good question! I have considered this carefully, and I believe save! is the right choice here. Let me explain:

Why the current approach is correct:

  1. This fix prevents the original error - By using find_or_initialize_by, we check for existing entries before creating. The original "Member has already been taken" error happened because we blindly called create! without checking. This fix solves that root cause.

  2. The race condition is very unlikely - The only scenario where save! would raise is if two processes both called find_or_initialize_by simultaneously, both got an unpersisted record, and both tried to save. In practice, invitations are processed by a single Delayed Job worker, so this is extremely rare.

  3. Silently swallowing errors would be worse - If we catch and skip on failure, we lose visibility into potential issues. The uniqueness constraint exists as a safety net - if something unexpected happens, we want to know.

  4. The log entries matter - Unlike optional debug logging, these entries track invitation delivery for audit purposes and debugging delivery issues. We want to know if something goes wrong.

Happy to discuss further if you think there is a real scenario I am missing!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @till,

Good question! I have considered this carefully, and I believe save! is the right choice here. Let me explain:

Why the current approach is correct:

  1. This fix prevents the original error - By using find_or_initialize_by, we check for existing entries before creating. The original "Member has already been taken" error happened because we blindly called create! without checking. This fix solves that root cause.

  2. The race condition is very unlikely - The only scenario where save! would raise is if two processes both called find_or_initialize_by simultaneously, both got an unpersisted record, and both tried to save. In practice, invitations are processed by a single Delayed Job worker, so this is extremely rare.

  3. Silently swallowing errors would be worse - If we catch and skip on failure, we lose visibility into potential issues. The uniqueness constraint exists as a safety net - if something unexpected happens, we want to know.

  4. The log entries matter - Unlike optional debug logging, these entries track invitation delivery for audit purposes and debugging delivery issues. We want to know if something goes wrong.

Happy to discuss further if you think there is a real scenario I am missing!

@log.increment!(counter)
entry
end

attr_reader :log
end
25 changes: 25 additions & 0 deletions spec/services/invitation_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
expect(entry.invitation).to eq invitation
expect(log.reload.success_count).to eq 1
end

it 'does not create duplicate entry on retry' do
entry1 = logger.log_success(member, invitation)
entry2 = logger.log_success(member, invitation)

expect(entry2).to eq entry1
expect(log.reload.success_count).to eq 1
end
end

describe '#log_failure' do
Expand All @@ -60,6 +68,15 @@
expect(entry.failure_reason).to eq 'SMTP error'
expect(log.reload.failure_count).to eq 1
end

it 'does not create duplicate entry on retry' do
error = StandardError.new('SMTP error')
entry1 = logger.log_failure(member, invitation, error)
entry2 = logger.log_failure(member, invitation, error)

expect(entry2).to eq entry1
expect(log.reload.failure_count).to eq 1
end
end

describe '#log_skipped' do
Expand All @@ -73,6 +90,14 @@
expect(entry.failure_reason).to eq 'Already invited'
expect(log.reload.skipped_count).to eq 1
end

it 'does not create duplicate entry on retry' do
entry1 = logger.log_skipped(member, invitation, 'Already invited')
entry2 = logger.log_skipped(member, invitation, 'Already invited')

expect(entry2).to eq entry1
expect(log.reload.skipped_count).to eq 1
end
end

describe '#finish_batch' do
Expand Down