Started work on schedule archiving.

This commit is contained in:
rainbow napkin 2025-02-12 07:41:08 -05:00
parent a41541d07b
commit c8cd336c91
2 changed files with 113 additions and 25 deletions

View file

@ -137,7 +137,7 @@ module.exports = class{
});
//Get schedule as a temporary array
const queue = Array.from(this.channel.queue.schedule);
const queue = await this.channel.queue.prepQueue(chanDB);
//Get schedule lock status
const queueLock = this.channel.queue.locked;

View file

@ -395,12 +395,12 @@ module.exports = class{
//Stop playing
const stoppedMedia = this.nowPlaying;
//End the media
this.end();
//Get difference between current time and start time and set as early end
stoppedMedia.earlyEnd = (new Date().getTime() - stoppedMedia.startTime) / 1000;
//End the media
this.end();
//Broadcast the channel queue
this.broadcastQueue();
}
@ -497,8 +497,11 @@ module.exports = class{
}
async start(mediaObj, timestamp = mediaObj.startTimeStamp){
//Silently end the media
this.end(true);
//If something is already playing
if(this.nowPlaying != null){
//Silently end the media
this.end(true);
}
//reset current timestamp
this.timestamp = timestamp;
@ -552,23 +555,55 @@ module.exports = class{
}
}
end(quiet = false){
//Call off any existing sync timer
clearTimeout(this.syncTimer);
async end(quiet = false){
try{
//Call off any existing sync timer
clearTimeout(this.syncTimer);
//Clear out the sync timer
this.syncTimer = null;
//Clear out the sync timer
this.syncTimer = null;
//Clear now playing
this.nowPlaying = null;
//Clear timestamp
this.timestamp = 0;
//Keep a copy of whats playing for later when we need to clear the DB
const wasPlaying = this.nowPlaying;
//If we're not being quiet
if(!quiet){
//Tell everyone of the end-times
this.server.io.in(this.channel.name).emit('end', {});
//Clear now playing
this.nowPlaying = null;
//Clear timestamp
this.timestamp = 0;
//If we're not being quiet
if(!quiet){
//Tell everyone of the end-times
this.server.io.in(this.channel.name).emit('end', {});
}
//Now that everything is clean, we can take our time with the DB :P
const chanDB = await channelModel.findOne({name:this.channel.name});
//If we couldn't find the channel
if(chanDB == null){
//FUCK
throw new Error(`Unable to find channel document ${this.channel.name} while ending queue item!`);
}
//If we haven't changed 'nowPlaying' in the play list
if(chanDB.media.nowPlaying.uuid == wasPlaying.uuid){
//Take it out
await chanDB.media.nowPlaying.deleteOne();
}
//Take it out of the active schedule
this.schedule.delete(wasPlaying.startTime);
//Add the item to the channel archive
chanDB.media.archived.push(wasPlaying);
//Save our changes to the DB
await chanDB.save();
}catch(err){
loggerUtils.localExceptionHandler(err);
}
}
@ -668,8 +703,58 @@ module.exports = class{
}
broadcastQueue(){
this.server.io.in(this.channel.name).emit('queue',{queue: Array.from(this.schedule)})
async broadcastQueue(chanDB){
this.server.io.in(this.channel.name).emit('queue',{queue: await this.prepQueue()});
}
async prepQueue(chanDB){
try{
console.log(this.schedule);
//If we didn't get handed a freebie
if(chanDB == null){
//Go out and get it done ourselves
chanDB = await channelModel.findOne({name:this.channel.name});
}
//If we couldn't find the channel
if(chanDB == null){
//FUCK
throw new Error(`Unable to find channel document ${this.channel.name} while rehydrating queue!`);
}
//Create an empty array to hold our schedule
let schedule = [];
//Get yestedays epoch
const yesterday = new Date().setDate(new Date().getDate() - 1);
//Iterate through the channel archive backwards to save time
for(let mediaIndex = chanDB.media.archived.length - 1; mediaIndex >= 0; mediaIndex--){
//Grab the current media record
const media = chanDB.media.archived[mediaIndex];
//If the media started within the last 24 hours
if(media.startTime > yesterday){
//Add it to the schedule array as if it where part of the actual schedule map
schedule.push([media.startTime, media]);
//Otherwise if it's older
}else{
//Then we should be done as archived items are added as they are played/end.
//No newer items should be beyond this point!
break;
}
}
//Concatonate the actual schedule to the items we pulled out of the archive return it
return schedule.concat(Array.from(this.schedule));
//If we can't get shit from the database
}catch(err){
//Complain
loggerUtils.localExceptionHandler(err);
//broadcast what we can from RAM
return Array.from(this.schedule);
}
}
async rehydrateQueue(chanDB){
@ -686,11 +771,14 @@ module.exports = class{
throw new Error(`Unable to find channel document ${this.channel.name} while rehydrating queue!`);
}
//Rehydrate the currently playing item
const wasPlaying = chanDB.media.nowPlaying.rehydrate();
//If something was playing
if(chanDB.media.nowPlaying != null){
//Rehydrate the currently playing item
wasPlaying = chanDB.media.nowPlaying.rehydrate();
//Schedule it
this.scheduleMedia(wasPlaying, null, true);
//Schedule it
this.scheduleMedia(wasPlaying, null, true);
}
//if something fucked up
}catch(err){