I am using an special approach to deliver email.
First, I abstract the call of method for delivering email into a control method send_email
Second, I delayed the execution of the control method.
It seems to be fine to send some value that are hard-coded into the method for delivering email, but not the value passed into that method.
Control method:
# this method is being delayed to control the time of sending email
def send_email
# approach 1 : just send a email after method send_email is executed
# In delay situtation : after 2 minutes
# Otherwise : no delay
Notifier.create_long_task(1234).deliver
# approach 2: explicitly states to delay sending email process
# (same result with the above one)
#
# Notifier.delay.create_long_task(1234)
end
handle_asynchronously :send_email, :run_at => Proc.new { 2.minutes.from_now }
In the above codes, I passed 1234 into delivering method and below the delivering method will assign 1234 to an instance variable @id
def create_long_task(longTask_id)
@greeting = "Hi"
@longTask ="delay setting"
@id = longTask_id
mail to: "j-@hotmail.com", :subject => 'Long Task Created'
end
The email template looks like :
Notifier#create_long_task
Instance variable 1 : <%= @greeting %>
Instance variable 2 : <%= @longTask %>
Instance Id : <%= @id %>
You created a long task
So to be optimistic, the email will show 3 instance variables, 2 are hard-coded in the delivering method, one is passed into delivering method from outside
But the results only can show 2 instance variables that are hard-coded in the delivering method.
Notifier#create_long_task
Instance variable 1 : Hi
Instance variable 2 : delay setting
Instance Id :
You created a long task
It is very strange.
When I comment out handle_asynchronously to send the email without any delay, I can see all the instance variables. So I think that 1234 cannot be passed into delivering method.
Result of no delay email just like:
Notifier#create_long_task
Instance variable 1 : Hi
Instance variable 2 : delay setting
Instance Id : 1234
You created a long task
This is a very long,complicated problem statement, I really appreciate if anyone can solve this problem.
Don’t forget to restart delayed job as it won’t pick up changes to your code until you do.