inconshreveable / go-update

Build self-updating Golang programs
Other
2.12k stars 235 forks source link

Possible to automatically reload itself? #5

Closed gust1n closed 9 years ago

gust1n commented 10 years ago

Wouldn't it be cool if go-update also supported automatic reload of the go binary (itself)? I don't know if it's possible or even desirable in the long run it would make the deploy process even smoother, just push a new release and then all nodes running a service would auto update and restart!

inconshreveable commented 10 years ago

I think this would be great to add! I would happily take a patch for this. At one point in the past I toyed around with adding an API call for this but got stuck and just haven’t had the time to come back to it yet.

On Apr 27, 2014, at 11:48 PM, Joakim Gustin notifications@github.com wrote:

Wouldn't it be cool if go-update also supported automatic reload of the go binary (itself)? I don't know if it's possible or even desirable in the long run it would make the deploy process even smoother, just push a new release and then all nodes running a service would auto update and restart!

— Reply to this email directly or view it on GitHub.

marbemac commented 9 years ago

I'm very interested in this. However, I'm not sure where to start - could either of you describe, at a high level, how one might build this functionality? Can a binary reload itself?

inconshreveable commented 9 years ago

@marbemac It can.

Here's a very simple example:

import (
    "os"
    "os/exec"

    "bitbucket.org/kardianos/osext"
)

func restart() error {
    name, err := osext.Executable()
    if err != nil {
        return err
    }
    return exec.Command(name, os.Args[1:]...).Start()
}

Keep in mind that doing this involves cooperation from your application to restart itself. Properly releasing resources or passing them to the new instance, remaining running until all in-progress processes are complete or aborted, etc.

A production-grade solution which can help with this is goagain (which also manages the restarting bit): https://github.com/rcrowley/goagain/

inconshreveable commented 9 years ago

Much time and reflection have proven that this kind of feature is out of scope for this package and best located in other packages (like goagain) that focus exclusively on that problem.