Rails Rake 和 mysql ssh 端口转发

发布于 2024-08-15 03:26:14 字数 1150 浏览 6 评论 0原文

我需要创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-08-22 03:26:14

该问题很可能与此处相同:

无法使用 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.

迷离° 2024-08-22 03:26:14

仅将代码本身作为 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?

愿与i 2024-08-22 03:26:14

只是一个猜测,但这里的问题可能是您的 :sync rake 任务将 Rails 环境作为先决条件吗?您的 Importer 类初始化中是否发生了任何事情,需要依赖加载时可用的 SSH 连接才能正常工作?

我想知道如果你不让环境成为这项任务的先决条件,而是尝试......,会发生什么?

...
Rake::Task["environment"].execute
Importer.sync
...

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...

...
Rake::Task["environment"].execute
Importer.sync
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文