I'm using delayed_jobs and rest_client in my app, I created a jobs folder and a job class jobs/test_job.rb.
class TestJob < Struct.new(:name)
def perform
RestClient.get "http://name"
end
end
RestClient and DelayedJob is working fine, but when the client(http://name) is down, the delayed_job gives me error SocketError and not retrying it.
In my controller I have this.
Delayed::Job.enqueue TestJob.new(my_other_rails_app), :queue => "my_test"
Does delayed_job not requeue when an error occured?
I solve the problem, delayed job works if error occured in different database, so it was my database has the problem, Im using openedge and the error is character string is too long for last_error field, so I need to modify the gem for not saving long error or change the field character length.
delayed-job-4-1-2\lib\delayed\backend\base.rb
From:
def error=(error)
#error = error
self.last_error = "#{error.message}\n#{error.backtrace.join("\n")}" if respond_to?(:last_error=)
end
To:
def error=(error)
#error = error
self.last_error = "#{error.message}" if respond_to?(:last_error=)
end
I am on Heroku with a custom domain, and I have the Redis add-on. I need help understanding how to create a background worker for email notifications. Users can inbox message each other, and I would like to send a email notification to the user for each new message received. I have the notifications working in development, but I am not good with creating background jobs which is required for Heroku, otherwise the server would timeout.
Messages Controller:
def create
#recipient = User.find(params[:user])
current_user.send_message(#recipient, params[:body], params[:subject])
flash[:notice] = "Message has been sent!"
if request.xhr?
render :json => {:notice => flash[:notice]}
else
redirect_to :conversations
end
end
User model:
def mailboxer_email(object)
if self.no_email
email
else
nil
end
end
Mailboxer.rb:
Mailboxer.setup do |config|
#Configures if you applications uses or no the email sending for Notifications and Messages
config.uses_emails = false
#Configures the default from for the email sent for Messages and Notifications of Mailboxer
config.default_from = "no-reply#domain.com"
#Configures the methods needed by mailboxer
config.email_method = :mailboxer_email
config.name_method = :name
#Configures if you use or not a search engine and which one are you using
#Supported enignes: [:solr,:sphinx]
config.search_enabled = false
config.search_engine = :sphinx
end
Sidekiq is definitely the way to go with Heroku. I don't think mailboxer supports background configuration out of the box. Thankfully, it's still really easy with sidekiq's queueing process.
Add gem 'sidekiq' to your gemfile and run bundle.
Create a worker file app/workers/message_worker.rb.
class MessageWorker
include Sidekiq::Worker
def perform(sender_id, recipient_id, body, subject)
sender = User.find(sender_id)
recipient = User.find(recipient_id)
sender.send_message(recipient, body, subject)
end
end
Update your Controller to Queue Up the Worker
Remove: current_user.send_message(#recipient, params[:body], params[:subject])
Add: MessageWorker.perform_async(current_user.id, #recipient.id, params[:body], params[:subject])
Note: You should never pass workers ActiveRecord objects. That's why I setup this method to pass the User ids and look them up in the worker's perform method, instead of the entire object.
Finally, restart your server and run bundle exec sidekiq. Now your app should be sending the email background.
When you deploy, you will need a separate dyno for the worker which should look like this: worker: bundle exec sidekiq. You will also need Heroku's redis add-on.
Sounds like a H21 Request Timeout:
An HTTP request took longer than 30 seconds to complete.
To create a background worker for this in RoR, you should grab Resque, a Redis-backed background queueing library for RoR. Here is a demo. Another demo. And another demo.
To learn more about using Resque in Heroku, you can also read the herokue article up here. Or this tutorial (it's an old one though). Another great tutorial.
There is also a resque_mailer gem that will speed things up for you.
gem install resque_mailer #or add it to your Gemfile & use bundler
It is fairly straightforward. Here is a snippet from a working demo by the author:
class Notifier < ActionMailer::Base
include Resque::Mailer
default :from => "from#example.com"
def test(data={})
data.symbolize_keys!
Rails.logger.info "sending test mail"
Rails.logger.info "params: #{data.keys.join(',')}"
Rails.logger.info ""
#subject = data[:subject] || "Testing mail"
mail(:to => "nap#localhost.local",
:subject => #subject)
end
end
doing Notifier.test.deliver will deliver the mail.
You can also consider using mail delivery services like SES.
Sidekiq is an option that you could consider. To get it working you can add something like RedisToGo, then configure an initializer for Redis. Then on Heroku you can add something like worker: bundle exec sidekiq ... to your Procfile.
https://github.com/mperham/sidekiq/wiki/Getting-Started
It also has a dashboard for monitoring.
https://github.com/mperham/sidekiq/wiki/Monitoring
def process_order
OrderProcess.delay(run_at:1.minutes.from_now).processing_order(params[:id])
redirect_to '/'
end
when process_order is executed
it will execute delayed job
class OrderProcess
def self.processing_order(order_id)
system("rails generate controller welcome index")
end
end
when we remove delay(run_at:1.minutes.from_now) and process_order then rails commands in system command are getting executed but when we run through delayed job rails commands are not getting executed
thanks in advance
While executing the mail related code asynchronously by using delay method of delayed_job, its executing properly but we didn't get mail.
we are getting the following o/p in jobs:work rake task console
[Worker(host:ip-0AASDF pid:3512)] Starting job worker
[Worker(host:ip-0AASDF pid:3512)] UserMailer.deliver_author_notification completed after 0.4850
[Worker(host:ip-0AASDF pid:3512)] 1 jobs processed at 1.3072 j/s, 0 failed ...
I have the following code
UserMailer.delay.deliver_author_notification(self,nfication)
I am using rails 2.3.5 and delayed_job 2.0.8 version.
Please help me on this.
Thanks in advanced....
user_mailer.rb contains the following code:
class UserMailer < ActionMailer::Base
def author_notification(user,nfication)
#recipients = user.creator.email
#site_url = get_site_url(user.creator.organization)
#from = "admin##{#site_url}"
#body[:body_template] = eval('"'+nfication.body+'"')
#subject = eval('"'+nfication.subject+'"')
content_type "text/html"
end
end
I'm using gem 'delayed_job_active_record' for sending out emails. When my SignupMailer is called to send out an email , I see this in my console:
Job Delayed::PerformableMethod (id=23) RUNNING
But the job never completes.
However if I try calling my SignupMailer directly in rails console, the mail gets sent just fine. Help please?
My classes:
user.rb
def send_init_emails
generate_token(:activation_token)
SignupMailer.delay.welcome(self)
end
signup_mailer.rb:
def welcome(user)
#user = user
mail to: #user.email, subject: 'Welcome!'
end