Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- var (
- ctx context.Context
- cancel func()
- )
- func main() {
- ctx, cancel := context.WithCancel(context.Background)
- defer cancel() // ensures on exit the routines are shut
- // waits for ^c in the background
- go interruptWatcher()
- // waits for file changes in /tmp or for context cancel
- watch(ctx)
- }
- func watch(ctx context.Context) {
- events := make(chan notify.EventInfo, 1)
- notify.Watch("/tmp", events, notify.InDelete, notify.InCloseWrite, notify.InMovedTo)
- for {
- select {
- case event := <-events:
- fmt.Printf("Handling event %#v\n", event)
- case <-ctx.Done(): // ^c notifies on this and we know to exit
- notify.Stop(events)
- return
- }
- }
- }
- func interruptWatcher() {
- sigs := make(chan os.Signal, 1)
- signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
- for {
- select {
- case sig := <-sigs:
- switch sig {
- case syscall.SIGINT, syscall.SIGTERM:
- fmt.Printf("Shutting down on %s", sig)
- cancel()
- }
- case <-ctx.Done():
- return
- }
- }
- }
Add Comment
Please, Sign In to add comment