Fallback to schedule automation in UTC timezone if unset

- Handle null automation ids in calls to get_automation function
This commit is contained in:
Debanjum
2025-02-01 19:12:51 +05:30
parent 0bfa7c1c45
commit f2eba667fc
2 changed files with 8 additions and 2 deletions

View File

@@ -1794,7 +1794,7 @@ class AutomationAdapters:
def get_automation(user: KhojUser, automation_id: str) -> Job: def get_automation(user: KhojUser, automation_id: str) -> Job:
# Perform validation checks # Perform validation checks
# Check if user is allowed to delete this automation id # Check if user is allowed to delete this automation id
if not automation_id.startswith(f"automation_{user.uuid}_"): if not is_none_or_empty(automation_id) or automation_id.startswith(f"automation_{user.uuid}_"):
raise ValueError("Invalid automation id") raise ValueError("Invalid automation id")
# Check if automation with this id exist # Check if automation with this id exist
automation: Job = state.scheduler.get_job(job_id=automation_id) automation: Job = state.scheduler.get_job(job_id=automation_id)

View File

@@ -1977,7 +1977,13 @@ def schedule_automation(
# Run automation at some random minute (to distribute request load) instead of running every X minutes # Run automation at some random minute (to distribute request load) instead of running every X minutes
crontime = " ".join([str(math.floor(random() * 60))] + crontime.split(" ")[1:]) crontime = " ".join([str(math.floor(random() * 60))] + crontime.split(" ")[1:])
user_timezone = pytz.timezone(timezone) # Convert timezone string to timezone object
try:
user_timezone = pytz.timezone(timezone)
except pytz.UnknownTimeZoneError:
logger.error(f"Invalid timezone: {timezone}. Fallback to use UTC to schedule automation.")
user_timezone = pytz.utc
trigger = CronTrigger.from_crontab(crontime, user_timezone) trigger = CronTrigger.from_crontab(crontime, user_timezone)
trigger.jitter = 60 trigger.jitter = 60
# Generate id and metadata used by task scheduler and process locks for the task runs # Generate id and metadata used by task scheduler and process locks for the task runs