Support for multiple Plex instances - #1155
Conversation
This adds support for configuring multiple Plex Media Servers. Changes: - Add PlexServer field to AppsConfig for additional servers - Change Apps.Plex from single instance to slice - Update plexcron to collect sessions from all servers - Update empty trash to work across all servers - Update service checks to monitor all servers - Update clientinfo to report all server versions - Add [[plex_server]] config template section Backward Compatibility: - The existing [plex] config section continues to work - Additional servers can be added via [[plex_server]] sections - Single server configs work exactly as before Resolves Notifiarr#790
| // The Lock ensures only one request to Plex happens at once. | ||
| // Because of the cache two requests may get the same answer. | ||
| func (c *cmd) getSessions(ctx context.Context, allowedAge time.Duration) (*plex.Sessions, error) { | ||
| func (c *cmd) getSessionsForServer(ctx context.Context, server *apps.Plex, allowedAge time.Duration) (*plex.Sessions, error) { |
There was a problem hiding this comment.
Does this even need to be renamed?
There was a problem hiding this comment.
Reverted - kept the original name getSessions since the server parameter makes it clear which server is being operated on.
| // Plex does not send a webhook or identify in any other way when an item is "finished". | ||
| func (c *cmd) checkForFinishedItems(ctx context.Context, _ *common.ActionInput) { | ||
| sessionCtx, cancel := context.WithTimeout(ctx, c.Plex.Timeout.Duration) | ||
| for idx := range c.Plex { |
There was a problem hiding this comment.
We should probably make the http method take in an index, and pick the server based on the index. Otherwise, set it to 0 if one isn't provided and assume the first server.
There was a problem hiding this comment.
Added GetByIndex(index int) method that returns a Plex server by index (0-based), or nil if out of range. Also added Len() helper. The First() method now uses GetByIndex(0).
| mnd.Log.Printf("==> Plex Sessions Collection Started, URL: %s, interval:%s timeout:%s webhook_cooldown:%v delay:%v", | ||
| c.Plex.Server.URL, cfg.Interval, c.Plex.Timeout, cfg.Cooldown, cfg.Delay) | ||
|
|
||
| for idx := range c.Plex { |
There was a problem hiding this comment.
"session collection" is enabled and disabled on the website. That configuration is in the "client info" (see a few lines above this). We'll need a way to allow the website to continue to control this, but it needs to default to just controlling the first plex server for now. We can add another structure member to the "actions" struct if needed to keep backward compatibility. In other words, the website needs to be able to independently enable and disable this feature per server.
There was a problem hiding this comment.
Fixed. Session collection and finished items checking now only fire for the first server (website-controlled). Added comments explaining this. The getAllSessions() method remains available for API calls that need to aggregate from all servers, but the timer-based features only use c.Plex[0].
- Revert unnecessary function renames (getSessions, plexSessionTracker) - Add GetByIndex method for index-based server selection - Fix session/finished collection to only fire for first server (website-controlled) - Website config currently controls only the first server for backward compat
|
Addressed all review comments:
Re: frontend and Tautulli plex picker - acknowledged, those will need separate work. |
| now := time.Now() | ||
| info := clientinfo.Get() | ||
| cacheKey := "plexCurrentSessions_" + server.Server.Name() |
There was a problem hiding this comment.
we should use the url not the name.
|
It's looking better. Move all the icons to another PR since they're not used here. They make this PR difficult to review. |
| name := PlexServerName | ||
| if len(plexServers) > 1 { | ||
| name = fmt.Sprintf("%s %d", PlexServerName, idx+1) | ||
| } |
There was a problem hiding this comment.
This may always have the instance number.
| name := PlexServerName | |
| if len(plexServers) > 1 { | |
| name = fmt.Sprintf("%s %d", PlexServerName, idx+1) | |
| } | |
| name := fmt.Sprintf("%s %d", PlexServerName, idx+1) |
(and then just move it below, don't set name)
| if c.Apps.Plex.Enabled() { | ||
| numPlex = 1 | ||
| } | ||
| numPlex := len(c.Apps.Plex) |
There was a problem hiding this comment.
You don't need to set this variable. Just put the len() below.
Support for multiple Plex instances
Adds support for configuring multiple Plex Media Servers, addressing use cases where users run backup Plex servers or have multiple independent Plex installations.
Fixes #790
Changes
PlexServerfield toAppsConfigfor additional servers (backward compatible)Apps.Plexfrom single instance to slice internally[[plex_server]]config template sectionBackward Compatibility
[plex]config section continues to work unchanged[[plex_server]]sectionsConfiguration Example
Backend/Website Implications
The website will now receive:
The website may need minor updates to:
numPlexcount being > 1 in app info