From e9ef92b330cf8f8035947cb45a7762addd2fd2a5 Mon Sep 17 00:00:00 2001 From: Mirian Okradze Date: Tue, 9 Jan 2024 23:55:22 +0400 Subject: [PATCH] fix: working hours --- apps/worker/main.py | 60 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/apps/worker/main.py b/apps/worker/main.py index b2df559e3..2995441ae 100644 --- a/apps/worker/main.py +++ b/apps/worker/main.py @@ -1,6 +1,7 @@ import time -from datetime import timedelta +from datetime import date, datetime, timedelta +import pytz import requests from celery import Celery @@ -141,6 +142,48 @@ def register_campaign_phone_call_tasks(): bind=True, ) def make_phone_call(self, campaign_id: str, contact_id: str, account_id: str): + campaign_res = requests.get( + f"{Config.PR_DEV_SERVER_URL}/v1/campaign/{campaign_id}", + headers={ + "Authorization": Config.SERVER_AUTH_TOKEN, + "Account-ID": account_id, + }, + ) + + campaign = campaign_res.json() + retry_interval = campaign.get("retry_interval", 15) + + working_hours_start = datetime.strptime( + campaign.get("working_hours_start"), "%H:%M" + ) + working_hours_end = datetime.strptime(campaign.get("working_hours_end"), "%H:%M") + + # Get current time in US timezone + # us_tz = pytz.timezone("US/Eastern") # replace with the appropriate US timezone + + current_time_us = datetime.now().time() + + # Check if current day is between Monday and Friday + current_day_us = datetime.now().weekday() + + if current_day_us >= 0 and current_day_us <= 4: # 0 is Monday, 4 is Friday + # Check if current time is within working hours + if not working_hours_start <= current_time_us <= working_hours_end: + # Retry the task when the working hours start + current_datetime_us = datetime.combine(date.today(), current_time_us) + working_hours_start_datetime = datetime.combine( + date.today(), working_hours_start + ) + countdown = (working_hours_start_datetime - current_datetime_us).seconds + self.retry(countdown=countdown) + return "Retrying in working hours" + else: + # Retry the task on next Monday + seconds_until_next_monday = (7 - current_day_us) * 24 * 60 * 60 + self.retry(countdown=seconds_until_next_monday) + return "Retrying on next Monday" + + # Start phone call res = requests.post( f"{Config.PR_DEV_SERVER_URL}/call/campaign", headers={ @@ -161,22 +204,11 @@ def make_phone_call(self, campaign_id: str, contact_id: str, account_id: str): chat_id = call_json.get("chat_id") - start_time = time.time() - - campaign_res = requests.get( - f"{Config.PR_DEV_SERVER_URL}/v1/campaign/{campaign_id}", - headers={ - "Authorization": Config.SERVER_AUTH_TOKEN, - "Account-ID": account_id, - }, - ) - - campaign = campaign_res.json() - retry_interval = campaign.get("retry_interval", 15) - # Checks for the status of the phone call to make sure phone call is finished FORTY_FIVE_MINUTES_IN_SECONDS = 2700 + start_time = time.time() + while True: # Just in case loop stuck for some reason if time.time() - start_time > FORTY_FIVE_MINUTES_IN_SECONDS: