kwfile

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 6 Imported by: 1

README

File Watcher

File Watcher watches over a file given in the config.

Usage

import (
	"fmt"
	"io/ioutil"
	"os"
	"time"

	"github.com/lalamove/konfig/watcher/kwfile"
)

func main() {
	f, _ := ioutil.TempFile("", "konfig")
	f.Write([]byte(`ABC`))

	defer os.Remove(f.Name())

	var n = kwfile.New(&kwfile.Config{
		Files: []string{f.Name()},
		Rate:  100 * time.Millisecond,
		Debug: true,
	})

	n.Start()

	time.Sleep(100 * time.Millisecond)

	f.Write([]byte(`12345`))

	var timer = time.NewTimer(200 * time.Millisecond)
	select {
	case now := <-timer.C:
		fmt.Println(now)
		break
	case <-n.Done():
		fmt.Println("done!")
		break
	case <-n.Watch():
		fmt.Println("file changed!") // will see this log
		break
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Files is the path to the files to watch
	Files []string
	// Rate is the rate at which the file is watched
	Rate time.Duration
	// Debug sets the debug mode on the filewatcher
	Debug bool
	// Logger is the logger used to print messages
	Logger nlogger.Provider
}

Config is the config of a FileWatcher

type FileWatcher

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

FileWatcher watches over a file given in the config

func New

func New(cfg *Config) *FileWatcher

New creates a new FileWatcher from the given *Config cfg

func (*FileWatcher) Close

func (fw *FileWatcher) Close() error

Close closes the FileWatcher

func (*FileWatcher) Done

func (fw *FileWatcher) Done() <-chan struct{}

Done indicates whether the filewatcher is done

func (*FileWatcher) Err

func (fw *FileWatcher) Err() error

Err returns the file watcher error

func (*FileWatcher) Start

func (fw *FileWatcher) Start() error

Start starts the file watcher

func (*FileWatcher) Watch

func (fw *FileWatcher) Watch() <-chan struct{}

Watch return the channel to which events are written

Jump to

Keyboard shortcuts

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