make cron delete fileBin files and adjust live counter accordingly

merge-requests/289/head
Moe Alam 2020-10-18 22:15:06 -07:00
parent 4465649c4c
commit 9d2629825b
3 changed files with 50 additions and 49 deletions

35
cron.js
View File

@ -88,6 +88,9 @@ const sendToWebSocket = (x,y) => {
const deleteVideo = (x) => {
postMessage({f:'s.deleteVideo',file:x})
}
const deleteFileBinEntry = (x) => {
postMessage({f:'s.deleteFileBinEntry',file:x})
}
const getVideoDirectory = function(e){
if(e.mid&&!e.id){e.id=e.mid};
if(e.details&&(e.details instanceof Object)===false){
@ -344,31 +347,21 @@ const deleteOldFileBins = function(v,callback){
['time','<', sqlDate(v.d.fileBin_days+' DAY')],
]
},(err,files) => {
if(files&&files[0]){
if(files && files[0]){
//delete the files
files.forEach(function(file){
fs.unlink(getFileBinDirectory(file) + file.name,function(err){
// if(err)console.error(err)
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()
})
})
//delete the database rows
knexQuery({
action: "delete",
table: "Files",
where: [
['ke','=',v.ke],
['time','<', sqlDate(v.d.fileBin_days+' DAY')],
]
},(err,rrr) => {
callback()
if(err)return console.error(err);
if(rrr.affectedRows && rrr.affectedRows.length>0 || config.debugLog === true){
postMessage({f:'deleteFileBins',msg:(rrr.affectedRows || 0)+' files older than '+v.d.fileBin_days+' days deleted',ke:v.ke,time:moment()})
}
})
}else{
callback()
}
}
callback()
})
}else{
callback()

View File

@ -90,39 +90,44 @@ module.exports = function(s,config,lang,app,io){
const groupKey = options.ke
const monitorId = options.mid
const filename = options.name
const whereQuery = {
ke: groupKey,
mid: monitorId,
name: filename,
}
if(!filename){
resolve('No Filename')
return
}
s.knexQuery({
action: "select",
columns: "size",
table: "Files",
where: {
ke: groupKey,
mid: monitorId,
name: filename,
}
},(err,rows) => {
if(rows[0]){
const fileSize = rows[0].size
s.knexQuery({
action: "delete",
table: "Files",
where: {
ke: groupKey,
mid: monitorId,
name: filename,
}
},(err) => {
resolve()
s.setDiskUsedForGroup(groupKey,-(fileSize/1048576),'fileBin')
s.purgeDiskForGroup(groupKey)
})
}else{
const deleteRow = (fileSize) => {
s.knexQuery({
action: "delete",
table: "Files",
where: whereQuery
},(err,r) => {
resolve()
}
})
s.file('delete',getFileBinDirectory(whereQuery) + filename)
s.setDiskUsedForGroup(groupKey,-(fileSize/1048576),'fileBin')
s.purgeDiskForGroup(groupKey)
})
}
if(options.size){
deleteRow(options.size)
}else{
s.knexQuery({
action: "select",
columns: "size",
table: "Files",
where: whereQuery
},(err,rows) => {
if(rows[0]){
const fileSize = rows[0].size
deleteRow(fileSize)
}else{
resolve()
}
})
}
})
}
const insertFileBinEntry = (options) => {

View File

@ -974,6 +974,9 @@ module.exports = function(s,config,lang,io){
case's.deleteVideo':
s.deleteVideo(d.file)
break;
case's.deleteFileBinEntry':
s.deleteFileBinEntry(d.file)
break;
case'start':case'end':
d.mid='_cron';s.userLog(d,{type:'cron',msg:d.msg})
break;