Skip to content

Instantly share code, notes, and snippets.

@nikushi
Last active August 29, 2015 14:00
Show Gist options
  • Save nikushi/11103614 to your computer and use it in GitHub Desktop.
Save nikushi/11103614 to your computer and use it in GitHub Desktop.

embeded mode

se = ServerEngine.create(nil, MyWorker, {
  :daemonize => true,
  :log => 'myserver.log',
  :pid_path => 'myserver.pid',
})
se.run

ここまでを追う

lib/serverengine.rb
  .create(server_module, worker_module, load_config_proc={}, &block
    ServerEngine::Daemon.new server_module, worker_module, load_config_proc, &block
|
v
lib/serverengine/daemon.rb
|
v
  class Daemon
    include ConfigLoader   # Daemon.new時にload_config_procをインスタンス変数にセット、@loggerの初期化
    
    #initialize            # @create_server_proc = Supervisor.create_server_proc    -> Supervison 初期化シーケンス

    #run
    |
    v
    #main
      rpipe, wpipe 作成
      fork
        |----------------------------------+
        parent                             child
          wpipe.close                        rpipe.close
          rpipeからchildpidを受信            Process.setsid             #ttyから切り離し、デーモン化
          @pid_pathにpid書き込み             fork
          exit 0                               |--------------------------------+
                                               parent                           child
                                               exit 0                              $0 = @daemon_process_name
                                                                                   wpipe.write Process.pid
                                                                                   ###権限の変更など
                                                                                   s = @create_server_proc.call 
                                                                                   stdin, stdout,stderrを/dev/nullにリダイレクト
                                                                                   サーバ(s)にシグナルハンドラをセット
                                                                                   wpipe.write "\n"
                                                                                   wpipe.close
                                                                                   s.main     #supervisorのmainへ
                                                                                   |
                                                                                   v
                                                                                   以後 lib/serverengine/supervisor.rb 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment