about summary refs log tree commit diff
path: root/app/workers/backup_worker.rb
blob: 7b0b52844d32a1872f3ae0d129643bc73d291be8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

class BackupWorker
  include Sidekiq::Worker

  sidekiq_options queue: 'pull', backtrace: true, retry: 5, dead: false

  sidekiq_retries_exhausted do |msg|
    backup_id = msg['args'].first

    ActiveRecord::Base.connection_pool.with_connection do
      begin
        backup = Backup.find(backup_id)
        backup.destroy
      rescue ActiveRecord::RecordNotFound
        true
      end
    end
  end

  def perform(backup_id)
    backup = Backup.find(backup_id)
    user   = backup.user

    BackupService.new.call(backup)

    user.backups.where.not(id: backup.id).destroy_all
    UserMailer.backup_ready(user, backup).deliver_later
  end
end