Skip to content

Commit

Permalink
fix: schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Nov 30, 2023
1 parent d3a7720 commit b6ded97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 13 additions & 2 deletions apps/server/controllers/schedule.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import List

from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from fastapi_sqlalchemy import db

from exceptions import ScheduleNotFoundException
from models.db import create_session
from models.schedule import ScheduleModel
from services.schedule import execute_scheduled_run
from typings.auth import UserAccount
Expand Down Expand Up @@ -40,6 +41,7 @@ def run_schedule(schedule_id: str):
@router.post("", status_code=201, response_model=ScheduleWithConfigsOutput)
def create_schedule(
schedule_with_configs: ScheduleConfigInput,
background_tasks: BackgroundTasks,
auth: UserAccount = Depends(authenticate),
) -> ScheduleWithConfigsOutput:
"""
Expand Down Expand Up @@ -68,6 +70,9 @@ def create_schedule(
ScheduleModel.get_schedule_by_id(db, db_schedule.id, auth.account)
)

if not schedule_with_configs.configs.is_recurring:
background_tasks.add_task(execute_scheduled_run, create_session(), db_schedule)

return schedule_with_configs


Expand All @@ -77,6 +82,7 @@ def create_schedule(
def update_schedule(
id: str,
schedule_with_configs: ScheduleConfigInput,
background_tasks: BackgroundTasks,
auth: UserAccount = Depends(authenticate),
) -> ScheduleWithConfigsOutput:
"""
Expand All @@ -101,8 +107,13 @@ def update_schedule(
)

db_schedule.next_run_date = schedule_with_configs.schedule.start_date

db.session.commit()

if not schedule_with_configs.configs.is_recurring:
background_tasks.add_task(
execute_scheduled_run, create_session(), db_schedule
)

return convert_model_to_response(
ScheduleModel.get_schedule_by_id(db, db_schedule.id, auth.account)
)
Expand Down
20 changes: 14 additions & 6 deletions apps/server/services/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from models.chat import ChatModel
from models.schedule import ScheduleModel
from services.chat import create_client_message
from services.chat import create_client_message, create_user_message
from typings.auth import UserAccount
from typings.chat import ChatInput, ChatMessageInput
from typings.chat import ChatInput, ChatMessageInput, ChatUserMessageInput
from typings.schedule import ScheduleStatus
from utils.schedule import convert_model_to_response

Expand Down Expand Up @@ -61,10 +61,18 @@ def execute_scheduled_run(
for task in configs.tasks:
prompt += f"- {task}\n"

create_client_message(
body=ChatMessageInput(prompt=prompt, chat_id=chat.id if chat else None),
auth=UserAccount(user=user.to_dict(), account=account.to_dict()),
)
if chat:
create_client_message(
body=ChatMessageInput(prompt=prompt, chat_id=chat.id if chat else None),
auth=UserAccount(user=user.to_dict(), account=account.to_dict()),
)
else:
create_user_message(
body=ChatUserMessageInput(
prompt=prompt, agent_id=configs.agent_id, team_id=configs.team_id
),
auth=UserAccount(user=user.to_dict(), account=account.to_dict()),
)

# Check if the schedule is recurring and the end date is not exceeded
if schedule_with_configs.configs.is_recurring:
Expand Down

0 comments on commit b6ded97

Please sign in to comment.