I-am-PUID-0 / pd_zurg

A combined docker image for the unified deployment of itsToggle's, yowmamasita's, and ncw's projects -- plex_debrid, zurg, and rclone
MIT License
170 stars 14 forks source link

Question: Can my Plex library be refreshed when a manual change is detected? #8

Closed nbn22385 closed 9 months ago

nbn22385 commented 9 months ago

Is there a way to configure pd_zurg to refresh my Plex library when files are added by a means other than requests/watchlist, but simply when changes are detected? This could come down to a configuration issue I missed.

Scenario 1: When I add a file to my RD library manually (via DebridMediaManager), pd_zurg detects the change to the rclone mount but the Plex library does not refresh automatically. I need to "Scan Library Files" in Plex to see the new file. This is also true for files I delete manually.

pd_zurg  | Jan 11, 2024 10:46:26 - INFO - Zurg w/ RealDebrid subprocess:    manager Detected changes! Refreshing 13 torrents
pd_zurg  | Jan 11, 2024 10:46:26 - INFO - Zurg w/ RealDebrid subprocess:    manager Compiled into 13 torrents, 0 were missing info
pd_zurg  | Jan 11, 2024 10:46:26 - INFO - Zurg w/ RealDebrid subprocess:    manager Finished refreshing torrents

Scenario 2: When I request a file via Overseerr, the file is added to my RD library and pd_zurg (attempted to) refresh the Plex library automatically. However, the file is not actually detected by Plex until I manually update, because from the logs below, it looks like pd_zurg is sending the update command with the wrong path. This looks a lot like the nested movie folder issue reported in #4. (Plex is attempting to process /torrents/movies/MOVIE.mkv rather than /torrents/movies/MOVIE/ and throws an error). Not sure why Plex is getting that bad path anyway and not just refreshing the library section as a whole.

pd_zurg logs:

pd_zurg  | [12/01/24 12:21:43] [overseerr] found new overseerr request by user "REMOVED".
pd_zurg  | [12/01/24 12:21:43] [overseerr] matching overseerr requests to service content.services.plex ... done
pd_zurg  | [12/01/24 12:21:44] [plex] getting plex library section/s "Movies","MoviesRD","ShowsRD","TV Shows" ... done
pd_zurg  | [12/01/24 12:21:44] [plex] getting metadata for 1 collected movies/shows ... done
pd_zurg  | [12/01/24 12:21:44] checking new content ... done
pd_zurg  | [12/01/24 12:21:44] scraping sources [torrentio] for query "knives.out.2019" ... done - found 50 releases
pd_zurg  | [12/01/24 12:21:44] checking cache status for scraped releases on: [Real Debrid] ... done
pd_zurg  | [12/01/24 12:21:46] sorting releases for version [1080p SDR] ... done - found 40 releases
pd_zurg  | [12/01/24 12:21:48] [realdebrid] adding cached release: ../.Knives.Out.(2019).BDRemux.1080p...|.D.P.A.|.iTunes
pd_zurg  | [12/01/24 12:21:48] took 3.58s
pd_zurg  | [12/01/24 12:21:58] [plex] refreshing movie library section/s: "MoviesRD"    <---- Attemping to refresh Plex
pd_zurg  | [12/01/24 12:21:58] [overserr] marking request as available
pd_zurg  | Jan 12, 2024 12:21:59 - INFO - Zurg w/ RealDebrid subprocess:    manager Detected changes! Refreshing 15 torrents
pd_zurg  | Jan 12, 2024 12:21:59 - INFO - Zurg w/ RealDebrid subprocess:    manager Compiled into 16 torrents, 0 were missing info
pd_zurg  | Jan 12, 2024 12:21:59 - INFO - Zurg w/ RealDebrid subprocess:    manager Finished refreshing torrents

Plex logs:

Jan 12, 2024 12:21:44.055 [140425979136824] WARN - [Req#1096a] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.152 [140425979136824] WARN - [Req#1096b] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.157 [140425979136824] WARN - [Req#1096c] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.160 [140425979136824] WARN - [Req#1096d] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.165 [140425979136824] WARN - [Req#1096e] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.224 [140425979136824] WARN - [Req#1096f] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.232 [140425979136824] WARN - [Req#10970] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:44.242 [140425979136824] WARN - [Req#10971] Missing X-Plex-Container-Size header. This will fail with status code 400 in the future.
Jan 12, 2024 12:21:58.140 [140426065804088] WARN - Error scanning directory, we'll skip and continue: boost::filesystem::last_write_time: No such file or directory [system:2]: "/torrents/movies/Knives.Out.2019.D.MVO.AVO.BDREMUX.1080p.seleZen.mkv"

^ That path in the last line is not valid, the movie file resides in a folder with the same name as the file.

My configuration is here.

I-am-PUID-0 commented 9 months ago

Thanks for the very detailed response.

A couple of notes:

  1. Your PLEX_MOUNT_DIR likely needs to be changed to /torrents
  2. The log line - [plex] refreshing movie library section/s: "MoviesRD" <---- Attemping to refresh Plex is from plex_debrid. If using PLEX_REFRESH, you may want to disable the Library update services for Plex from within plex_debrid.
nbn22385 commented 9 months ago

A follow-up: I changed PLEX_MOUNT_DIR=/torrents and did the following tests,

First I set PLEX_REFRESH=false and used the plex_debrid Plex Library Update Service, the Overseerr request goes through and Plex updates successfully.

Then I set PLEX_REFRESH=true and removed Plex from "Library Update Services" in the plex_debrid menu and tried the following:

  1. When adding a request in Overseerr, the file is scraped and added to my RD library but Plex is not updated. Not sure what I could be doing wrong here.
Config
Log ``` pd_zurg | [13/01/24 20:34:48] [overseerr] found new overseerr request by user "REMOVED". pd_zurg | [13/01/24 20:34:48] [overseerr] matching overseerr requests to service content.services.plex ... done pd_zurg | [13/01/24 20:34:49] [plex] getting plex library section/s "Movies","MoviesRD","ShowsRD","TV Shows" ... done pd_zurg | [13/01/24 20:34:49] checking new content ... done pd_zurg | [13/01/24 20:34:49] error: media item has no title or release year. This unknown movie/show might not be released yet. pd_zurg | [13/01/24 20:34:49] error: media item has no title or release year. This unknown movie/show might not be released yet. pd_zurg | [13/01/24 20:34:49] error: media item has no title or release year. This unknown movie/show might not be released yet. pd_zurg | [13/01/24 20:34:49] scraping sources [torrentio] for query "indiana.jones.and.the.dial.of.destiny.2023" ... done - found 113 releases pd_zurg | [13/01/24 20:34:50] checking cache status for scraped releases on: [Real Debrid] ... done pd_zurg | [13/01/24 20:34:52] sorting releases for version [1080p SDR] ... done - found 101 releases pd_zurg | [13/01/24 20:34:55] [realdebrid] adding cached release: Indiana.Jones.And.The.Dial.Of.Destiny.2023.1080p.REMUX.ENG.ITA.LATINO.DTS-HD.Master.DDP5.1... done pd_zurg | [13/01/24 20:34:55] took 5.85s pd_zurg | [13/01/24 20:34:55] [overserr] marking request as available pd_zurg | Jan 13, 2024 20:35:09 - INFO - Zurg w/ RealDebrid subprocess: manager Detected changes! Refreshing 15 torrents pd_zurg | Jan 13, 2024 20:35:10 - INFO - Zurg w/ RealDebrid subprocess: manager Compiled into 15 torrents, 0 were missing info pd_zurg | Jan 13, 2024 20:35:10 - INFO - Zurg w/ RealDebrid subprocess: manager Finished refreshing torrents ```
  1. When adding a file manually via DebridMediaManager, there is also no Plex update performed.
I-am-PUID-0 commented 9 months ago

I assume the environment variables that =SECRET are actually uncommented when running?

Do you ever see anything like the below?

Jan 13, 2024 12:36:39 - DEBUG - Zurg w/ RealDebrid subprocess:  manager Output of hook on_library_update:
Jan 13, 2024 12:36:39 - INFO - Zurg w/ RealDebrid subprocess: Starting Plex Update for __all__/Reacher.S02E07.MULTI.2160p.WEB-DL.HDR.H265-AOC
Jan 13, 2024 12:36:39 - INFO - Zurg w/ RealDebrid subprocess: Starting Plex Update for shows/Reacher.S02E07.MULTI.2160p.WEB-DL.HDR.H265-AOC
nbn22385 commented 9 months ago

I assume the environment variables that =SECRET are actually uncommented when running?

They are commented out since I followed the secrets section of the Readme and have text files for each of the secrets along with the secrets: keyword in the config.

Do you ever see anything like the below?

Nope, after another attempt the closest I got was this (PLEX_REFRESH=true with Plex Library Update Services removed from plex_debrid:

pd_zurg  | Jan 13, 2024 21:45:39 - INFO - Zurg w/ RealDebrid subprocess:    manager Detected changes! Refreshing 14 torrents
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   realdebrid  Got info for torrent SRRVQEIAYVZNK (progress=100%)
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Saved torrent SRRVQEIAYVZNK to file
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Fetched info for 14 torrents
pd_zurg  | Jan 13, 2024 21:45:39 - INFO - Zurg w/ RealDebrid subprocess:    manager Compiled into 14 torrents, 0 were missing info
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Added Indiana.Jones.And.The.Dial.Of.Destiny.2023.1080p.REMUX.ENG.ITA.LATINO.DTS-HD.Master.DDP5.1.MKV-BEN.THE.MEN to [movies]
pd_zurg  | Jan 13, 2024 21:45:39 - INFO - Zurg w/ RealDebrid subprocess:    manager Finished refreshing torrents
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Triggering hook on_library_update for 2 path(s)
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Found 0 torrents to repair
pd_zurg  | Jan 13, 2024 21:45:39 - DEBUG - Zurg w/ RealDebrid subprocess:   manager Finished repairing all torrents

Do you have a working config available for viewing?

I-am-PUID-0 commented 9 months ago

Right. Sorry. Working from my phone at the moment.

The Triggering hook on_library_update for 2 path(s) is the zurg call to the plex_refresh.py file in /zurg/RD. Unfortunately, since the zurg app is calling the update script, any logging is limted by zurg until manager Output of hook on_library_update: is displayed.

Can you check your config.yml in the /zurg/RD directory for the following:

on_library_update: |
    tmpfile=$(mktemp)
    for arg in "$@"
    do
        echo "$arg" >> "$tmpfile"
    done

    unique_args=$(sort -u "$tmpfile")

    if [ -n "$unique_args" ]; then
        IFS=$'\n'
        for line in $unique_args; do
            python plex_refresh.py "$line"
        done
        unset IFS
    fi
    rm "$tmpfile"

# network configs
nbn22385 commented 9 months ago

Yes my /pd_zurg/RD/config.yml has that code. I wonder if I have a path issue/mismatch somewhere. Do my volumes look ok?

  pd_zurg:
    ...
    volumes:
      - /pd_zurg/config:/config
      - /pd_zurg/log:/log
      - /pd_zurg/cache:/cache
      - /pd_zurg/RD:/zurg/RD
      - /pd_zurg/mnt:/data:shared

  plex: 
    ...
    volumes:
        ...
      - /pd_zurg/mnt/pd_zurg/:/torrents

I also noticed plex_debrid doesn't like when I have no library update service set up:

Looks like your current settings didnt pass preflight checks. Please edit the following setting/s:
Library update services: Please add at least one libary update service.

Thanks for all the help.

nbn22385 commented 9 months ago

Update! After much debugging, I've figured out the issue is related to my use of Docker secrets. When I revert to placing my api keys in the docker environment: section, the Plex update works across the board for files added both through Overseerr and DebridMediaManager (Produces the INFO - Zurg w/ RealDebrid subprocess: Starting Plex Update for ... log statements you had asked about).

That answers my original question regarding the ability to update Plex with manually added files 👍

The issue remains why my Docker secrets setup is not working or producing any error logs, but I will try regenerating their text files and see what I can find out. I had a feeling it had something to do with creating my secret files with printf/echo -n vs echo, but haven't been able to prove it yet. I put a print statement into the main() of plex_refresh.py script and it seems like the script is not called at all when I utilize docker secrets.

Are these variables getting set properly in the docker secrets use case, or should they be using the app variables PLEXADD/PLEXTOKEN instead?

I-am-PUID-0 commented 9 months ago

Thanks for continuing to troubleshoot! I believe you may be correct in your assessment. Since the Zurg application directly calls plex_refresh.py, it may not be in the same namespace as main.py. I'll see if I can resolve this by importing the base init.py to the plex_refresh.py. I'll report back once I've confirmed and resolved the issue.

I-am-PUID-0 commented 9 months ago

I won't have time to test it for a bit, but if you'd like, you can try modifying the plex_refresh.py in the /zurg/RD directory with the below:

import sys
sys.path.append('../')
from base import PLEXTOKEN, PLEXADD, PLEXMOUNT, RCLONEMN
from plexapi.server import PlexServer
import os
import time

# Configuration
plex_url = PLEXADD.replace("'", "").replace('"', '')
token = PLEXTOKEN.replace("'", "").replace('"', '')
plex_mount = PLEXMOUNT.replace("'", "").replace('"', '')
zurg_mount = f"/data/{RCLONEMN}"
nbn22385 commented 9 months ago

So far I couldn't get it to work with those modifications in both /zurg/RD/plex_refresh.py and /zurg/plex_refresh.py inside the container since I wasn't sure which was the true version. I also tried hardcoding sys.path.append('/base') since base seems to be 2 levels up from /zurg/RD/plex_refresh.py. Perhaps I am testing it wrong and you will have better results.

I-am-PUID-0 commented 9 months ago

So far I couldn't get it to work with those modifications in both /zurg/RD/plex_refresh.py and /zurg/plex_refresh.py inside the container since I wasn't sure which was the true version. I also tried hardcoding sys.path.append('/base') since base seems to be 2 levels up from /zurg/RD/plex_refresh.py. Perhaps I am testing it wrong and you will have better results.

Thanks. I believe it may not have been going far enough for the parent directories. the /zurg/RD/plex_refresh.py is the one that will be called by Zurg when new content is added. /zurg/plex_refresh.py is the default, which gets added to the subdirectories if the file does not exist. Please try the below:

import os
import sys
import time
from plexapi.server import PlexServer
from pathlib import Path
plex_refresh_path = Path(__file__).resolve()
project_path = plex_refresh_path.parent.parent.parent
sys.path.append(str(project_path))
from base import PLEXTOKEN, PLEXADD, PLEXMOUNT, RCLONEMN

# Configuration
plex_url = PLEXADD.replace("'", "").replace('"', '')
token = PLEXTOKEN.replace("'", "").replace('"', '')
plex_mount = PLEXMOUNT.replace("'", "").replace('"', '')
zurg_mount = f"/data/{RCLONEMN}"
zurg_timeout = 300  # 5 minutes in seconds for Zurg file availability
plex_timeout = 60   # Maximum time to wait for Plex to process the refresh
wait_increment = 1  # Time increment for each wait step
max_retries = 20     # Maximum number of retries for Plex refresh

### Do not alter below ###
nbn22385 commented 9 months ago

I updated to your latest suggestion and everything seems to work when using the Docker secrets 👍. The Plex library was updated in all of my scenarios: Overseerr request, Plex watchlist, and DebridMediaManager.


Just one final question if you have time: do you have a certain dependency order between your plex and pd_zurg services? I didn't see this documented anywhere but was wondering if it was possible to avoid this error on startup:

pd_zurg  | [18/01/24 12:12:44] [plex error]: couldnt reach local plex server at: http://ubuntu-server.local:32400 to determine library sections. Make sure the address is correct, the server is running, and youve set up at least one library.

I typically start pd_zurg and then plex since the rclone mount needs to be present for Plex to start up, but I'm not sure the above error can be avoided on initial startup. It might be a non-issue since the services seem to work fine once Plex is up a few seconds later. Using depends_on seems to start Plex too soon before the mount is available.

I ended up going with this method to utilize your healthcheck, although there is a delay of about 30 seconds before Plex is up (it's much faster if I manually start the services). Maybe that's just how long the healthcheck takes.

plex:
  depends_on:
    pd_zurg:
      condition: service_healthy
$ time docker compose up -d plex
[+] Running 2/2
 ✔ Container pd_zurg  Healthy                                                                                                                                              0.0s
 ✔ Container plex     Started                                                                                                                                              0.0s
docker compose up -d plex  0.36s user 0.22s system 0% cpu 1:01.00 total

Edit: I kind of answered my own question about the long delay (it depends on the --interval of the healthcheck); I overrode your healthcheck interval from 60s to 10s to reduce my startup time and so far it's working ok.

pd_zurg:
  healthcheck:
    interval: 10s
I-am-PUID-0 commented 9 months ago

I updated to your latest suggestion and everything seems to work when using the Docker secrets 👍. The Plex library was updated in all of my scenarios: Overseerr request, Plex watchlist, and DebridMediaManager.

Just one final question if you have time: do you have a certain dependency order between your plex and pd_zurg services? I didn't see this documented anywhere but was wondering if it was possible to avoid this error on startup:

pd_zurg  | [18/01/24 12:12:44] [plex error]: couldnt reach local plex server at: http://ubuntu-server.local:32400 to determine library sections. Make sure the address is correct, the server is running, and youve set up at least one library.

I typically start pd_zurg and then plex since the rclone mount needs to be present for Plex to start up, but I'm not sure the above error can be avoided on initial startup. It might be a non-issue since the services seem to work fine once Plex is up a few seconds later. Using depends_on seems to start Plex too soon before the mount is available.

I ended up going with this method to utilize your healthcheck, although there is a delay of about 30 seconds before Plex is up (it's much faster if I manually start the services). Maybe that's just how long the healthcheck takes.

plex:
  depends_on:
    pd_zurg:
      condition: service_healthy
$ time docker compose up -d plex
[+] Running 2/2
 ✔ Container pd_zurg  Healthy                                                                                                                                              0.0s
 ✔ Container plex     Started                                                                                                                                              0.0s
docker compose up -d plex  0.36s user 0.22s system 0% cpu 1:01.00 total

Edit: I kind of answered my own question about the long delay (it depends on the --interval of the healthcheck); I overrode your healthcheck interval from 60s to 10s to reduce my startup time and so far it's working ok.

pd_zurg:
  healthcheck:
    interval: 10s

Excellent! Glad to see it is working. Thank you for testing. I'll add the fix to the next release. I also like the work around to speed up deployment. I'll add it to the wiki as well.

I-am-PUID-0 commented 9 months ago

Resolved in Version [2.0.2]