Created
May 2, 2013 11:28
-
-
Save crosbymichael/5501620 to your computer and use it in GitHub Desktop.
Go directory monitor Not enterprise ready, need more XML...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
zmq "github.com/alecthomas/gozmq" | |
"github.com/howeyc/fsnotify" | |
"log" | |
"sync" | |
) | |
var group sync.WaitGroup | |
//Type to marshal the data onto the queue | |
type ChangeRequest struct { | |
Name string //File name | |
Created bool //Was the file created | |
} | |
//Listen for file change events and push them to the router | |
func processEvents(watcher *fsnotify.Watcher, router *zmq.Socket) { | |
for { | |
select { | |
case ev := <-watcher.Event: | |
log.Println(ev) | |
request := ChangeRequest{ | |
Name: ev.Name, | |
Created: ev.IsCreate(), | |
} | |
if data, err := json.Marshal(request); err == nil { | |
router.Send(data, 0) | |
} | |
case err := <-watcher.Error: | |
log.Fatal(err) | |
} | |
} | |
group.Done() | |
} | |
func createSocket(context *zmq.Context, port string) *zmq.Socket { | |
s, err := context.NewSocket(zmq.PUSH) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err = s.Bind(fmt.Sprintf("tcp://*:%s", port)); err != nil { | |
log.Fatal(err) | |
} | |
return s | |
} | |
func main() { | |
path := flag.String("path", "", "Path to the dir that you want to watch.") | |
port := flag.String("port", "8080", "Port to broadcast on.") | |
flag.Parse() | |
context, _ := zmq.NewContext() | |
defer context.Close() | |
socket := createSocket(context, *port) | |
defer socket.Close() | |
if *path == "" || *path == "/" { | |
log.Fatal("Path cannot be empty or root") | |
} | |
group = sync.WaitGroup{} | |
group.Add(1) | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
log.Fatal(err) | |
} | |
//Process events in the background | |
go processEvents(watcher, socket) | |
if err = watcher.Watch(*path); err != nil { | |
log.Fatal(err) | |
} | |
group.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
zmq "github.com/alecthomas/gozmq" | |
"log" | |
) | |
func createSocket(context *zmq.Context, url string) *zmq.Socket { | |
s, err := context.NewSocket(zmq.PULL) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err = s.Connect(fmt.Sprintf("tcp://%s", url)); err != nil { | |
log.Fatal(err) | |
} | |
return s | |
} | |
func main() { | |
url := flag.String("url", "localhost:5555", "Url and port to the watcher.") | |
flag.Parse() | |
context, _ := zmq.NewContext() | |
defer context.Close() | |
socket := createSocket(context, *url) | |
defer socket.Close() | |
for { | |
data, err := socket.Recv(0) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println(string(data)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment