make all deleteOldVideos actions occur in cron.js, promisify cron funcs
parent
3a4a405e1c
commit
bf1d78235b
685
cron.js
685
cron.js
|
@ -41,6 +41,7 @@ const {
|
|||
const {
|
||||
sqlDate,
|
||||
knexQuery,
|
||||
knexQueryPromise,
|
||||
initiateDatabaseEngine
|
||||
} = require('./libs/sql/utils.js')(s,config)
|
||||
var theCronInterval = null
|
||||
|
@ -108,358 +109,370 @@ const getFileBinDirectory = function(e){
|
|||
}
|
||||
//filters set by the user in their dashboard
|
||||
//deleting old videos is part of the filter - config.cron.deleteOld
|
||||
const checkFilterRules = function(v,callback){
|
||||
//filters
|
||||
if(!v.d.filters||v.d.filters==''){
|
||||
v.d.filters={};
|
||||
}
|
||||
//delete old videos with filter
|
||||
if(config.cron.deleteOld === true){
|
||||
var where = [{
|
||||
"p1":"end",
|
||||
"p2":"<=",
|
||||
"p3": sqlDate(v.d.days+" DAY")
|
||||
}]
|
||||
//exclude monitors with their own max days
|
||||
v.monitorsWithMaxKeepDays.forEach(function(mid){
|
||||
where.push({
|
||||
"p1":"mid",
|
||||
"p2":"!=",
|
||||
"p3":mid,
|
||||
})
|
||||
})
|
||||
v.d.filters.deleteOldVideosByCron={
|
||||
"id":"deleteOldVideosByCron",
|
||||
"name":"deleteOldVideosByCron",
|
||||
"sort_by":"time",
|
||||
"sort_by_direction":"ASC",
|
||||
"limit":"",
|
||||
"enabled":"1",
|
||||
"archive":"0",
|
||||
"email":"0",
|
||||
"delete":"1",
|
||||
"execute":"",
|
||||
"where":where
|
||||
};
|
||||
}
|
||||
s.debugLog('Filters')
|
||||
var keys = Object.keys(v.d.filters)
|
||||
if(keys.length>0){
|
||||
keys.forEach(function(m,current){
|
||||
// b = filter
|
||||
var b = v.d.filters[m];
|
||||
s.debugLog(b)
|
||||
if(b.enabled==="1"){
|
||||
const whereQuery = [
|
||||
['ke','=',v.ke],
|
||||
['status','!=',"0"],
|
||||
['details','NOT LIKE','%"archived":"1"%'],
|
||||
]
|
||||
b.where.forEach(function(condition){
|
||||
if(condition.p1 === 'ke'){condition.p3 = v.ke}
|
||||
whereQuery.push([condition.p1,condition.p2 || '=',condition.p3])
|
||||
})
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Videos",
|
||||
where: whereQuery,
|
||||
orderBy: [b.sort_by,b.sort_by_direction.toLowerCase()],
|
||||
limit: b.limit
|
||||
},(err,r) => {
|
||||
if(r && r[0]){
|
||||
if(r.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'filterMatch',msg:r.length+' SQL rows match "'+m+'"',ke:v.ke,time:moment()})
|
||||
}
|
||||
b.cx={
|
||||
f:'filters',
|
||||
name:b.name,
|
||||
videos:r,
|
||||
time:moment(),
|
||||
ke:v.ke,
|
||||
id:b.id
|
||||
};
|
||||
if(b.archive==="1"){
|
||||
postMessage({f:'filters',ff:'archive',videos:r,time:moment(),ke:v.ke,id:b.id});
|
||||
}else if(b.delete==="1"){
|
||||
postMessage({f:'filters',ff:'delete',videos:r,time:moment(),ke:v.ke,id:b.id});
|
||||
}
|
||||
if(b.email==="1"){
|
||||
b.cx.ff='email';
|
||||
b.cx.delete=b.delete;
|
||||
b.cx.mail=v.mail;
|
||||
b.cx.execute=b.execute;
|
||||
b.cx.query=b.sql;
|
||||
postMessage(b.cx);
|
||||
}
|
||||
if(b.execute&&b.execute!==""){
|
||||
postMessage({f:'filters',ff:'execute',execute:b.execute,time:moment()});
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
if(current===keys.length-1){
|
||||
//last filter
|
||||
callback()
|
||||
}
|
||||
})
|
||||
}else{
|
||||
//no filters
|
||||
callback()
|
||||
}
|
||||
}
|
||||
//database rows with no videos in the filesystem
|
||||
const deleteRowsWithNoVideo = function(v,callback){
|
||||
if(
|
||||
config.cron.deleteNoVideo===true&&(
|
||||
config.cron.deleteNoVideoRecursion===true||
|
||||
(config.cron.deleteNoVideoRecursion===false&&!alreadyDeletedRowsWithNoVideosOnStart[v.ke])
|
||||
)
|
||||
){
|
||||
alreadyDeletedRowsWithNoVideosOnStart[v.ke]=true;
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Videos",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['status','!=','0'],
|
||||
['details','NOT LIKE','%"archived":"1"%'],
|
||||
['time','<', sqlDate('10 MINUTE')],
|
||||
]
|
||||
},(err,evs) => {
|
||||
if(evs && evs[0]){
|
||||
const videosToDelete = [];
|
||||
evs.forEach(function(ev){
|
||||
var filename
|
||||
var details
|
||||
try{
|
||||
details = JSON.parse(ev.details)
|
||||
}catch(err){
|
||||
if(details instanceof Object){
|
||||
details = ev.details
|
||||
}else{
|
||||
details = {}
|
||||
}
|
||||
}
|
||||
var dir = getVideoDirectory(ev)
|
||||
if(details.isUTC === true){
|
||||
filename = localToUtc(ev.time).format('YYYY-MM-DDTHH-mm-ss')+'.'+ev.ext
|
||||
}else{
|
||||
filename = formattedTime(ev.time)+'.'+ev.ext
|
||||
}
|
||||
fileExists = fs.existsSync(dir+filename)
|
||||
if(fileExists !== true){
|
||||
deleteVideo(ev)
|
||||
sendToWebSocket({f:'video_delete',filename:filename+'.'+ev.ext,mid:ev.mid,ke:ev.ke,time:ev.time,end: formattedTime(new Date,'YYYY-MM-DD HH:mm:ss')},'GRP_'+ev.ke);
|
||||
}
|
||||
});
|
||||
if(videosToDelete.length>0 || config.debugLog === true){
|
||||
postMessage({f:'deleteNoVideo',msg:videosToDelete.length+' SQL rows with no file deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
}
|
||||
setTimeout(function(){
|
||||
callback()
|
||||
},3000)
|
||||
})
|
||||
}else{
|
||||
callback()
|
||||
}
|
||||
}
|
||||
//info about what the application is doing
|
||||
const deleteOldLogs = function(v,callback){
|
||||
if(!v.d.log_days||v.d.log_days==''){v.d.log_days=10}else{v.d.log_days=parseFloat(v.d.log_days)};
|
||||
if(config.cron.deleteLogs===true&&v.d.log_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Logs",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.log_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
callback()
|
||||
if(err)return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length>0 || config.debugLog === true){
|
||||
postMessage({f:'deleteLogs',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.log_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
}else{
|
||||
callback()
|
||||
}
|
||||
}
|
||||
//events - motion, object, etc. detections
|
||||
const deleteOldEvents = function(v,callback){
|
||||
if(!v.d.event_days||v.d.event_days==''){v.d.event_days=10}else{v.d.event_days=parseFloat(v.d.event_days)};
|
||||
if(config.cron.deleteEvents===true&&v.d.event_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Events",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.event_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
callback()
|
||||
if(err)return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'deleteEvents',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.event_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
}else{
|
||||
callback()
|
||||
}
|
||||
}
|
||||
//event counts
|
||||
const deleteOldEventCounts = function(v,callback){
|
||||
if(!v.d.event_days||v.d.event_days==''){v.d.event_days=10}else{v.d.event_days=parseFloat(v.d.event_days)};
|
||||
if(config.cron.deleteEvents===true&&v.d.event_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Events Counts",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.event_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
callback()
|
||||
if(err && err.code !== 'ER_NO_SUCH_TABLE')return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'deleteEvents',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.event_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
}else{
|
||||
callback()
|
||||
}
|
||||
}
|
||||
//check for temporary files (special archive)
|
||||
const deleteOldFileBins = function(v,callback){
|
||||
if(!v.d.fileBin_days||v.d.fileBin_days==''){v.d.fileBin_days=10}else{v.d.fileBin_days=parseFloat(v.d.fileBin_days)};
|
||||
if(config.cron.deleteFileBins===true&&v.d.fileBin_days!==0){
|
||||
var fileBinQuery = " FROM Files WHERE ke=? AND `time` < ?";
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Files",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.fileBin_days+' DAY')],
|
||||
]
|
||||
},(err,files) => {
|
||||
if(files && files[0]){
|
||||
//delete the files
|
||||
files.forEach(function(file){
|
||||
deleteFileBinEntry(file)
|
||||
})
|
||||
if(config.debugLog === true){
|
||||
postMessage({
|
||||
f: 'deleteFileBins',
|
||||
msg: files.length + ' files older than ' + v.d.fileBin_days + ' days deleted',
|
||||
ke: v.ke,
|
||||
time: moment()
|
||||
const checkFilterRules = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
//filters
|
||||
if(!v.d.filters||v.d.filters==''){
|
||||
v.d.filters={};
|
||||
}
|
||||
s.debugLog('Filters')
|
||||
var keys = Object.keys(v.d.filters)
|
||||
if(keys.length>0){
|
||||
keys.forEach(function(m,current){
|
||||
// b = filter
|
||||
var b = v.d.filters[m];
|
||||
s.debugLog(b)
|
||||
if(b.enabled==="1"){
|
||||
const whereQuery = [
|
||||
['ke','=',v.ke],
|
||||
['status','!=',"0"],
|
||||
['details','NOT LIKE','%"archived":"1"%'],
|
||||
]
|
||||
b.where.forEach(function(condition){
|
||||
if(condition.p1 === 'ke'){condition.p3 = v.ke}
|
||||
whereQuery.push([condition.p1,condition.p2 || '=',condition.p3])
|
||||
})
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Videos",
|
||||
where: whereQuery,
|
||||
orderBy: [b.sort_by,b.sort_by_direction.toLowerCase()],
|
||||
limit: b.limit
|
||||
},(err,r) => {
|
||||
if(r && r[0]){
|
||||
if(r.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'filterMatch',msg:r.length+' SQL rows match "'+m+'"',ke:v.ke,time:moment()})
|
||||
}
|
||||
b.cx={
|
||||
f:'filters',
|
||||
name:b.name,
|
||||
videos:r,
|
||||
time:moment(),
|
||||
ke:v.ke,
|
||||
id:b.id
|
||||
};
|
||||
if(b.archive==="1"){
|
||||
postMessage({f:'filters',ff:'archive',videos:r,time:moment(),ke:v.ke,id:b.id});
|
||||
}else if(b.delete==="1"){
|
||||
postMessage({f:'filters',ff:'delete',videos:r,time:moment(),ke:v.ke,id:b.id});
|
||||
}
|
||||
if(b.email==="1"){
|
||||
b.cx.ff='email';
|
||||
b.cx.delete=b.delete;
|
||||
b.cx.mail=v.mail;
|
||||
b.cx.execute=b.execute;
|
||||
b.cx.query=b.sql;
|
||||
postMessage(b.cx);
|
||||
}
|
||||
if(b.execute&&b.execute!==""){
|
||||
postMessage({f:'filters',ff:'execute',execute:b.execute,time:moment()});
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
callback()
|
||||
if(current===keys.length-1){
|
||||
//last filter
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}else{
|
||||
//no filters
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
const deleteVideosByDays = async (v,days,addedQueries) => {
|
||||
const whereQuery = [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(days+' DAY')],
|
||||
addedQueries
|
||||
]
|
||||
const selectResponse = await knexQueryPromise({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Videos",
|
||||
where: whereQuery
|
||||
})
|
||||
const videoRows = selectResponse.rows
|
||||
let affectedRows = 0
|
||||
if(videoRows.length > 0){
|
||||
var i;
|
||||
for (i = 0; i < videoRows.length; i++) {
|
||||
const row = videoRows[i];
|
||||
const dir = getVideoDirectory(row)
|
||||
const filename = formattedTime(row.time) + '.' + row.ext
|
||||
await fs.promises.unlink(dir + filename)
|
||||
sendToWebSocket({
|
||||
f: 'video_delete',
|
||||
filename: filename + '.' + row.ext,
|
||||
mid: row.mid,
|
||||
ke: row.ke,
|
||||
time: row.time,
|
||||
end: formattedTime(new Date,'YYYY-MM-DD HH:mm:ss')
|
||||
},'GRP_' + row.ke)
|
||||
}
|
||||
const deleteResponse = await knexQueryPromise({
|
||||
action: "delete",
|
||||
table: "Videos",
|
||||
where: whereQuery
|
||||
})
|
||||
}else{
|
||||
callback()
|
||||
affectedRows = deleteResponse.rows.affectedRows
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
affectedRows: affectedRows,
|
||||
}
|
||||
}
|
||||
//check for files with no database row
|
||||
const checkForOrphanedFiles = function(v,callback){
|
||||
if(config.cron.deleteOrphans === true){
|
||||
console.log('"config.cron.deleteOrphans" has been removed. It has been replace by a one-time-run at startup with "config.insertOrphans". As the variable name suggests, instead of deleting, it will insert videos found without a database row.')
|
||||
console.log('By default "config.orphanedVideoCheckMax" will only check up to 20 video. You can raise this value to any number you choose but be careful as it will check that number of videos on every start.')
|
||||
}
|
||||
callback()
|
||||
}
|
||||
//user processing function
|
||||
const processUser = function(number,rows){
|
||||
var v = rows[number];
|
||||
if(!v){
|
||||
//no user object given
|
||||
return
|
||||
}
|
||||
s.debugLog(v)
|
||||
if(!alreadyDeletedRowsWithNoVideosOnStart[v.ke]){
|
||||
alreadyDeletedRowsWithNoVideosOnStart[v.ke]=false;
|
||||
}
|
||||
if(!overlapLocks[v.ke]){
|
||||
// set overlap lock
|
||||
overlapLocks[v.ke] = true
|
||||
//set permissions
|
||||
v.d=JSON.parse(v.details);
|
||||
//size
|
||||
if(!v.d.size||v.d.size==''){v.d.size=10000}else{v.d.size=parseFloat(v.d.size)};
|
||||
//days to keep videos
|
||||
if(!v.d.days||v.d.days==''){v.d.days=5}else{v.d.days=parseFloat(v.d.days)};
|
||||
knexQuery({
|
||||
const deleteOldVideos = async (v) => {
|
||||
// v = group, admin user
|
||||
if(config.cron.deleteOld === true){
|
||||
const daysOldForDeletion = !isNaN(v.d.days) ? parseFloat(v.d.days) : 5
|
||||
const monitorsIgnored = []
|
||||
const monitorsResponse = await knexQueryPromise({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Monitors",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
]
|
||||
},(err,rr) => {
|
||||
if(!v.d.filters||v.d.filters==''){
|
||||
v.d.filters={};
|
||||
})
|
||||
const monitorRows = monitorsResponse.rows
|
||||
var i;
|
||||
for (i = 0; i < monitorRows.length; i++) {
|
||||
const monitor = monitorRows[i]
|
||||
const monitorId = monitor.id
|
||||
const details = JSON.parse(monitor.details);
|
||||
const monitorsMaxDaysToKeep = !isNaN(details.max_keep_days) ? parseFloat(details.max_keep_days) : null
|
||||
if(monitorsMaxDaysToKeep){
|
||||
const { affectedRows } = await deleteVideosByDays(v,monitorsMaxDaysToKeep,['mid','=',monitorId])
|
||||
const hasDeletedRows = affectedRows && affectedRows.length > 0;
|
||||
if(hasDeletedRows || config.debugLog === true){
|
||||
postMessage({
|
||||
f: 'deleteOldVideosByMonitorId',
|
||||
msg: `${affectedRows} SQL rows older than ${monitorsMaxDaysToKeep} days deleted`,
|
||||
ke: v.ke,
|
||||
mid: monitorId,
|
||||
time: moment(),
|
||||
})
|
||||
}
|
||||
monitorsIgnored.push(['mid','!=',monitorId])
|
||||
}
|
||||
v.monitorsWithMaxKeepDays = []
|
||||
rr.forEach(function(b,m){
|
||||
b.details=JSON.parse(b.details);
|
||||
if(b.details.max_keep_days&&b.details.max_keep_days!==''){
|
||||
v.monitorsWithMaxKeepDays.push(b.mid)
|
||||
v.d.filters['deleteOldVideosByCron'+b.mid]={
|
||||
"id":'deleteOldVideosByCron'+b.mid,
|
||||
"name":'deleteOldVideosByCron'+b.mid,
|
||||
"sort_by":"time",
|
||||
"sort_by_direction":"ASC",
|
||||
"limit":"",
|
||||
"enabled":"1",
|
||||
"archive":"0",
|
||||
"email":"0",
|
||||
"delete":"1",
|
||||
"execute":"",
|
||||
"where":[{
|
||||
"p1":"mid",
|
||||
"p2":"=",
|
||||
"p3":b.mid
|
||||
},{
|
||||
"p1":"end",
|
||||
"p2":"<",
|
||||
"p3": sqlDate(b.details.max_keep_days+" DAY")
|
||||
}]
|
||||
};
|
||||
}
|
||||
const { affectedRows } = await deleteVideosByDays(v,daysOldForDeletion,monitorsIgnored)
|
||||
const hasDeletedRows = affectedRows && affectedRows.length > 0;
|
||||
if(hasDeletedRows || config.debugLog === true){
|
||||
postMessage({
|
||||
f: 'deleteOldVideos',
|
||||
msg: `${affectedRows} SQL rows older than ${daysOldForDeletion} days deleted`,
|
||||
ke: v.ke,
|
||||
time: moment(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
//database rows with no videos in the filesystem
|
||||
const deleteRowsWithNoVideo = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
if(
|
||||
config.cron.deleteNoVideo===true&&(
|
||||
config.cron.deleteNoVideoRecursion===true||
|
||||
(config.cron.deleteNoVideoRecursion===false&&!alreadyDeletedRowsWithNoVideosOnStart[v.ke])
|
||||
)
|
||||
){
|
||||
alreadyDeletedRowsWithNoVideosOnStart[v.ke]=true;
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Videos",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['status','!=','0'],
|
||||
['details','NOT LIKE','%"archived":"1"%'],
|
||||
['time','<', sqlDate('10 MINUTE')],
|
||||
]
|
||||
},(err,evs) => {
|
||||
if(evs && evs[0]){
|
||||
const videosToDelete = [];
|
||||
evs.forEach(function(ev){
|
||||
var filename
|
||||
var details
|
||||
try{
|
||||
details = JSON.parse(ev.details)
|
||||
}catch(err){
|
||||
if(details instanceof Object){
|
||||
details = ev.details
|
||||
}else{
|
||||
details = {}
|
||||
}
|
||||
}
|
||||
var dir = getVideoDirectory(ev)
|
||||
filename = formattedTime(ev.time)+'.'+ev.ext
|
||||
fileExists = fs.existsSync(dir+filename)
|
||||
if(fileExists !== true){
|
||||
deleteVideo(ev)
|
||||
sendToWebSocket({f:'video_delete',filename:filename+'.'+ev.ext,mid:ev.mid,ke:ev.ke,time:ev.time,end: formattedTime(new Date,'YYYY-MM-DD HH:mm:ss')},'GRP_'+ev.ke);
|
||||
}
|
||||
});
|
||||
if(videosToDelete.length>0 || config.debugLog === true){
|
||||
postMessage({f:'deleteNoVideo',msg:videosToDelete.length+' SQL rows with no file deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
}
|
||||
setTimeout(function(){
|
||||
resolve()
|
||||
},3000)
|
||||
})
|
||||
}else{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
//info about what the application is doing
|
||||
const deleteOldLogs = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!v.d.log_days||v.d.log_days==''){v.d.log_days=10}else{v.d.log_days=parseFloat(v.d.log_days)};
|
||||
if(config.cron.deleteLogs===true&&v.d.log_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Logs",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.log_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
resolve()
|
||||
if(err)return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length>0 || config.debugLog === true){
|
||||
postMessage({f:'deleteLogs',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.log_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
deleteOldLogs(v,function(){
|
||||
s.debugLog('--- deleteOldLogs Complete')
|
||||
deleteOldFileBins(v,function(){
|
||||
s.debugLog('--- deleteOldFileBins Complete')
|
||||
deleteOldEvents(v,function(){
|
||||
s.debugLog('--- deleteOldEvents Complete')
|
||||
deleteOldEventCounts(v,function(){
|
||||
s.debugLog('--- deleteOldEventCounts Complete')
|
||||
checkFilterRules(v,function(){
|
||||
s.debugLog('--- checkFilterRules Complete')
|
||||
deleteRowsWithNoVideo(v,function(){
|
||||
s.debugLog('--- deleteRowsWithNoVideo Complete')
|
||||
checkForOrphanedFiles(v,function(){
|
||||
//done user, unlock current, and do next
|
||||
overlapLocks[v.ke]=false;
|
||||
processUser(number+1,rows)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}else{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
//events - motion, object, etc. detections
|
||||
const deleteOldEvents = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!v.d.event_days||v.d.event_days==''){v.d.event_days=10}else{v.d.event_days=parseFloat(v.d.event_days)};
|
||||
if(config.cron.deleteEvents===true&&v.d.event_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Events",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.event_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
resolve()
|
||||
if(err)return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'deleteEvents',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.event_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
})
|
||||
}else{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
//event counts
|
||||
const deleteOldEventCounts = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!v.d.event_days||v.d.event_days==''){v.d.event_days=10}else{v.d.event_days=parseFloat(v.d.event_days)};
|
||||
if(config.cron.deleteEvents===true&&v.d.event_days!==0){
|
||||
knexQuery({
|
||||
action: "delete",
|
||||
table: "Events Counts",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.event_days+' DAY')],
|
||||
]
|
||||
},(err,rrr) => {
|
||||
resolve()
|
||||
if(err && err.code !== 'ER_NO_SUCH_TABLE')return console.error(err);
|
||||
if(rrr.affectedRows && rrr.affectedRows.length > 0 || config.debugLog === true){
|
||||
postMessage({f:'deleteEvents',msg:(rrr.affectedRows || 0)+' SQL rows older than '+v.d.event_days+' days deleted',ke:v.ke,time:moment()})
|
||||
}
|
||||
})
|
||||
}else{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
//check for temporary files (special archive)
|
||||
const deleteOldFileBins = function(v){
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!v.d.fileBin_days||v.d.fileBin_days==''){v.d.fileBin_days=10}else{v.d.fileBin_days=parseFloat(v.d.fileBin_days)};
|
||||
if(config.cron.deleteFileBins===true&&v.d.fileBin_days!==0){
|
||||
var fileBinQuery = " FROM Files WHERE ke=? AND `time` < ?";
|
||||
knexQuery({
|
||||
action: "select",
|
||||
columns: "*",
|
||||
table: "Files",
|
||||
where: [
|
||||
['ke','=',v.ke],
|
||||
['time','<', sqlDate(v.d.fileBin_days+' DAY')],
|
||||
]
|
||||
},(err,files) => {
|
||||
if(files && files[0]){
|
||||
//delete the files
|
||||
files.forEach(function(file){
|
||||
deleteFileBinEntry(file)
|
||||
})
|
||||
if(config.debugLog === true){
|
||||
postMessage({
|
||||
f: 'deleteFileBins',
|
||||
msg: files.length + ' files older than ' + v.d.fileBin_days + ' days deleted',
|
||||
ke: v.ke,
|
||||
time: moment()
|
||||
})
|
||||
}
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
}else{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
//user processing function
|
||||
const processUser = async function(number,rows){
|
||||
var v = rows[number];
|
||||
if(!v){
|
||||
//no user object given
|
||||
return
|
||||
}
|
||||
s.debugLog(`Checking Group Key : ${v.ke}`)
|
||||
s.debugLog(`Owner : ${v.mail}`)
|
||||
if(!alreadyDeletedRowsWithNoVideosOnStart[v.ke]){
|
||||
alreadyDeletedRowsWithNoVideosOnStart[v.ke]=false;
|
||||
}
|
||||
if(!overlapLocks[v.ke]){
|
||||
overlapLocks[v.ke] = true
|
||||
v.d=JSON.parse(v.details);
|
||||
if(!v.d.filters||v.d.filters==''){
|
||||
v.d.filters={};
|
||||
}
|
||||
await deleteOldVideos(v)
|
||||
s.debugLog('--- deleteOldVideos Complete')
|
||||
await deleteOldLogs(v)
|
||||
s.debugLog('--- deleteOldLogs Complete')
|
||||
await deleteOldFileBins(v)
|
||||
s.debugLog('--- deleteOldFileBins Complete')
|
||||
await deleteOldEvents(v)
|
||||
s.debugLog('--- deleteOldEvents Complete')
|
||||
await deleteOldEventCounts(v)
|
||||
s.debugLog('--- deleteOldEventCounts Complete')
|
||||
await checkFilterRules(v)
|
||||
s.debugLog('--- checkFilterRules Complete')
|
||||
await deleteRowsWithNoVideo(v)
|
||||
s.debugLog('--- deleteRowsWithNoVideo Complete')
|
||||
//done user, unlock current, and do next
|
||||
overlapLocks[v.ke]=false;
|
||||
await processUser(number+1,rows)
|
||||
}else{
|
||||
processUser(number+1,rows)
|
||||
await processUser(number+1,rows)
|
||||
}
|
||||
}
|
||||
//recursive function
|
||||
|
|
|
@ -236,6 +236,17 @@ module.exports = (s,config,databaseOptions) => {
|
|||
knexError(dbQuery,options,err)
|
||||
}
|
||||
}
|
||||
const knexQueryPromise = (options) => {
|
||||
return new Promise((resolve,reject) => {
|
||||
knexQuery(options,(err,rows) => {
|
||||
resolve({
|
||||
ok: !err,
|
||||
err: err,
|
||||
rows: rows,
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
const initiateDatabaseEngine = () => {
|
||||
s.databaseEngine = knex(databaseOptions)
|
||||
return s.databaseEngine
|
||||
|
@ -251,6 +262,7 @@ module.exports = (s,config,databaseOptions) => {
|
|||
processWhereCondition: processWhereCondition,
|
||||
knexError: knexError,
|
||||
knexQuery: knexQuery,
|
||||
knexQueryPromise: knexQueryPromise,
|
||||
initiateDatabaseEngine: initiateDatabaseEngine
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ module.exports = (s,config,lang) => {
|
|||
// const findCmd = [videosDirectory].concat(options.flags || ['-maxdepth','1'])
|
||||
fs.writeFileSync(
|
||||
tempDirectory + 'orphanCheck.sh',
|
||||
`find "${videosDirectory}" -maxdepth 1 -type f -exec stat -c "%y %n" {} + | sort -r | head -n ${options.checkMax}`
|
||||
`find "${videosDirectory}" -maxdepth 1 -type f -exec stat -c "%n" {} + | sort -r | head -n ${options.checkMax}`
|
||||
);
|
||||
let listing = spawn('sh',[tempDirectory + 'orphanCheck.sh'])
|
||||
// const onData = options.onData ? options.onData : () => {}
|
||||
|
|
Loading…
Reference in New Issue