-
Notifications
You must be signed in to change notification settings - Fork 5
/
monitor.go
95 lines (84 loc) · 2.35 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Copyright © 2017-2019 Solus Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"os"
"path/filepath"
"strings"
"github.com/radu-munteanu/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/getsolus/ferryd/src/ferryd/core"
"github.com/getsolus/ferryd/src/ferryd/jobs"
)
// InitWatcher will set up the watcher for the first time
func (s *Server) InitWatcher() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
// Monitor the incoming dir
if err = watcher.Add(s.manager.IncomingPath); err != nil {
return err
}
s.watchChan = make(chan bool)
s.watcher = watcher
return nil
}
// WatchIncoming will wait for events on the incoming directory
// and process incoming .tram files
func (s *Server) WatchIncoming() {
s.watchGroup.Add(1)
go func() {
defer s.watchGroup.Done()
for {
select {
case event := <-s.watcher.Events:
// Not interested in subdirs
if filepath.Dir(event.Name) != s.manager.IncomingPath {
continue
}
if event.Op&fsnotify.Update == fsnotify.Update {
if strings.HasSuffix(event.Name, core.TransitManifestSuffix) {
s.processTransitManifest(filepath.Base(event.Name))
}
}
case <-s.watchChan:
return
}
}
}()
}
// StopWatching will force the fsnotify code to shut down
func (s *Server) StopWatching() {
s.watchChan <- true
s.watchGroup.Wait()
}
// processTransitManifest is invoked when a .tram file is closed in our incoming
// directory. We'll now push it for further processing
func (s *Server) processTransitManifest(name string) {
fullpath := filepath.Join(s.manager.IncomingPath, name)
st, err := os.Stat(fullpath)
if err != nil {
return
}
if !st.Mode().IsRegular() {
return
}
log.WithFields(log.Fields{
"id": name,
}).Info("Received transit manifest upload")
s.jproc.PushJob(jobs.NewTransitJob(fullpath))
}