1
0
Fork 0

add lock on storage

refactor/models
Matthew 2019-12-27 21:19:32 -05:00
parent 13faf9b4f9
commit f866592513
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
1 changed files with 28 additions and 22 deletions

View File

@ -9,8 +9,8 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io/ioutil"
"time"
"strings"
"strings"
"time"
)
// Collection the MongoDB Account collection
@ -39,6 +39,7 @@ func HandleError(e error, serv int) {
}
func main() {
var status bool
type Config struct {
MongoDB string `json:"mongoURL"`
}
@ -52,11 +53,11 @@ func main() {
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")
err = client.Ping(context.TODO(), nil)
fmt.Printf("Connected to MongoDB [GO]\n")
HandleError(err, 1)
Collection = client.Database("cloudservices").Collection("accounts")
Collection = client.Database("cloudservices").Collection("accounts")
RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
@ -64,36 +65,41 @@ func main() {
DB: 0,
})
_, err = RedisClient.Ping().Result()
fmt.Printf("Connected to Redis [GO]\n")
_, err = RedisClient.Ping().Result()
fmt.Printf("Connected to Redis [GO]\n")
HandleError(err, 1)
status = false
for {
fmt.Printf("Calling handler func [GO]\n")
handler()
time.Sleep(1000000 * time.Millisecond)
}
if status == false {
handler(&status)
time.Sleep(1000000 * time.Millisecond)
}
}
}
func handler() {
func handler(status* bool) {
*status = true
cur, err := Collection.Find(context.TODO(), bson.D{})
HandleError(err, 0)
for cur.Next(context.TODO()) {
go checkAccountSizeAndUpdate(cur.Current.Lookup("username").String(), cur.Current.Lookup("id").String())
fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String())
go checkAccountSizeAndUpdate(cur.Current.Lookup("username").String(), cur.Current.Lookup("id").String())
fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String())
time.Sleep(600000 * time.Millisecond)
}
*status = false
}
func checkAccountSizeAndUpdate(username string, id string) {
var size float64 = 0
var userHomeDirectory string = strings.Replace(strings.Join([]string{"/home/", string(username)}, ""), "\"", "", -1)
fmt.Println(userHomeDirectory)
sizeHome := DirSize(&userHomeDirectory)
size += sizeHome
sizeMail := DirSize(&userHomeDirectory)
size += sizeMail
RedisClient.Set("storage"+"-"+string(id), size, 0)
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n", string(username), string(id), size)
var size float64 = 0
var userHomeDirectory string = strings.Replace(strings.Join([]string{"/home/", string(username)}, ""), "\"", "", -1)
fmt.Println(userHomeDirectory)
sizeHome := DirSize(&userHomeDirectory)
size += sizeHome
sizeMail := DirSize(&userHomeDirectory)
size += sizeMail
RedisClient.Set("storage"+"-"+string(id), size, 0)
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n", string(username), string(id), size)
}