Rails Rake 和 mysql ssh 端口转发
我需要创建一个 rake 任务来通过 ssh 隧道执行一些活动记录操作。
rake 任务在远程 Windows 机器上运行,所以我想将东西保留在 ruby 中。这是我的最新尝试。
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 任务来创建隧道,然后在不同的终端中运行 rake 同步时,我取得了成功。然而,我想将这些结合起来,这样如果隧道出现错误,它就不会尝试同步。
这是我第一次使用 ruby Threads 和 Net::SSH 转发,所以我不确定这里的问题是什么。
有什么想法吗?
谢谢
I need to create a rake task to do some active record operations via a ssh tunnel.
The rake task is run on a remote windows machine so I would like to keep things in ruby. This is my latest attempt.
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
The task seems to hang at "tunnel ready" and never attempts the sync.
I have had success when running first a rake task to create the tunnel and then running the rake sync in a different terminal. I want to combine these however so that if there is an error with the tunnel it will not attempt the sync.
This is my first time using ruby Threads and Net::SSH forwarding so I am not sure what is the issue here.
Any Ideas!?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该问题很可能与此处相同:
无法使用 ssh 隧道和 activerecord 连接到远程数据库
不要使用线程,您需要在另一个进程中分叉导入器才能使其工作,否则您将锁定 ssh 事件循环。
The issue is very likely the same as here:
Cannot connect to remote db using ssh tunnel and activerecord
Don't use threads, you need to fork the importer off in another process for it to work, otherwise you will lock up with the ssh event loop.
仅将代码本身作为 ruby 脚本运行(禁用 Importer.sync)似乎可以正常工作,没有任何错误。这向我表明问题出在 Import.sync 上。您可以粘贴 Import.sync 代码吗?
Just running the code itself as a ruby script (with Importer.sync disabled) seems to work without any errors. This would suggest to me that the issue is with Import.sync. Would it be possible for you to paste the Import.sync code?
只是一个猜测,但这里的问题可能是您的 :sync rake 任务将 Rails 环境作为先决条件吗?您的 Importer 类初始化中是否发生了任何事情,需要依赖加载时可用的 SSH 连接才能正常工作?
我想知道如果你不让环境成为这项任务的先决条件,而是尝试......,会发生什么?
Just a guess, but could the issue here be that your :sync rake task has the rails environment as a prerequisite? Is there anything happening in your Importer class initialization that would rely on this SSH connection being available at load time in order for it to work correctly?
I wonder what would happen if instead of having environment be a prereq for this task, you tried...