Skip to content

Commit

Permalink
Genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Aug 23, 2017
1 parent ae49d06 commit 4a98cb2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2016-2017 © Teamwork.com

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# reload
The reload package offers lightweight automatic reloading of running processes.
After initialisation with `reload.Do()` any changes to the binary will restart
the process.

This works well with the standard `go install` and `go build` commands.

This is an alternative to the "restart binary after any `*.go` file
changed"-strategy that some other projects take (such as
[go-watcher](https://github.com/canthefason/go-watcher)). The advantage of
`reload`'s approach is that you have a bit more control over when the process
restarts, and it only watches a single directory for changes, which has some
performance benefits when used over NFS or Docker.

Caveat: the old process will continue running happily if `go install` has a
compile error, so if you missed any compile errors due to switching the window
too soon you may get confused.
67 changes: 67 additions & 0 deletions reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package reload offers lightweight automatic reloading of running processes.
//
// After initialisation with `reload.Do()` any changes to the binary will
// restart the process.
package reload // import "github.com/teamwork/reload"

import (
"fmt"
"os"
"path/filepath"
"syscall"
"time"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/teamwork/log"
)

// Do reload the current process when its binary changes.
func Do() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "cannot setup watcher")
}
defer watcher.Close() // nolint: errcheck

bin, err := filepath.Abs(os.Args[0])
if err != nil {
return errors.Wrapf(err, "cannot get Abs of %#v", os.Args[0])
}

dir := filepath.Dir(bin)

done := make(chan bool)
go func() {
for {
select {
case err := <-watcher.Errors:
log.Error(err)
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write && event.Name == bin {
// Wait for writes to finish.
time.Sleep(100 * time.Millisecond)
exec(bin)
}

}
}
}()

// Watch the directory, because a recompile renames the existing file
// (rather than rewriting it), so we won't get events for that.
if err := watcher.Add(dir); err != nil {
return errors.Wrapf(err, "cannot add %#v to watcher", dir)
}

log.Printf("restarting %#v when it changes", bin)
<-done
return nil
}

func exec(bin string) {
err := syscall.Exec(bin, []string{bin}, os.Environ())
if err != nil {
panic(fmt.Sprintf("cannot restart: %v", err))
}
}

0 comments on commit 4a98cb2

Please sign in to comment.