我需要創建一個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