Skip to content

Commit

Permalink
Add task to backfill log downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatas committed Nov 18, 2024
1 parent 0cde453 commit 81eb03d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Maintenance
# This task is used to backfill log downloads from log tickets.
# It is used to migrate from past to present using created_at date to order
# limit 500 per iteration and use latest created_at date to get the next 500
# later union with pending tickets.
class BackfillLogDownloadsFromLogTicketsTask < MaintenanceTasks::Task
def collection
# migrate from past to present using created_at date to order
# limit 500 per iteration and use latest created_at date to get the next 500
# later union with pending tickets
scope = LogTicket.processed.order(created_at: :asc)
last_created_at = LogDownload.latest_created_at
scope = scope.where("created_at < ?", last_created_at) if last_created_at
scope
.limit(500)
.union(LogTicket.pending.order(created_at: :asc).limit(500))
end

def process(batch)
LogDownload.insert_all(batch.select(:id, :status, :directory, :key, :created_at).to_a.map(&:attributes))
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "test_helper"

module Maintenance
class BackfillLogDownloadsFromLogTicketsTaskTest < ActiveSupport::TestCase
setup do
# create a list of log ticket statuses
@log_ticket_statuses = %w[pending processed]
@log_ticket_statuses.each do |status|
3.times { create(:log_ticket, status: status) }
end
end

test "#process performs a task iteration" do
assert_equal LogTicket.count, 6
assert_equal LogDownload.count, 0
Maintenance::BackfillLogDownloadsFromLogTicketsTask.process(LogTicket.all)
assert_equal LogDownload.count, 6
end
end
end

0 comments on commit 81eb03d

Please sign in to comment.