I have rescue 1.22.0 installed locally and on a server. To be able to catch MultiJson::DecodeErrors I added the following to my application.rb:
config.middleware.swap ActionDispatch::ParamsParser, ::MyParamsParser
and added the class to my lib folder. In dev mode this works fine, I can rescue from DecodeErrors and I can start a worker using:
QUEUE=* bundle exec rake environment resque:work
In production mode on my server the code itself works as well, but my god process was not able to start workers again. The error that occurs after god starts a worker:
QUEUE=* /usr/local/rvm/rubies/ruby-1.9.2-p320/bin/ruby /usr/local/rvm/gems/ruby-1.9.2-p320#global/bin/bundle exec rake -f /home/deployer/apps/kassomat/current/Rakefile environment resque:work
rake aborted!
No such middleware to insert before: ActionDispatch::ParamsParser
I tried to patch my application.rb
config.middleware.swap ActionDispatch::ParamsParser, ::MyParamsParser if Object.const_defined?('ActionDispatch') && ActionDispatch.const_defined?('ParamsParser')
but that did not work out. I do not understand why this works in development but fails in production.
Can anyone help?
Regards
Felix
Related
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!
I have the following code in config/application.rb
config.after_initialize do
IndividualProject::Application.load_tasks
#load File.join(Rails.root, 'lib', 'tasks', 'download_csv.rake')
Rake::Task[ 'download_csv:get_files' ].invoke
Rake::Task[ 'download_csv:place_in_database' ].invoke
end
My problem is that if I try to execute migrations, I get a database error which says that one of tables I'm referencing in the rake task does not exist.
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "currencies" does not exist
I can solve the issue by commenting out the code and then running the migrations. After this, the server runs fine.
However, I want to deploy to Heroku, where I can't comment out the code before running the migrations.
How should I solve this issue?
Do I need to place the code somewhere else in the project?
Remove your code from config/application.rb and change the web process in Procfile like following:
web: rake download_csv:get_files && rake download_csv:place_in_database && bundle exec rails server -p $PORT
Change bundle exec rails server -p $PORT with whatever code you use to start your server.
If you don't have Procfile in your project yet, create one and add it to git.
Now your rake tasks will be executed only before starting the server.
I cant figure how to start Delayed Jobs on a dedicated Ubuntu server.
It works fine on my localhost but when I run on my server
sudo RAILS_ENV=production bin/delayed_job restart
I get
sudo: bin/delayed_job: command not found
On top of that, if I run the "rake jobs:work RAILS_ENV=production" command Im getting the following error:
PG::FeatureNotSupported: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries
Apparently theres an issue with my psql version.
Is there any way I can get the script to work? Any effective Capistrano recipes available? All ive found on the web are old recipes for Rails 3 and older versions of capistrano.
Thanks in advance.
EDIT:
I have already bundled install the daemons gem and generated "delayed_job:active_record" on my local machine, then proceded to cap deploy which bundle installed and migrated in the production server.
The bin/delayed_job file exists in the server yet it fails with command not found.
And add this to config/environment.rb:
ENV['RAILS_ENV'] ||= 'production'
Then at your production server:
RAILS_ENV=production rake db:migrate
RAILS_ENV=test production generate delayed_job:active_record && RAILS_ENV=production rake db:migrate
Now after you do that:
RAILS_ENV=production script/delayed_job start
As for Capistrano error you are facing, please try to add the command like:
run "cd #{current_path}; #{sudo} RACK_ENV=production bundle exec #{current_path}/bin/delayed_job start"
You must run this on target server:
bundle exec rails generate delayed_job
I followed the tutorial at https://devcenter.heroku.com/articles/queuing-ruby-resque to queue and run background jobs in a Rails app. After queueing the jobs, it doesn't seem to run any of the jobs since in the console I can see the job has not been processed
>Resque.info
=> {:pending=>1, :processed=>0, :queues=>1, :workers=>0, :working=>0, :failed=>0, :servers=>["redis://dory.redistogo.com:9826/0"], :environment=>"production"}
If I try to do (locally)
bundle exec rake jobs:work
I get
rake aborted!
Don't know how to build task 'jobs:work'
On heroku, if I try
heroku run rake jobs:work
I again get `Don't know how to build task'
In my Rakefile, I have require 'resque/tasks' and in my Procfile I have
resque: env TERM_CHILD=1 bundle exec rake jobs:work
resque: env TERM_CHILD=1 bundle exec rake jobs:work
I have the Resque and redis gems in my Gemfile, but not delayed_job.
Update:
Here is my Rakefile:
#!/usr/bin/env rake
require File.expand_path('../config/application', __FILE__)
Guard::Application.load_tasks
/lib/tasks is empty. I have a worker in app/workers that I am enqueueing in a controller.
This doesn't appear to have anything to do with Heroku, if it doesn't work locally. You'll have to reveal some of your source to help people help you. For example, what's your Rakefile look like? The demo app in that article defines one with a Rake task with the task. Have you defined your rake task and added the appropriate gem references?
Try resque:work instead of jobs:work and see if that beings the desired results.
You just need to add require 'resque/tasks' in your Rakefile, then on the command line:
QUEUE=file_serve rake environment resque:work
Where file_serve is the name of your job (the class would be FileServeJob).
See the docs here - I found them slightly confusing since the setup and run info comes after the job class creation.
I have installed resque correctly, but to process all queues I need to run
rake resque:work QUEUE='*'
The problem is that I need to keep the terminal window opened, otherwise resque:work won't works.
Do you know a way to auto-run that rake command every time I run "rails server" ?
I'm on Localhost
lib/tasks/resque.rake
require 'resque/tasks'
task "resque:setup" => :environment do
ENV['QUEUE'] = "*"
end
Instead of calling the invoke function, you can use a gem like foreman that can invoke all the other tasks.
This is useful if you are looking to have a largely platform neutral solution and also while deploying into cloud.
Your Procfile can have the following contents:
web: bundle exec thin start -p $PORT
worker: bundle exec rake resque:work QUEUE=*
clock: bundle exec rake resque:scheduler
Source:introduction to foreman.
Now to start the server, you just have to issue foreman start command which forks off child threads to perform individual work.
Edit: Answer from 2012! Seems that this works just for Rails 2!
Add an initializer in config/initializers with something like this:
Rake::Task["resque:work QUEUE='*'"].invoke
Not tested!
The best way to do it is
ENV['QUEUE'] = "*"
Rake::Task["resque:work"].invoke