filewatcher

package
v0.0.0-...-bde19ca Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package filewatcher provides a go implementation of baseplate's FileWatcher: https://baseplate.readthedocs.io/en/stable/baseplate/file_watcher.html

Example

This example demonstrates how to use filewatcher.

package main

import (
	"context"
	"encoding/json"
	"io"
	"log"
	"time"

	"github.com/fizx/baseplate.go/filewatcher"
)

func main() {
	const (
		// The path to the file.
		path = "/opt/data.json"
		// Timeout on the initial read.
		timeout = time.Second * 30
	)

	// The type of the parsed data
	type dataType map[string]interface{}

	// Wrap a json decoder as parser
	parser := func(f io.Reader) (interface{}, error) {
		var data dataType
		err := json.NewDecoder(f).Decode(&data)
		return data, err
	}

	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	data, err := filewatcher.New(
		ctx,
		path,
		parser,
		nil, // logger
	)
	if err != nil {
		log.Panic(err)
	}

	getData := func() dataType {
		return data.Get().(dataType)
	}

	// Whenever you need to use the parsed data, just call getData():
	_ = getData()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var InitialReadInterval = time.Second / 2

InitialReadInterval is the interval to keep retrying to open the file when creating a new file watcher, when the file was not initially available.

It's intentionally defined as a variable instead of constant, so that the caller can tweak its value when needed.

Functions

This section is empty.

Types

type Parser

type Parser func(f io.Reader) (data interface{}, err error)

A Parser is a callback function to be called when a watched file has its content changed, or is read for the first time.

Please note that Parser should always return the consistent type. Inconsistent type will cause panic, as does returning nil data and nil error.

type Result

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

Result is the return type of New. Use Get function to get the actual data.

func New

func New(ctx context.Context, path string, parser Parser, logger log.Wrapper) (*Result, error)

New creates a new file watcher.

If the path is not available at the time of calling, it blocks until the file becomes available, or context is cancelled, whichever comes first.

When logger is non-nil, it will be used to log errors, either returned by parser or by the underlying file system watcher. Please note that this does not include errors returned by the first parser call, which will be returned directly.

func (*Result) Get

func (r *Result) Get() interface{}

Get returns the latest parsed data from the file watcher.

Although the type is interface{}, it's guaranteed to be whatever actual type is implemented inside Parser.

func (*Result) Stop

func (r *Result) Stop()

Stop stops the file watcher.

After Stop is called you won't get any updates on the file content, but you can still call Get to get the last content before stopping.

It's OK to call Stop multiple times. Calls after the first one are essentially no-op.

Jump to

Keyboard shortcuts

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