Giter VIP home page Giter VIP logo

recwatch's Introduction

recwatch

Build Status

Watch filesystem changes recursively.

recwatch started out as a fork of looper (by Nathan Youngman), but most of the code is now original.

Features and limitations

  • Can be used for watching filesystem changes recursively.
  • Can be used for setting up a SSE (server-sent events) server, for serving filesystem changes as events.
  • Filenames beginning with . or _ are ignored.
  • The file search is concurrent.
  • Will follow symlinks.
  • Note that some systems may have a very low ulimit which may limit how many files can be watched at the same time.

Example usage

Short example

recwatch.EventServer(pathToWatch, "*", eventAddr, eventPath, refreshDuration)

Longer example

package main

import (
	"github.com/xyproto/recwatch"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"path/filepath"
	"strings"
	"time"
)

func plural(passed time.Duration) string {
	if passed.Seconds() != 1 {
		return "s"
	}
	return ""
}

// Some errors are ignored since this is just a quick example
func main() {
	eventAddr := "0.0.0.0:5555"
	eventPath := "/"
	pathToWatch := "tempdir"
	refreshDuration, err := time.ParseDuration("350ms")
	if err != nil {
		log.Fatalln(err)
	}
	now := time.Now().UTC()
	_ = os.Mkdir(pathToWatch, 0755)
	recwatch.EventServer(pathToWatch, "*", eventAddr, eventPath, refreshDuration)
	URL := "http://" + strings.Replace(eventAddr, "0.0.0.0", "localhost", 1) + eventPath
	log.Printf("Serving filesystem events for %s as SSE (server-sent events) on %s\n", pathToWatch, URL)
	tempFileName := filepath.Join(pathToWatch, "hello.txt")
	_ = os.Remove(tempFileName)
	// Set up a handler for SIGINT (ctrl-c)
	quit := false
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for range c {
			// ctrl-c was pressed
			quit = true
			log.Println("ctrl-c was pressed, cleaning up")
			_ = os.Remove(tempFileName)
			_ = os.Remove(pathToWatch) // will only remove the directory if it's empty
			log.Println("done")
		}
	}()
	for {
		passed := time.Since(now)
		log.Printf("%.0f second%s passed. Visit %s to see events appear.\n", passed.Seconds(), plural(passed), URL)
		time.Sleep(1 * time.Second)
		if quit {
			break
		}
		log.Println("Creating and writing to " + tempFileName)
		data := []byte("Hi\n")
		err := ioutil.WriteFile(tempFileName, data, 0644)
		if err != nil {
			log.Fatal(err)
		}
		time.Sleep(1 * time.Second)
		if quit {
			break
		}
		log.Println("Removing " + tempFileName)
		err = os.Remove(tempFileName)
		if err != nil {
			log.Fatal(err)
		}
		time.Sleep(1 * time.Second)
		if quit {
			break
		}
		if passed.Seconds() > 200 {
			log.Println("Time waits for no man.")
		}
	}
}

General info

  • Version: 1.1.0
  • License: Simplified BSD
  • Author: Alexander F. Rødseth <[email protected]>

recwatch's People

Contributors

nathany avatar xyproto avatar boutros avatar benmanns avatar jtacoma avatar landaire avatar waffle-iron avatar sudhirj avatar tomjowitt avatar aibou avatar

Stargazers

Chris Bednarski avatar VGISC Solutions avatar  avatar  avatar Enrico avatar  avatar  avatar mincau avatar lhl avatar  avatar  avatar  avatar  avatar Wade Welles avatar Juri Hahn avatar andrea denisse avatar Kent Gruber avatar Denys Sedchenko avatar  avatar ik5 avatar Denis Denisov avatar Ike Devolder avatar  avatar carlos L avatar  avatar  avatar Guillem CANAL avatar runelabs avatar Sean Williams avatar Tobias V. Langhoff avatar Isagani Mendoza avatar Max avatar

Watchers

 avatar James Cloos avatar mincau avatar Sean Williams avatar  avatar  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.