Skip to content

Commit

Permalink
Add error handling to smartsheet hours check
Browse files Browse the repository at this point in the history
  • Loading branch information
RobGeada committed Oct 8, 2024
1 parent 83320ef commit 022f049
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/hibernate_clusters_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,19 @@ def good_time_to_hibernate_cluster(inactive_hours_start:str):
buffer_seconds = buffer_hours * 60 * 60
day_start_time = '00:00:00'
day_end_time = '23:59:59'
inactive_hours_start = datetime.datetime.strptime(inactive_hours_start, '%H:%M:%S')
current_utc_time = datetime.datetime.strptime(datetime.datetime.now(datetime.timezone.utc).strftime('%H:%M:%S'),
'%H:%M:%S')
try:
inactive_hours_start = datetime.datetime.strptime(inactive_hours_start, '%H:%M:%S')
# if the inactive hours start is misconfigured, default to hibernating cluster immediately
except ValueError:
return true

current_utc_time = datetime.datetime.strptime(datetime.datetime.now(datetime.timezone.utc).strftime('%H:%M:%S'), '%H:%M:%S')
day_start_time = datetime.datetime.strptime(day_start_time, '%H:%M:%S')
day_end_time = datetime.datetime.strptime(day_end_time, '%H:%M:%S')
diff = (current_utc_time - inactive_hours_start).total_seconds()
if diff < 0 and 24 - buffer_hours < inactive_hours_start.hour <= 24 and 0 <= current_utc_time.hour <= buffer_hours:
diff = (day_end_time - inactive_hours_start).total_seconds() + (current_utc_time - day_start_time).total_seconds()
if diff < 0 and 24 - buffer_hours < inactive_hours_start.hour <= 24 and 0 <= current_utc_time.hour <= buffer_hours:
diff = (day_end_time - inactive_hours_start).total_seconds() + (
current_utc_time - day_start_time).total_seconds()

return 0 <= diff <= buffer_seconds

Expand Down
8 changes: 7 additions & 1 deletion src/resume_clusters_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ def good_time_to_resume_cluster(inactive_hours_end: str):
buffer_seconds = buffer_hours * 60 * 60
day_start_time = '00:00:00'
day_end_time = '23:59:59'
inactive_hours_end = datetime.datetime.strptime(inactive_hours_end, '%H:%M:%S')

try:
inactive_hours_end = datetime.datetime.strptime(inactive_hours_end, '%H:%M:%S')
# if the inactive hours start is misconfigured, default to keeping cluster hibernated
except ValueError:
return False

current_utc_time = datetime.datetime.strptime(datetime.datetime.now(datetime.timezone.utc).strftime('%H:%M:%S'),
'%H:%M:%S')
day_start_time = datetime.datetime.strptime(day_start_time, '%H:%M:%S')
Expand Down

0 comments on commit 022f049

Please sign in to comment.