-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |