Added persistent rescheduling of nowPlaying after server goes down.

This commit is contained in:
rainbow napkin 2025-02-11 07:39:20 -05:00
parent 179a10fb72
commit a41541d07b
10 changed files with 124 additions and 25 deletions

View file

@ -19,6 +19,7 @@ const connectedUser = require('./connectedUser');
const queue = require('./media/queue');
const flairModel = require('../../schemas/flairSchema');
const permissionModel = require('../../schemas/permissionSchema');
const channelModel = require('../../schemas/channel/channelSchema');
module.exports = class{
constructor(server, chanDB){
@ -27,7 +28,7 @@ module.exports = class{
this.tokeCommands = chanDB.tokeCommands;
//Keeping these in a map was originally a vestige but it's more preformant than an array or object so :P
this.userList = new Map();
this.queue = new queue(server, this);
this.queue = new queue(server, chanDB, this);
}
async handleConnection(userDB, chanDB, socket){

View file

@ -24,7 +24,7 @@ const loggerUtils = require('../../../utils/loggerUtils');
const channelModel = require('../../../schemas/channel/channelSchema');
module.exports = class{
constructor(server, channel){
constructor(server, chanDB, channel){
//Set server
this.server = server
//Set channel
@ -47,6 +47,9 @@ module.exports = class{
//create boolean to hold schedule lock
this.locked = false;
//Rehydrate channel queue from database
this.rehydrateQueue(chanDB);
}
defineListeners(socket){
@ -441,8 +444,8 @@ module.exports = class{
return false;
}
//If the item has already started and it's not being forced
if((mediaObj.startTime < new Date().getTime())){
//If the item has already started
if((mediaObj.startTime < new Date().getTime()) && !force){
//Set time stamp to existing timestamp plus the difference between the orginal start-date and now
mediaObj.startTimeStamp = mediaObj.startTimeStamp + ((new Date().getTime() - mediaObj.startTime) / 1000)
//Start the item now
@ -493,7 +496,7 @@ module.exports = class{
return mediaObj;
}
start(mediaObj, timestamp = mediaObj.startTimeStamp){
async start(mediaObj, timestamp = mediaObj.startTimeStamp){
//Silently end the media
this.end(true);
@ -503,6 +506,19 @@ module.exports = class{
//Set current playing media
this.nowPlaying = mediaObj;
try{
//Get our channel
const chanDB = await channelModel.findOne({name: this.channel.name});
//Set the now playing queued media document
chanDB.media.nowPlaying = mediaObj;
//Save the channel
await chanDB.save();
}catch(err){
loggerUtils.localExceptionHandler(err);
}
//Send play signal out to the channel
this.sendMedia();
@ -655,4 +671,31 @@ module.exports = class{
broadcastQueue(){
this.server.io.in(this.channel.name).emit('queue',{queue: Array.from(this.schedule)})
}
async rehydrateQueue(chanDB){
try{
//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!`);
}
//Rehydrate the currently playing item
const wasPlaying = chanDB.media.nowPlaying.rehydrate();
//Schedule it
this.scheduleMedia(wasPlaying, null, true);
//if something fucked up
}catch(err){
//bitch about it in the server console
loggerUtils.localExceptionHandler(err);
}
}
}

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const media = require('./media');
module.exports = class extends media{
constructor(title, fileName, url, id, type, duration, startTime, startTimeStamp){
constructor(title, fileName, url, id, type, duration, startTime, startTimeStamp, earlyEnd, uuid){
//Call derived constructor
super(title, fileName, url, id, type, duration);
//Set media start time
@ -27,10 +27,17 @@ module.exports = class extends media{
this.startTimeStamp = startTimeStamp;
//Create empty variable to hold early end if media is stopped early
this.earlyEnd = null;
//Set status for discriminator key
this.status = 'queued';
//Generate id unique to this specific entry of this specific file within this specific channel's queue
//That way even if we have six copies of the same video queued, we can still uniquely idenitify each instance
this.genUUID();
//If we have a null uuid (can't use default argument because of 'this')
if(uuid == null){
//Generate id unique to this specific entry of this specific file within this specific channel's queue
//That way even if we have six copies of the same video queued, we can still uniquely idenitify each instance
this.genUUID();
}else{
this.uuid = uuid;
}
}
//statics