diff --git a/src/go/storage/storage.go b/src/go/storage/storage.go index afd6abf..ab87d75 100644 --- a/src/go/storage/storage.go +++ b/src/go/storage/storage.go @@ -41,7 +41,7 @@ func HandleError(e error, serv int) { } func main() { - var status bool + fmt.Println("Starting CSD-GO-STR Daemon [GO]") config := &ConfigStruct{} file, err := ioutil.ReadFile("../config.json") HandleError(err, 1) @@ -56,19 +56,18 @@ func main() { _, err = RedisClient.Ping().Result() fmt.Printf("Connected to Redis [GO]\n") HandleError(err, 1) - status = false - for { + /*for { fmt.Printf("Calling handler func [GO]\n") if status == false { - handler(&status, *config) + handler(*config) time.Sleep(1000000 * time.Millisecond) } - } + }*/ + handler(*config) } -func handler(status *bool, config ConfigStruct) { - *status = true +func handler(config ConfigStruct) { mongoClient, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB)) HandleError(err, 1) ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) @@ -88,7 +87,6 @@ func handler(status *bool, config ConfigStruct) { } err = mongoClient.Disconnect(ctx) HandleError(err, 1) - *status = false } func checkAccountSizeAndUpdate(username string, id string) { @@ -97,12 +95,10 @@ func checkAccountSizeAndUpdate(username string, id string) { usernameFormat := strings.Replace(username, "\"", "", -1) sizeHome := DirSize(&userHomeDirectory) size += sizeHome - sizeMail := DirSize(&userHomeDirectory) - size += sizeMail status := RedisClient.Set("storage"+"-"+usernameFormat, size, 0) fmt.Println(status.Name()) if status.Err() != nil { fmt.Println(status.Err()) } - 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 [GO]", string(username), string(id), size) } diff --git a/src/intervals/storage.ts b/src/intervals/storage.ts index e8ef484..e41ba29 100644 --- a/src/intervals/storage.ts +++ b/src/intervals/storage.ts @@ -3,12 +3,31 @@ import { spawn } from 'child_process'; import { Client } from '../class'; +let interval: NodeJS.Timeout; + export default async function storage(client: Client) { - let storageGo = spawn(`${__dirname}/../bin/storage`, [], { cwd: __dirname }); + async function determineInterval(client0: Client) { + const accounts = await client0.db.Account.countDocuments(); + return (accounts * 2) * 60000; + } + let intervalTiming = await determineInterval(client); + /* let storageGo = spawn(`${__dirname}/../bin/storage`, [], { cwd: __dirname }); storageGo.stdout.on('data', (data) => client.signale.log(data.toString())); storageGo.stderr.on('data', (data) => client.signale.log(data.toString())); storageGo.on('exit', (code) => { client.signale.log(`Go storage func exited with code ${code}, restarting`); storageGo = spawn(`${__dirname}/../bin/storage`, [], { cwd: __dirname }); - }); + }); */ + interval = setInterval(async () => { + intervalTiming = await determineInterval(client); + const storageDaemon = spawn(`${__dirname}/../bin/storage`, [], { cwd: __dirname }); + storageDaemon.stdout.on('data', (data) => client.signale.log(data.toString())); + storageDaemon.stderr.on('data', (data) => client.signale.log(data.toString())); + storageDaemon.on('exit', (code) => { + client.signale.log(`CSD-GO-STR Daemon exited with code ${code}.`); + storageDaemon.removeAllListeners(); + }); + }, intervalTiming); } + +export const clear = () => clearInterval(interval);