Refactor scheduled task repository and schemas for improved type hints and consistency
- Updated type hints from List/Optional to list/None for better readability and consistency across the codebase. - Refactored import statements for better organization and clarity. - Enhanced the ScheduledTaskBase schema to use modern type hints. - Cleaned up unnecessary comments and whitespace in various files. - Improved error handling and logging in task execution handlers. - Updated test cases to reflect changes in type hints and ensure compatibility with the new structure.
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
"""Scheduled task model for flexible task scheduling with timezone support."""
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
from sqlmodel import JSON, Column, Field, SQLModel
|
||||
from sqlmodel import JSON, Column, Field
|
||||
|
||||
from app.models.base import BaseModel
|
||||
|
||||
@@ -57,11 +56,11 @@ class ScheduledTask(BaseModel, table=True):
|
||||
description="Timezone for scheduling (e.g., 'America/New_York', 'Europe/Paris')",
|
||||
)
|
||||
recurrence_type: RecurrenceType = Field(default=RecurrenceType.NONE)
|
||||
cron_expression: Optional[str] = Field(
|
||||
cron_expression: str | None = Field(
|
||||
default=None,
|
||||
description="Cron expression for custom recurrence (when recurrence_type is CRON)",
|
||||
)
|
||||
recurrence_count: Optional[int] = Field(
|
||||
recurrence_count: int | None = Field(
|
||||
default=None,
|
||||
description="Number of times to repeat (None for infinite)",
|
||||
)
|
||||
@@ -75,29 +74,29 @@ class ScheduledTask(BaseModel, table=True):
|
||||
)
|
||||
|
||||
# User association (None for system tasks)
|
||||
user_id: Optional[int] = Field(
|
||||
user_id: int | None = Field(
|
||||
default=None,
|
||||
foreign_key="user.id",
|
||||
description="User who created the task (None for system tasks)",
|
||||
)
|
||||
|
||||
# Execution tracking
|
||||
last_executed_at: Optional[datetime] = Field(
|
||||
last_executed_at: datetime | None = Field(
|
||||
default=None,
|
||||
description="When the task was last executed (UTC)",
|
||||
)
|
||||
next_execution_at: Optional[datetime] = Field(
|
||||
next_execution_at: datetime | None = Field(
|
||||
default=None,
|
||||
description="When the task should be executed next (UTC, for recurring tasks)",
|
||||
)
|
||||
error_message: Optional[str] = Field(
|
||||
error_message: str | None = Field(
|
||||
default=None,
|
||||
description="Error message if execution failed",
|
||||
)
|
||||
|
||||
# Task lifecycle
|
||||
is_active: bool = Field(default=True, description="Whether the task is active")
|
||||
expires_at: Optional[datetime] = Field(
|
||||
expires_at: datetime | None = Field(
|
||||
default=None,
|
||||
description="When the task expires (UTC, optional)",
|
||||
)
|
||||
@@ -122,4 +121,4 @@ class ScheduledTask(BaseModel, table=True):
|
||||
|
||||
def is_system_task(self) -> bool:
|
||||
"""Check if this is a system task (no user association)."""
|
||||
return self.user_id is None
|
||||
return self.user_id is None
|
||||
|
||||
Reference in New Issue
Block a user