From 89a8dbb81ab76fc2d0ec97b61c48db250403562d Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 1 May 2024 09:32:15 +0530 Subject: [PATCH] Fix edit job API. Use user timezone, pass all reqd. params to automation - Pass user and calling_url to the scheduled chat too when modifying params of automation - Update to use user timezone even when update job via API - Move timezone string to timezone object calculation into the schedule automation method --- src/khoj/routers/api.py | 23 ++++++++++++++++------- src/khoj/routers/helpers.py | 6 +++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index b7c5d5ab..625238c1 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -34,7 +34,6 @@ from khoj.routers.helpers import ( ApiUserRateLimiter, CommonQueryParams, ConversationCommandRateLimiter, - create_automation, schedule_automation, update_telemetry_state, ) @@ -457,10 +456,8 @@ async def post_automation( # Schedule automation with query_to_run, timezone, subject directly provided by user try: - # Get user timezone - user_timezone = pytz.timezone(timezone) # Use the query to run as the scheduling request if the scheduling request is unset - automation = await schedule_automation(query_to_run, subject, crontime, user_timezone, q, user, request.url) + automation = await schedule_automation(query_to_run, subject, crontime, timezone, q, user, request.url) except Exception as e: logger.error(f"Error creating automation {q} for {user.email}: {e}") return Response( @@ -518,14 +515,26 @@ def edit_job( # Construct updated automation metadata automation_metadata = json.loads(automation.name) + automation_metadata["scheduling_request"] = q automation_metadata["query_to_run"] = query_to_run automation_metadata["subject"] = subject.strip() + automation_metadata["crontime"] = crontime - # Modify automation with updated query, subject, crontime - automation.modify(kwargs={"query_to_run": query_to_run, "subject": subject}, name=json.dumps(automation_metadata)) + # Modify automation with updated query, subject + automation.modify( + name=json.dumps(automation_metadata), + kwargs={ + "query_to_run": query_to_run, + "subject": subject, + "scheduling_request": q, + "user": user, + "calling_url": request.url, + }, + ) # Reschedule automation if crontime updated - trigger = CronTrigger.from_crontab(crontime) + user_timezone = pytz.timezone(timezone) + trigger = CronTrigger.from_crontab(crontime, user_timezone) if automation.trigger != trigger: automation.reschedule(trigger=trigger) diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 559aaf38..0736e68f 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -919,9 +919,8 @@ def scheduled_chat(query_to_run: str, scheduling_request: str, subject: str, use async def create_automation(q: str, timezone: str, user: KhojUser, calling_url: URL, meta_log: dict = {}): - user_timezone = pytz.timezone(timezone) crontime, query_to_run, subject = await schedule_query(q, meta_log) - job = await schedule_automation(query_to_run, subject, crontime, user_timezone, q, user, calling_url) + job = await schedule_automation(query_to_run, subject, crontime, timezone, q, user, calling_url) return job, crontime, query_to_run, subject @@ -929,11 +928,12 @@ async def schedule_automation( query_to_run: str, subject: str, crontime: str, - user_timezone, + timezone: str, scheduling_request: str, user: KhojUser, calling_url: URL, ): + user_timezone = pytz.timezone(timezone) trigger = CronTrigger.from_crontab(crontime, user_timezone) # Generate id and metadata used by task scheduler and process locks for the task runs job_metadata = json.dumps(