Coder Social home page Coder Social logo

autorestart's Introduction

Autorestart

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.

Quick start

go get github.com/slayer/autorestart

Basic usage

package main

import "github.com/slayer/autorestart"

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

Extended usage

package main

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

func main() {
    // set period
    autorestart.WatchPeriod = 3 * time.Second
    // custom file to watch
    autorestart.WatchFilename = "tmp/restart.txt"
    // custom restart function
    autorestart.RestartFunc = autorestart.SendSIGUSR2 // usefull for `github.com/facebookgo/grace`

    // 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

autorestart's People

Contributors

slayer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.