How to switch Resque environment from development to test? - ruby-on-rails

I'm very new to rails and recently I'm trying to use Resque and Rspec to test my codes.
What I'm stocked with is to change Resque environment to test.
This is my state:
Redis-server is lunched: port 6379, PID: 6046
resque-web is lunched:
$ resque-web
[2014-09-19 21:52:43 +0430] Starting 'resque-web'...
[2014-09-19 21:52:43 +0430] trying port 5678...
Couldn't get a file descriptor referring to the console</li>
Now I can see resque web console at http://0.0.0.0:5678/stats/resque
Well, on Stats tab, there's a table and at the first row, environment is set to development.
Resque Client connected to redis://127.0.0.1:6379/0
environment development
failed 0
pending 0
processed 37780
queues 0
servers ["redis://127.0.0.1:6379/0"]
workers 0
working 0
I want to set Resque environment to test, so that I test jobs created by my rspec tests using test database, but I don't know how.Any tips?

Try to set the RAILS_ENV environment variable before running resque-web, e.g.
$ RAILS_ENV=test resque-web

Related

Delayed job stopped working after changing server from passenger to puma

Recently I just changed server for my production environment from passenger to puma. Before puma delayed job was working fine without any error but after puma it stopped working.
I am running 3 Delayed job to handle incoming requests on two different servers by dividing them as server 1 : 1 Job and server 2 : 2 Jobs.
RAILS_ENV=production bin/delayed_job -i first --queues=job_1,job_2 start
This command I am using to start delayed job after deployment. I am using Capistrano 3 for deployment. Also I have upgrade version of Capistrano version 2 to 3.
In Capistrano 2 we had
require 'delayed/recepies'
but as per delayed job official documentation I have created a rake task and executing in order to start job again.
It creates a new entry in delayed_jobs table as Delayed::Backend::ActiveRecord::Job.
What's wrong I am doing here? Is there any worker not listening to my incoming calls? Should I need to start additional things?
----UPDATED----
This is the rake task I have created to restart the jobs.
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute('cd #{current_path};RAILS_ENV=production bin/delayed_job -i first --queues=job_1,job_2 stop')
execute('cd #{current_path};RAILS_ENV=production bin/delayed_job -i second --queues=job_1,job_2 start')
end
end
end
end
after 'deploy:publishing', 'delayed_job:restart'
As I have mentioned that I am running two seperate servers to manage delayed_jobs. On server 2 delayed_job.log file is not updated with latest logs. When I try to run delayed_job it executes on server 1 instead of server 2. Queues are running over server 2 to handle those jobs and when it runs on server 1 it couldn't find the exact source, which I am looking for to get executed.

Sidekiq with Clockwork triggers but doesn't process job on heroku

Running: sidekiq 2.1.17, rails 3.2.21
I'm trying to use clockwork to schedule some recurring tasks to be done by sidekiq workers. Running foreman in development, everything runs perfectly as scheduled.
When I deploy to heroku, however, I get the "Triggering 'NameWorker.perform_async'" message in the logs at the appropriate times but then the respective jobs don't actually run.
When I instead call NameWorker.perform_async in a controller action, the job runs as it should. The jobs I'm trying to schedule just contain puts statements to verify that they're working. Anyone have any ideas about what I'm doing wrong? Thanks in advance.
Solved by adding the Sidekiq.configure_client logic from the sidekiq.rb file into clock.rb. This logic was already there and in unicorn but sidekiq was not being initialized in production for the purposes of the clockwork process.

Proper deployment of a Rails app with Mina and Foreman

For production purposes I need three processes running. This is my procfile and I use Foreman to start them:
web: bundle exec rails s Puma -p $PORT
queuing: bundle exec clockwork clock.rb
workers: bundle exec rake resque:workers
For deployment I'm using Mina. What's the appropriate way to start Foreman at the end of deploy task? Currently I'm starting like this:
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
to :launch do
queue "touch #{deploy_to}/tmp/restart.txt"
queue "bundle exec foreman start"
end
end
end
... but I don't think that's the proper way since the "mina deploy" command never successfully exits and the local console just starts outputting whatever these processes are doing.
Question number two: How do I initialize logging for each of these three processes separately in separate files?
And how do I prevent killing all of these three processes when one of them crashes? How do I make the process restart when it crashes?
Thanks!
OK, so that's 3 questions.
1) I think you want to detach foreman process from the terminal. That way the deployment process will finish and foreman process will be running even after you have disconnected from the server. nohup is great for that, e.g. this will launch your app and pipe all logs to server.log file:
nohup foreman start > server.log 2>&1 &
2) AFAIK, foreman doesn't let you do that. You should probably use another process management service (e.g. systemd, upstart). Thankfully, foreman lets you easily export your config to different process management formats (http://ddollar.github.io/foreman/#EXPORTING).
3) Again, you probably want to separate your processes and manage them separately via upstart, systemd, etc.

Sidekiq processing with local redis but not with remote

I have a RoR app with background jobs using whenever and sidekiq gems.
In development environment when I launch sidekiq with local redis instance (on localhost) the job keeps getting executed without problems. But when I switch to a remote redis instance (Heroku add-on) and restart sidekiq, it says it started processing, but nothing happens and workers aren't doing any jobs.
Here's my config/schedule.rb (for whenever gem)
every 2.minutes do
rake "crawler:crawl"
end
Here's my initializers/redis.rb:
Sidekiq.configure_server do |config|
config.redis = { :url => 'redis://user:pass#spinyfin.redistogo.com:9098/' }
end
Sidekiq.configure_client do |config|
config.redis = { :url => 'redis://user:pass#spinyfin.redistogo.com:9098/' }
end
If I comment out the content in redis.rb and run a local redis instance, the jobs are processed normally. But when I use this remote redis instance, this shows up and then nothing gets processed:
2013-11-29T15:09:26Z 95156 TID-ov6y7e14o INFO: Booting Sidekiq 2.13.0 using redis://redistogo:user#spinyfin.redistogo.com:9098/ with options {}
2013-11-29T15:09:26Z 95156 INFO: Running in ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]
2013-11-29T15:09:26Z 95156 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2013-11-29T15:09:26Z 95156 INFO: Starting processing, hit Ctrl-C to stop
Maybe you connecting to wrong redis database or not connected at all.
In my apps I use redis url without trailing slash. In your case:
This is for database "0"
redis://user:pass#spinyfin.redistogo.com:9098
And this for database "1"
redis://user:pass#spinyfin.redistogo.com:9098/1
I use the environment variable REDIS_URL to ensure that everything is using the same Redis.
Re: Heroku - I just read this here as I was searching for my own solution:
If you're running on Heroku, you can't rely on the config/database.yml as that platform relies on the DATABASE_URL environment variable to determine the database connection configuration. Heroku overwrites the database.yml during slug compilation so that it reads from DATABASE_URL.

Code that only runs when a Rails application is actually running, not during rake tasks, generators, etc

We're trying to establish a block of gem-ready initialization code that runs exclusively in the context of a running Rails application, but it's proving mind-numbingly hard.
We want it to run when:
An app is started locally from the command line via rails server
An app is started on Heroku or other deployment environments
During testing
but NOT when:
A Rake task is being run
A generator is being run
Whatever is being run during the deploy process to Heroku
The last instance, especially, is tripping us up. Whenever we push to Heroku via git push heroku master, our app executes in some strange context where it's in production but no Heroku config variables are available to initializers.. can't find any info on it anywhere..
We've seen this asked elsewhere, but passing ENV variables isn't an option because it'll be in a gem.
I don't know how you can detect that you're in the Heroku asset pre-comp phase and avoid running some code, but you can get the Heroku environment variable to be available during pre-comp by using the 'user-env-compile' feature from Heroku Labs.
https://devcenter.heroku.com/articles/labs-user-env-compile
heroku labs:enable user-env-compile -a myapp

Resources