ssh トンネルを介してアクティブなレコード操作を実行するには、rake タスクを作成する必要があります。
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 syncを別のターミナルで実行すると成功しました。ただし、これらを組み合わせて、トンネルにエラーが発生した場合に同期を試行しないようにしたいと考えています。
ruby Threads と Net::SSH 転送を使用するのはこれが初めてなので、ここで何が問題なのかわかりません。
何か案は!?
ありがとう