feat(WriteDB): Added file lock, prevented race condition

This commit is contained in:
HamodiGit 2024-11-23 15:37:20 -07:00
parent b150eee58b
commit bcdaa37d97

View File

@ -1,10 +1,12 @@
import pandas as pd
import json
from openpyxl import load_workbook
from send_email import send_email
#from send_email import send_email
from filelock import FileLock
# Define the path to the Excel file
# Define the path to the Excel file and the lock file
file_path = "./interview_database.xlsx"
lock_path = "./interview_database.xlsx.lock" # Lock file for synchronization
def ReadDatabase():
"""
@ -18,6 +20,8 @@ def ReadDatabase():
``Contact``: ahmad.ahmad1@ucalgary.ca
"""
# Use a file-based lock for thread-safe and process-safe access
with FileLock(lock_path):
# Load the Excel file into a pandas DataFrame with specific columns
df = pd.read_excel(file_path, usecols=['Date', 'Start Time', 'Slot', 'Interviewee Name', 'Interviewee Email', 'Meeting Duration'])
@ -63,6 +67,7 @@ def AppendAppointment(date, start_time, interviewee_name, interviewee_email):
# Check if the requested slot is available in the `available_slots` structure
if date in available_slots and start_time in available_slots[date]:
with FileLock(lock_path): # Ensure process-safe access to the file
# Load workbook and select "Sheet1" for updating appointments
workbook = load_workbook(file_path)
sheet = workbook["Sheet1"]
@ -88,12 +93,13 @@ def AppendAppointment(date, start_time, interviewee_name, interviewee_email):
email_cell.value = updated_emails
workbook.save(file_path)
send_email(interviewee_email, interviewee_name, date, start_time)
#send_email(interviewee_email, interviewee_name, date, start_time)
return True
# If no slots available, return that the slot is unavailable
return False
def run_tests():
"""
Executes test cases to verify appointment scheduling and slot availability.