ruby-on-rails - Rails Rake和mysql ssh端口转发

标签 ruby-on-rails multithreading ssh rake portforwarding

我需要创建一个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事件循环锁定。

关于ruby-on-rails - Rails Rake和mysql ssh端口转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1920500/

相关文章:

ruby-on-rails - Rails Active Record 上的依赖销毁与依赖删除之间的区别

mysql - 在 Ruby 中查询 has_and_belongs_to_many 关联

c++ - 基础服务器占用大量CPU资源——如何缓解?

c++ - OpenMP 中树结构的线程安全

SSH-X "Warning: untrusted X11 forwarding setup failed: xauth key data not generated"

mysql - 使用 MySQL Workbench(或其他)通过代理通过 SSH 连接到 MySQL

ruby-on-rails - 如何动态添加路由到 Rails3 中的作用域资源?

ruby-on-rails - 使用 CanCan 的上下文感知授权

java - 多个 ExecutorService 在 main 之后完成

ssh - 如何通过主机从外部 SSH 到 VirtualBox guest ?