Skip to content

Commit

Permalink
Various fixes (rivenmedia#509)
Browse files Browse the repository at this point in the history
* Fix: Actually add new items to the database and back to queue

* Fix: Pass existing item to state_transition if one exists

* feat: add critical error message and fail out when trying to enhance the same item due to multiple folder entries

* Fix: add the item_id to the added array so the enhancement error will trigger.

* Fix: Don't expunge the objects

---------

Co-authored-by: Administrator <[email protected]>
  • Loading branch information
centerionware and Administrator authored Jul 12, 2024
1 parent cc1984e commit 3dd25ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 10 additions & 5 deletions backend/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def _get_item_from_db(session, item: MediaItem):
match type:
case "movie":
r = session.execute(select(Movie).where(MediaItem.imdb_id==item.imdb_id).options(joinedload("*"))).unique().scalar_one()
session.expunge(r)
return r
case "show":
r = session.execute(select(Show).where(MediaItem.imdb_id==item.imdb_id).options(joinedload("*"))).unique().scalar_one()
session.expunge(r)
for season in r.seasons:
for episode in season.episodes:
episode.parent = season
Expand All @@ -49,7 +47,6 @@ def _get_item_from_db(session, item: MediaItem):
r = session.execute(select(Season).where(Season._id==item._id).options(joinedload("*"))).unique().scalar_one()
r.parent = r.parent
r.parent.seasons = r.parent.seasons
session.expunge(r)
for episode in r.episodes:
episode.parent = r
return r
Expand All @@ -59,7 +56,6 @@ def _get_item_from_db(session, item: MediaItem):
r.parent.parent = r.parent.parent
r.parent.parent.seasons = r.parent.parent.seasons
r.parent.episodes = r.parent.episodes
session.expunge(r)
return r
case _:
logger.error(f"_get_item_from_db Failed to create item from type: {type}")
Expand Down Expand Up @@ -110,9 +106,18 @@ def _run_thread_with_db_item(fn, service, program, input_item: MediaItem | None)
session.expunge_all()
return res
for i in fn(input_item):
if isinstance(i, (Show, Movie, Season, Episode)):
with db.Session() as session:
_check_for_and_run_insertion_required(session, i)
program._push_event_queue(Event(emitted_by=service, item=i))
yield i
return
else:
for i in fn():
yield i
if isinstance(i, (Show, Movie, Season, Episode)):
with db.Session() as session:
_check_for_and_run_insertion_required(session, i)
program._push_event_queue(Event(emitted_by=service, item=i))
else:
program._push_event_queue(Event(emitted_by=service, item=i))
return
7 changes: 6 additions & 1 deletion backend/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,16 @@ def start(self):
run_migrations()
with db.Session() as session:
res = session.execute(select(func.count(MediaItem._id))).scalar_one()
added = []
if res == 0:
for item in self.services[SymlinkLibrary].run():
if settings_manager.settings.map_metadata:
if isinstance(item, (Movie, Show)):
item = next(self.services[TraktIndexer].run(item))
if item.item_id in added:
logger.error(f"Cannot enhance metadata, {item.title} ({item.item_id}) contains multiple folders. Manual resolution required.")
exit(0)
added.append(item.item_id)
item.store_state()
session.add(item)
logger.debug(f"Mapped metadata to {item.type.title()}: {item.log_string}")
Expand Down Expand Up @@ -379,7 +384,7 @@ def run(self):
with db.Session() as session:
existing_item = DB._get_item_from_db(session, event.item)
updated_item, next_service, items_to_submit = process_event(
existing_item, event.emitted_by, event.item
existing_item, event.emitted_by, existing_item if existing_item is not None else event.item
)

if updated_item and isinstance(existing_item, (Movie, Show)) and updated_item.state == States.Symlinked:
Expand Down

0 comments on commit 3dd25ef

Please sign in to comment.