forked from jc01rho/scheduler
/
mongo.go
161 lines (127 loc) · 3.92 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// +build cgo
package storage
import (
"context"
"errors"
"fmt"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
const COLLECTION_NAME string = "task_store"
// MongoDBConfig is the config structure holding information about mongo db.
type MongoDBConfig struct {
ConnectionUrl string
Db string
}
// MongoDBStorage is the structure responsible for handling mongo storage.
type MongoDBStorage struct {
config MongoDBConfig
client *mongo.Client
}
// NewMongoDBStorage returns a new instance of MongoDBStorage.
func NewMongoDBStorage(config MongoDBConfig) *MongoDBStorage {
return &MongoDBStorage{
config: config,
}
}
// Connect creates the database file.
func (mongodb *MongoDBStorage) Connect() error {
var client *mongo.Client
client, err := mongo.NewClient(options.Client().ApplyURI(mongodb.config.ConnectionUrl))
if err != nil {
return err
}
mongodb.client = client
err = mongodb.client.Connect(context.TODO())
return err
}
// Close will close the open DB file.
func (mongodb *MongoDBStorage) Close() error {
return mongodb.client.Disconnect(context.Background())
}
// Initialize mongodb collection
func (mongodb *MongoDBStorage) Initialize() error {
task_store := mongodb.client.
Database(mongodb.config.Db).Collection(COLLECTION_NAME)
if task_store == nil {
log.Printf("could not initialize collection")
return errors.New("mongo error")
}
return nil
}
// Stores the task to mongo
func (mongodb MongoDBStorage) Add(task TaskAttributes) error {
task_store := mongodb.client.Database(mongodb.config.Db).Collection(COLLECTION_NAME)
if task_store == nil {
return errors.New("could not get collection")
}
// filter := bson.NewDocument(bson.EC.String("hash", task.Hash))
filter := bsonx.Doc{{"hash", bsonx.String(task.Hash)}}
res, err := task_store.CountDocuments(context.Background(), filter)
if err != nil {
return errors.New(fmt.Sprintf("%v", err))
}
if res == 0 {
res, err := task_store.InsertOne(context.Background(),
map[string]string{
"name": task.Name,
"params": task.Params,
"duration": task.Duration,
"last_run": task.LastRun,
"next_run": task.NextRun,
"is_recurring": task.IsRecurring,
"hash": task.Hash,
})
if res == nil {
return errors.New("element not inserted")
}
return err
}
return nil
}
// Remove will delete the task from storage.
func (mongodb MongoDBStorage) Remove(task TaskAttributes) error {
task_store := mongodb.client.Database(mongodb.config.Db).Collection(COLLECTION_NAME)
if task_store == nil {
return errors.New("could not get collection")
}
// filter := bson.NewDocument(bson.EC.String("hash", task.Hash))
filter := bsonx.Doc{{"hash", bsonx.String(task.Hash)}}
_, err := task_store.DeleteOne(context.Background(), filter)
return err
}
// Fetch will return the list of all stored tasks.
func (mongodb MongoDBStorage) Fetch() ([]TaskAttributes, error) {
task_store := mongodb.client.Database(mongodb.config.Db).Collection(COLLECTION_NAME)
if task_store == nil {
log.Println("taskstore empty")
return nil, errors.New("could not get collection")
}
var tasks []TaskAttributes
cur, err := task_store.Find(context.Background(), nil)
if err != nil {
return nil, err
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
var elem bsonx.Doc
err := cur.Decode(&elem)
if err != nil {
log.Println(err.Error())
return nil, err
}
task := TaskAttributes{
Name: elem.Lookup("name").StringValue(),
Params: elem.Lookup("params").StringValue(),
LastRun: elem.Lookup("last_run").StringValue(),
NextRun: elem.Lookup("next_run").StringValue(),
Duration: elem.Lookup("duration").StringValue(),
IsRecurring: elem.Lookup("is_recurring").StringValue(),
Hash: elem.Lookup("hash").StringValue(),
}
tasks = append(tasks, task)
}
return tasks, nil
}