Fixes #3787 - UploadCacheCleanupJob does not execute.

This commit is contained in:
Rolf Schmidt 2021-10-05 17:18:31 +02:00 committed by Thorsten Eckel
parent b33bca9910
commit 3df384c758
3 changed files with 50 additions and 30 deletions

View file

@ -3,6 +3,7 @@
class UploadCacheCleanupJob < ApplicationJob class UploadCacheCleanupJob < ApplicationJob
def perform def perform
taskbar_form_ids = Taskbar.with_form_id.filter_map(&:persisted_form_id) taskbar_form_ids = Taskbar.with_form_id.filter_map(&:persisted_form_id)
return if store_object_id.blank?
Store.where(store_object_id: store_object_id).where('created_at < ?', 1.month.ago).where.not(o_id: taskbar_form_ids).find_each do |store| Store.where(store_object_id: store_object_id).where('created_at < ?', 1.month.ago).where.not(o_id: taskbar_form_ids).find_each do |store|
Store.remove_item(store.id) Store.remove_item(store.id)
@ -12,6 +13,6 @@ class UploadCacheCleanupJob < ApplicationJob
private private
def store_object_id def store_object_id
Store::Object.lookup(name: 'UploadCache').id Store::Object.lookup(name: 'UploadCache')&.id
end end
end end

View file

@ -0,0 +1,11 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
class Issue3787FixJob < ActiveRecord::Migration[6.0]
def change
# return if it's a new setup
return if !Setting.exists?(name: 'system_init_done')
Scheduler.find_by(name: 'Delete old upload cache entries.').update(error_message: nil, status: nil, active: true)
end
end

View file

@ -3,45 +3,53 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe UploadCacheCleanupJob, type: :job do RSpec.describe UploadCacheCleanupJob, type: :job do
let(:upload_cache) { UploadCache.new(1337) } context 'when upload cache exists' do
let(:upload_cache) { UploadCache.new(1337) }
before do before do
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
upload_cache.add(
data: 'current example',
filename: 'current.txt',
preferences: {
'Content-Type' => 'text/plain',
},
)
travel_to 1.month.ago
# create one taskbar and related upload cache entry, which should not be deleted
create(:taskbar, state: { form_id: 9999 })
UploadCache.new(9999).add(
data: 'Some Example with related Taskbar',
filename: 'another_example_with_taskbar.txt',
preferences: {
'Content-Type' => 'text/plain',
}
)
3.times do
upload_cache.add( upload_cache.add(
data: 'hello world', data: 'current example',
filename: 'some.txt', filename: 'current.txt',
preferences: { preferences: {
'Content-Type' => 'text/plain', 'Content-Type' => 'text/plain',
}, },
) )
travel_to 1.month.ago
# create one taskbar and related upload cache entry, which should not be deleted
create(:taskbar, state: { form_id: 9999 })
UploadCache.new(9999).add(
data: 'Some Example with related Taskbar',
filename: 'another_example_with_taskbar.txt',
preferences: {
'Content-Type' => 'text/plain',
}
)
3.times do
upload_cache.add(
data: 'hello world',
filename: 'some.txt',
preferences: {
'Content-Type' => 'text/plain',
},
)
end
travel_back
end end
travel_back it 'cleanup the store items which are expired with job' do
expect { described_class.perform_now }.to change(Store, :count).by(-3)
end
end end
it 'cleanup the store items which are expired with job' do context 'when upload cache does not exist' do
expect { described_class.perform_now }.to change(Store, :count).by(-3) it 'does not crash' do
expect { described_class.perform_now }.not_to raise_error
end
end end
end end