Detect if application was started as HTTP server or not (rake task, rconsole etc)

前端 未结 6 1972
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 19:47

I\'m using EventMachine and Monetarily to start e TCP server along with my rails application. This is started from config/initializers/momentarily.rb.

6条回答
  •  猫巷女王i
    2020-12-09 20:18

    Set an environment variable in config.ru file, and use it anywhere in the code to detect if it's executed using a rails server command only.

    For e.g.

    • File: config.ru

      ENV['server_mode'] = '1'
      

    And using it somewhere as:

    • File: config/environment.rb

      Thread.new { infinite_loop! }.join if ENV['server_mode'] = '1'
      

    Reference: Answer

提交回复
热议问题