Skip to content

Commit

Permalink
chore: rename store_item and add prefixes to threads to indentify
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaisberg authored and Gaisberg committed Oct 17, 2024
1 parent a44c3ec commit b79c0c6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
21 changes: 5 additions & 16 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,15 @@ def get_item_from_db(session: Session, item_id: int) -> "MediaItem":
logger.error(f"Unknown item type for ID {item_id}")
return None

def store_or_update_item(item: "MediaItem"):
"""Store or update a MediaItem in the database."""
def store_item(item: "MediaItem"):
"""Store a MediaItem in the database."""
from program.media.item import MediaItem

with db.Session() as session:
try:
# Check if the item exists in the database
existing_item = session.query(MediaItem).filter_by(imdb_id=item.imdb_id).first()

if existing_item:
# Merge the existing item to ensure it's bound to the session
existing_item.store_state()
session.merge(existing_item)
logger.log("DATABASE", f"Updated {existing_item.log_string} in the database.")
else:
# Ensure the item is attached to the current session
item.store_state()
session.add(item)
logger.log("DATABASE", f"Inserted {item.log_string} into the database.")

item.store_state()
session.add(item)
logger.log("DATABASE", f"Inserted {item.log_string} into the database.")
session.commit()
except Exception as e:
session.rollback()
Expand Down
2 changes: 1 addition & 1 deletion src/program/libraries/symlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def process_directory(directory, file_map):
if not local_broken_symlinks:
return

with ThreadPoolExecutor(max_workers=max_workers) as executor:
with ThreadPoolExecutor(thread_name_prefix="FixSymlinks", max_workers=max_workers) as executor:
futures = [executor.submit(check_and_fix_symlink, symlink_path, file_map) for symlink_path in local_broken_symlinks]
for future in as_completed(futures):
future.result()
Expand Down
2 changes: 1 addition & 1 deletion src/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def _init_db_from_symlinks(self):
task = progress.add_task("Enriching items with metadata", total=len(items), log="")
with Live(progress, console=console, refresh_per_second=10):
workers = os.getenv("SYMLINK_MAX_WORKERS", 4)
with ThreadPoolExecutor(max_workers=int(workers)) as executor:
with ThreadPoolExecutor(thread_name_prefix="EnhanceSymlinks", max_workers=int(workers)) as executor:
future_to_item = {executor.submit(self._enhance_item, item): item for item in items if isinstance(item, (Movie, Show))}
for future in as_completed(future_to_item):
try:
Expand Down
4 changes: 2 additions & 2 deletions src/utils/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ensure_item_exists_in_db,
get_item_ids,
run_thread_with_db_item,
store_or_update_item
store_item
)
from program.types import Event

Expand Down Expand Up @@ -298,7 +298,7 @@ def add_item(self, item, service="Manual"):
item (MediaItem): The item to add to the queue as an event.
"""
if not ensure_item_exists_in_db(item):
store_or_update_item(item)
store_item(item)

# Get the item's ID before closing or detaching the session
with db.Session() as session:
Expand Down

0 comments on commit b79c0c6

Please sign in to comment.