Skip to content

Commit

Permalink
Overhaul of Event streaming (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa authored Jan 24, 2024
1 parent 9eb66fe commit 8d59ecf
Show file tree
Hide file tree
Showing 19 changed files with 355 additions and 186 deletions.
16 changes: 10 additions & 6 deletions server/api/ffmpegSettingsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const ffmpegSettingsRouter: FastifyPluginCallback = (
if (typeof err !== 'undefined') {
return res.status(400).send(err);
}
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'FFMPEG configuration updated.',
module: 'ffmpeg',
detail: {
Expand All @@ -42,14 +43,15 @@ export const ffmpegSettingsRouter: FastifyPluginCallback = (
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error updating FFMPEG configuration.',
module: 'ffmpeg',
detail: {
action: 'update',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand All @@ -64,7 +66,8 @@ export const ffmpegSettingsRouter: FastifyPluginCallback = (
ffmpeg.ffmpegExecutablePath = req.body.ffmpegPath;
await req.serverCtx.settings.updateFfmpegSettings(ffmpeg);
ffmpeg = req.serverCtx.settings.ffmpegSettings();
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'FFMPEG configuration reset.',
module: 'ffmpeg',
detail: {
Expand All @@ -76,14 +79,15 @@ export const ffmpegSettingsRouter: FastifyPluginCallback = (
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error reseting FFMPEG configuration.',
module: 'ffmpeg',
detail: {
action: 'reset',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand Down
18 changes: 11 additions & 7 deletions server/api/hdhrSettingsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ export const hdhrSettingsRouter: FastifyPluginCallback = (
await req.serverCtx.settings.updateSettings('hdhr', req.body);
const hdhr = req.serverCtx.settings.hdhrSettings();
await res.send(hdhr);
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'HDHR configuration updated.',
module: 'hdhr',
detail: {
action: 'update',
},
level: 'info',
level: 'success',
});
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error updating HDHR configuration',
module: 'hdhr',
detail: {
action: 'action',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand All @@ -61,7 +63,8 @@ export const hdhrSettingsRouter: FastifyPluginCallback = (
});
const hdhr = req.serverCtx.settings.hdhrSettings();
await res.send(hdhr);
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'HDHR configuration reset.',
module: 'hdhr',
detail: {
Expand All @@ -72,14 +75,15 @@ export const hdhrSettingsRouter: FastifyPluginCallback = (
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error reseting HDHR configuration',
module: 'hdhr',
detail: {
action: 'reset',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
});
Expand Down
28 changes: 17 additions & 11 deletions server/api/plexServersApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,29 @@ export const plexServersRouter: RouterPluginCallback = (
}
const report = await req.serverCtx.plexServerDB.deleteServer(name);
await res.send(report);
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: `Plex server ${name} removed.`,
module: 'plex-server',
detail: {
serverName: name,
action: 'delete',
},
level: 'warn',
level: 'warning',
});
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error deleting plex server.',
module: 'plex-server',
detail: {
action: 'delete',
serverName: name,
error: isError(err) ? err.message : 'Missing',
},
level: 'danger',
level: 'error',
});
}
},
Expand All @@ -149,7 +151,8 @@ export const plexServersRouter: RouterPluginCallback = (
destroyedPrograms += r.destroyedPrograms;
});
await res.status(204).send('Plex server updated.');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: `Plex server ${req.body.name} updated. ${modifiedPrograms} programs modified, ${destroyedPrograms} programs deleted`,
module: 'plex-server',
detail: {
Expand All @@ -161,15 +164,16 @@ export const plexServersRouter: RouterPluginCallback = (
} catch (err) {
logger.error('Could not update plex server.', err);
await res.status(400).send('Could not add plex server.');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error updating plex server.',
module: 'plex-server',
detail: {
action: 'update',
serverName: firstDefined(req, 'body', 'name'),
error: isObject(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand All @@ -181,27 +185,29 @@ export const plexServersRouter: RouterPluginCallback = (
try {
await req.serverCtx.plexServerDB.addServer(req.body);
await res.status(201).send('Plex server added.');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: `Plex server ${req.body.name} added.`,
module: 'plex-server',
detail: {
serverName: req.body.name,
action: 'add',
},
level: 'info',
level: 'success',
});
} catch (err) {
logger.error('Could not add plex server.', err);
await res.status(400).send('Could not add plex server.');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error adding plex server.',
module: 'plex-server',
detail: {
action: 'add',
serverName: firstDefined(req, 'body', 'name'),
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand Down
18 changes: 11 additions & 7 deletions server/api/plexSettingsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ export const plexSettingsRouter: FastifyPluginCallback = (
await req.serverCtx.settings.updateSettings('plexStream', req.body);
const plex = req.serverCtx.settings.plexSettings();
await res.send(plex);
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Plex configuration updated.',
module: 'plex',
detail: {
action: 'update',
},
level: 'info',
level: 'success',
});
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error updating Plex configuration',
module: 'plex',
detail: {
action: 'update',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
},
Expand All @@ -62,7 +64,8 @@ export const plexSettingsRouter: FastifyPluginCallback = (
);
const plex = req.serverCtx.settings.plexSettings();
await res.send(plex);
req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Plex configuration reset.',
module: 'plex',
detail: {
Expand All @@ -74,14 +77,15 @@ export const plexSettingsRouter: FastifyPluginCallback = (
logger.error(err);
await res.status(500).send('error');

req.serverCtx.eventService.push('settings-update', {
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error reseting Plex configuration',
module: 'plex',
detail: {
action: 'reset',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
});
Expand Down
26 changes: 15 additions & 11 deletions server/api/xmltvSettingsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,29 @@ export const xmlTvSettingsRouter: FastifyPluginCallback = (
});
xmltv = req.serverCtx.settings.xmlTvSettings();
await res.send(xmltv);
req.serverCtx.eventService.push('settings-update', {
message: 'xmltv settings updated.',
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'XMLTV settings updated',
module: 'xmltv',
detail: {
action: 'update',
},
level: 'info',
level: 'success',
});
await updateXmltv();
} catch (err) {
logger.error(err);
await res.status(500).send('error');

req.serverCtx.eventService.push('settings-update', {
message: 'Error updating xmltv configuration',
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error updating XMLTV configuration',
module: 'xmltv',
detail: {
action: 'update',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
});
Expand All @@ -72,8 +74,9 @@ export const xmlTvSettingsRouter: FastifyPluginCallback = (
);
const xmltv = req.serverCtx.settings.xmlTvSettings();
await res.send(xmltv);
req.serverCtx.eventService.push('settings-update', {
message: 'xmltv settings reset.',
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'XMLTV settings reset',
module: 'xmltv',
detail: {
action: 'reset',
Expand All @@ -85,14 +88,15 @@ export const xmlTvSettingsRouter: FastifyPluginCallback = (
} catch (err) {
logger.error(err);
await res.status(500).send('error');
req.serverCtx.eventService.push('settings-update', {
message: 'Error reseting xmltv configuration',
req.serverCtx.eventService.push({
type: 'settings-update',
message: 'Error reseting XMLTV configuration',
module: 'xmltv',
detail: {
action: 'reset',
error: isError(err) ? firstDefined(err, 'message') : 'unknown',
},
level: 'danger',
level: 'error',
});
}
});
Expand Down
6 changes: 4 additions & 2 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ export async function initServer(opts: ServerOptions) {
(ctx.hdhrService.ssdp as any).start();
}

ctx.eventService.push('lifecycle', {
ctx.eventService.push({
type: 'lifecycle',
message: `Server Started`,
detail: {
time: new Date().getTime(),
Expand All @@ -246,7 +247,8 @@ export async function initServer(opts: ServerOptions) {
onShutdown('log', [], async () => {
const ctx = await serverContext();
const t = new Date().getTime();
ctx.eventService.push('lifecycle', {
ctx.eventService.push({
type: 'lifecycle',
message: `Initiated Server Shutdown`,
detail: {
time: t,
Expand Down
Loading

0 comments on commit 8d59ecf

Please sign in to comment.