Execute a rake task on a remote server

3.9k views Asked by At

The physical architecture of the production environment includes several machines doing different jobs (rake tasks), all of them over the same database.

One of the jobs would do a large UPDATE over a table that usually returns a postgres deadlock if the other jobs are running.

I already have a rake task to gracefully stop the other jobs, but I can only execute it from the local machines.

What I want to achieve is:

task :big_update => :environment do
  stop_tasks_on_another_servers

  # do the SQL UPDATE
  ...
end

where the stop_tasks_on_another_servers should execute a rake task on the other servers.

My best try was to use the https://github.com/capistrano/sshkit gem. The same that Capistrano uses it, but I'm still missing a step here. I'm trying the following on a rails console in the production machine:

require 'sshkit/dsl'
hosts = ['machine1', 'machine2']

on hosts do
  within "/home/me/project/current" do
    with rails_env: :production do
      rake "stop_tasks"
    end
  end
end

But it returns:

INFO [70a0610a] Running /usr/bin/env rake stop_tasks on machine1
SSHKit::Command::Failed: rake stdout: Nothing written

What am I missing or is there an easier way to execute remote tasks?

2

There are 2 answers

1
Josh On BEST ANSWER

Check out Rake Remote Task. Here is a snippet to show you how it works:

require 'rake/remote_task'

set :domain, 'abc.example.com'

remote_task :foo do
  run "ls"
end
1
bryanp On