Taka Cho | 1851089 | 2019-05-14 17:37:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | ============LICENSE_START========================================== |
| 3 | =================================================================== |
| 4 | Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. |
| 5 | =================================================================== |
| 6 | |
| 7 | Unless otherwise specified, all software contained herein is licensed |
| 8 | under the Apache License, Version 2.0 (the License); |
| 9 | you may not use this software except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | |
| 20 | ============LICENSE_END============================================ */ |
| 21 | //.. processing on server side |
| 22 | 'use strict'; |
| 23 | |
| 24 | const fs = require('fs'); |
| 25 | //var spawn = require("child_process").spawn; |
| 26 | var Chproc = require("child_process"); |
| 27 | var logger = require('./srvlogger'); |
| 28 | |
| 29 | var clName= "ServerDataSvc"; |
| 30 | //const DIST_FLDR = join(process.cwd(), 'dist'); |
| 31 | //var DIST_FLDR = "dist"; |
| 32 | var cdt_home = "."; |
| 33 | var LOG_DIR = "/tmp"; |
| 34 | var LogF = "ndserver.log"; |
| 35 | var SubProcScript= "/app/subnproc.js"; |
| 36 | //var HTML_ROOT_FLDR= DIST_FLDR; |
| 37 | //var DelScript= DIST_FLDR +"/app/deleteOldData.js"; |
| 38 | //var inputFilePfx = DIST_FLDR +"/posted_data_"; |
| 39 | var fsAvSpaceKB= -1; |
| 40 | var rsfname= ""; |
| 41 | var remAddr= ""; //.. remote client address |
| 42 | var taskId= ""; |
| 43 | var fdI; |
| 44 | var fdRsf; |
| 45 | var fDelim= "&"; |
| 46 | |
| 47 | //.. to be called by server at initialization |
| 48 | exports.setHomeDir = function( homeDir, logDir ) { |
| 49 | this.cdt_home= homeDir; |
| 50 | this.LOG_DIR= logDir; |
| 51 | // this.DIST_FLDR= this.cdt_home+"/dist"; |
| 52 | this.LogF= this.LOG_DIR+"/ndserver.log"; |
| 53 | console.log( clName+": setHomeDir: cdt_home:["+this.cdt_home+"]"); |
| 54 | console.log( clName+": setHomeDir: LogF:["+this.LogF+"]"); |
| 55 | }; |
| 56 | |
| 57 | //.. to be called by server on receiving GET /getUserRole request |
| 58 | // (simulation only) |
| 59 | exports.getUserRoles = function( requrl ) { |
| 60 | var methName= "getUserRoles"; |
| 61 | logger.addLog( clName+": "+methName+ |
| 62 | ": start: url:["+ requrl+"]\n cdt_home:["+this.cdt_home+"]\n",this.LogF); |
| 63 | let timeStamp = new Date().toISOString(); |
| 64 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 65 | var resp= '{\"respStr\":\"notfoundResp\"}'; |
| 66 | this.urolfname= this.cdt_home+"/userRoles.json"; |
| 67 | resp= this.readResultFile( this.urolfname ); |
| 68 | logger.addLog( clName+": "+methName+ |
| 69 | ": result length="+resp.length+"\n",this.LogF); |
| 70 | return resp; |
| 71 | }; |
| 72 | |
| 73 | //.. to be called by server on receiving GET /api/* request |
| 74 | exports.getData = function( requrl ) { |
| 75 | var methName= "getData"; |
| 76 | //console.log(clName+": getData: start: url:["+ requrl+"]\n cdt_home:["+ |
| 77 | // this.cdt_home+"]"); |
| 78 | logger.addLog( clName+": "+methName+ |
| 79 | ": start: url:["+ requrl+"]\n cdt_home:["+this.cdt_home+"]\n",this.LogF); |
| 80 | let timeStamp = new Date().toISOString(); |
| 81 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 82 | var resp= '{\"respStr\":\"notfoundResp\"}'; |
| 83 | var taskStr= "taskId="; |
| 84 | //... Regular Expressions for different requests ... |
| 85 | //.. to get processing result |
| 86 | var rexpGRESUL= new RegExp(/\/api\/get_result/); |
| 87 | //.. to get progress percentage of the processing |
| 88 | var rexpGPROGR= new RegExp(/\/api\/get_progress/); |
| 89 | //.. to get parameters calculated via the processing |
| 90 | var rexpGPRMS= new RegExp(/\/api\/get_params/); |
| 91 | //.. to get parameters format-error file |
| 92 | var rexpPFMTERR= new RegExp(/\/api\/get_pfmterr/); |
| 93 | //.. to get Available space in server-side file-system |
| 94 | var rexpGFSAVSP= new RegExp(/\/api\/get_fsAvSpace/); |
| 95 | //.. to check for ACE-editor resource files |
| 96 | var rexpACERES= new RegExp(/\/node_modules\/ace-builds\/src-min/); |
| 97 | // |
| 98 | //... checking which url is matching ... |
| 99 | if( requrl == '/' ) { |
| 100 | // console.log(clName+": getData: this is a ROOT request ..."); |
| 101 | logger.addLog( clName+": getData: this is a ROOT request ...\n",this.LogF); |
| 102 | return this.getRoot(); |
| 103 | }; |
| 104 | //.. non-root, api url |
| 105 | var match1= null; |
| 106 | if( (match1= rexpGRESUL.exec(requrl)) != null ) { |
| 107 | logger.addLog( clName+": "+methName+": URL match /api/get_result\n",this.LogF); |
| 108 | //.. assuming: /api/get_result?taskId=f7be82e43e34cb4e960b45d35e8d9596 |
| 109 | var indT= requrl.indexOf( taskStr, 0); |
| 110 | if( indT < 0 ) { |
| 111 | var errMsg= |
| 112 | clName+": "+methName+": Error: No taskId found in the URL !\n"; |
| 113 | logger.addLog( errMsg,this.LogF); |
| 114 | resp= '{\"Error\":\"'+errMsg+'\"}'; |
| 115 | } |
| 116 | else { //.. indT > -1 - have taskId in URL |
| 117 | this.taskId= requrl.substr( indT+ taskStr.length ); |
| 118 | logger.addLog( clName+": "+methName+ |
| 119 | ": extracted from URL: taskId:["+this.taskId+"]\n",this.LogF); |
| 120 | this.rsfname= this.cdt_home+"/sync_template_res_"+this.taskId+".txt"; |
| 121 | resp= this.readResultFile( this.rsfname ); |
| 122 | logger.addLog( clName+": "+methName+ |
| 123 | ": result length="+resp.length+"\n",this.LogF); |
| 124 | logger.addLog( clName+": "+methName+ |
| 125 | ": got result -Start data cleanup...\n",this.LogF); |
| 126 | this.deleteOldFiles( this.taskId ); |
| 127 | }; |
| 128 | } |
| 129 | else |
| 130 | if( (match1= rexpGPROGR.exec(requrl)) != null ) { |
| 131 | logger.addLog( clName+": "+methName+ |
| 132 | ": URL match /api/get_progress\n",this.LogF); |
| 133 | //.. assuming: /api/get_progress?taskId=f7be82e43e34cb4e960b45d35e8d9596 |
| 134 | var indT= requrl.indexOf( taskStr, 0); |
| 135 | if( indT < 0 ) { |
| 136 | var errMsg= |
| 137 | clName+": "+methName+": Error: No taskId found in the URL !\n"; |
| 138 | logger.addLog( errMsg,this.LogF); |
| 139 | resp= '{\"Error\":\"'+errMsg+'\"}'; |
| 140 | } |
| 141 | else { //.. indT > -1 - have taskId in URL |
| 142 | this.taskId= requrl.substr( indT+ taskStr.length ); |
| 143 | logger.addLog( clName+": "+methName+ |
| 144 | ": extracted from URL: taskId:["+this.taskId+"]\n",this.LogF); |
| 145 | this.pgfname= this.cdt_home+"/proc_status_"+this.taskId+".json"; |
| 146 | // resp= '{\"percentage\":\"3\"}'; |
| 147 | resp= this.readResultFile( this.pgfname ); |
| 148 | } |
| 149 | logger.addLog( clName+": "+methName+ |
| 150 | ": result length="+resp.length+"\n",this.LogF); |
| 151 | logger.addLog( clName+": "+methName+": progress:["+resp+"]\n",this.LogF); |
| 152 | } |
| 153 | else |
| 154 | if( (match1= rexpGPRMS.exec(requrl)) != null ) { |
| 155 | logger.addLog( clName+": "+methName+ |
| 156 | ": URL match /api/get_params\n",this.LogF); |
| 157 | //.. assuming: /api/get_params?taskId=f7be82e43e34cb4e960b45d35e8d9596 |
| 158 | var indT= requrl.indexOf( taskStr, 0); |
| 159 | if( indT < 0 ) { |
| 160 | var errMsg= |
| 161 | clName+": "+methName+": Error: No taskId found in the URL !\n"; |
| 162 | logger.addLog( errMsg,this.LogF); |
| 163 | resp= '{\"Error\":\"'+errMsg+'\"}'; |
| 164 | } |
| 165 | else { //.. indT > -1 - have taskId in URL |
| 166 | this.taskId= requrl.substr( indT+ taskStr.length ); |
| 167 | logger.addLog( clName+": "+methName+ |
| 168 | ": extracted from URL: taskId:["+this.taskId+"]\n",this.LogF); |
| 169 | var prmfname= this.cdt_home+"/template_params_"+this.taskId+".txt"; |
| 170 | resp= this.readResultFile( prmfname ); |
| 171 | } |
| 172 | logger.addLog( clName+": "+methName+ |
| 173 | ": params length="+resp.length+"\n",this.LogF); |
| 174 | } |
| 175 | else |
| 176 | if( (match1= rexpPFMTERR.exec(requrl)) != null ) { |
| 177 | //.. parameter format error file |
| 178 | logger.addLog( clName+": "+methName+ |
| 179 | ": URL match /api/get_pfmterr\n",this.LogF); |
| 180 | //.. assuming: /api/get_pfmterr?taskId=f7be82e43e34cb4e960b45d35e8d9596 |
| 181 | var indT= requrl.indexOf( taskStr, 0); |
| 182 | if( indT < 0 ) { |
| 183 | var errMsg= |
| 184 | clName+": "+methName+": Error: No taskId found in the URL !\n"; |
| 185 | logger.addLog( errMsg,this.LogF); |
| 186 | resp= '{\"Error\":\"'+errMsg+'\"}'; |
| 187 | } |
| 188 | else { //.. indT > -1 - have taskId in URL |
| 189 | this.taskId= requrl.substr( indT+ taskStr.length ); |
| 190 | logger.addLog( clName+": "+methName+ |
| 191 | ": extracted from URL: taskId:["+this.taskId+"]\n",this.LogF); |
| 192 | var errfname= this.cdt_home+"/sync_template_err_"+this.taskId+".txt"; |
| 193 | resp= this.readResultFile( errfname ); |
| 194 | } |
| 195 | logger.addLog( clName+": "+methName+ |
| 196 | ": params length="+resp.length+"\n",this.LogF); |
| 197 | } |
| 198 | else |
| 199 | if( (match1= rexpGFSAVSP.exec(requrl)) != null ) { |
| 200 | //.. Available Space in Server-side file-system |
| 201 | logger.addLog( clName+": "+methName+ |
| 202 | ": URL match /api/get_fsAvSpace \n",this.LogF); |
| 203 | var errMsg= this.getFsAvailSpace( this.cdt_home ); |
| 204 | if( errMsg.length > 0 ) { |
| 205 | //.. return non-empty Error message |
| 206 | resp= '{\"Error\":\"'+errMsg+'\"}'; |
| 207 | }else{ |
| 208 | resp= fsAvSpaceKB; //.. empty return means no error |
| 209 | }; |
| 210 | } |
| 211 | else |
| 212 | if( (match1= rexpACERES.exec(requrl)) != null ) { |
| 213 | //.. (simulated env.) ACE-editor resource files |
| 214 | logger.addLog( clName+": "+methName+ |
| 215 | ": URL match /node_modules/ace-builds...\n",this.LogF); |
| 216 | var acebldStr= "src-min\/"; |
| 217 | var indAB= requrl.indexOf( acebldStr, 0); |
| 218 | if( indAB < 0 ) { |
| 219 | logger.addLog( clName+": "+methName+ |
| 220 | ": Warn: No URL match("+acebldStr+").\n",this.LogF); |
| 221 | } |
| 222 | else { //.. indAB > -1 -got acebldStr |
| 223 | var acefname= requrl.substr( indAB +acebldStr.length ); |
| 224 | logger.addLog( clName+": "+methName+ |
| 225 | ": req.file name:["+acefname+"]\n",this.LogF); |
| 226 | // if( acefname == "mode-velocity.js" || acefname == "theme-chrome.js" ) |
| 227 | if( acefname == "mode-velocity.js" ) |
| 228 | { |
| 229 | var aresfname= this.cdt_home+"/app/"+acefname; |
| 230 | resp= this.readResultFile( aresfname ); |
| 231 | } |
| 232 | else { |
| 233 | logger.addLog( clName+": "+methName+ |
| 234 | ": Warn: No ace URL match("+acebldStr+"...js).\n",this.LogF); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | else { //.. |
| 239 | logger.addLog( clName+": "+methName+ |
| 240 | ": Warn: No URL match - nothing to do !\n",this.LogF); |
| 241 | }; |
| 242 | logger.addLog( clName+": "+methName+ |
| 243 | ": resp.length="+resp.length+"\n finish.\n",this.LogF); |
| 244 | return resp; |
| 245 | }; |
| 246 | |
| 247 | exports.deleteOldFiles = function( ctaskId ) { |
| 248 | var methName= "deleteOldFiles"; |
| 249 | logger.addLog( clName+": "+methName+ |
| 250 | ": start. the complete task Id:["+ctaskId+"]\n",this.LogF); |
| 251 | var cp_args= new Array( 0 ); |
| 252 | cp_args.push( ctaskId ); //.. give it taskId in args |
| 253 | cp_args.push( cdt_home ); |
| 254 | cp_args.push( LOG_DIR ); |
| 255 | var DelScript= this.cdt_home +"/app/deleteOldData.js"; |
| 256 | var chprocI= Chproc.fork( DelScript, cp_args ); |
| 257 | logger.addLog( clName+": "+methName+ |
| 258 | ": DelScript started ...\n",this.LogF); |
| 259 | let timeStamp = new Date().toISOString(); |
| 260 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 261 | }; |
| 262 | |
| 263 | exports.getRoot = function() { |
| 264 | var methName= "getRoot"; |
| 265 | logger.addLog( clName+": "+methName+": start. ROOT URL match \n",this.LogF); |
| 266 | logger.addLog( clName+": "+methName+ |
| 267 | ": cdt_home:["+this.cdt_home+"]\n",this.LogF); |
| 268 | var HTML_ROOT_FLDR= this.cdt_home; |
| 269 | this.rsfname= HTML_ROOT_FLDR+"/index.html"; |
| 270 | var resp= this.readResultFile( this.rsfname ); |
| 271 | logger.addLog( clName+": "+methName+ |
| 272 | ": result length="+resp.length+"\n",this.LogF); |
| 273 | logger.addLog( clName+": "+methName+": resp:["+resp+"]\n",this.LogF); |
| 274 | return resp; |
| 275 | }; |
| 276 | |
| 277 | exports.readResultFile = function( rfname ) { |
| 278 | var methName= "readResultFile"; |
| 279 | //.. read processing result from file and send back to the client |
| 280 | logger.addLog( clName+": "+methName+ |
| 281 | ": start. rfname:["+rfname+"]\n",this.LogF); |
| 282 | var rdata= ""; |
| 283 | try { |
| 284 | rdata= fs.readFileSync( rfname, 'utf8'); |
| 285 | } |
| 286 | catch( err ) { |
| 287 | logger.addLog( clName+": "+methName+ |
| 288 | ": result-file read: Error: code="+err.code+"\n",this.LogF); |
| 289 | throw err; |
| 290 | } |
| 291 | logger.addLog( clName+": "+methName+ |
| 292 | ": result-file content length="+rdata.length+"\n",this.LogF); |
| 293 | return rdata; |
| 294 | }; |
| 295 | |
| 296 | exports.postSrvLogRec = function ( requrl, inpdata, remaddr ) { |
| 297 | //.. to be called by server on receiving POST /api/post_logrec request |
| 298 | var methName= "postSrvLogRec"; |
| 299 | logger.addLog( clName+": "+methName+ |
| 300 | ": start: url:["+ requrl+"]\n",this.LogF); |
| 301 | let timeStamp = new Date().toISOString(); |
| 302 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 303 | logger.addLog( clName+": "+methName+ |
| 304 | ": cdt_home:\n["+cdt_home+"]\n",this.LogF); |
| 305 | logger.addLog( clName+": "+methName+ |
| 306 | ": client: remaddr:["+ remaddr+"]\n",this.LogF); |
| 307 | if( remaddr != null && remaddr != undefined ) { |
| 308 | this.remAddr= remaddr; |
| 309 | }; |
| 310 | logger.addLog( clName+": "+methName+ |
| 311 | ": Log Record:\n-["+inpdata+"]-\n",this.LogF); |
| 312 | var resp= '{\"respStr\":\"OK\"}'; |
| 313 | logger.addLog( clName+": "+methName+": done: resp:["+resp+"]\n",this.LogF); |
| 314 | return( resp ); |
| 315 | }; |
| 316 | |
| 317 | exports.procReq = function ( requrl, inpdata, remaddr ) { |
| 318 | //.. to be called by server on receiving POST /api/* request |
| 319 | // receive input data, save into a file and start server-side processing task |
| 320 | var methName= "procReq"; |
| 321 | logger.addLog( clName+": "+methName+ |
| 322 | ": start: url:["+ requrl+"]\n",this.LogF); |
| 323 | let timeStamp = new Date().toISOString(); |
| 324 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 325 | logger.addLog( clName+": "+methName+ |
| 326 | ": cdt_home:\n["+cdt_home+"]\n",this.LogF); |
| 327 | logger.addLog( clName+": "+methName+ |
| 328 | ": client: remaddr:["+ remaddr+"]\n",this.LogF); |
| 329 | if( remaddr != null && remaddr != undefined ) { |
| 330 | this.remAddr= remaddr; |
| 331 | }; |
| 332 | logger.addLog( clName+": "+methName+ |
| 333 | ": remAddr:["+ this.remAddr+"]\n",this.LogF); |
| 334 | var inpdLen= inpdata.length; |
| 335 | logger.addLog( clName+": "+methName+": inpdata length="+inpdLen+"\n",this.LogF); |
| 336 | if( inpdLen > 300 ) { |
| 337 | logger.addLog( clName+": "+methName+ |
| 338 | ": inpdata:\n["+inpdata.substr(0,299)+"...]\n",this.LogF); |
| 339 | }else{ |
| 340 | logger.addLog( clName+": "+methName+ |
| 341 | ": inpdata:\n["+inpdata+"]\n",this.LogF); |
| 342 | }; |
| 343 | var resp= '{\"respStr\":\"postNotFoundResp\"}'; |
| 344 | var taskStr= "taskId="; |
| 345 | var rexp2= new RegExp(/\/api\/proc_cont/); |
| 346 | var match2= rexp2.exec( requrl ); |
| 347 | if( match2 ) { |
| 348 | logger.addLog( clName+": "+methName+ |
| 349 | ": URL match: proc_cont - processing content...\n",this.LogF); |
| 350 | //.. check available space before server-side processiong |
| 351 | var emsg= this.getFsAvailSpace( this.cdt_home ); |
| 352 | var doAdd= ''; //.. false |
| 353 | var doProc= ''; //.. false |
| 354 | //.. assuming format: |
| 355 | // url: /api/proc_cont?taskId=1fcf9ebb05cec21caeb71ebac7e34b2c&part=2of2 |
| 356 | var indT= requrl.indexOf( taskStr, 0); |
| 357 | if( indT < 0 ) { |
| 358 | logger.addLog( clName+": "+methName+ |
| 359 | ": Warn: No taskId found in the URL - New Task !\n",this.LogF); |
| 360 | this.calcTaskId(); |
| 361 | logger.addLog( clName+": "+methName+ |
| 362 | ": calculated New taskId:["+this.taskId+"]\n", this.LogF); |
| 363 | resp= '{\"respStr\":\"proc_start\",\"taskId\":\"'+this.taskId+'\"}'; |
| 364 | } |
| 365 | else { //.. indT > -1 - have taskId in URL |
| 366 | var indTV= indT+ taskStr.length; |
| 367 | var indDL= requrl.indexOf( fDelim, indTV ); |
| 368 | if( indDL < 0 ) { |
| 369 | logger.addLog( clName+": "+methName+ |
| 370 | ": no more fields after taskId.\n",this.LogF); |
| 371 | this.taskId= requrl.substr( indTV ); |
| 372 | }else{ //.. found delimiter |
| 373 | this.taskId= requrl.substring( indTV, indDL ); |
| 374 | }; |
| 375 | logger.addLog( clName+": "+methName+ |
| 376 | ": extracted from URL: taskId:["+this.taskId+"]\n", this.LogF); |
| 377 | doAdd= "true"; //.. will add to old content |
| 378 | resp= '{\"respStr\":\"proc_upload\"}'; //.. no return of old taskId |
| 379 | } |
| 380 | var inputFilePfx = this.cdt_home +"/posted_data_"; |
| 381 | var infname= inputFilePfx +this.taskId +".txt"; |
| 382 | logger.addLog( clName+": "+methName+ |
| 383 | ": got input file name:["+infname+"]\n",this.LogF); |
| 384 | //.. extracting parts info, e.g.: "...&part=2of2" |
| 385 | var rexp2p= new RegExp(/part=(\d+)of(\d+)/); |
| 386 | var match2p= rexp2p.exec( requrl ); |
| 387 | if( match2p == null ) { |
| 388 | logger.addLog( clName+": "+methName+ |
| 389 | ": Warn: No part number found in the URL "+ |
| 390 | "-cant determine the last part !\n",this.LogF); |
| 391 | } |
| 392 | else { //.. match2p != null |
| 393 | logger.addLog( clName+": "+methName+ |
| 394 | ": match2p: length="+match2p.length+"\n",this.LogF); |
| 395 | for( var i1=0; i1 < match2p.length; i1++ ) { |
| 396 | logger.addLog( clName+": "+methName+ |
| 397 | ": match2p:["+match2p[i1]+"]\n",this.LogF); |
| 398 | }; |
| 399 | var partNum= match2p[ 1]; |
| 400 | var partCnt= match2p[ 2]; |
| 401 | logger.addLog( clName+": "+methName+ |
| 402 | ": partNum:["+partNum+"] partCnt:["+partCnt+"]\n",this.LogF); |
| 403 | if( partNum == partCnt ) { |
| 404 | logger.addLog( clName+": "+methName+ |
| 405 | ": this is the last part -need doProc\n",this.LogF); |
| 406 | doProc= "true"; |
| 407 | }; |
| 408 | }; |
| 409 | //.. write data into the file and start processing right after the close |
| 410 | var errW= this.writeInp2File( infname, inpdata, doAdd, doProc ); |
| 411 | if( errW.length > 0 ) { |
| 412 | resp= errW; |
| 413 | }; |
| 414 | } |
| 415 | else { //.. no match2 |
| 416 | logger.addLog( clName+": "+methName+": Warn: No URL match.\n",this.LogF); |
| 417 | } |
| 418 | logger.addLog( clName+": "+methName+": done: resp:["+resp+"]\n",this.LogF); |
| 419 | return( resp ); |
| 420 | }; |
| 421 | |
| 422 | exports.getFsAvailSpace = function ( fspath ) { |
| 423 | var methName= "getFsAvailSpace"; |
| 424 | //.. get available space (kB) in filesystem where the fspath is mounted |
| 425 | logger.addLog( clName+": "+methName+ |
| 426 | ": start: fspath:\n["+fspath+"]\n",this.LogF); |
| 427 | var timeStampI = Date.now(); |
| 428 | logger.addLog( clName+": "+methName+": timeStampI="+timeStampI+"\n",this.LogF); |
| 429 | var timeStamp = new Date( timeStampI ).toISOString(); |
| 430 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 431 | if( fspath.length < 1 ) { |
| 432 | var errMsg= methName+": Error: the filesystem directory path is empty !"; |
| 433 | logger.addLog( clName+ errMsg+"\n", this.LogF); |
| 434 | return errMsg; |
| 435 | }; |
| 436 | // var carg= "-k "+fspath; |
| 437 | // var mopts= { input: carg, timeout: 4000 }; |
| 438 | //var moutObj= Chproc.spawnSync('df',[],mopts); |
| 439 | var c_args= new Array( 0 ); |
| 440 | c_args.push( '-k' ); |
| 441 | c_args.push( fspath ); |
| 442 | var mopts= { timeout: 4000 }; |
| 443 | logger.addLog( clName+": "+methName+ |
| 444 | ": mopts:["+JSON.stringify(mopts)+"]\n",this.LogF); |
| 445 | //.. start |
| 446 | var moutObj= Chproc.spawnSync('df', c_args, mopts); |
| 447 | logger.addLog( clName+": "+methName+ |
| 448 | ": df done: moutObj: status:["+moutObj.status+"](0 means Ok)\n",this.LogF); |
| 449 | var timeStampI = Date.now(); |
| 450 | logger.addLog( clName+": "+methName+": timeStampI="+timeStampI+"\n",this.LogF); |
| 451 | if( moutObj.error != null ) { |
| 452 | var errMsg= clName+": "+methName+ |
| 453 | ": spawn df: Error:["+JSON.stringify(moutObj.error)+"]"; |
| 454 | logger.addLog( errMsg+"\n", this.LogF); |
| 455 | return errMsg; |
| 456 | }; |
| 457 | logger.addLog( clName+": "+methName+ |
| 458 | ": df moutObj.output: length="+moutObj.output.length+"\n",this.LogF); |
| 459 | if( moutObj.output.length > 0 ) { |
| 460 | for( var i0=0; i0 < moutObj.output.length; i0++ ) { |
| 461 | logger.addLog( clName+": "+methName+ |
| 462 | ": moutObj.output["+i0+"]:["+moutObj.output[i0]+"]\n",this.LogF); |
| 463 | }; //.. loop |
| 464 | //.. output[1]: should contain 2 lines: |
| 465 | // [Filesystem 1K-blocks Used Available Use% Mounted on\n |
| 466 | // /dev/dm-0 37383720 8933604 26528068 26% /\n] |
| 467 | var out1= moutObj.output[1]; |
| 468 | logger.addLog( clName+": "+methName+": to parse out1:["+out1+"]\n",this.LogF); |
| 469 | var rexp1= new RegExp(/\s+(\d+)\s+(\d+)\s+(\d+)\s+/); |
| 470 | logger.addLog( clName+": "+methName+": rexp1:["+rexp1+"]\n",this.LogF); |
| 471 | var matchArr= rexp1.exec( out1 ); |
| 472 | if( matchArr == null ) { |
| 473 | var errMsg= clName+": "+methName+ |
| 474 | ": spawn: Error: No digitals (bytes counts) found in the output !"; |
| 475 | logger.addLog( errMsg+"\n", this.LogF); |
| 476 | return errMsg; |
| 477 | }; |
| 478 | if( matchArr[3] == null || matchArr[3] == undefined ) { |
| 479 | var errMsg= clName+": "+methName+ |
| 480 | ": parsed: Error: The 3-rd byte count is empty (no Available Kbytes) !"; |
| 481 | logger.addLog( errMsg+"\n", this.LogF); |
| 482 | return errMsg; |
| 483 | }; |
| 484 | fsAvSpaceKB= matchArr[3]; |
| 485 | logger.addLog( clName+": "+methName+ |
| 486 | ": extracted fsAvSpaceKB="+fsAvSpaceKB+"\n",this.LogF); |
| 487 | }else{ |
| 488 | var errMsg= clName+": "+methName+ |
| 489 | ": spawn: Error: Empty df-command output array !"; |
| 490 | logger.addLog( errMsg+"\n", this.LogF); |
| 491 | return errMsg; |
| 492 | } |
| 493 | logger.addLog( clName+": "+methName+": finished.\n",this.LogF); |
| 494 | return ''; |
| 495 | }; |
| 496 | |
| 497 | exports.calcTaskId = function () { |
| 498 | var methName= "calcTaskId"; |
| 499 | //.. calculate taskId |
| 500 | logger.addLog( clName+": "+methName+": start:\n",this.LogF); |
| 501 | var timeStampI = Date.now(); |
| 502 | logger.addLog( clName+": "+methName+": timeStampI="+timeStampI+"\n",this.LogF); |
| 503 | var timeStamp = new Date( timeStampI ).toISOString(); |
| 504 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 505 | |
| 506 | var rawId = String( this.remAddr+'-'+timeStampI ); |
| 507 | logger.addLog( clName+": "+methName+": rawId:["+rawId+"]\n",this.LogF); |
| 508 | |
| 509 | var mopts= { input: rawId, timeout: 4000 }; |
| 510 | logger.addLog( clName+": "+methName+ |
| 511 | ": mopts:["+JSON.stringify(mopts)+"]\n",this.LogF); |
| 512 | |
| 513 | var moutObj= Chproc.spawnSync('md5sum',[],mopts); |
| 514 | logger.addLog( clName+": "+methName+ |
| 515 | ": moutObj: status:["+moutObj.status+"]\n",this.LogF); |
| 516 | if( moutObj.error != null ) { |
| 517 | logger.addLog( clName+": "+methName+ |
| 518 | ": moutObj calc.: Error:["+JSON.stringify(moutObj.error)+"]\n",this.LogF); |
| 519 | throw moutObj.error; |
| 520 | } |
| 521 | else { //.. no errors |
| 522 | logger.addLog( clName+": "+methName+ |
| 523 | ": moutObj: output: length="+moutObj.output.length+"\n",this.LogF); |
| 524 | if( moutObj.output.length > 0 ) { |
| 525 | for( var i0=0; i0 < moutObj.output.length; i0++ ) { |
| 526 | logger.addLog( clName+": "+methName+ |
| 527 | ": moutObj: output:["+moutObj.output[i0]+"]\n",this.LogF); |
| 528 | } |
| 529 | } |
| 530 | var out1= moutObj.output[ 1 ]; |
| 531 | logger.addLog( clName+": "+methName+": out1:["+out1+"]\n",this.LogF); |
| 532 | var rexp1= new RegExp(/\w+/); //.. alphanumeric |
| 533 | logger.addLog( clName+": "+methName+": rexp1:["+rexp1+"]\n",this.LogF); |
| 534 | var match1= rexp1.exec( out1 ); |
| 535 | if( match1 == null ) { |
| 536 | logger.addLog( clName+": "+methName+ |
| 537 | ": Warn.: No Match of rexp1 in the cmd output (No taskId) !\n",this.LogF); |
| 538 | } |
| 539 | else { |
| 540 | logger.addLog( clName+": "+methName+ |
| 541 | ": match1:["+match1[0]+"] length="+match1.length+"\n",this.LogF); |
| 542 | this.taskId= match1[0]; |
| 543 | }; |
| 544 | }; |
| 545 | logger.addLog( clName+": "+methName+ |
| 546 | ": done: calculated taskId:["+this.taskId+"]\n",this.LogF); |
| 547 | }; |
| 548 | |
| 549 | exports.writeInp2File = function ( fname, inpd, doAdd, doProc ) { |
| 550 | var methName= "writeInp2File"; |
| 551 | //.. write input data to the file |
| 552 | logger.addLog( clName+": "+methName+ |
| 553 | ": Start: file:["+fname+"]\n",this.LogF); |
| 554 | let timeStamp = new Date().toISOString(); |
| 555 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 556 | var inpdLen= inpd.length; |
| 557 | logger.addLog( clName+": "+methName+": inpd length="+ inpdLen+"\n",this.LogF); |
| 558 | if( inpdLen < 1 ) { |
| 559 | logger.addLog( clName+": "+methName+ |
| 560 | ": Warn.: Empty input - nothing to do !\n",this.LogF); |
| 561 | return ''; //.. not an error |
| 562 | }; |
| 563 | if( inpdLen > 300 ) { |
| 564 | logger.addLog( clName+": "+methName+ |
| 565 | ": inpd(begin):["+ inpd.substr(0,299)+"...]\n",this.LogF); |
| 566 | } |
| 567 | else { |
| 568 | logger.addLog( clName+": "+methName+": inpd:["+ inpd+"]\n",this.LogF); |
| 569 | }; |
| 570 | logger.addLog( clName+": "+methName+ |
| 571 | ": doAdd:["+doAdd+"] doProc:["+doProc+"]\n",this.LogF); |
| 572 | if( ! doAdd ) { |
| 573 | logger.addLog( clName+": "+methName+ |
| 574 | ": Dont doAdd - need new content.\n",this.LogF); |
| 575 | //.. removing old file if is there with the same name (async is Not OK !) |
| 576 | var fname_o= fname+".save"; |
| 577 | try { |
| 578 | fs.renameSync( fname, fname_o ); |
| 579 | } |
| 580 | catch( err ) { |
| 581 | var errMsg= clName+": "+methName+ |
| 582 | ": Warn.: Failed to rename old file:["+fname+"]"+ |
| 583 | " err.code="+err.code+"\n"; |
| 584 | logger.addLog( errMsg, this.LogF ); |
| 585 | }; |
| 586 | }; |
| 587 | logger.addLog( clName+": "+methName+": opening file:["+fname+"]\n",this.LogF); |
| 588 | try { |
| 589 | this.fdI= fs.openSync( fname, 'a' ); |
| 590 | } |
| 591 | catch( err ) { |
| 592 | var errMsg= clName+": "+methName+ |
| 593 | ": Error: Failed to open file:["+fname+"]"+ |
| 594 | " err.code="+err.code+"\n"; |
| 595 | logger.addLog( errMsg, this.LogF ); |
| 596 | return( errMsg ); |
| 597 | }; |
| 598 | logger.addLog( clName+": "+methName+ |
| 599 | ": writing data into the file: fdI="+ this.fdI+"\n",this.LogF); |
| 600 | try { |
| 601 | fs.appendFileSync( this.fdI, inpd, 'utf8' ); |
| 602 | } |
| 603 | catch( err ) { |
| 604 | var errMsg= clName+": "+methName+ |
| 605 | ": Error: Failed to append input data to file: err.code="+err.code+"\n"; |
| 606 | logger.addLog( errMsg, this.LogF ); |
| 607 | return( errMsg ); |
| 608 | }; |
| 609 | logger.addLog( clName+": "+methName+ |
| 610 | ": closing input data file...\n",this.LogF); |
| 611 | try { |
| 612 | fs.closeSync( this.fdI ); |
| 613 | } |
| 614 | catch( err ) { |
| 615 | var errMsg= clName+": "+methName+ |
| 616 | ": Error: Failed to close input data file: err.code="+err.code+"\n"; |
| 617 | logger.addLog( errMsg, this.LogF ); |
| 618 | return( errMsg ); |
| 619 | }; |
| 620 | logger.addLog( clName+": "+methName+ |
| 621 | ": input file was written and closed successfully.\n",this.LogF); |
| 622 | timeStamp = new Date().toISOString(); |
| 623 | logger.addLog( clName+": "+methName+": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 624 | if( doProc ) { |
| 625 | logger.addLog( clName+": "+methName+ |
| 626 | ": starting processing...\n",this.LogF); |
| 627 | this.startProcFile(); |
| 628 | }; |
| 629 | return ''; //.. no error |
| 630 | }; |
| 631 | |
| 632 | exports.startProcFile = function () { |
| 633 | var methName= "startProcFile"; |
| 634 | //.. start processing of the input file in external sub-process |
| 635 | logger.addLog( clName+": "+methName+ |
| 636 | ": Start: taskId:["+this.taskId+"]\n",this.LogF); |
| 637 | let timeStamp = new Date().toISOString(); |
| 638 | logger.addLog( clName+": "+methName+ |
| 639 | ": timeStamp:["+timeStamp+"]\n",this.LogF); |
| 640 | console.log(methName+": cdt_home:["+this.cdt_home+"]"); |
| 641 | var SubProcScriptP= this.cdt_home + SubProcScript; |
| 642 | logger.addLog( clName+": "+methName+ |
| 643 | ": run SubProcScript:\n["+SubProcScriptP+"]\n",this.LogF); |
| 644 | var cp_args= new Array( 0 ); |
| 645 | cp_args.push( this.taskId ); //.. give it taskId in args |
| 646 | cp_args.push( this.cdt_home ); |
| 647 | cp_args.push( this.LOG_DIR ); |
| 648 | var chprocI= Chproc.fork( SubProcScriptP, cp_args ); |
| 649 | |
| 650 | // chprocI.on('message', (msg) => { |
| 651 | // console.log(methName+": chprocI got msgJ:["+JSON.stringify(msg)+"]"); |
| 652 | // }); |
| 653 | // console.log("startProcFile: sending a message to child."); |
| 654 | // chprocI.send({ hello: 'world' }); |
| 655 | logger.addLog( clName+": "+methName+" done submit.\n",this.LogF); |
| 656 | }; |