在远程服务器上执行 rake 任务 [英] Execute a rake task on a remote server

查看:42
本文介绍了在远程服务器上执行 rake 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

其中一个作业会对表执行大型 UPDATE,如果其他作业正在运行,通常会返回 postgres 死锁.

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

我已经有一个 rake 任务可以正常停止其他作业,但我只能从本地机器执行它.

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

我想要实现的是:

task :big_update => :environment do
  stop_tasks_on_another_servers

  # do the SQL UPDATE
  ...
end

stop_tasks_on_another_servers 应该在哪里在其他服务器上执行 rake 任务.

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

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

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

但它返回:

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?

推荐答案

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

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

这篇关于在远程服务器上执行 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆