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