Rails初始化程序(使用JRuby,Puma)at_exit偶尔也不会执行

问题描述 投票:3回答:1

我正在尝试创建一个包装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个回答
0
投票

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

否则请关注此主题以获取更多参考。

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

© www.soinside.com 2019 - 2024. All rights reserved.