delayed_job rake jobs:work rails 4 not sending mail - ruby-on-rails

UserMailer with delay method not working in rails 4. As mentioned below, this code doesn't works
def welcome_email
UserMailer.delay.welcome_email(self)
end
But when I use below code it works fine.
UserMailer.welcome_email(self).deliver
I have installed 2 gems, i.e
gem 'delayed_job'
gem 'delayed_job_active_record'

You need to pass ENV variables to your worker command.
Use this command in your command line:
rake jobs:work RAILS_ENV=development VARIABLE_1='' VARIABLE_2=''

Related

How to test rake tasks with Whenever Gem?

I'm trying to execute a simple rake task using whenever gem but the code isn't being executed.
I already set the environment to development, I updated the cron using the whenever --update-crontab command and the rake task works well if I run the command on console. But, when I run the server the log file is not being generated.
I saw a question here too with the same problem but it was solved setting the environment to development, but didn't work out for me.
My rake task:
namespace :testando do
task :consulta => :environment do
produto = Produto.first
puts produto.nm_produto
end
end
My schedule.rb:
set :output, "#{path}/log/cron_log.log"
set :environment, 'development'
every 1.minute do
rake "testando:consulta"
end
I'm using rails 5.0.0.1 and I'm programing in Cloud9, so I think the OS is Ubuntu.
What's missing ?
Update:
I followed the instructions of the main answer in this topic Cron job not working in Whenever gem
And it worked! The task is running even with the server not being started (with "rails s" command).
please run crontab -l to see if you have updated the crontab successfully

NotImplementedError (Use a queueing backend...) using delayed_job

In my Rails app (4.2.4), I have been trying to get asynchronous mail sending to work.
I installed delayed_job as my queue adapter, and set it as the adapter in several places: config/application.rb, config/environments/{development,production}.rb, and config/initializers/active_job.rb.
Installation:
I added this to my Gemfile:
gem 'delayed_job_active_record'
Then, I ran the following commands:
$ bundle install
$ rails generate delayed_job:active_record
$ rake db:migrate
$ bin/delayed_job start
In config/application.rb, config/environments/production.rb, config/environments/development.rb:
config.active_job.queue_adapter = :delayed_job
In config/initializers/active_job.rb (added when the above did not work):
ActiveJob::Base.queue_adapter = :delayed_job
I've also run an ActiveRecord migration for delayed_job, and started bin/delayed_job before running my server.
That being said, any time I try:
UserMailer.welcome_email(#user).deliver_later(wait: 1.minutes)
I get the following error:
NotImplementedError (Use a queueing backend to enqueue jobs in the
future. Read more at http://guides.rubyonrails.org/active_job_basics.html):
app/controllers/user_controller.rb:25:in `create'
config.ru:25:in `call'
I was under the impression that delayed_job is a queueing backend... am I missing something?
EDIT:
I can't get sucker_punch to work either. When installing sucker_punch in the bundler, and using:
config.active_job.queue_adapter = :sucker_punch
in config/application.rb, I get the same error and stack trace.
If you are having this issue in your development environment even though you are using an adapter capable of asynchronous jobs like Sidekiq, make sure that Rails.application.config.active_job.queue_adapter is set to :async instead of :inline.
# config/environments/development.rb
Rails.application.config.active_job.queue_adapter = :async
Provide you are following all the steps listed here, I feel you didn't start delayed_job running
bin/delayed_job start
Please also check you run
rails generate delayed_job:active_record
rake db:migrate
Try this:
in controller:
#user.delay.welcome_email
in your model
def welcome_email
UserMailer.welcome_email(self).deliver_later(wait: 1.minutes)
end
Figured out what it was: I typically start my server and everything associated with it using a single shell script. In this script, I was running bin/delayed_job start in the background, and starting the server before bin/delayed_job start finished. The solution was to make sure delayed_job start finished before starting the server by running it in the foreground in my startup script.
Thanks everyone for all the help!

is delayed job will work rails 4?

Will delayed job work with Rails 4?
Currently, I am upgrading my application to Rails 4 and using
gem "delayed_job", :git => 'git://github.com/collectiveidea/delayed_job.git'
in gemfile.
when i run rake jobs:work i got error like this
Error while reserving job: undefined method reserve for
Delayed::Job:Class
any help on this?
add this gem 'delayed_job_active_record' line below gem "delayed_job" to your gem file like this,
gem "delayed_job", :git => 'git://github.com/collectiveidea/delayed_job.git'
gem 'delayed_job_active_record'
and do
bundle install
then try
bundle exec rake jobs:work
hope it will work.
Delayed job will work on rails 4. But the delayed_job folder inside the bin folder.
So, You can run delayed job by following command
bin/delayed_job start`

Ruby on Rails 3.0 Delayed Job

I've added gem 'delayed_job' to my gem file and ran a bundle install.
After that I ran rails generate delayed_job
I've created a controller named Online with a method online.
In turn after the method declaration I added the following line:
handle_asynchronously :online
I start up my app, but the code in that method does not run.
What am I doing wrong?
I'd guess that you haven't done rake jobs:work anywhere. From the fine manual:
Running the jobs
You can invoke rake jobs:work which will start working off jobs. You can cancel the rake task with CTRL-C.
You might want to set up Foreman to start the Rails server and the Rake task at the same time in your development environment; there's even a Railscast about it:
http://railscasts.com/episodes/281-foreman

How to run a custom command when run the 'rails server' command?

I am using Ruby on Rails 3.1.0 and the DelayedJob gem. In order to automate processes in development mode I would like to "auto"-run the following command
rake jobs:work
when in my console\terminal I run this other command
rails server
# and/or
#
# rails db:create
# rails db:migrate
# ...
Is it possible? How can I do that?
The gem "foreman" will sort this out for you. Here's the railscast: http://railscasts.com/episodes/281-foreman

Resources