Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Rails Mailer with delayed_job_active_record

I'm trying to use delayed_job_active_record with Rails 3.2.13.

Whenever I use MyMailer.delay(args) the task never appears to run (based on log output). Putting the .delay anywhere else results in an error being thrown.

User Gravatar

jmaddington

Posted Jun 25 2014 6:17 UTC

$30


  • Assigned To jmaddington
  • Solved
  • ruby
    ruby-on-rails
  • 2086 Views

17 Replies


Are you running a worker in another process? Else nothing will actually run the task for you.

User Gravatar

ohm

Posted Jun 25 2014 6:19 UTC

Yes. The queue runs without errors that I see, but nothing inside actually seems to run.

User Gravatar

jmaddington

Posted Jun 25 2014 6:20 UTC

You are certain that the jobs are being placed in the delayed_job table in the database?

You can see if the job is present here, by looking for it with

Delayed::Job.last

and checking if the run_at is in the past

Delayed::Job.last.run_at.past?

or if it has run and failed

Delayed::Job.last.failed_at

User Gravatar

ohm

Posted Jun 25 2014 6:24 UTC

what version of gem are you using?

User Gravatar

RejectKid

Posted Jun 25 2014 6:28 UTC

You can try something simple from console like MyMailer.delay.some_email and check if the process gets executed (or as ohm said at least queued in the db).

Also you can change delayed job config to do background jobs more frequently so that you don't have to wait delayed job to wake up (default is 60s I think) while you're testing e.g. put inside:

# config/initializers/delayed_job_config.rb
Delayed::Worker.sleep_delay = 0.5
User Gravatar

sfsgagi

Posted Jun 25 2014 6:29 UTC

Are you sure your mailer can send mail?

User Gravatar

user2923

Posted Jun 25 2014 6:42 UTC

Check this out, might help you. https://github.com/collectiveidea/delayed_job_active_record/issues/51

User Gravatar

m1aw

Posted Jun 25 2014 6:58 UTC

^^ this is why i asked about the Gems version your using, as the problem doesnt actually lie with that version of ruby rails, but rather the ruby gems

User Gravatar

RejectKid

Posted Jun 25 2014 7:00 UTC

You said "Putting the .delay anywhere else results in an error being thrown.", can you explicit about what kind of error you saw?

User Gravatar

tubaxenor

Posted Jun 25 2014 7:00 UTC

Have you start the process with script/delayed_job start?

User Gravatar

jonathantribouharet

Posted Jun 25 2014 7:02 UTC

Mails go out without delayed_job, I have other tasks in the same queue that run without issue.

@ohm, I'll check that as soon as I can, getting pulled off somewhere else at the moment.

User Gravatar

jmaddington

Posted Jun 25 2014 8:18 UTC

Also, check that you can create the job from the console:

MyMailer.delay.mail_method(:with, :correct, :args)

and see that this is injected into the queue.

User Gravatar

ohm

Posted Jun 25 2014 8:23 UTC

This is a forked version of https://github.com/jmaddington/the-city-groupie-text

Line 31 of https://github.com/jmaddington/the-city-groupie-text/blob/master/lib/send_messages.rb is essentially where I am trying to insert this

I can't easily create the job from the console as the session is initialized with a POST from a third party I don't control followed by same API calls that are going to be a pain to mimic.

That said, I can confirm the job is being created

  • queue on web site
  • select count(*) from delayed_jobs;
  • count ------- 1
  • rake jobs:workoff

    [Worker(host:142f13f7-abca-480a-997c-7fa45666e0a4 pid:2)] Job Delayed::PerformableMailer (id=78) RUNNING

    (2.0ms) BEGIN

    SQL (6.3ms) DELETE FROM "delayed_jobs" WHERE "delayed_jobs"."id" = $1 [["id", 78]]

    (4.2ms) COMMIT [Worker(host:142f13f7-abca-480a-997c-7fa45666e0a4 pid:2)] No more jobs available. Exiting

    SQL (2.3ms) UPDATE "delayed_jobs" SET "locked_by" = NULL, "locked_at" = NULL WHERE "delayed_jobs"."locked_by" = 'host:142f13f7-abca-480a-997c-7fa45666e0a4 pid:2'

  • select count(*) from delayed_jobs;

  • count ------- 0

@tubaxenor: According to the official docs the syntax for delayed jobs with Rails 3 mailers is MyMailer.delay.send_something(args) instead of MyMailer.send_something(args).delay.deliver (which is how it works on most other functions), I'm really just confirming that this is the point.

@sfsgagi The jobs run every second or so already

@user2923 Yes, it sends find when I use MyMailer.send_something(args).deliver

Blockquote

Gems (from Heroku push):

-----> Ruby app detected

-----> Compiling Ruby/Rails

-----> Using Ruby version: ruby-2.1.1

-----> Installing dependencies using 1.6.3

   Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4

   Fetching gem metadata from https://rubygems.org/..........

   Fetching additional metadata from https://rubygems.org/..

   Resolving dependencies...

   Using i18n 0.6.1

   Using builder 3.0.4

   Using multi_json 1.10.1

   Using rake 10.3.2

   Using erubis 2.7.0

   Using journey 1.0.4

   Using rack 1.4.5

   Using hike 1.2.3

   Using tilt 1.4.1

   Using polyglot 0.3.5

   Using mime-types 1.25.1

   Using thor 0.19.1

   Using sass 3.3.8

   Using json 1.8.1

   Installing thread_safe 0.3.4

   Using arel 3.0.3

   Using tzinfo 0.3.39

   Using bundler 1.6.3

   Using coffee-script-source 1.7.0

   Using execjs 2.2.0

   Using ffi 1.9.3

   Using multipart-post 2.0.0

   Using http_parser.rb 0.5.3

   Using jwt 1.0.0

   Using libv8 3.16.14.3

   Using pg 0.17.1

   Using ref 1.0.5

   Using activesupport 3.2.13

   Using rack-cache 1.2

   Using rack-test 0.6.2

   Using rack-ssl 1.3.4

   Installing orm_adapter 0.5.0

   Using sprockets 2.2.2

   Using treetop 1.4.15

   Installing warden 1.2.3

   Using rdoc 3.12.2

   Using coffee-script 2.2.0

   Using uglifier 2.5.1

   Using ethon 0.7.0

   Using faraday 0.9.0

   Using http 0.5.1

   Using twilio-ruby 3.11.5

   Using therubyracer 0.12.1

   Using activemodel 3.2.13

   Installing bourbon 3.2.3

   Using delayed_job 4.0.2

   Using mail 2.5.4

   Using typhoeus 0.6.8

   Using thecity 0.0.9

   Using actionpack 3.2.13

   Using activerecord 3.2.13

   Using activeresource 3.2.13

   Using actionmailer 3.2.13

   Using railties 3.2.13

   Installing arbre 1.0.1

   Installing formtastic 2.2.1

   Installing has_scope 0.6.0.rc

   Installing polyamorous 0.5.0

   Using delayed_job_active_record 4.0.1

   Installing kaminari 0.16.1

   Installing responders 1.1.0

   Using rails 3.2.13

   Using coffee-rails 3.2.2

   Installing jquery-rails 2.3.0

   Using sass-rails 3.2.6

   Installing jquery-ui-rails 4.1.2

   Installing meta_search 1.1.3

   Installing inherited_resources 1.5.0

   Installing bcrypt 3.1.7

   Installing devise 3.2.4

   Installing activeadmin 0.6.3

   Your bundle is complete!

   Gems in the groups development and test were not installed.

   It was installed into ./vendor/bundle

   Post-install message from meta_search:

   *** Thanks for installing MetaSearch! ***

   Be sure to check out http://metautonomo.us/projects/metasearch/ for a

   walkthrough of MetaSearch's features, and click the donate button if

   you're feeling especially appreciative. It'd help me justify this

   "open source" stuff to my lovely wife. :)

   Bundle completed (17.02s)

   Cleaning up the bundler cache.

   Removing jquery-rails (3.1.1)

   Removing sendgrid (1.2.0)

-----> Writing config/database.yml to read from DATABASE_URL

   Detected manifest.yml, assuming assets were compiled locally

-----> WARNINGS:

   Injecting plugin 'rails_log_stdout'

   Injecting plugin 'rails3_serve_static_assets'

   Add 'rails_12factor' gem to your Gemfile to skip plugin injection
User Gravatar

jmaddington

Posted Jun 25 2014 12:53 UTC

Also tried upgrading to Rails 3.2.18 without result. Part of notifier.rb (which has the action mailer) is

puts "CTDEBUG: Getting ready to send mail"

mail( :from => ApiSetting.first.SMTP_FROM ,:to => @user.email,:subject => subject )

puts "CTDEBUG: Mail should have been sent"

The "puts" show up without the delay. With it, the jobs are worked off but I never see the puts statements run.

User Gravatar

jmaddington

Posted Jun 25 2014 12:54 UTC

Just to get this straight, this:

Notifier.send_group_email(@user, message, "From: #{@leader} To: #{@group}").deliver

works, but this:

Notifier.delay.send_group_email(@user, message, "From: #{@leader} To: #{@group}")

doesn't?

Does it work with other mails - or is it all delays on mails that isn't working?

It seems like it actually executes the job, are you sure that the worker has a connection to the SMTP server, so that it can actually send the mail?

Have you tried it locally and used a mail catcher gem like mailcatcher (http://mailcatcher.me/) to setup a local SMTP server, that will catch the mail?

User Gravatar

ohm

Posted Jun 25 2014 14:00 UTC

@Ohm, correct, it only fails with the delay. I can see the job being processed but it looks like it doesn't actually run.

User Gravatar

jmaddington

Posted Jun 26 2014 3:26 UTC

I finally got it working, instead of passing @user I starting passing just the email address, works without an issue now.

User Gravatar

jmaddington

Posted Jun 26 2014 3:37 UTC

Solution

This didn't solve your task? Get your own custom solution.

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions