add more logging to thread handling

+minor cleanup
pushover
Moe 2019-12-08 21:49:27 +06:00
parent 3531e006f1
commit 0637e32d38
3 changed files with 60 additions and 21 deletions

View File

@ -15,7 +15,12 @@ module.exports = function(jsonData,pamDiffResponder){
var pamDiff
var p2p
var writeToStderr = function(text){
fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
try{
stdioWriters[2].write(Buffer.from(`${text}`, 'utf8' ))
// stdioWriters[2].write(Buffer.from(`${new Error('writeToStderr').stack}`, 'utf8' ))
}catch(err){
fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
}
}
createPamDiffEngine = function(){
var width,
@ -172,7 +177,7 @@ module.exports = function(jsonData,pamDiffResponder){
checkTriggerThreshold(region, detectorObject, function(err2) {
if(!err1 && ! err2){
detectorObject.doObjectDetection = (jsonData.globalInfo.isAtleatOneDetectorPluginConnected && jsonData.rawMonitorConfig.details.detector_use_detect_object === '1')
pamDiffResponder.write(Buffer.from(JSON.stringify(detectorObject)))
}
})
})
@ -403,12 +408,19 @@ module.exports = function(jsonData,pamDiffResponder){
return trigger
}
return function(cameraProcess){
return function(cameraProcess,fallback){
if(jsonData.rawMonitorConfig.details.detector === '1' && jsonData.rawMonitorConfig.coProcessor === false){
//frames from motion detect
if(jsonData.rawMonitorConfig.details.detector_pam === '1'){
createPamDiffEngine()
cameraProcess.stdio[3].pipe(p2p).pipe(pamDiff)
p2p.pipe(pamDiff)
cameraProcess.stdio[3].on('data',(data)=>{
try{
p2p.write(data)
}catch(err){
fallback(err)
}
})
}
}
};

View File

@ -1,11 +1,6 @@
const fs = require('fs')
const spawn = require('child_process').spawn
try{
fs.unlinkSync('/home/Shinobi/test.log')
}catch(err){
}
process.send = process.send || function () {};
// [CTRL] + [C] = exit
process.on('SIGINT', function() {
@ -24,11 +19,11 @@ var newPipes = []
var stdioWriters = [];
var writeToStderr = function(text){
if(!stdioWriters[2]){
fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
}else{
stdioWriters[2].write(Buffer.from(`${text}`, 'utf8' ))
try{
stdioWriters[2].write(Buffer.from(`${text}`, 'utf8' ))
// stdioWriters[2].write(Buffer.from(`${new Error('writeToStderr').stack}`, 'utf8' ))
}catch(err){
fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
}
}
process.on('uncaughtException', function (err) {
@ -38,6 +33,15 @@ process.on('uncaughtException', function (err) {
for(var i=0; i < stdioPipes; i++){
switch(i){
case 0:
newPipes[i] = 0
break;
case 1:
newPipes[i] = 1
break;
case 2:
newPipes[i] = 2
break;
case 3:
stdioWriters[i] = fs.createWriteStream(null, {fd: i});
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
@ -52,18 +56,30 @@ for(var i=0; i < stdioPipes; i++){
break;
}
}
stdioWriters.forEach((writer)=>{
writer.on('error', (err) => {
writeToStderr(err.stack);
});
})
var cameraProcess = spawn(ffmpegAbsolutePath,ffmpegCommandString,{detached: true,stdio:newPipes})
cameraProcess.on('close',()=>{
writeToStderr('Process Closed')
stdioWriters.forEach((writer)=>{
writer.end()
})
process.exit();
})
writeToStderr('Thread Opening')
writeToStderr(JSON.stringify(rawMonitorConfig))
setTimeout(()=>{
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
try{
const attachPamDetector = require(__dirname + '/detector.js')(jsonData,stdioWriters[3])
attachPamDetector(cameraProcess)
attachPamDetector(cameraProcess,(err)=>{
writeToStderr(err)
})
}catch(err){
writeToStderr(err.stack)
}

View File

@ -357,7 +357,6 @@ module.exports = function(s,config,lang){
ke : e.ke,
mid : e.id
},e.ke)
s.group[e.ke].activeMonitors[e.id].spawn.stdio[3].unpipe();
// if(s.group[e.ke].activeMonitors[e.id].p2pStream){s.group[e.ke].activeMonitors[e.id].p2pStream.unpipe();}
if(s.group[e.ke].activeMonitors[e.id].p2p){s.group[e.ke].activeMonitors[e.id].p2p.unpipe();}
delete(s.group[e.ke].activeMonitors[e.id].p2pStream)
@ -404,15 +403,21 @@ module.exports = function(s,config,lang){
}else{
s.coSpawnClose(e)
if(!x||x===1){return};
p=x.pid;
p = x.pid;
if(s.group[e.ke].rawMonitorConfigurations[e.id].type===('dashcam'||'socket'||'jpeg'||'pipe')){
x.stdin.pause();setTimeout(function(){x.kill('SIGTERM');},500)
x.stdin.pause()
setTimeout(function(){
x.kill('SIGTERM')
},500)
}else{
try{
x.stdin.setEncoding('utf8');x.stdin.write('q');
x.stdin.setEncoding('utf8')
x.stdin.write('q')
}catch(er){}
}
setTimeout(function(){exec('kill -9 '+p,{detached: true})},1000)
setTimeout(function(){
exec('kill -9 '+p,{detached: true})
},1000)
}
}
}
@ -662,7 +667,7 @@ module.exports = function(s,config,lang){
if(!options)options = {}
s.checkDetails(e)
if(config.doSnapshot === true){
if(s.group[e.ke] && s.group[e.ke].rawMonitorConfigurations && s.group[e.ke].rawMonitorConfigurations[e.mid].mode !== 'stop'){
if(s.group[e.ke] && s.group[e.ke].rawMonitorConfigurations && s.group[e.ke].rawMonitorConfigurations[e.mid] && s.group[e.ke].rawMonitorConfigurations[e.mid].mode !== 'stop'){
var pathDir = s.dir.streams+e.ke+'/'+e.mid+'/'
s.getRawSnapshotFromMonitor(s.group[e.ke].rawMonitorConfigurations[e.mid],Object.assign({
flags: '-s 200x200'
@ -747,6 +752,11 @@ module.exports = function(s,config,lang){
}
})
})
}
try{
fs.unlinkSync('/home/Shinobi/test.log')
}catch(err){
}
var createCameraFolders = function(e,callback){
//set the recording directory
@ -1064,7 +1074,7 @@ module.exports = function(s,config,lang){
var frameToStreamPrimary
switch(e.details.stream_type){
case'mp4':
// delete(s.group[e.ke].activeMonitors[e.id].mp4frag['MAIN'])
delete(s.group[e.ke].activeMonitors[e.id].mp4frag['MAIN'])
if(!s.group[e.ke].activeMonitors[e.id].mp4frag['MAIN'])s.group[e.ke].activeMonitors[e.id].mp4frag['MAIN'] = new Mp4Frag()
s.group[e.ke].activeMonitors[e.id].mp4frag['MAIN'].on('error',function(error){
s.userLog(e,{type:lang['Mp4Frag'],msg:{error:error}})
@ -1125,6 +1135,7 @@ module.exports = function(s,config,lang){
var frameToStreamAdded
switch(channel.stream_type){
case'mp4':
delete(s.group[e.ke].activeMonitors[e.id].mp4frag[pipeNumber])
if(!s.group[e.ke].activeMonitors[e.id].mp4frag[pipeNumber])s.group[e.ke].activeMonitors[e.id].mp4frag[pipeNumber] = new Mp4Frag();
s.group[e.ke].activeMonitors[e.id].spawn.stdio[pipeNumber].pipe(s.group[e.ke].activeMonitors[e.id].mp4frag[pipeNumber])
break;