【发布时间】: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