Skip to content

Commit

Permalink
Changed error handling on writing file
Browse files Browse the repository at this point in the history
  • Loading branch information
majbyr committed Feb 27, 2023
1 parent 61d356b commit 3f316a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ __pycache__/
*.iml
*.env
config/config.*.yaml
app/storage/*
storage/*
15 changes: 10 additions & 5 deletions app/translation/v2_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ async def translate(body: Request,
async def correction(body: Correction, application: Optional[str] = Header(None, convert_underscores=True, deprecated=True)):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
filename = 'app/storage/corrections.txt'
output = f"date: {dt_string}\n\nrequest: {body.request}\n\noriginalTranslation: {body.response}\n\ncorrectedTranslation: {body.correction}\n---\n\n"
while True:

filePath= api_config.corrections_data_storage_path

maxAttempts = 50
attempt = 0
while attempt < maxAttempts:
try:
with open(filename, 'a') as f:
with open(filePath, 'a') as f:
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
f.write(output)
break
return {"message": "Correction saved."}
except IOError:
attempt += 1
# Failed to acquire lock, wait and try again
time.sleep(.1)
return {"message": "success"}
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to save data on a server.")

0 comments on commit 3f316a6

Please sign in to comment.