I've created a Capistrano task to execute a rake command. I plan to redirect the output (STDOUT) to a file. E.g.
cap production invoke:rake TASK=mytask > out
This works, but my output includes some additional Capistrano status output, e.g.
00:00 invoke:rake
01 $HOME/.rbenv/bin/rbenv exec bundle exec rake mytask
...
✔ 01 ubuntu#mydomain.com 11.399s
Is there any way to suppress this?
It's probably stderr output. If so, you can redirect standard error to standard out like so:
cap production invoke:rake TASK=mytask > out 2>&1
Okay, so I think I figured out a pretty good solution.
The key was to use capture (not documented in Capistrano 3.x, but still works).
https://github.com/capistrano/capistrano-2.x-docs/blob/master/2.x-DSL-Action-Inspection-Capture.md
namespace :invoke do
desc "Execute a rake task on a remote server"
task :rake do
if ENV['TASK']
on roles(:app) do
with rails_env: fetch(:rails_env) do
puts capture :rake, ENV['TASK']
end
end
else
puts "\n\nFailed! You need to specify the 'TASK' parameter!",
"Usage: cap <stage> invoke:rake TASK=your:task"
end
end
end
Related
I have whenever gem setup properly. How can I run that capistrano from my whenever schedule.rb?
my schedule.rb
every 1.minute, roles: [:app] do
# how to run here a capistrano task
# invoke 'my_app:test'
end
My capistrano task:
namespace :my_app do
desc 'test'
task :test do
on roles(:web) do
puts "the task runs"
end
end
end
Or should I move that task into a rake task. And should I run that rake task within whenever and capistrano?
Jan, you may find documentation very useful https://github.com/javan/whenever. The code example below - I just copied and edited it from the doc.
schedule.rb
# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, at: '12:20am', roles: [:web] do
rake "app_server:task"
end
lib/tasks/test_task.rb
namespace :my_app do
desc 'test'
task :test do
puts "the task runs"
end
end
I believe it's easier to create Rake task and run it via Whenever. You can choose the Role, so basically you don't need to use Capistrano task (I believe you want to use it just because of Role).
I'd suggest your latter option, moving the logic to a rake task and executing it from both whenever and Capistrano. It'll be vastly easier and cleaner to do.
I want to create a gem that has de ability to take a task and wrap all commands in another command.
For example, the capistrano3-unicorn gem unicorn:start task will execute on the server something like bundle exec unicorn -c unicorn.rb -E production, but the execute method is wrapped by a within method, so the command to be executed on the server will be something like cd /home/deploy/application/myapp/current && bundle exec unicorn -c unicorn.rb -E production
I want to be able to create a rake task that takes that unicorn:start task and wrap it inside another task.
For example if I want to create an upstart config file for the app, I could adds this command to a upstart.conf template and the run service my-unicorn-app start
That would be a use case I'm trying to pursue.
In SSHKit formatters the write command is called with a command arg that have what I'm looking for. But I need this at capistrano task level.
Thanks
I think what you're asking is how to access the exact command that is sent via SSH. What you're looking for is the #command method, on the return value of which you can call #to_command. Since #command is a private method, we need to use #send.
namespace :unicorn
task :set_restart_command do
on(roles(:web)) do
within release_path do
set(:unicorn_start_command,
send(:command, :unicorn, "-c unicorn.rb -E production").to_command)
end
end
end
end
Now, in another task, you can use fetch(:unicorn_start_command)
Here is an tutorial how to pass parameters to an capistrano 3 task.
namespace :task do
desc 'Execute the specific cmd task'
task :invoke, :command do |task, args|
on roles(:app) do
execute :cmd, args[:command]
end
end
end
Can executed with:
$ cap staging "task:invoke[arg]"
How can i use this in my deploy.rb? The following does not work.
before :started, "task:invoke[arg]"
Not sure about before/after, but with Capistrano 3 you can always use the rake syntax and call task from within another task:
Rake::Task["mynamespace:mytask"].invoke(arg)
You can use invoke method:
before :started, :second_param do
invoke 'task:invoke', 'arg'
end
Also one thing which might be helpful, capistrano and rake allowing to execute task only first time, this might be common issue for task with parameters, cause you may reuse it with different value. To allow doing it you need to re-enable the task:
namespace :task do
desc 'Execute the specific cmd task'
task :invoke, :command do |task, args|
on roles(:app) do
execute :cmd, args[:command]
task.reenable # <--------- this how to re-enable it
end
end
end
I have a project including a number of vendored javascripts, e.g. jQuery. I'm including these scripts as git submodules. However, for my build process, I need the built script, not the whole repository of that script. So I'm trying to set up a rake task to build each script - preferably using the script's own rakefile - and then copy the built script into my asset directory.
file "app/vendor/scriptname.js" do
# Call the task that builds the script here
sh "cp app/vendor/script-submodule/dist/scriptname.js app/vendor/"
end
desc "Make vendor scripts available for build"
task :vendor => "app/vendor/scriptname.js" do
end
If I use import 'app/vendor/scriptname/Rakefile' in my Rakefile, I should have access to the rake task that builds the script, right? How would I call it? Or should I just use sh "cd app/vendor/script-submodule/ && rake dist" and call it good?
I'm working out a similar problem and it would seem to work just fine by calling the rake task as you normally would. Here's what my example looks like, see if you can get yours to fit.
# Rakefile
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
load 'engines/foo_engine/Rakefile'
MyApp::Application.load_tasks
Then in my submodule's Rakefile:
# engines/foo_engine/Rakefile
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each do |f|
load f
end
And a sample rake task:
# engines/foo_engine/lib/tasks/foo/bar/task.rake
namespace :foo do
namespace :bar do
desc "FooBar task"
task :foo_bar => :environment do
# perform task
end
end
end
Then from the command prompt:
rake foo:bar:task
I need to profile the rake task. Cause I'm noob I know only how to profile .rb code
like this: ruby -Ilib -S ruby-prof -p graph_html profile.rb > profile.html
But how do I profile a specific Rake task?
Rake is just a Ruby script, so you should be able to just call ruby-prof against rake, in the same way you'd profile any other script.
Given your invocation of ruby-prof, try:
ruby -Ilib -S ruby-prof -p graph_html `which rake` TASK > profile.html
I've just used the following command line:
ruby-prof -p graph_html /usr/local/bin/rake19 import_from_aws file=~/sourcedata batch=test1 > /tmp/profile.html
To profile an invocation of:
rake19 import_from_aws file=~/sourcedata batch=test1
If you want 'coarse' profiling and want to find out which task is the bottleneck, I suggest Mike William's excellent piece of code from here. It worked beautifully when I was profiling my Rake tasks.
module Rake
class Task
def execute_with_timestamps(*args)
start = Time.now
execute_without_timestamps(*args)
execution_time_in_seconds = Time.now - start
printf("** %s took %.1f seconds\n", name, execution_time_in_seconds)
end
alias :execute_without_timestamps :execute
alias :execute :execute_with_timestamps
end
end
I think it's worth mentioning that you might want to profile it using bundle instead of rake directly if you happen to be using bundler.
ruby-prof -p graph_html `which bundle` -- 'exec' 'rake' '-T' > profile.html