【问题标题】:Rails initializer (using JRuby, Puma) at_exit is occasionally not executedRails 初始化程序(使用 JRuby、Puma)at_exit 偶尔不执行
【发布时间】:2017-10-22 03:25:33
【问题描述】:

我正在尝试制作一个包含march_hare 并用作我们Rails 应用程序的一部分的小库。它需要在应用启动时连接并在关闭时断开连接。

我的目标是通过初始化程序实现类似于 ruby-kafka Rails 集成:

  • 配置我的服务
  • 运行它
  • 指定关机程序

问题是我的at_exit 块有时不被执行。这个问题的原因可能是什么?有没有办法修复它并确保我的 at_exit 块被调用?

调查问题后,我使用以下初始化程序创建了一个空白演示应用程序:

class SomeClass
  def self.shutdown(reason)
    msg = "#{Time.now} SHUTDOWN via #{reason}!"

    puts msg
    open('log/development.log', 'a') do |f|
      f.puts msg
    end
  end
end

at_exit { SomeClass.shutdown(:at_exit1) }
at_exit { SomeClass.shutdown(:at_exit2) }
at_exit { SomeClass.shutdown(:at_exit3) }

puts "#{Time.now} INITIALIZED!"

在 10 个运行 + 终止周期中有 9 个我得到:

$ rails s
=> Booting Puma
=> Rails 4.2.7.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
2017-05-22 15:29:16 +0300 INITIALIZED!
Puma starting in single mode...
* Version 3.4.0 (jruby 9.1.2.0 - ruby 2.3.0), codename: Owl Bowl Brawl
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
Exiting
=== puma shutdown: 2017-05-22 15:29:30 +0300 ===
- Goodbye!
2017-05-22 15:29:30 +0300 SHUTDOWN via at_exit3!
2017-05-22 15:29:30 +0300 SHUTDOWN via at_exit2!
2017-05-22 15:29:30 +0300 SHUTDOWN via at_exit1!

但有时结果是:

$ rails s
=> Booting Puma
=> Rails 4.2.7.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
2017-05-22 15:29:45 +0300 INITIALIZED!
Puma starting in single mode...
* Version 3.4.0 (jruby 9.1.2.0 - ruby 2.3.0), codename: Owl Bowl Brawl
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
Exiting
=== puma shutdown: 2017-05-22 15:30:17 +0300 ===
- Goodbye!
2017-05-22 15:30:17 +0300 SHUTDOWN via at_exit2!
2017-05-22 15:30:17 +0300 SHUTDOWN via at_exit1!
  • 我的 Gemfile 仅添加了 gem 'puma'
  • Ruby 版本:jruby 9.1.2.0
  • Rails 版本:Rails 4.2.7.1
  • 系统版本:macOS 10.12.4

【问题讨论】:

    标签: ruby-on-rails jruby puma


    【解决方案1】:

    这与 JRuby 有关。您可以尝试将 JRuby 升级到最新版本,看看是否已修复。

    请关注此主题以获得更多参考。

    https://github.com/jruby/jruby/issues/5437

    【讨论】:

    • 将 JRuby 升级到 9.2.5.0 为我解决了这个问题。 at_exit 现在每次都会被调用。
    • @nbbg 你能查到这个吗?
    猜你喜欢
    • 2015-11-22
    • 2011-09-20
    • 2015-04-14
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多