kardianos / service

Run go programs as a service on major platforms.
zlib License
4.46k stars 678 forks source link

service + httpserver + graceful shutdown #59

Open dryaf opened 8 years ago

dryaf commented 8 years ago

Hi,

I'd like to use "service" with my webservice. I use https://github.com/facebookgo/grace for graceful shutdown. -> server waits until connections are closed so no data gets lost.

This only works when I change your code to not being async.

// Program structures.
//  Define Start and Stop methods.
type program struct {

}

func (p *program) Start(s service.Service) error {
        //go p.run()
    //return nil
    return gracehttp.Serve(e.Routes().Server("0.0.0.0:3000"))
}

func (p *program) Stop(s service.Service) error {
    // Any work in Stop should be quick, usually a few seconds at most.
    logger.Info("I'm Stopping!")
    return nil
}

Does it have to be async? Will I have problems using "service"?

best ps: govendor is great! Saved my sanity today :)

kardianos commented 8 years ago

I integrated something similar for a PR for Caddy. However I've never looked closely at facebook's grace. It might be possible to integrate, but I'll need to look into how grace works. Feel free to also describe how it works too.

kardianos commented 8 years ago

I don't have time today to look into this, but you may be instersted in the Caddy patch which supported a hot reload: https://github.com/mholt/caddy/pull/588

dryaf commented 8 years ago

Well I tested the code I posted here. And the gracefull shutdown works. Though maybe not being async in your code isn't a problem.