Skip to content

Commit

Permalink
Extra predict logging, constant for error codes, dont delete tmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
TimKoornstra committed Jul 10, 2024
1 parent f620408 commit 67750f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
7 changes: 5 additions & 2 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
DEFAULT_MEMORY_LIMIT = min(80 * 1024 * MEGABYTE, SAFE_LIMIT) # 80GB
MEMORY_USAGE_PERCENTAGE = 0.8 # 80%

ERR_PORT_IN_USE = 98
ERR_PERMISSION_DENIED = 13

# Set up logging
logging_level = get_env_variable("LOGGING_LEVEL", "INFO")
logger = setup_logging(logging_level)
Expand Down Expand Up @@ -162,10 +165,10 @@ async def run_server():
await server.serve()
except OSError as e:
logger.error(f"Error starting server: {e}")
if e.errno == 98:
if e.errno == ERR_PORT_IN_USE:
logger.error(
f"Port {port} is already in use. Try a different port.")
elif e.errno == 13:
elif e.errno == ERR_PERMISSION_DENIED:
logger.error(
f"Permission denied when trying to bind to port {port}. Try a "
"port number > 1024 or run with sudo.")
Expand Down
62 changes: 31 additions & 31 deletions src/api/batch_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,35 +194,29 @@ def save_prediction_outputs(
os.makedirs(temp_dir, exist_ok=True)
logging.debug(f"Using temporary directory: {temp_dir}")

try:
for prediction, group_id, image_id, metadata in zip(
prediction_data, group_ids, image_ids, image_metadata
):
confidence, predicted_text = prediction
output_text = (
f"{image_id}\t{metadata}\t{confidence}\t{predicted_text}")
output_texts.append(output_text)

group_output_dir = os.path.join(base_output_path, group_id)
os.makedirs(group_output_dir, exist_ok=True)
logging.debug("Ensured output directory exists: %s",
group_output_dir)

output_file_path = os.path.join(
group_output_dir, f"{image_id}.txt")

try:
write_file_atomically(output_text, output_file_path, temp_dir)
logging.debug("Atomically wrote file: %s", output_file_path)
except IOError as e:
logging.error("Failed to write file %s. Error: %s",
output_file_path, e)
raise
finally:
# Clean up the temporary directory
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
logging.debug(f"Cleaned up temporary directory: {temp_dir}")
for prediction, group_id, image_id, metadata in zip(
prediction_data, group_ids, image_ids, image_metadata
):
confidence, predicted_text = prediction
output_text = (
f"{image_id}\t{metadata}\t{confidence}\t{predicted_text}")
output_texts.append(output_text)

group_output_dir = os.path.join(base_output_path, group_id)
os.makedirs(group_output_dir, exist_ok=True)
logging.debug("Ensured output directory exists: %s",
group_output_dir)

output_file_path = os.path.join(
group_output_dir, f"{image_id}.txt")

try:
write_file_atomically(output_text, output_file_path, temp_dir)
logging.debug("Atomically wrote file: %s", output_file_path)
except IOError as e:
logging.error("Failed to write file %s. Error: %s",
output_file_path, e)
raise

return output_texts

Expand Down Expand Up @@ -250,15 +244,21 @@ def write_file_atomically(content: str, target_path: str, temp_dir: str) \
try:
# Create a temporary file in the provided temporary directory
with tempfile.NamedTemporaryFile(mode='w', dir=temp_dir,
delete=True, encoding="utf-8") \
delete=False, encoding="utf-8") \
as temp_file:
temp_file.write(content + "\n")
temp_file_path = temp_file.name
os.replace(temp_file_path, target_path)

# On POSIX systems, this is atomic. On Windows, it's the best we can do
os.replace(temp_file_path, target_path)
except IOError as e:
if temp_file_path and os.path.exists(temp_file_path):
# Clean up the temporary file if it exists
os.unlink(temp_file_path)
raise IOError(
f"Failed to atomically write file: {target_path}. Error: {e}")
except Exception as e:
if temp_file_path and os.path.exists(temp_file_path):
# Clean up the temporary file if it exists
os.unlink(temp_file_path)
raise e # Re-raise the exception after cleanup
2 changes: 2 additions & 0 deletions src/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# > Standard library
import datetime
import logging
from typing import List, Optional
from multiprocessing.queues import Full

Expand Down Expand Up @@ -83,6 +84,7 @@ async def predict(
"high volume of requests. Please try again "
"later.")

logging.info(f"Request received: {group_id} - {identifier}")
return _create_response(202, "Request received", "Your request is "
"being processed",
extra={"group_id": group_id,
Expand Down

0 comments on commit 67750f3

Please sign in to comment.