Restarting web server from handler rather than command line?

agolangf · · 474 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, new to Go here :)</p> <p>I&#39;ve built small http daemon in Go using <a href="https://github.com/facebookgo/grace">https://github.com/facebookgo/grace</a> to handle the restarts, but now I need to be able to control it from web (restart namely). I looked at the source of the library and I can&#39;t see any exported methods to deal with such situation. It uses channels internally which are not accessible from outside (or i don&#39;t know how).</p> <p>So I came with this approach:</p> <pre><code>mux.HandleFunc(&#34;/restart&#34;, func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, &#34;Restaring pid %d.\n&#34;, os.Getpid()) syscall.Kill(os.Getpid(), syscall.SIGUSR2) return }) </code></pre> <p>Here is my question: It works but is this the right and idiomatic way to do it? Cheers and sorry if the question is too noobish :)</p> <hr/>**评论:**<br/><br/>tclineks: <pre><p>Your approach is valid.</p> <p>SIGUSR2 is the only &#39;entrypoint&#39; here.</p> <p><a href="https://github.com/facebookgo/grace/blob/053ab5d25436faedf3fe76fbf3da797c8c27c659/gracehttp/http.go#L99">https://github.com/facebookgo/grace/blob/053ab5d25436faedf3fe76fbf3da797c8c27c659/gracehttp/http.go#L99</a></p></pre>code-chimp: <pre><p>Thank you very much, was having second thoughts since worked from first try, and per Murphy&#39;s law this is wrong :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

474 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传