Skip to content

Commit

Permalink
Add a maximum for mails to keep
Browse files Browse the repository at this point in the history
inspired by (stolen from) sj26#375
  • Loading branch information
billkirtley committed Nov 17, 2019
1 parent 76b855b commit bcf2180
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 @@ -129,6 +130,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
10 changes: 10 additions & 0 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def add_message(message)
cid = part.cid if part.respond_to? :cid
add_message_part(message_id, cid, part.mime_type || "text/plain", part.attachment? ? 1 : 0, part.filename, part.charset, body, body.length)
end
delete_older_messages!

EventMachine.next_tick do
message = MailCatcher::Mail.message message_id
Expand Down Expand Up @@ -165,4 +166,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 ?)"
@delete_older_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE message_id NOT IN (SELECT id FROM message ORDER BY created_at DESC LIMIT ?)"

@delete_older_message_parts_query.execute(max_mail_to_keep) and
@delete_older_messages_query.execute(max_mail_to_keep)
end
end

0 comments on commit bcf2180

Please sign in to comment.