2019-04-04 23:07:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
class EnsureS3UploadsExistence < ::Jobs::Scheduled
|
2019-04-04 23:07:44 +08:00
|
|
|
every 1.day
|
|
|
|
|
2020-09-11 03:37:42 +08:00
|
|
|
WAIT_AFTER_RESTORE_HOURS = 48
|
|
|
|
|
2020-07-29 08:49:45 +08:00
|
|
|
def perform(*args)
|
|
|
|
super
|
|
|
|
ensure
|
|
|
|
if @db_inventories
|
|
|
|
@db_inventories.values.each { |f| f.close; f.unlink }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-29 19:23:04 +08:00
|
|
|
def s3_helper
|
|
|
|
Discourse.store.s3_helper
|
|
|
|
end
|
|
|
|
|
2020-07-29 08:49:45 +08:00
|
|
|
def prepare_for_all_sites
|
|
|
|
inventory = S3Inventory.new(s3_helper, :upload)
|
|
|
|
@db_inventories = inventory.prepare_for_all_sites
|
|
|
|
@inventory_date = inventory.inventory_date
|
|
|
|
end
|
|
|
|
|
2019-04-04 23:07:44 +08:00
|
|
|
def execute(args)
|
2020-09-11 03:37:42 +08:00
|
|
|
return if !executable?
|
2020-07-29 08:49:45 +08:00
|
|
|
require 's3_inventory'
|
|
|
|
|
|
|
|
if !@db_inventories && Rails.configuration.multisite && GlobalSetting.use_s3?
|
|
|
|
prepare_for_all_sites
|
|
|
|
end
|
|
|
|
|
|
|
|
if @db_inventories && preloaded_inventory_file = @db_inventories[RailsMultisite::ConnectionManagement.current_db]
|
|
|
|
S3Inventory.new(
|
|
|
|
s3_helper,
|
|
|
|
:upload,
|
|
|
|
preloaded_inventory_file: preloaded_inventory_file,
|
|
|
|
preloaded_inventory_date: @inventory_date
|
|
|
|
).backfill_etags_and_list_missing
|
|
|
|
else
|
|
|
|
S3Inventory.new(s3_helper, :upload).backfill_etags_and_list_missing
|
|
|
|
end
|
2019-04-04 23:07:44 +08:00
|
|
|
end
|
2020-09-11 03:37:42 +08:00
|
|
|
|
|
|
|
def executable?
|
|
|
|
return false if !SiteSetting.enable_s3_inventory
|
|
|
|
|
|
|
|
if last_restore_date = BackupMetadata.last_restore_date
|
|
|
|
return false if last_restore_date > WAIT_AFTER_RESTORE_HOURS.hours.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2019-04-04 23:07:44 +08:00
|
|
|
end
|
|
|
|
end
|