Execute code once Sinatra server is running

后端 未结 4 1028
陌清茗
陌清茗 2021-01-18 14:33

I have a Sinatra Application enclosed in Sinatra::Base and I\'d like to run some code once the server has started, how should I go about doing this?

Her

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 14:59

    This is how I do it; basically running either sinatra or the other code in a separate thread:

    require 'sinatra/base'
    
    Thread.new { 
      sleep(1) until MyApp.settings.running?
      p "this code executes after Sinatra server is started"
    }
    class MyApp < Sinatra::Application
      # ... app code here ...
    
      # start the server if ruby file executed directly
      run! if app_file == $0
    end
    

提交回复
热议问题