title: Mailman saves Mails multiple times
body: <p>I've got the mailman gem integrated into my rails project. It fetches emails from gmail successfully. In my app there is a model Message for my emails. The emails are properly saved as Message model.</p>
  
  <p>The problem is that the emails are saved multiple times sometimes and I can't recognize a pattern. Some emails are saved once, some two times and some are saved three times.</p>
  
  <p>But I can't find the failure in my code.</p>
  
  <p>Here is my mailman_server script:</p>
  
  <p><strong>script/mailman_server</strong></p>
  
  <pre><code>#!/usr/bin/env ruby
  # encoding: UTF-8
  require "rubygems"
  require "bundler/setup"
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
  require 'mailman'
  
  Mailman.config.ignore_stdin = true
  #Mailman.config.logger = Logger.new File.expand_path("../../log/mailman_#{Rails.env}.log", __FILE__)
  
  if Rails.env == 'test'
    Mailman.config.maildir = File.expand_path("../../tmp/test_maildir", __FILE__)
  else
    Mailman.config.logger = Logger.new File.expand_path("../../log/mailman_#{Rails.env}.log", __FILE__)
    Mailman.config.poll_interval = 15
    Mailman.config.imap = {
      server: 'imap.gmail.com',
      port: 993,  # usually 995, 993 for gmail
      ssl: true,
      username: 'my@email.com',
      password: 'my_password'
    }
  end
  
  Mailman::Application.run do
    default do
      begin
        Message.receive_message(message)
      rescue Exception =&gt; e
        Mailman.logger.error "Exception occurred while receiving message:\n#{message}"
        Mailman.logger.error [e, *e.backtrace].join("\n")
      end
    end
  end
  </code></pre>
  
  <p>The email is processed inside my Message class:</p>
  
  <pre><code>  def self.receive_message(message)
      if message.from.first == "my@email.com"
        Message.save_bcc_mail(message)
      else
        Message.save_incoming_mail(message)
      end
    end
  
    def self.save_incoming_mail(message)
      part_to_use = message.html_part || message.text_part || message
      if Kontakt.where(:email =&gt; message.from.first).empty?
        encoding = part_to_use.content_type_parameters['charset']
        Message.create topic: message.subject, message: part_to_use.body.decoded.force_encoding(encoding).encode('UTF-8'), communication_partner: message.from.first, inbound: true, time: message.date
      else
        encoding = part_to_use.content_type_parameters['charset']
        Message.create topic: message.subject, message: part_to_use.body.decoded.force_encoding(encoding).encode('UTF-8'), communication_partner: message.from.first, inbound: true, time: message.date, messageable_type: 'Company', messageable_id: Kontakt.where(:email =&gt; message.from.first).first.year.id
      end
    end
  
    def self.save_bcc_mail(message)
      part_to_use = message.html_part || message.text_part || message
      if Kontakt.where(:email =&gt; message.to.first).empty?
        encoding = part_to_use.content_type_parameters['charset']
        Message.create topic: message.subject, message: part_to_use.body.decoded.force_encoding(encoding).encode('UTF-8'), communication_partner: message.to.first, inbound: false, time: message.date
      else
        encoding = part_to_use.content_type_parameters['charset']
        Message.create topic: message.subject, message: part_to_use.body.decoded.force_encoding(encoding).encode('UTF-8'), communication_partner: message.to.first, inbound: false, time: message.date, messageable_type: 'Company', messageable_id: Kontakt.where(:email =&gt; message.to.first).first.year.id
      end
    end
  </code></pre>
  
  <p>I have daemonized the mailman_server with this script:</p>
  
  <p><strong>script/mailman_daemon</strong></p>
  
  <pre><code>#!/usr/bin/env ruby
  
  require 'rubygems'  
  require "bundler/setup"  
  require 'daemons'
  
  Daemons.run('script/mailman_server') 
  </code></pre>
  
  <p>I deploy with capistrano.</p>
  
  <p>This are the parts which are responsible for stopping, starting and restarting my mailman_server:</p>
  
  <p>script/deploy.rb</p>
  
  <pre><code>set :rails_env, "production" #added for delayed job  
  after "deploy:stop",    "delayed_job:stop"
  after "deploy:start",   "delayed_job:start"
  after "deploy:restart", "delayed_job:restart"
  after "deploy:stop",    "mailman:stop"
  after "deploy:start",   "mailman:start"
  after "deploy:restart", "mailman:restart"
  
  namespace :deploy do
    desc "mailman script ausfuehrbar machen"
    task :mailman_executable, :roles =&gt; :app do
     run "chmod +x #{current_path}/script/mailman_server"
    end
  
    desc "mailman daemon ausfuehrbar machen"
    task :mailman_daemon_executable, :roles =&gt; :app do
     run "chmod +x #{current_path}/script/mailman_daemon"
    end
  end
  
  namespace :mailman do  
    desc "Mailman::Start"
    task :start, :roles =&gt; [:app] do
     run "cd #{current_path};RAILS_ENV=#{fetch(:rails_env)} bundle exec script/mailman_daemon start"
    end
  
    desc "Mailman::Stop"
    task :stop, :roles =&gt; [:app] do
     run "cd #{current_path};RAILS_ENV=#{fetch(:rails_env)} bundle exec script/mailman_daemon stop"
    end
  
    desc "Mailman::Restart"
    task :restart, :roles =&gt; [:app] do
     mailman.stop
     mailman.start
    end
  end
  </code></pre>
  
  <p>Could it be that multiple instances of the mailman server are started during my deploy at nearly the same time and then each instance polls nearly at the same time? The second and third instance pools before the first instance marks the email as read and polls and processes the email as well? </p>
  
  <p><strong>Update 30.01.</strong></p>
  
  <p>I had set the polling intervall to 60 seconds. but that changes nothing.</p>
  
  <p>I checked the folder where the mailman pid file is stored. there is only one mailman pid file. So there is definitely only one mailman server running. I checked the logfile and can see, that the messages are fetched multiple times:</p>
  
  <pre><code>Mailman v0.7.0 started
  IMAP receiver enabled (my@email.com).
  Polling enabled. Checking every 60 seconds.
  Got new message from 'my.other@email.com' with subject 'Test nr 0'.
  Got new message from 'my.other@email.com' with subject 'Test nr 1'.
  Got new message from 'my.other@email.com' with subject 'test nr 2'.
  Got new message from 'my.other@email.com' with subject 'test nr 2'.
  Got new message from 'my.other@email.com' with subject 'test nr 3'.
  Got new message from 'my.other@email.com' with subject 'test nr 4'.
  Got new message from 'my.other@email.com' with subject 'test nr 4'.
  Got new message from 'my.other@email.com' with subject 'test nr 4'.
  </code></pre>
  
  <p>So that seems to me, that the problem is definitely in my mailman server code.</p>
  
  <p><strong>Update 31.1.</strong></p>
  
  <p>Seems to me, that is has something to do with my production machine. when I'm testing this in development with the exact same configuration (changed my local database from sqlite to mysql this morning to test it) as on the production machine I don't get duplicates. Probably is everything ok with my code, but there is a problem with the production machine. Will ask my hoster if they could see a solution for this. To fix this I will go with Ariejan'S suggestion.</p>
  
  <p><strong>The solution:</strong>
  I found the problem. I deploy to a machine where the tmp directory is a shared one between all releases. I forgot to define the path where the pid file of the mailman_daemon should be saved. So it was saved in the script directory instead of the /tmp/pids directory. Because of this the old mailman_daemon could not be stopped after a new deploy. That had led to an army of working mailman_daemons which were polling my mailaccount... After killing all these processes all went well! No more duplicates!</p>