我需要创建一个rake任务,以通过ssh隧道进行一些 Activity 的记录操作。

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转发,所以我不确定这里的问题是什么。

有任何想法吗!?

谢谢

请您参考如下方法:

问题很可能与这里相同:

Cannot connect to remote db using ssh tunnel and activerecord

不要使用线程,您需要在另一个进程中 fork 导入器,以使其正常工作,否则您将被ssh事件循环锁定。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!