senju

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2019 License: MIT Imports: 6 Imported by: 1

README

senju

file watch event handle framework using fsnotify

usage

package main

import (
	"context"
	"fmt"
	"github.com/wano/senju"
	"gopkg.in/fsnotify/fsnotify.v1"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

func main() {
	sut := senju.New()
	eventHandler := senju.NewEventHandler()
	eventHandler.SetHandler(func(event fsnotify.Event) func(context.Context) error {
		return func(ctx context.Context) error {
			fmt.Println(event)
			return nil
		}
	}, senju.Rename) //watch Rename event
	sut.Add("/tmp/senju", eventHandler)

	sigCh := make(chan os.Signal)
	signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT)

	wg := &sync.WaitGroup{}
	go func(){
		defer wg.Done()
		if err := sut.Run(); err != nil {
			log.Println(err)
		}
	}()
	wg.Add(1)
	<-sigCh
	sut.Close()
	wg.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandler

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

func NewEventHandler

func NewEventHandler() *EventHandler

func (*EventHandler) GetHandler

func (e *EventHandler) GetHandler(name EventName) (Handler, bool)

func (*EventHandler) SetHandler

func (e *EventHandler) SetHandler(handler Handler, name EventName)

type EventName

type EventName = string
const (
	Create EventName = "Create"
	Write  EventName = "Write"
	Rename EventName = "Rename"
	Remove EventName = "Remove"
	Chmod  EventName = "Chmod"
)

type Handler

type Handler func(fsnotify.Event) func(context.Context) error

type Senju

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

func New

func New() *Senju

func (*Senju) Add

func (s *Senju) Add(name string, handler *EventHandler)

func (*Senju) Close

func (s *Senju) Close() error

func (*Senju) Run

func (s *Senju) Run() error

func (*Senju) RunWithContext

func (s *Senju) RunWithContext(ctx context.Context) error

func (*Senju) SetDuration

func (s *Senju) SetDuration(d time.Duration)

func (*Senju) SetErrorHandler added in v0.0.3

func (s *Senju) SetErrorHandler(f func(error))

Jump to

Keyboard shortcuts

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