Skip to content

Commit

Permalink
Merge pull request #2 from RobGeada/ErrorCheckHours
Browse files Browse the repository at this point in the history
Add error handling to Smartsheet inactive hours checks
  • Loading branch information
dchourasia authored Oct 9, 2024
2 parents 83320ef + 7b26ddd commit 8c2131b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 11 additions & 6 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 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')
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 Expand Up @@ -239,4 +244,4 @@ def main():


if __name__ == '__main__':
main()
main()
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 8c2131b

Please sign in to comment.