This commit is contained in:
Martin Geno 2016-02-19 11:30:42 +01:00
parent 708e8c4563
commit b74a2480cd
3 changed files with 40 additions and 11 deletions

View File

@ -12,7 +12,7 @@ type Announced struct {
collectors []*Collector
}
func NewAnnounced(ns NodeServer) *Announced {
func NewAnnounced(ns *NodeServer) *Announced {
collects := []*Collector{
NewCollector("statistics"),
NewCollector("nodeinfo"),
@ -21,7 +21,7 @@ func NewAnnounced(ns NodeServer) *Announced {
return &Announced{
ns,
NewNodes(),
output,
"webroot/nodes.json",
time.Second * time.Duration(15),
time.Second * time.Duration(15),
collects,

35
main.go
View File

@ -1,19 +1,42 @@
package main
import (
"flag"
"log"
"time"
"net/http"
)
var (
nodeserver = NewNodeServer("/nodes")
nodes = NewNodes()
outputFile string
collectInterval time.Duration
saveInterval time.Duration
)
func main(){
node := NewNodeServer("/nodes")
go node.Listen()
annouced := NewAnnouced(node)
go annouced.Run()
var collectSeconds, saveSeconds int
flag.StringVar(&outputFile, "output", "nodes.json", "path output file")
flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections")
flag.IntVar(&saveSeconds, "saveInterval", 5, "interval for data saving")
flag.Parse()
collectInterval = time.Second * time.Duration(collectSeconds)
saveInterval = time.Second * time.Duration(saveSeconds)
collectors := []*Collector{
NewCollector("statistics"),
NewCollector("nodeinfo"),
NewCollector("neighbours"),
}
go nodeserver.Listen()
// static files
http.Handle("/", http.FileServer(http.Dir("webroot")))
log.Fatal(http.ListenAndServe(":8080", nil))
for _, c := range collectors {
c.Close()
}
}

View File

@ -70,12 +70,18 @@ func (nodes *Nodes) save() {
nodes.Unlock()
if err !=nil{
log.Panic(e)
log.Panic(err)
}
log.Println("saving", len(nodes.List), "nodes")
tmpFile := outputFile + ".tmp"
check(ioutil.WriteFile(tmpFile, data, 0644))
check(os.Rename(tmpFile, outputFile))
err = ioutil.WriteFile(tmpFile, data, 0644)
if err !=nil{
log.Panic(err)
}
err = os.Rename(tmpFile, outputFile)
if err !=nil{
log.Panic(err)
}
}