1
0
Fork 0

daemonization changes for storage calculations

refactor/models
Matthew 2020-06-29 23:46:51 -04:00
parent 9c6e900802
commit 8378b27c96
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 28 additions and 13 deletions

View File

@ -41,7 +41,7 @@ func HandleError(e error, serv int) {
} }
func main() { func main() {
var status bool fmt.Println("Starting CSD-GO-STR Daemon [GO]")
config := &ConfigStruct{} config := &ConfigStruct{}
file, err := ioutil.ReadFile("../config.json") file, err := ioutil.ReadFile("../config.json")
HandleError(err, 1) HandleError(err, 1)
@ -56,19 +56,18 @@ func main() {
_, 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")
if status == false { if status == false {
handler(&status, *config) handler(*config)
time.Sleep(1000000 * time.Millisecond) time.Sleep(1000000 * time.Millisecond)
} }
} }*/
handler(*config)
} }
func handler(status *bool, config ConfigStruct) { func handler(config ConfigStruct) {
*status = true
mongoClient, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB)) mongoClient, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB))
HandleError(err, 1) HandleError(err, 1)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
@ -88,7 +87,6 @@ func handler(status *bool, config ConfigStruct) {
} }
err = mongoClient.Disconnect(ctx) err = mongoClient.Disconnect(ctx)
HandleError(err, 1) HandleError(err, 1)
*status = false
} }
func checkAccountSizeAndUpdate(username string, id string) { func checkAccountSizeAndUpdate(username string, id string) {
@ -97,12 +95,10 @@ func checkAccountSizeAndUpdate(username string, id string) {
usernameFormat := strings.Replace(username, "\"", "", -1) usernameFormat := strings.Replace(username, "\"", "", -1)
sizeHome := DirSize(&userHomeDirectory) sizeHome := DirSize(&userHomeDirectory)
size += sizeHome size += sizeHome
sizeMail := DirSize(&userHomeDirectory)
size += sizeMail
status := RedisClient.Set("storage"+"-"+usernameFormat, size, 0) status := RedisClient.Set("storage"+"-"+usernameFormat, size, 0)
fmt.Println(status.Name()) fmt.Println(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", string(username), string(id), size) fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n [GO]", string(username), string(id), size)
} }

View File

@ -3,12 +3,31 @@
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import { Client } from '../class'; import { Client } from '../class';
let interval: NodeJS.Timeout;
export default async function storage(client: Client) { 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.stdout.on('data', (data) => client.signale.log(data.toString()));
storageGo.stderr.on('data', (data) => client.signale.log(data.toString())); storageGo.stderr.on('data', (data) => client.signale.log(data.toString()));
storageGo.on('exit', (code) => { storageGo.on('exit', (code) => {
client.signale.log(`Go storage func exited with code ${code}, restarting`); client.signale.log(`Go storage func exited with code ${code}, restarting`);
storageGo = spawn(`${__dirname}/../bin/storage`, [], { cwd: __dirname }); 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);