How to get rake output to show in Capistrano - ruby-on-rails

I am running rake tasks on the server using a snippet like the following in the deploy.rb of a rails app.
desc 'Invoke rake task on the server'
task :invoke do
fail 'no task provided' unless ENV['task']
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, ENV['task']
end
end
end
end
The rake task runs ok but I would like to see the output from the rake task displayed in the console where I run the capistrano task.
puts commands in the rake task do not appear.

If in Debug mode, capistrano prints out the outputs of the commands if I'm not mistaken. Probably you set your log level to :info. Just find the line in your deploy.rb and change it to (add it if its not there):
set :log_level, :debug
If you want to have this only for the single command or I was wrong and the above solution does not work for you, maybe checkout this answer (dunno if its still working):
Capistrano log level
UPDATE: How about using Rails logger instead of puts in the game task:
Rails.logger.debug "test"

Related

How to invoke RSpec inside a rake task?

We wrote some tests that are necessary, but very slow. So we configured RSpec to exclude them except on Solano, where we set up an ENV variable.
# spec_helper
unless ENV['RUN_ALL_TESTS'] == 'true'
config.filter_run_excluding :slow
end
That works, but I'm trying to write a rake task we can call to run every test locally by setting that same ENV variable and then running the suite. I'm having trouble figuring out how to trigger RSpec. This is what I've got now:
# all_tests.rake
require 'rspec/core/rake_task'
desc 'Run all tests, even those usually excluded.'
task all_tests: :environment do
ENV['RUN_ALL_TESTS'] = 'true'
RSpec::Core::RakeTask.new(:spec)
end
When I run it, it doesn't run any tests.
Most of the stuff I found is for triggering a rake task inside of a test, testing a rake task, or setting up a Rakefile. We're using rspec-rails, our default rake task is already set up.
To run RSpec through its rake integration, you need to both define a task and invoke it:
# all_tests.rake
require 'rspec/core/rake_task'
# Define the "spec" task, at task load time rather than inside another task
RSpec::Core::RakeTask.new(:spec)
desc 'Run all tests, even those usually excluded.'
task all_tests: :environment do
ENV['RUN_ALL_TESTS'] = 'true'
Rake::Task['spec'].invoke
end
Rake::Task['spec'].invoke did nothing when you tried it because rake turns a task name which is not a name of a defined task but is a file name into a Rake::FileTask, both on the command line and in Rake::Task. You had no 'spec' task defined, but you have a spec directory, so rake spec ran without error and did nothing.
I had a similar problem where bundle exec rake default would properly create an RSpec::Core::RakeTask, but bundle exec rake spec would instead create a a Rake::FileTask on the spec directory, with with bundle exec rake spec --trace outputting:
** Invoke spec (first_time, not_needed)
It turned out that rspec-rails was in the :test gem group, where it needed (per the docs) to be in both :test and :development.
Interestingly (?), once I did that, gems that had previously only been in the :test group were also available from the specs even when launched with RAILS_ENV=development. I assume that rspec-rails engages in some magic environment shenanigans behind the scenes.

Running db:test:prepare in Rake task throws off environment

I have the following 2 rake tasks:
task :clone => :environment do |t, args|
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
system "pg_restore -O -d database_name last_dump"
Rake::Task["db:migrate"].invoke
Rake::Task["db:test:prepare"].invoke
# Try to force the rails env to reload, but this doesn't solve the problem
Rake::Task["environment"].execute
Rake::Task["db:company_count"].invoke
end
task :company_count => :environment do
puts Company.count
end
When I run rake db:clone the output the Company.count is 0 indicating there are no Companies in the database, but when I run rake db:clone && rake db:company_count the output is 2.
How do I get the correct Company.count after loading the database in the first task?
The Company.count is correct if I remove Rake::Task["db:test:prepare"].invoke from the clone task, but I'm not sure why
My guess is first task is not using the console environment because it creates its own terminal session for the rake, and the second one is.
Try printenv and compare the variables
Also try prefixing the commands with RAILS_ENV=development or whatever environment you want.
guides.rubyonrails.org indicates that the task db:test:prepare is used to do the following to your test database:
drop the database
create the database
run the migrations
After this task completes, you will not have any data in there. Here are stack overflow answers that explains this:
PG undefinedtable error relation users does not exist
What does rake db:test:prepare actually do
I believe the behavior you are seeing has nothing to do with the environment, but a misunderstanding in the intention of this rake task.

Rails - Run Rake tasks with Capistrano

Im scratching my head here wondering if I'm barking up the wrong tree. I have a server which I've deployed a Rails app onto using Capistrano. Recently I added a new data type to one of the models, and now I need to run a Rake task to update the existing records.
After a lot of Googling I'm starting to wonder if people use Rake tasks with Capistrano. Some forum posts from 2013 or so mention that Capistrano supports the .rake extension. Whereas other posts I've found indicate that Capistrano has its own task automation system, and is incompatible with rake.
I found Cape, but I'm unsure if this is what I'm looking for as it seems to convert Rake tasks into cap recipes. Its possible I'm mistaken on this point, I really don't have any experience working with Capistrano or even working in the full stack spectrum.
What I'm wondering is this: How do I run a simple Rake task on my remote server?
Some quick points for clarity, I've installed the app on the latest Ubuntu LTS, 14.10 if memory serves. I followed the tutorial found here. I have full sudo access and I can ssh into the server.
thanks in advance for helping a noob
If you need to update models, you can of course write a Rails migration - this will ensure that it's run if it hasn't been run yet.
The easiest way to execute a rake task on the server would be just via ssh if it's a one-time task. See the last paragraph in the tutorial you mentioned:
cd /opt/www/testapp/current ; bin/rake RAILS_ENV=production db:seed
To answer your original question about rake: you can execute rake tasks via capistrano similar to how you would execute it locally, only within the capistrano script. Here's an example:
deploy.rb:
namespace :rake do
desc "My task"
task :my_task do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, "my_task"
# !!!see NOTE at end of answer!!!
end
end
end
end
end
You can view all your cap tasks via cap -T locally. The capistrano task I wrote above should show up as cap tasks:my_rake_task.
If you want to be ably to run any available rake task without configuring, do the following:
namespace :rake do
desc "Invoke rake task"
task :invoke do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, ENV['task']
# !!!see NOTE at end of answer!!!
end
end
end
end
end
Then you can write:
cap production deploy:invoke task=my:rake:task
NOTE: you might want to replace the execution line with
run "bin/rake RAILS_ENV=#{rails_env} #{ENV['task']}"
to use the same syntax as the tutorial (without the binstubs you might need to configure capistrano/bundler and capistrano/rbenv first ...)
Check out capistrano-rake
Once installed, run any rake task on your production/staging servers without messy capistrano recipes by simply doing this:
$ cap production invoke:rake TASK=your:rake:task
Full Disclosure: I wrote it

Rails - Rake test and rubocop in one task

I'm trying to setup my rails project so that all the verification required by a contributor is in one command, currently we have been running:
rake test
But now we also want to use rubocop for static analysis:
rubocop -R -a
I want this to be executable in one simple rake task. It would be nice to override 'rake test' to run rubocop then the standard rake test stuff for a rails project, as then no-one will have to remember to change the command. But if I have to create a separate rake task, that's probably fine too.
I've seen the rubocop rake integration here, at the bottom, but I'm not sure how to bundle that with 'rake test' into one task... Any thoughts?
I prefer to set my default task to run rubocop then the tests. Either way, it is a good idea to have those tasks separate rather than have one task do two things.
require 'rubocop/rake_task'
task :default => [:rubocop, :test]
desc 'Run tests'
task(:test) do
# run your specs here
end
desc 'Run rubocop'
task :rubocop do
RuboCop::RakeTask.new
end
Your tasks:
> rake -T
rake rubocop # Run rubocop
rake test # Run tests
This is the .rake file that I ended up with in the end.
desc 'Run tests and rubocop'
task :validate do
Rake::Task['rubocop'].invoke
Rake::Task['test'].invoke
end
task :rubocop do
require 'rubocop'
cli = Rubocop::CLI.new
cli.run(%w(--rails --auto-correct))
end
You could easily define your own rake task which first invokes Rails' test rake task and then the code snippet you mentioned for rubocop.
For example, in a .rake file you could have something like that:
require 'rubocop/rake_task'
desc 'Run tests and rubocop'
task :my_test do
Rake::Task['test'].invoke
RuboCop::RakeTask.new
end
If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.
Finally, an alternative to creating your own rake task and sticking with rake test would be to modify your test_helper to invoke whatever you need invoked after testing is completed.
It appears to have changed from:
Rubocop::RakeTask.new
to:
RuboCop::RakeTask.new
See ma? I know how to CamelCase!
I used following .rake file to run the test and rubocop tasks at once:
task default: %w[rubocop test]
RuboCop::RakeTask.new(:rubocop) do |task|
task.patterns = ['**/*.rb']
task.fail_on_error = false
task.options = ["--auto-correct-all"]
end
task :test do
ruby 'test/program_test.rb'
end
The first line allows both tasks to be run by calling rake.
Command line arguments can also be added in the options array.

Rake: How do I forward params to child Rake calls?

So, in my rake command, when I do --trace, it only does it on the command I manually execute, and none of the rake commands that the custom rake executes.
My Rake command:
namespace :db do
task :regenesis do
#because of how devestating this command could be, it's going
# to be forced to use the Test Environment
puts "Re-Generating the Database"
Rake::Task["db:drop RAILS_ENV=test --trace"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:bootstrap RAILS_ENV=test"].invoke
end
end
Twould be ideal to not have to hard code the --trace in there =D
So, I should be able to do rake db:regenesis --trace, and it should append trace onto all of those rake commands.
How do I do that?
Please check this question. If you cannot modify your tasks to add the parameters ( for some reason ), then you can use environment variables, like:
namespace :db do
task :regenesis do
#because of how devestating this command could be, it's going
# to be forced to use the Test Environment
puts "Re-Generating the Database"
ENV["extra_option"] = "--trace"
Rake::Task["db:drop RAILS_ENV=test"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:create RAILS_ENV=test"].invoke
Rake::Task["db:bootstrap RAILS_ENV=test"].invoke
end
end
And in your tasks you'd have to look for ENV["extra_option"]

Resources