forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintenance: Improved storage of unprocessable / oversized emails.
- Loading branch information
Showing
11 changed files
with
188 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
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,21 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class RelocateUnprocessableMails < ActiveRecord::Migration[6.1] | ||
def change | ||
# return if it's a new setup | ||
return if !Setting.exists?(name: 'system_init_done') | ||
|
||
relocate_files('unprocessable_mail') | ||
relocate_files('oversized_mail') | ||
end | ||
|
||
def relocate_files(type) | ||
old_dir = Rails.root.join('tmp', type) | ||
return if !old_dir.exist? || old_dir.children.empty? | ||
|
||
new_dir = Rails.root.join('var/spool', type) | ||
FileUtils.mkdir_p(new_dir) | ||
FileUtils.cp_r(old_dir.children, new_dir) | ||
FileUtils.rm_r(old_dir) | ||
end | ||
end |
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
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,87 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
require 'rails_helper' | ||
|
||
UNPROCESSABLE_DIR_OLD = Rails.root.join('tmp/unprocessable_mail') | ||
UNPROCESSABLE_DIR_NEW = Rails.root.join('var/spool/unprocessable_mail') | ||
OVERSIZED_DIR_OLD = Rails.root.join('tmp/oversized_mail') | ||
OVERSIZED_DIR_NEW = Rails.root.join('var/spool/oversized_mail') | ||
OLD_DIRS = [UNPROCESSABLE_DIR_OLD, OVERSIZED_DIR_OLD].freeze | ||
NEW_DIRS = [UNPROCESSABLE_DIR_NEW, OVERSIZED_DIR_NEW].freeze | ||
DIRS = [UNPROCESSABLE_DIR_OLD, UNPROCESSABLE_DIR_NEW, OVERSIZED_DIR_OLD, OVERSIZED_DIR_NEW].freeze | ||
|
||
RSpec.describe RelocateUnprocessableMails, :aggregate_failures, type: :db_migration do | ||
|
||
def remove_all_directories | ||
DIRS.each { |dir| FileUtils.rm_r(dir) if File.exist?(dir) } | ||
end | ||
|
||
def create_old_directories | ||
OLD_DIRS.each { |dir| FileUtils.mkdir_p(dir) } | ||
end | ||
|
||
def create_all_directories | ||
DIRS.each { |dir| FileUtils.mkdir_p(dir) } | ||
end | ||
|
||
before do | ||
remove_all_directories | ||
end | ||
|
||
after do | ||
remove_all_directories | ||
end | ||
|
||
context 'when migrating' do | ||
context 'without unprocessable mail directory' do | ||
it 'does nothing' do | ||
migrate | ||
DIRS.each { |dir| expect(dir).not_to exist } | ||
end | ||
end | ||
|
||
context 'with empty unprocessable mail directory' do | ||
before do | ||
create_old_directories | ||
end | ||
|
||
it 'does nothing' do | ||
migrate | ||
expect(OLD_DIRS).to all(exist) | ||
NEW_DIRS.each { |dir| expect(dir).not_to exist } | ||
end | ||
end | ||
|
||
context 'with unprocessable mails' do | ||
before do | ||
create_all_directories | ||
%w[unprocessable_mail oversized_mail].each do |type| | ||
files = [ | ||
"tmp/#{type}/1.eml", | ||
"tmp/#{type}/2.eml", | ||
"var/spool/#{type}/2.eml", | ||
"var/spool/#{type}/3.eml", | ||
] | ||
FileUtils.touch(files.map { |f| Rails.root.join(f) }) | ||
end | ||
end | ||
|
||
it 'removes source directories' do | ||
migrate | ||
[UNPROCESSABLE_DIR_OLD, OVERSIZED_DIR_OLD].each { |dir| expect(dir).not_to exist } | ||
end | ||
|
||
it 'migrates files and keeps existing' do | ||
migrate | ||
%w[unprocessable_mail oversized_mail].each do |type| | ||
files = [ | ||
"var/spool/#{type}/1.eml", | ||
"var/spool/#{type}/2.eml", | ||
"var/spool/#{type}/3.eml", | ||
] | ||
files.each { |f| expect(Pathname.new(f)).to exist } | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
43 changes: 43 additions & 0 deletions
43
spec/models/channel/email_parser/process_unprocessable_mail_spec.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,43 @@ | ||
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Channel::EmailParser#process_unprocessable_mail', aggregate_failures: true, type: :model do | ||
|
||
context 'when receiving unprocessable mail' do | ||
let(:mail) do | ||
From: ME Bob <[email protected]> | ||
To: [email protected] | ||
Subject: some subject | ||
Some Text | ||
end | ||
let(:dir) { Channel::EmailParser::UNPROCESSABLE_MAIL_DIRECTORY } | ||
|
||
before do | ||
FileUtils.rm_r(dir) if dir.exist? | ||
stub_const('Channel::EmailParser::PROZESS_TIME_MAX', -1) | ||
begin | ||
Channel::EmailParser.new.process({}, mail) | ||
rescue RuntimeError | ||
# expected | ||
end | ||
end | ||
|
||
after do | ||
FileUtils.rm_r(dir) if dir.exist? | ||
end | ||
|
||
it 'saves the unprocessable email into a file' do | ||
expect(dir.join('ce61e7319bcc4297c1d7dfea2fbc87dd.eml')).to exist | ||
end | ||
|
||
it 'allows reprocessing of the stored email' do | ||
stub_const('Channel::EmailParser::PROZESS_TIME_MAX', nil) | ||
expect { Channel::EmailParser.process_unprocessable_mails }.to change(Ticket, :count).by(1) | ||
expect(dir.join('ce61e7319bcc4297c1d7dfea2fbc87dd.eml')).not_to exist | ||
end | ||
end | ||
end |
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,2 @@ | ||
This folder holds volatile data and may thus be mounted on volatile file systems | ||
like tmpfs / ramdisk. It does not need to be shared in cluster setups. |
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,6 @@ | ||
This folder stores persistent application data like unprocessable emails, | ||
and must be shared in cluster setups to be available for all nodes. | ||
|
||
Possible subfolders (depending on system configuration and usage): | ||
- `var/spool/unprocessable_mail` - stores emails that could not properly be imported | ||
- `var/spool/oversized_mail` - stores emails that were rejected as too big |