我需要创建一个rake任务来通过ssh隧道执行一些活动的记录操作。rails Rake和mysql ssh端口转发
该rake任务运行在远程Windows机器上,所以我想保持红宝石的东西。这是我最近的尝试。
desc "Syncronizes the tablets DB with the Server"
task(:sync => :environment) do
require 'rubygems'
require 'net/ssh'
begin
Thread.abort_on_exception = true
tunnel_thread = Thread.new do
Thread.current[:ready] = false
hostname = 'host'
username = 'tunneluser'
Net::SSH.start(hostname, username) do|ssh|
ssh.forward.local(3333, "mysqlhost.com", 3306)
Thread.current[:ready] = true
puts "ready thread"
ssh.loop(0) { true }
end
end
until tunnel_thread[:ready] == true do
end
puts "tunnel ready"
Importer.sync
rescue StandardError => e
puts "The Database Sync Failed."
end
end
该任务似乎挂在“隧道就绪”,并从不尝试同步。
我已经成功运行第一个rake任务来创建隧道,然后在其他终端中运行耙式同步。不过,我想将这些结合起来,这样如果隧道发生错误,它就不会尝试同步。
这是我第一次使用ruby线程和Net :: SSH转发,所以我不确定这里有什么问题。
任何想法!?
感谢
导入同步代码太多,无法在这里发布,但它基本上是这种修改=> http://openmonkey.com/articles/2009/05/importing-legacy-data-in-rails。 修改它也更新记录以及导入它们。 它使用两套模型,一套用于本地遗留数据库,另一套用于服务器上的数据库(通过隧道)。 – stellard 2009-12-28 04:41:55
好的,它应该工作。我认为下一步是运行脚本/控制台并手动执行命令。如果您正在运行Unix,还可以尝试在另一个窗口中窥探该接口,以查看是否有任何类型的流量。喜欢的东西: 窥探-i LO端口3333 或 的tcpdump -i接口名称端口3333 当然,我可能有错误的命令 - 检查手册页。 如果这没有帮助,请尝试查看是否可以通过该端口执行手动查询,而不是运行同步。希望这不是某种延迟(尽管我会认为你会看到早些时候运行这些任务)。 – Rilindo 2009-12-28 18:28:28