Skip to content

Commit

Permalink
feat: add pause function
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfemir committed Nov 28, 2024
1 parent c147a75 commit f395b7f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def upgrade() -> None:
sa.Column('is_paused', sa.Boolean(), nullable=True, default=False))
op.add_column('MediaItem',
sa.Column('paused_at', sa.DateTime(), nullable=True))
op.add_column('MediaItem',
sa.Column('unpaused_at', sa.DateTime(), nullable=True))
op.add_column('MediaItem',
sa.Column('paused_by', sa.String(), nullable=True))

Expand All @@ -36,5 +38,6 @@ def downgrade() -> None:
# Remove pause-related columns from MediaItem table
op.drop_index(op.f('ix_mediaitem_is_paused'), table_name='MediaItem')
op.drop_column('MediaItem', 'paused_by')
op.drop_column('MediaItem', 'unpaused_at')
op.drop_column('MediaItem', 'paused_at')
op.drop_column('MediaItem', 'is_paused')
9 changes: 7 additions & 2 deletions src/program/media/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class MediaItem(db.Model):
# Pause related fields
is_paused: Mapped[Optional[bool]] = mapped_column(sqlalchemy.Boolean, default=False)
paused_at: Mapped[Optional[datetime]] = mapped_column(sqlalchemy.DateTime, nullable=True)
unpaused_at: Mapped[Optional[datetime]] = mapped_column(sqlalchemy.DateTime, nullable=True)
paused_by: Mapped[Optional[str]] = mapped_column(sqlalchemy.String, nullable=True)

__mapper_args__ = {
"polymorphic_identity": "mediaitem",
Expand Down Expand Up @@ -248,6 +250,7 @@ def to_dict(self):
"scraped_times": self.scraped_times,
"is_paused": self.is_paused,
"paused_at": str(self.paused_at) if self.paused_at else None,
"unpaused_at": str(self.unpaused_at) if self.unpaused_at else None,
}

def to_extended_dict(self, abbreviated_children=False, with_streams=True):
Expand Down Expand Up @@ -408,6 +411,7 @@ def pause(self) -> None:
try:
self.is_paused = True
self.paused_at = datetime.now()
self.unpaused_at = None

session = object_session(self)
if session:
Expand All @@ -428,13 +432,14 @@ def unpause(self) -> None:
logger.debug(f"Unpausing {self.id}")
try:
self.is_paused = False
self.paused_at = None
self.unpaused_at = datetime.now()
# Keep paused_at for history

session = object_session(self)
if session:
session.flush()

logger.info(f"{self.log_string} unpaused, is_paused={self.is_paused}, paused_at={self.paused_at}")
logger.info(f"{self.log_string} unpaused, is_paused={self.is_paused}, unpaused_at={self.unpaused_at}")
except Exception as e:
logger.error(f"Failed to unpause {self.log_string}: {str(e)}")
raise
Expand Down
8 changes: 4 additions & 4 deletions src/routers/secure/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,12 @@ async def pause_items(request: Request, ids: str = None, db: Session = Depends(g

try:
for item in items:
item.pause() # or item.unpause()
item.pause()
db.commit()
except Exception as e:
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Failed to update items: {str(e)}")
raise HTTPException(status_code=500, detail="Failed to update items.")
logger.error(f"Failed to pause items: {str(e)}")
raise HTTPException(status_code=500, detail="Failed to pause items")

return {
"message": f"Successfully paused {len(items)} items",
Expand Down

0 comments on commit f395b7f

Please sign in to comment.