make cron do queries through main parentProcess

plugin-touch-ups
Moe 2024-09-05 18:38:36 -07:00
parent 6e8d4bf098
commit b7cd3b921a
2 changed files with 35 additions and 3 deletions

View File

@ -13,6 +13,15 @@ module.exports = (s,config,lang) => {
workerProcess.on('message',function(data){
if(data.time === 'moment()')data.time = moment();
switch(data.f){
case'knexQuery':
s.knexQuery(...data.args, function(...args){
workerProcess.postMessage({
f: 'callback',
rid: data.rid,
args,
})
});
break;
case'debugLog':
s.debugLog(...data.data)
break;

View File

@ -42,6 +42,13 @@ parentPort.on('message',(data) => {
setDefaultConfigOptions()
beginProcessing()
break;
case'callback':
if(pendingCallbacks[data.rid]){
pendingCallbacks[data.rid](...data.args)
// console.log(data.rid,typeof pendingCallbacks[data.rid])
delete(pendingCallbacks[data.rid])
}
break;
case'start':case'restart':
setIntervalForCron()
break;
@ -51,7 +58,7 @@ parentPort.on('message',(data) => {
}
})
function debugLog(...args){
if(config.debugLog === true){
if(config.debugLog === true || config.logCronInfo === true){
console.log(...([`CRON.js DEBUG LOG ${new Date()}`].concat(args)))
}
}
@ -64,6 +71,7 @@ function errorLog(...args){
const s = {
debugLog,
}
const pendingCallbacks = {};
function beginProcessing(){
normalLog(`Worker Processing!`)
const {
@ -74,8 +82,6 @@ function beginProcessing(){
} = require('../basic/utils.js')(process.cwd())
const {
sqlDate,
knexQuery,
knexQueryPromise,
initiateDatabaseEngine
} = require('../sql/utils.js')(s,config)
var theCronInterval = null
@ -111,6 +117,23 @@ function beginProcessing(){
const setDiskUsedForGroup = (groupKey,size,target,videoRow) => {
postMessage({f:'s.setDiskUsedForGroup', ke: groupKey, size: size, target: target, videoRow: videoRow})
}
const knexQuery = (...args) => {
const requestId = generateRandomId();
const callback = args.pop();
pendingCallbacks[requestId] = callback;
postMessage({ f: 'knexQuery', args: args, rid: requestId })
}
const knexQueryPromise = (options) => {
return new Promise((resolve,reject) => {
knexQuery(options,(err,rows) => {
resolve({
ok: !err,
err: err,
rows: rows,
})
})
})
}
const getVideoDirectory = function(e){
if(e.mid&&!e.id){e.id=e.mid};
if(e.details&&(e.details instanceof Object)===false){