Skip to content

Commit

Permalink
[preprocessor/folder] run file watcher handler in coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jun 3, 2024
1 parent 7e7833a commit efd3a0a
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions preprocessor/folder/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,28 @@ func Run(args []string) error {
ei := <-c
event := ei.Event()
if event == notify.Rename || event == notify.Create {
filePath := ei.Path()
file, err := os.Open(filePath)
if err != nil {
// File may be moved away for notify.Rename
continue
}
fileInfo, err := file.Stat()
if err != nil {
log.Println("error getting file info:", err)
continue
}
if shouldIncludeFile(fileInfo.Name()) {
cid, err := handleNewFile(filePath)
go func() {
filePath := ei.Path()
file, err := os.Open(filePath)
if err != nil {
log.Println(err)
} else {
log.Printf("File %s uploaded to webhook with CID %s\n", filePath, cid)
// File may be moved away for notify.Rename
return
}
}
file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Println("error getting file info:", err)
return
}
if shouldIncludeFile(fileInfo.Name()) {
cid, err := handleNewFile(filePath)
if err != nil {
log.Println(err)
} else {
log.Printf("file %s uploaded to webhook with CID %s\n", filePath, cid)
}
}
file.Close()
}()
}
}
}

0 comments on commit efd3a0a

Please sign in to comment.