Search code examples
ruby-on-railscachingdelayed-jobmailing

Rails 4: Delayed job keep cache for email "from" header value


I am developing a mailing web application in Ruby on Rails and I am in front of an issue with delayed_job gem:

In the settings of my application, I give the ability for the customer to update the email address from where mailing are sent. But I discovered there was something like a cache from delayed_job which doesn’t use the update email address for the « from » header.

When I use the delayed_job task by Capistrano manually it works so I tried to add a callback after_update in my model to handle the restart of delayed_job but without any success.

Capistrano command:

cap <my_env> delayed_job:restart # this works but it’s a manual command so useless in my case

What I tried is to dynamically restart delayed_job from the model:

class Setting < ActiveRecord::Base
  after_save :restart_delayed_job

  def restart_delayed_job
    if email_changed?
      system "RAILS_ENV=#{Rails.env} do bundle exec bin/delayed_job -n 1 restart"
    end
  end
end

My Mailing class:

class MyMailer < ApplicationMailer
  default from: Setting.first.email # After updating the email value in setting, it still the old one used.
  # more code skipped
end

Does anyone knows how can I restart delayed_job from Rails ?
Is there a way to do it exclusively in Ruby without writing shell script ?
In order to help me to understand better, is there several instance of delayed_job (one by website in the server) or one for all website ?

Thanks for your help !

My project:
- Rails 4.2.5
- Ruby 2.2.2
- ActiveAdmin 1.0.0 pre2
- Delayed job 4.1.1
- Capistrano 3.4.0


Solution

  • The solution was to move the from header into the mail function. The reason is the default from is set only once when the application start and cannot be changed this way.

    mail(from: email, subject: subject, ...) do
      # skipped code
    end
    

    Here is the answer who helped me to understand that: https://github.com/collectiveidea/delayed_job/issues/882