forked from TheThingsArchive/jolie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
65 lines (55 loc) · 1.46 KB
/
mongo.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/thethingsnetwork/server-shared"
"gopkg.in/mgo.v2"
)
const (
MONGODB_ATTEMPTS = 20
)
type MongoDatabase struct {
session *mgo.Session
}
func ConnectMongoDatabase() (PacketHandler, error) {
var err error
uri := os.Getenv("MONGODB_URI")
for i := 0; i < MONGODB_ATTEMPTS; i++ {
var session *mgo.Session
session, err = mgo.Dial(fmt.Sprintf("%s:27017", uri))
if err != nil {
log.Printf("Failed to connect to %s: %s", uri, err.Error())
time.Sleep(time.Duration(2) * time.Second)
} else {
log.Printf("Connected to %s", uri)
session.SetMode(mgo.Monotonic, true)
session.SetSocketTimeout(time.Duration(6) * time.Second)
session.SetSyncTimeout(time.Duration(6) * time.Second)
return &MongoDatabase{session}, nil
}
}
return nil, err
}
func (db *MongoDatabase) Configure() error {
return nil
}
func (db *MongoDatabase) HandleStatus(status *shared.GatewayStatus) {
err := db.session.DB("jolie").C("gateway_statuses").Insert(status)
if err != nil {
log.Printf("Failed to save status: %s", err.Error())
}
}
func (db *MongoDatabase) HandlePacket(packet *shared.RxPacket) {
err := db.session.DB("jolie").C("rx_packets").Insert(packet)
if err != nil {
log.Printf("Failed to save packet: %s", err.Error())
}
}
func (db *MongoDatabase) RecordGatewayStatus(status *shared.GatewayStatus) error {
return nil
}
func (db *MongoDatabase) RecordRxPacket(packet *shared.RxPacket) error {
return nil
}