8

生产环境的物理架构包括几台执行不同工作(rake 任务)的机器,它们都在同一个数据库上。

如果其他作业正在运行,其中一个作业会在UPDATE通常返回 postgres 死锁的表上执行大量操作。

我已经有一个 rake 任务来优雅地停止其他作业,但我只能从本地机器执行它。

我想要实现的是:

task :big_update => :environment do
  stop_tasks_on_another_servers

  # do the SQL UPDATE
  ...
end

stop_tasks_on_another_servers 应该rake task在其他服务器上执行 a 。

我最好的尝试是使用https://github.com/capistrano/sshkit gem。Capistrano 使用它的方式相同,但我在这里仍然缺少一步。我正在生产机器的 Rails 控制台上尝试以下操作:

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

但它返回:

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

我错过了什么或者有更简单的方法来执行远程任务?

4

2 回答 2

3

查看Rake 远程任务。这是一个片段,向您展示它是如何工作的:

require 'rake/remote_task'

set :domain, 'abc.example.com'

remote_task :foo do
  run "ls"
end
于 2013-12-17T14:51:51.653 回答
0

我使用这种方法取得了成功:

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

于 2013-12-17T02:24:17.167 回答