From 4e05f0c12182f476c5ec3b4fd0d954ae779f6e5d Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Tue, 5 Oct 2021 17:18:31 +0200 Subject: [PATCH] Fixes #3787 - UploadCacheCleanupJob does not execute. --- app/jobs/upload_cache_cleanup_job.rb | 3 +- .../20211005110047_issue3787_fix_job.rb | 11 ++++ spec/jobs/upload_cache_cleanup_job_spec.rb | 66 +++++++++++-------- 3 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20211005110047_issue3787_fix_job.rb diff --git a/app/jobs/upload_cache_cleanup_job.rb b/app/jobs/upload_cache_cleanup_job.rb index b4c9431b7..819432c9a 100644 --- a/app/jobs/upload_cache_cleanup_job.rb +++ b/app/jobs/upload_cache_cleanup_job.rb @@ -3,6 +3,7 @@ class UploadCacheCleanupJob < ApplicationJob def perform 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.remove_item(store.id) @@ -12,6 +13,6 @@ class UploadCacheCleanupJob < ApplicationJob private def store_object_id - Store::Object.lookup(name: 'UploadCache').id + Store::Object.lookup(name: 'UploadCache')&.id end end diff --git a/db/migrate/20211005110047_issue3787_fix_job.rb b/db/migrate/20211005110047_issue3787_fix_job.rb new file mode 100644 index 000000000..6f4897915 --- /dev/null +++ b/db/migrate/20211005110047_issue3787_fix_job.rb @@ -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 diff --git a/spec/jobs/upload_cache_cleanup_job_spec.rb b/spec/jobs/upload_cache_cleanup_job_spec.rb index 5b701e8e7..95512472b 100644 --- a/spec/jobs/upload_cache_cleanup_job_spec.rb +++ b/spec/jobs/upload_cache_cleanup_job_spec.rb @@ -3,45 +3,53 @@ require 'rails_helper' 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 - UserInfo.current_user_id = 1 + before do + 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( - data: 'hello world', - filename: 'some.txt', + 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( + data: 'hello world', + filename: 'some.txt', + preferences: { + 'Content-Type' => 'text/plain', + }, + ) + end + + travel_back 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 - it 'cleanup the store items which are expired with job' do - expect { described_class.perform_now }.to change(Store, :count).by(-3) + context 'when upload cache does not exist' do + it 'does not crash' do + expect { described_class.perform_now }.not_to raise_error + end end end