autoreload

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2022 License: Unlicense Imports: 9 Imported by: 1

README

GoDoc

autoreload

autoreload provides a package and command for automatically reloading an executable when that executable changes. It is intended to be used in a local development environment to reload the executable after it has been modified. An example use case would be to reload a go web app after you have edited the source code and recompiled the executable.

This approach can be useful when you prefer to manually rebuild your application instead of relying on functionality that watches your source files, recompiles the application and finally restarts it. In my experience, the manual approach to rebuilding allows for greater control of when it is triggered and provides more visibility on when it completes so that you can know that your changes are present when you retest. Additionally, if you run your local environment via Docker, e.g. something like go-local, this approach to reloading the application avoids having to install anything extra on your host machine.

Installation

autoreload can be used as a package that is integrated into your application or as a command that is supplied an executable to monitor.

Installation via Package

To integrate the package into your application, follow the example below.

package main

import (
    "github.com/agschwender/autoreload"
)

func main() {
    // Application setup
    
    autoreload.New().Start()

    // Application run and waiting
}

See the provided example for greater detail on how to integrate the package into your application.

Installation via Command

To integrate the command into your application, you must first install the autoreload command:

$ go install github.com/agschwender/autoreload/autoreloader@v1.1.2

Once installed, you can then execute the command by supplying it with the executable you want it to monitor and restart. For example if you wanted to run your server command, it may look like this:

$ autoreloader server --port=8080

Demo

You can verify the behavior of the package or command installation by using the provided example command.

In one terminal, compile the commands

$ go install ./...

In another terminal, run

$ example
2022/11/18 10:06:43 Starting application
2022/11/18 10:06:43 Auto-reload is enabled
2022/11/18 10:06:43 Starting HTTP server

Change the example/main.go file and then re-install, using the first terminal

$ go install ./...

You should see the reload happen in your second terminal

2022/11/18 10:06:57 Executable changed; reloading process
2022/11/18 10:06:57 Received change event, shutting down
2022/11/18 10:06:58 Starting application
2022/11/18 10:06:58 Auto-reload is enabled
2022/11/18 10:06:58 Starting HTTP server 2

Similarly, you can run via the autoreloader with the example command's built-in reloading turned off.

In your second terminal, run

$ autoreloader example --autoreload=false
2022/11/18 10:10:11 Starting application
2022/11/18 10:10:11 Starting HTTP server

Again make a change to the example/main.go file and then re-install, using the first terminal

$ go install ./...

You should see the reload happen in your second terminal

2022/11/18 10:11:08 Executable changed; reloading process
2022/11/18 10:11:09 Killing process
2022/11/18 10:11:09 Starting application
2022/11/18 10:11:09 Starting HTTP server 2

Documentation

Overview

Package autoreload restarts a process if its executable changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCommand

func WithCommand(cmd string) option

WithCommand defines the command executable that AutoReloader should watch. By default, this will be the currently running command.

func WithLogger

func WithLogger(logger Logger) option

WithLogger defines the logger that the AutoReloader will use. By default, it will log using the built-in log package. When a nil value is supplied for the logger, logging will be disabled.

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) option

WithMaxAttempts defines how many times the AutoReloader should attempt to reload the application. By default, this is 10. If the supplied maxAttempts is less than 1, it will be treated as 1.

func WithOnReload

func WithOnReload(onReload onReloadFunc) option

WithOnReload defines a callback that is executed just prior to reloading the application. This is useful for gracefully shutting down your application.

Types

type AutoReloader

type AutoReloader struct {
	// contains filtered or unexported fields
}

AutoReloader provides functionality for reloading an application.

func New

func New(opts ...option) AutoReloader

New creates a new AutoReloader with the supplied options.

func (AutoReloader) Start

func (ar AutoReloader) Start()

Start launches a goroutine that periodically checks if the modified time of the command has changed. If so, the binary is re-executed with the same arguments. This is a developer convenience and not intended to be started in a production environment.

func (AutoReloader) Stop

func (ar AutoReloader) Stop()

Stop will stop the autoreloader from watching the executable and reloading it.

type Logger

type Logger interface {
	// Info is intended to log an informational message
	Info(string)

	// Error is intended to log an error message
	Error(string, error)
}

Logger defines an interface for logging info and fatal errors out of the autoreloader process.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL