-
-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
app/tasks/maintenance/backfill_log_downloads_from_log_tickets_task.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
22 changes: 22 additions & 0 deletions
22
test/tasks/maintenance/backfill_log_downloads_from_log_tickets_task_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |