Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer the possibility to keep a finite number of mails #375

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def log_exception(message, context, exception)
:http_ip => "127.0.0.1",
:http_port => "1080",
:http_path => "/",
:max_mail_to_keep => -1,
:verbose => false,
:daemon => !windows?,
:browse => false,
Expand Down Expand Up @@ -126,6 +127,10 @@ def parse! arguments=ARGV, defaults=@defaults
options[:http_port] = port
end

parser.on("--max_mail_to_keep NUM", Integer, "Set the maximum number of mails to keep") do |num|
options[:max_mail_to_keep] = num
end

parser.on("--http-path PATH", String, "Add a prefix to all HTTP paths") do |path|
clean_path = Rack::Utils.clean_path_info("/#{path}")

Expand Down
9 changes: 9 additions & 0 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ def delete_message!(message_id)
@delete_messages_query.execute(message_id) and
@delete_message_parts_query.execute(message_id)
end

def delete_older_messages!(max_mail_to_keep = MailCatcher::options[:max_mail_to_keep])
return if max_mail_to_keep <= 0
@delete_older_messages_query ||= db.prepare "DELETE FROM message WHERE id NOT IN (SELECT id FROM message ORDER BY created_at DESC LIMIT ?)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since id is the primary key, it has an index. Can we ORDER BY id?
Also, dealing with just a single indexed column, most databases run much quicker, avoiding hitting the data page altogether.
It may be just a marginal savings though. So feel free to ignore

@delete_older_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE id NOT IN (SELECT id FROM message_part ORDER BY created_at DESC LIMIT ?)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the right records getting deleted here?
Should this delete the same rows as the previous line?

DELETE from message_part
WHERE message_id NOT IN (SELECT id from message ORDER BY created_at DESC LIMIT ?)

(or ORDER BY id if the previous ORDER BY change was made)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I change this :).


@delete_older_messages_query.execute(max_mail_to_keep) and
@delete_older_message_parts_query.execute(max_mail_to_keep)
end
end
1 change: 1 addition & 0 deletions lib/mail_catcher/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def receive_data_chunk(lines)

def receive_message
MailCatcher::Mail.add_message current_message
MailCatcher::Mail.delete_older_messages!
puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)"
true
rescue => exception
Expand Down