@@ -2,11 +2,16 @@ import { ObjLogger } from "@scramjet/obj-logger";
22import { IComponent , LoadCheckStat , LoadCheckRequirements , LoadCheckContstants } from "@scramjet/types" ;
33import { defer } from "@scramjet/utility" ;
44
5- import sysinfo from "systeminformation" ;
65import { DataStream , StringStream } from "scramjet" ;
76import { LoadCheckConfig } from "./config/load-check-config" ;
7+ import { loadavg } from "os" ;
88
9- const MB = 1024 * 1024 ;
9+ import _du from "diskusage-ng" ;
10+ import { promisify } from "util" ;
11+ import { mem } from "node-os-utils" ;
12+
13+ const MEBIBYTE = 1024 * 1024 ;
14+ const du = promisify ( _du ) ;
1015
1116/**
1217 * Provides methods to monitor resources usage and determine if machine is not overloaded.
@@ -37,11 +42,11 @@ export class LoadCheck implements IComponent {
3742 if ( ! config . isValid ( ) ) throw new Error ( "Invalid load-check configuration" ) ;
3843 this . config = config . get ( ) ;
3944 this . constants = {
40- SAFE_OPERATION_LIMIT : this . config . safeOperationLimit * MB ,
45+ SAFE_OPERATION_LIMIT : this . config . safeOperationLimit * MEBIBYTE ,
4146 MIN_INSTANCE_REQUIREMENTS : {
42- freeMem : this . config . instanceRequirements . freeMem * MB ,
47+ freeMem : this . config . instanceRequirements . freeMem * MEBIBYTE ,
4348 cpuLoad : this . config . instanceRequirements . cpuLoad ,
44- freeSpace : this . config . instanceRequirements . freeSpace * MB
49+ freeSpace : this . config . instanceRequirements . freeSpace * MEBIBYTE
4550 }
4651 } ;
4752 }
@@ -52,18 +57,32 @@ export class LoadCheck implements IComponent {
5257 * @returns {Promise<LoadCheckStat> } Promise resolving to gathered load check data.
5358 */
5459 async getLoadCheck ( ) : Promise < LoadCheckStat > {
55- const [ load , disksInfo , memInfo ] = await Promise . all ( [
56- sysinfo . currentLoad ( ) ,
57- sysinfo . fsSize ( ) ,
58- sysinfo . mem ( )
60+ const safeOperationsLimit = this . constants . SAFE_OPERATION_LIMIT ;
61+
62+ const [ currentLoad = 85 , , avgLoad ] = loadavg ( ) ;
63+
64+ const [ usage , _fsSize ] = await Promise . all ( [
65+ mem . info ( ) ,
66+ Promise . all ( this . config . fsPaths . map ( async ( path ) => ( { path, usage : await du ( path ) } ) ) )
5967 ] ) ;
6068
69+ const memFree = ( usage . totalMemMb - usage . usedMemMb ) * MEBIBYTE ;
70+ const memUsed = usage . usedMemMb * MEBIBYTE ;
71+
72+ const fsSize = _fsSize . map ( ( { path, usage : fsUsage } ) => ( {
73+ fs : path ,
74+ available : fsUsage . available - safeOperationsLimit ,
75+ size : fsUsage . total ,
76+ used : fsUsage . used ,
77+ use : fsUsage . used / fsUsage . total
78+ } ) ) ;
79+
6180 return {
62- avgLoad : load . avgLoad ,
63- currentLoad : load . currentLoad || 85 ,
64- memFree : memInfo . free + Math . max ( 0 , memInfo . buffcache - this . constants . SAFE_OPERATION_LIMIT ) ,
65- memUsed : memInfo . used ,
66- fsSize : disksInfo
81+ avgLoad,
82+ currentLoad,
83+ memFree,
84+ memUsed,
85+ fsSize
6786 } ;
6887 }
6988
@@ -95,27 +114,12 @@ export class LoadCheck implements IComponent {
95114 * @returns {DataStream } Stream with load check data.
96115 */
97116 getLoadCheckStream ( ) : StringStream {
98- const safeOperationsLimit = this . constants . SAFE_OPERATION_LIMIT ;
117+ const _this = this ;
99118
100119 return DataStream . from (
101120 async function * ( ) {
102121 while ( true ) {
103- const [ load , disksInfo , memInfo ] = await Promise . all ( [
104- sysinfo . currentLoad ( ) ,
105- sysinfo . fsSize ( ) ,
106- sysinfo . mem ( )
107- ] ) ;
108-
109- yield {
110- avgLoad : load . avgLoad ,
111- currentLoad : load . currentLoad || 85 ,
112- memFree : memInfo . free + Math . max (
113- 0 ,
114- memInfo . buffcache - safeOperationsLimit
115- ) ,
116- memUsed : memInfo . used ,
117- fsSize : disksInfo
118- } ;
122+ yield _this . getLoadCheck ( ) ;
119123
120124 await defer ( 1000 ) ;
121125 }
0 commit comments