forked from engineering/cloudservices
optimizations for storage calculations
parent
f8d466686d
commit
81b19b6267
|
@ -13,8 +13,13 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// Collection the MongoDB Account collection
|
||||
var Collection *mongo.Collection
|
||||
// ConfigStruct the configuration struct
|
||||
type ConfigStruct struct {
|
||||
MongoDB string `json:"mongoURL"`
|
||||
}
|
||||
|
||||
// Config config
|
||||
var Config *ConfigStruct
|
||||
|
||||
// RedisClient the Redis client
|
||||
var RedisClient *redis.Client
|
||||
|
@ -40,24 +45,10 @@ func HandleError(e error, serv int) {
|
|||
|
||||
func main() {
|
||||
var status bool
|
||||
type Config struct {
|
||||
MongoDB string `json:"mongoURL"`
|
||||
}
|
||||
config := &Config{}
|
||||
Config := &ConfigStruct{}
|
||||
file, err := ioutil.ReadFile("../config.json")
|
||||
HandleError(err, 1)
|
||||
err = json.Unmarshal(file, &config)
|
||||
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB))
|
||||
HandleError(err, 1)
|
||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
err = client.Connect(ctx)
|
||||
HandleError(err, 1)
|
||||
err = client.Ping(context.TODO(), nil)
|
||||
fmt.Printf("Connected to MongoDB [GO]\n")
|
||||
HandleError(err, 1)
|
||||
|
||||
Collection = client.Database("cloudservices").Collection("accounts")
|
||||
err = json.Unmarshal(file, &Config)
|
||||
|
||||
RedisClient = redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
|
@ -81,7 +72,16 @@ func main() {
|
|||
|
||||
func handler(status* bool) {
|
||||
*status = true
|
||||
cur, err := Collection.Find(context.TODO(), bson.D{})
|
||||
mongoClient, err := mongo.NewClient(options.Client().ApplyURI(Config.MongoDB))
|
||||
HandleError(err, 1)
|
||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
err = mongoClient.Connect(ctx)
|
||||
HandleError(err, 1)
|
||||
err = mongoClient.Ping(context.TODO(), nil)
|
||||
fmt.Printf("Connected to MongoDB [GO]\n")
|
||||
HandleError(err, 1)
|
||||
collection := mongoClient.Database("cloudservices").Collection("accounts")
|
||||
cur, err := collection.Find(context.TODO(), bson.D{})
|
||||
HandleError(err, 0)
|
||||
|
||||
for cur.Next(context.TODO()) {
|
||||
|
@ -89,6 +89,8 @@ func handler(status* bool) {
|
|||
fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String())
|
||||
time.Sleep(600000 * time.Millisecond)
|
||||
}
|
||||
err = mongoClient.Disconnect(ctx)
|
||||
HandleError(err, 1)
|
||||
*status = false
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue