FIX: refactor calling of timed backup deletion

refactor calling of timed backup deletion so it runs regardless of SiteSetting.automatic_backups_enabled value
This commit is contained in:
marstall 2024-01-08 15:41:00 -05:00 committed by GitHub
parent c62d1197b9
commit 3837657449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 17 deletions

View File

@ -529,6 +529,7 @@ PLATFORMS
x86_64-darwin-18
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-22
x86_64-linux
DEPENDENCIES

View File

@ -6,6 +6,7 @@ module Jobs
sidekiq_options retry: false
def execute(args)
delete_prior_to_n_days
return unless SiteSetting.enable_backups? && SiteSetting.automatic_backups_enabled?
store = BackupRestore::BackupStore.create
@ -25,6 +26,10 @@ module Jobs
raise
end
def delete_prior_to_n_days
BackupRestore::Backuper.new(Discourse.system_user.id).delete_prior_to_n_days
end
def notify_user(ex)
SystemMessage.create_from_system_user(
Discourse.system_user,

View File

@ -51,13 +51,19 @@ module BackupRestore
@backup_filename
ensure
delete_old
delete_prior_to_n_days
clean_up
notify_user
log "Finished!"
publish_completion
end
def delete_prior_to_n_days
return if Rails.env.development?
store.delete_prior_to_n_days
rescue => ex
log "Something went wrong while deleting backups prior to n days....", ex
end
protected
def ensure_no_operation_is_running
@ -359,15 +365,6 @@ module BackupRestore
log "Something went wrong while deleting old backups.", ex
end
def delete_prior_to_n_days
return if Rails.env.development?
log "Deleting backups prior to n days..."
store.delete_prior_to_n_days
rescue => ex
log "Something went wrong while deleting backups prior to n days....", ex
end
def notify_user
return if success && @user.id == Discourse::SYSTEM_USER_ID

View File

@ -131,9 +131,13 @@ module BackupRestore
def unsorted_files
objects = []
s3_helper.list.each do |obj|
objects << create_file_from_object(obj) if obj.key.match?(file_regex)
end
begin
s3_helper.list.each do |obj|
objects << create_file_from_object(obj) if obj.key.match?(file_regex)
end
rescue StandardError
NoMethodError
end #fired when s3_helper.list is nil - wont respond to .nil?
objects
rescue Aws::Errors::ServiceError => e

View File

@ -87,10 +87,6 @@ RSpec.describe BackupRestore::Backuper do
store.expects(:reset_cache).at_least_once
run
end
it "deletes any old backups" do
store.expects(:delete_prior_to_n_days)
run
end
end
end
end

View File

@ -169,6 +169,24 @@ RSpec.shared_examples "backup store" do
store.delete_prior_to_n_days
expect(store.files).to eq([backup1])
end
it "runs if SiteSetting.automatic_backups_enabled? is true" do
stub_request(
:get,
"https://s3-backup-bucket.s3.amazonaws.com/?list-type=2&prefix=default/",
).to_return(status: 200, body: "", headers: {})
stub_request(:head, "https://s3-backup-bucket.s3.amazonaws.com/").to_return(
status: 200,
body: "",
headers: {
},
)
SiteSetting.automatic_backups_enabled = true
scheduleBackup = Jobs::ScheduleBackup.new
scheduleBackup.expects(:delete_prior_to_n_days)
scheduleBackup.perform
end
end
describe "#file" do