Skip to content

Commit

Permalink
WATCHER: Build out consumer receive & process functions using new Event
Browse files Browse the repository at this point in the history
struct.
  • Loading branch information
Cian911 committed Dec 11, 2021
1 parent dab560a commit 43d4cfd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

func main() {
path := "/Users/cian.gallagher/test_events"
destination := "/tmp"
path := "/Users/cian.gallagher/input"
destination := "/Users/cian.gallagher/output"

var pw watcher.Producer = &watcher.PathWatcher{
Path: path,
Expand All @@ -15,6 +15,7 @@ func main() {
var pc watcher.Consumer = &watcher.PathConsumer{
Path: path,
Destination: destination,
Ext: ".txt",
}

pw.Register(&pc)
Expand Down
30 changes: 25 additions & 5 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"

"github.com/cian911/switchboard/event"
"github.com/cian911/switchboard/utils"
"github.com/fsnotify/fsnotify"
)

Expand All @@ -17,7 +19,7 @@ type Producer interface {

type Consumer interface {
Receive(path, event string)
Process(path, destination string)
Process(e *event.Event)
}

type PathWatcher struct {
Expand All @@ -29,14 +31,32 @@ type PathWatcher struct {
type PathConsumer struct {
Path string
Destination string
Ext string
}

func (pc *PathConsumer) Receive(path, event string) {
log.Printf("CONSUMER EVENT: path: %s, event: %s", path, event)
func (pc *PathConsumer) Receive(path, ev string) {
log.Printf("Event Received: %s, Path: %s\n", ev, path)

e := &event.Event{
File: filepath.Base(path),
Path: path,
Destination: pc.Destination,
Ext: utils.ExtractFileExt(path),
Operation: ev,
}

log.Printf("pc.Path: {%s}", pc.Path)
log.Printf("Event: %v", e)

if e.IsValidEvent(pc.Ext) {
log.Println("Event is valid")
pc.Process(e)
}
}

func (pc *PathConsumer) Process(path, destination string) {
log.Printf("CONSUMER PROCESSING EVENT: path: %s, event: %s", path, destination)
func (pc *PathConsumer) Process(e *event.Event) {
log.Println("Event has been processed.")
e.Move()
}

func (pw *PathWatcher) Register(consumer *Consumer) {
Expand Down

0 comments on commit 43d4cfd

Please sign in to comment.