Categorygithub.com/seitart/autorestart
repositorypackage
0.0.0-20200118174117-09bb20ae362c
Repository: https://github.com/seitart/autorestart.git
Documentation: pkg.go.dev

# README

Autorestart

This project was forked from github.com/slayer/autorestart and works under windows.
autorestart is used for autorestarting Go apps in development or staging environments, but you can try to use in production at your own risk. It designed to be as lightweight as possible, it does not uses fsnotify it just periodically poll os.Stat(filename)

Where is filename is a self binary by default, but you can setup to watch tmp/restart.txt or something else. On file change it will call syscall.Exec(selfbinary) or you can use function SendSIGUSR2 (useful for grace restart) or write your own.

Works well with go build and rsync or nice package github.com/inconshreveable/go-update.

Quick start

go get github.com/SeitArt/autorestart

Basic usage

package main

import "github.com/SeitArt/autorestart"

func main() {
    autorestart.StartWatcher()
    http.ListenAndServe(":8080", nil) // for example
}

Extended usage

package main

import (
    "log"
    "http"
    "github.com/SeitArt/autorestart"
)

func main() {
    // set period
    autorestart.WatchPeriod = 3 * time.Second
    // custom file to watch
    autorestart.WatchFilename = "tmp/restart.txt"

    // or
    autorestart.RestartFunc = func () {
        if proc, err := os.FindProcess(os.Getpid()); err == nil {
            proc.Signal(syscall.SIGHUP)
        }
    }

    // Notifier
    restart := autorestart.GetNotifier()
    go func() {
        <- restart
        log.Printf("I will restart shortly")
    }()

    autorestart.StartWatcher()
    http.ListenAndServe(":8080", nil) // for example
}

Licence

Apache 2.0