rails Action Mailer perform_deliveries=false is not working - ruby-on-rails

I am running rails 5.1.0
In config/environments/test, I have config.action_mailer.perform_deliveries = false.
I am running a rails server with -e test to perform end to end to tests.
When I switch the server back to the dev environment, it loads a new browser tab rendering every email that wasn't sent during my tests. If I run my tests 5-6 times, this results in 50 browser tabs opening at once when I switch back to development.
For perform_deliveries Rails docs say:
If this value is false, deliveries array will not be populated even if
delivery_method is :test.
I am also running sidekiq and redis, so I wasn't sure if I have to somehow set the environment on those.
Is there a way for me to completely prevent emails from being queued into the system in my testing envrionment?

You can clear the sidekiq queue after you have run the server in the test environment. Or use other queue name in the test environment.
But the main thing is you should never run server in this mode.
For test you should use unit-tests or better rspec.

Related

Rails: How to run code when server starts up, but not when running a rake task or the console?

Before, I added code to a file called config/initializers/remote_publishers.rb which set up a connection to RabbitMQ using the Bunny gem on server startup.
However, this is now also executed when running rails c, rails g model SomeModel foo:integer, rails db:migrate etc.
For this app, the RabbitMQ-connection only makes sense when rails is started using rails s(erver).
What is the proper way to conditionally execute this code? Is there a way to see if Rails is starting as server, or only as task-runner?
What web server are you using? On Puma, for example, you can use
on_worker_boot do
# Establish RabbitMQ connection
end
Another possibility might be to check if defined?(Rails::Server) in your initializer: this should only be true when running in the context of the web server.

How do I see delayed_job jobs in production?

I'm have a server where I deploy using capistrano and I use delayed_jobs to do some mailing but at my server for some reason the jobs do not execute. The delayed_job process is running (running bin/delayed_job status answers me correctly saying there's a process there by some pid), but I don't know if the process just isn't executing my jobs or even if my jobs aren't being enqueued. Locally it all works fine, but at production staging in the server it does not.
I'd like to know if there's a way I can at least check what jobs are there, since I can't do it by accessing the console
Another gem that works with delayed job is delayed-web which you can find here https://github.com/tatey/delayed-web
you add it to your gemfile
gem 'delayed-web'
then run
rails generate delayed:web:install
this will generate an initializer file delayed_web.rb under config/initializers with the following:
Rails.application.config.to_prepare do
Delayed::Web::Job.backend = 'active_record'
end
and in config/application.rb this will be added for you as well by the generator
# config/application.rb
config.assets.enabled = true
config.assets.precompile << 'delayed/web/application.css'
In routes.rb it may add a route as well but if you are using devise then maybe you want to restrict access to admin user(s) only as follows:
authenticated :user, -> user { user.admin? } do
mount Delayed::Web::Engine, at: '/jobs'
end
Ok so I'm checked my jobs through the database itself, I entered psql through postgres user and did some queries in the delayed_jobs table, you can also try doing RAILS_ENV=production bin/delayed_jobs run (for rails 4, rails 3 use "script/" instead of "bin/") which will show you what the workers are doing while they execute the job.
You can also, as Swards commented above, use a gem to have a web interface for delayed_jobs: https://github.com/ejschmitt/delayed_job_web
If you still wanna see what was my problem with the email sending I've opened another question because it got to far away from what this one was about: What port to use sending email with SMTP (mailgun) in rails app on production server (DigitalOcean)?

When running specs with RSpec in Rails, how do I access the test_database concurrently via the rails console or an external script?

With RSpec, I'm trying to test a Rails program that calls an external python script. When running the spec, RSpec has access to the test database rails_program_test. However, the python script cannot access the database rails_program_test.
Furthermore, while running rails console test in a separate terminal, queries show no records in the test database, even though the spec reports that records do exist. Finally, when I switch the RAILS_ENV to development, the python script has access to the rails_program_development database. Is there a way to access the test database outside of the spec?
This may be because the Rails app is using transactional fixtures. They are rolled back at the end of each test, so are never visible to external processes.
You might want to look at the DatabaseCleaner gem for a non-transactional approach, but your use case sounds very unusual.

Clear worker cache in delayed jobs in production

I am using delayed jobs in my rails application. it works fine but there is an issue occurred on production server. I created a class in lib and call its method from controller to generate a csv file through delayed jobs. It was working fine when I ran the delayed jobs on local and production server but then I made some changes to this class for file naming convention and restarted the delayed jobs on local and then on production server. Now when I call that method through delayed job then it works according to latest changes I made to the class and sometimes it uses the old logic of file naming convention.
What could be the issue?
Delayed job has a hidden "feature" which is to ignore any changes to your app, and just use old settings, env-variables, email-templates, etc. You can clear every cache and restart your server, and it still holds onto data which no longer exists anywhere in your app's codebase.
delayed_job - Performs not up to date code?
Also be aware that DJ's "restart" does not always kill and restart all the workers, so you need to hunt them down and kill them all manually with
ps aux | grep delay
See: Rails + Delayed Job => email view template does not get updated
I have not yet found a "clear delayed job cache" function. If it exists, someone please post it here.
In my case, I just spent almost 4 hours trying everything to delete failing delayed_jobs in Heroku. In case you get here trying to kill a zombie delayed_job, but you're in Heroku, this won't work.
You can not do ps aux like you'd do in a regular server, nor you can do rake jobs:clear, and if you check via Rails console, you'll see the jobs there, but not in the Database, so nothing you can do there either.
What I did was placing the app in maintenance mode, made a deployment totally uninstalling delayed_job gem and all its references, and then another deployment reverting that change. That cleared the zombie cache, and that did the trick.
I had a similar issue in Dokku. My solution was to remove the worker=1 entry from my DOKKU_SCALE file (so all it contained was web=1) and also to remove the worker: bundle exec rake jobs:work line from my Procfile.
I pushed that to my production server, reversed the changes above, pushed again and it was fixed.

Rails Test Environment Named Routes

I've noticed that when I run tests (using Rspec and spork, if that matters), my hostname is set to www.example.com. So if I do:
visit sports_url
the test is actually going to www.example.com/sports.
This is a problem because I need to run some of the tests with selenium (to get javascript support). Also, I noticed my emails where being marked as coming from www.example.com.
What did I mess up? Why is the test environment running as example.com? Can I configure this somewhere? I would assume it should be programatic (depending on what port the test server starts up on).
You can configure the test environment domain, then set up DNS to do what you need.
How do I change the default "www.example.com" domain for testing in rails?

Resources