changes to go func

merge-requests/4/head
Matthew 2020-07-07 21:15:10 -04:00
parent 3a0305b6ec
commit 98ec12266d
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 11 additions and 7 deletions

View File

@ -18,7 +18,7 @@ check_cert_signatures:
file dist/bin/checkCertSignatures file dist/bin/checkCertSignatures
storage: storage:
HOME=/root go build -ldflags="-s -w" -o dist/bin/storage ${storage_files} HOME=/root go build -ldflags="-s -w" -buildmode=pie -o dist/bin/storage ${storage_files}
@chmod 740 dist/bin/storage @chmod 740 dist/bin/storage
file dist/bin/storage file dist/bin/storage

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"sync"
"github.com/go-redis/redis/v7" "github.com/go-redis/redis/v7"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -81,28 +82,31 @@ func handler(config ConfigStruct) {
collection := mongoClient.Database("cloudservices").Collection("accounts") collection := mongoClient.Database("cloudservices").Collection("accounts")
cur, err := collection.Find(context.TODO(), bson.D{}) cur, err := collection.Find(context.TODO(), bson.D{})
HandleError(err, 0) HandleError(err, 0)
var wg sync.WaitGroup
for cur.Next(context.TODO()) { for cur.Next(context.TODO()) {
checkAccountSizeAndUpdate(cur.Current.Lookup("username").String(), cur.Current.Lookup("id").String()) wg.Add(1)
go checkAccountSizeAndUpdate(cur.Current.Lookup("username").String(), cur.Current.Lookup("id").String(), &wg)
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(300000 * time.Millisecond)
} }
wg.Wait()
err = mongoClient.Disconnect(ctx) err = mongoClient.Disconnect(ctx)
HandleError(err, 1) HandleError(err, 1)
fmt.Println("CSD-GO-STR finished, exiting with code 0. [GO]") fmt.Println("CSD-GO-STR finished, exiting with code 0. [GO]")
os.Exit(0) os.Exit(0)
} }
func checkAccountSizeAndUpdate(username string, id string) { func checkAccountSizeAndUpdate(username string, id string, wg *sync.WaitGroup) {
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)
usernameFormat := strings.Replace(username, "\"", "", -1) usernameFormat := strings.Replace(username, "\"", "", -1)
sizeHome := DirSize(&userHomeDirectory) sizeHome := DirSize(&userHomeDirectory)
size += sizeHome size += sizeHome
status := RedisClient.Set("storage"+"-"+usernameFormat, size, 0) status := RedisClient.Set("storage"+"-"+usernameFormat, size, 0)
fmt.Println(status.Name()) fmt.Printf("Redis Status: %s [GO]\n", status.Name())
if status.Err() != nil { if status.Err() != nil {
fmt.Println(status.Err()) fmt.Println(status.Err())
} }
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n [GO]", string(username), string(id), size) fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f [GO]\n", string(username), string(id), size)
wg.Done()
} }