Ruby on Rails 3.0 Delayed Job - ruby-on-rails

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

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!

During testing, why is rails running my initializers twice?

I've created a brand new rails app, added a dummy controller, with an 'assert true' functional test, and an initializer that calls 'puts Rails.env'.
When I run rake test on this app, the initializer prints 'development' and 'test'.
Can anyone tell me why this is?
Peter.
running rake boots up rails, then running your tests (test_helper, spec_helper) loads rails again for the testing environment
if you are using rspec you can invoke it without rake
bundle exec rspec spec
pretty sure with mini-test you can invoke using rake or ruby executable
there should not be any bad side effects running them via rake, just that you need to wait for rails to boot twice

Ruby on Rails Delayed Job Local Wont Run

I'm working with delayed job for active record gem https://github.com/collectiveidea/delayed_job I'm trying to set up a job to run five minutes after an event occurs in my application. After five minutes passes, I need to make some database updates. I've tried rake jobs:work and RAILS_ENV=development script/delayed_job start. Prior to this all, I have run bundle install, rails generate delayed_job:active_record, and rake db:migrate. I have a lottery website that needs to check winners every five minutes and update tokens for winning players.
I wait five minutes, but no updates are made in my local application.
Here's what I have so far:
Gem File:
gem 'delayed_job_active_record'
gem "daemons"
Job (located in lib)
class WinnersJob < Struct.new(:blast_id)
def perform
...
end
Controller
require 'winners'
Delayed::Job.enqueue(WinnersJob.new(blast.id), 1, 5.minutes.from_now)
end
I think you have to launch the background workers locally using foreman. The following worked on my Mac.
From Heroku docs:
You then need to tell your application to process jobs put into your job queue, you can do that by adding this to your Procfile:
worker: bundle exec rake jobs:work
Now when you start your application using Foreman it will start processing your job queue.
foreman start
Having said all that, unless you are deploying on a Mac, it doesn't really matter if they run locally. (I noticed this after I got it working.) It only matters if it works on your servers. If you are deploying on Heroku, then Delayed Job works well.
Reference:
https://devcenter.heroku.com/articles/delayed-job
https://devcenter.heroku.com/articles/procfile

Getting extra rufus-scheduler thread with each delayed_job rake jobs:work

Am trying to use rufus-scheduler to check every minute or so to see if
there are jobs ready to be placed in the delayed_job queue.
Have an initializer script in #{RAILS_ROOT}/config/initializers that
starts the scheduler. Unfortunately the rake jobs:work also runs the
rails initialization process so another gets started for each
jobs:work started.
How can I prevent this?
Running ruby 1.8.6.26, rails 2.3.5, dj 1.8.5, rufus-scheduler 2.0.6 on
XP pro sp3
In your initializer, find a way not to run the schedule if the rails initialization process is invoked via Rake.
For sure there is a more railsy way, but you could do
unless defined?(Rake)
# do the scheduling...
end
The block 'do the scheduling' won't get called if the constant Rake is defined (for a Rake task it is defined).

Resources