11var _ = require ( 'lodash' )
22var events = require ( 'events' )
33var fs = require ( 'fs' )
4+ var filesize = require ( 'filesize' )
45var Gamedig = require ( 'gamedig' )
6+ var usage = require ( 'pidusage' )
57var slugify = require ( 'slugify' )
68
79var ArmaServer = require ( 'arma-server' )
810
911var config = require ( '../config.js' )
1012
13+ var processInterval = 2000
1114var queryInterval = 5000
1215var queryTypes = {
1316 arma1 : 'arma' ,
@@ -69,6 +72,32 @@ Server.prototype.update = function (options) {
6972 this . port = parseInt ( this . port , 10 ) // If port is a string then gamedig fails
7073}
7174
75+ Server . prototype . queryProcess = function ( ) {
76+ if ( ! this . instance ) {
77+ return
78+ }
79+
80+ var self = this
81+ usage ( self . instance . pid , function ( err , stats ) {
82+ if ( ! self . instance ) {
83+ return
84+ }
85+
86+ if ( err ) {
87+ self . process = null
88+ } else {
89+ self . process = {
90+ cpu : stats . cpu ,
91+ cpuFormatted : stats . cpu . toFixed ( 0 ) + ' %' ,
92+ memory : stats . memory ,
93+ memoryFormatted : filesize ( stats . memory )
94+ }
95+ }
96+
97+ self . emit ( 'state' )
98+ } )
99+ }
100+
72101Server . prototype . queryStatus = function ( ) {
73102 if ( ! this . instance ) {
74103 return
@@ -190,8 +219,10 @@ Server.prototype.start = function () {
190219 logStream . end ( )
191220 }
192221
222+ clearInterval ( self . queryProcessInterval )
193223 clearInterval ( self . queryStatusInterval )
194224 self . state = null
225+ self . process = null
195226 self . pid = null
196227 self . instance = null
197228
@@ -206,6 +237,9 @@ Server.prototype.start = function () {
206237
207238 this . pid = instance . pid
208239 this . instance = instance
240+ this . queryProcessInterval = setInterval ( function ( ) {
241+ self . queryProcess ( )
242+ } , processInterval )
209243 this . queryStatusInterval = setInterval ( function ( ) {
210244 self . queryStatus ( )
211245 } , queryInterval )
@@ -318,6 +352,7 @@ Server.prototype.toJSON = function () {
318352 persistent : this . persistent ,
319353 pid : this . pid ,
320354 port : this . port ,
355+ process : this . process ,
321356 state : this . state ,
322357 title : this . title ,
323358 von : this . von ,
0 commit comments