Execute a rake task on a remote server

怎甘沉沦 提交于 2019-12-12 10:44:06

问题


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?


回答1:


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



回答2:


I've had success using this approach:

http://gistflow.com/posts/372-execute-rake-task-on-remote-server-with-capistrano



来源:https://stackoverflow.com/questions/20615329/execute-a-rake-task-on-a-remote-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!