Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 69886c7

Browse files
committed
add watcher on files added manually (adb)
1 parent 35e31de commit 69886c7

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

main.go

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
"time"
1212

1313
mqtt "github.com/eclipse/paho.mqtt.golang"
14+
"github.com/fsnotify/fsnotify"
1415
"github.com/hpcloud/tail"
1516
"github.com/namsral/flag"
1617
logger "github.com/nats-io/gnatsd/logger"
1718
server "github.com/nats-io/gnatsd/server"
1819
nats "github.com/nats-io/go-nats"
20+
1921
"github.com/pkg/errors"
2022
)
2123

@@ -144,33 +146,72 @@ func (p program) run() {
144146
addIntelLibrariesToLdPath()
145147

146148
files, err := ioutil.ReadDir(sketchFolder)
147-
148149
if err == nil {
149150
for _, file := range files {
150151

151152
//add all files as sketches, stopped, without any PID
152153
if file.IsDir() {
153154
continue
154155
}
155-
id, err := GetSketchIDFromDB(file.Name())
156-
if err != nil {
157-
id = file.Name()
158-
}
159-
fmt.Println("Getting sketch from " + id + " " + file.Name())
160-
s := SketchStatus{
161-
ID: id,
162-
PID: 0,
163-
Name: file.Name(),
164-
Status: "STOPPED",
165-
}
166-
status.Set(id, &s)
167-
status.Publish()
156+
addFileToSketchDB(file, status)
168157
}
169158
}
170159

160+
addWatcherForManuallyAddedSketches("/tmp/sketches", sketchFolder, status)
161+
171162
select {}
172163
}
173164

165+
func addFileToSketchDB(file os.FileInfo, status *Status) *SketchStatus {
166+
id, err := GetSketchIDFromDB(file.Name())
167+
if err != nil {
168+
id = file.Name()
169+
}
170+
fmt.Println("Getting sketch from " + id + " " + file.Name())
171+
s := SketchStatus{
172+
ID: id,
173+
PID: 0,
174+
Name: file.Name(),
175+
Status: "STOPPED",
176+
}
177+
status.Set(id, &s)
178+
status.Publish()
179+
return &s
180+
}
181+
182+
func addWatcherForManuallyAddedSketches(folderOrigin, folderDest string, status *Status) {
183+
watcher, err := fsnotify.NewWatcher()
184+
defer watcher.Close()
185+
done := make(chan bool)
186+
go func() {
187+
for {
188+
select {
189+
case event := <-watcher.Events:
190+
log.Println("event:", event)
191+
if event.Op&fsnotify.Create == fsnotify.Create {
192+
// give it some time to settle
193+
time.Sleep(2 * time.Second)
194+
name := filepath.Base(strings.TrimSuffix(event.Name, filepath.Ext(event.Name)))
195+
filename := filepath.Join(folderDest, name)
196+
os.Rename(event.Name, filename)
197+
os.Chmod(filename, 0755)
198+
log.Println("Moving new sketch to sketches folder")
199+
fileInfo, _ := os.Stat(filename)
200+
s := addFileToSketchDB(fileInfo, status)
201+
applyAction(s, "START", status)
202+
}
203+
case err := <-watcher.Errors:
204+
log.Println("error:", err)
205+
}
206+
}
207+
}()
208+
err = watcher.Add(folderOrigin)
209+
if err != nil {
210+
log.Fatal(err)
211+
}
212+
<-done
213+
}
214+
174215
func tailAndReport(listenFile string, status *Status) {
175216
t, err := tail.TailFile(listenFile, tail.Config{Follow: true})
176217
for err != nil {

0 commit comments

Comments
 (0)