-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
143 lines (122 loc) · 2.82 KB
/
serve.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"bufio"
"context"
"encoding/json"
"log"
"os"
"os/signal"
"syscall"
"github.com/sergeleger/powermeter/power"
"github.com/sergeleger/powermeter/storage/sqlite"
"github.com/urfave/cli/v2"
)
// ServeCommand accepts power measurement from standard input and writes them to the
// database.
var ServeCommand = cli.Command{
Name: "serve",
Usage: "start the PowerMeter server",
Action: serveAction,
Flags: []cli.Flag{
&cli.StringFlag{Name: "db", Value: "power.db", Usage: "SQLite file"},
&cli.StringFlag{Name: "http", Usage: "start HTTP service at `addr`ess. (example: localhost:8088)"},
&cli.Int64Flag{Name: "meter", Usage: "Accept only `meter_id` measurements.", EnvVars: []string{"POWERMETER_FILTER"}},
&cli.IntFlag{Name: "batch", Value: 1, Usage: "transaction batch size, small values for live updating"},
},
}
func serveAction(c *cli.Context) (err error) {
// Connect to SQLite service
service, err := sqlite.Open(c.String("db"))
if err != nil {
return err
}
defer service.Close()
// create shutdown context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// setup termination signals and wait for
go func() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
select {
case <-stop:
cancel()
case <-ctx.Done():
return
}
}()
// start API service in the background
var httpService APIService
if c.IsSet("http") {
go func() {
httpService = NewAPIService(service)
err := httpService.Listen(ctx, c.String("http"))
if err != nil {
log.Println(err)
}
}()
}
// Start accepting entries from standard input
ch := make(chan power.Measurement, 5)
go func() {
log.Println("Start scanning.")
// Create filtering method
accept := func(id int64) bool { return true }
if c.IsSet("meter") {
meter := c.Int64("meter")
accept = func(id int64) bool {
return id == meter
}
}
var err error
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
var m power.Measurement
if err = json.Unmarshal(sc.Bytes(), &m); err != nil {
log.Println(err)
continue
}
if !accept(m.MeterID) {
continue
}
ch <- m
}
if err := sc.Err(); err != nil {
log.Println(err)
}
close(ch)
}()
i, n := 0, c.Int("batch")
batch := make([]power.Measurement, n)
var stop bool
for !stop {
select {
case <-ctx.Done():
stop = true
case m, ok := <-ch:
if !ok {
stop = true
break
}
batch[i] = m
i++
if i < n {
continue
}
if err := service.Insert(batch[0:i]); err != nil {
log.Println(err)
}
i = 0
}
}
os.Stdin.Close()
// attempt to update remaining batch
if i > 0 {
if err := service.Insert(batch[0:i]); err != nil {
log.Println(err)
}
}
log.Println("Shutting down services.")
err = httpService.Shutdown()
return err
}