Skip to content

Commit

Permalink
docker and threadding fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ggpwnkthx committed May 13, 2024
1 parent 9414c89 commit 66dd199
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye"
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
Expand All @@ -12,7 +12,7 @@
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
"postCreateCommand": "pip3 install --user -r requirements.txt"

// Configure tool-specific properties.
// "customizations": {},
Expand Down
75 changes: 41 additions & 34 deletions app/server_threaded.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from aiosmtpd.controller import Controller
from handlers.microsoft_graph import MicrosoftGraphHandler
import asyncio, logging, os, signal
import asyncio, logging, os, signal, sys

# Load environment variables from a .env file if the CLIENT_ID is not set in the environment
if not os.environ.get("CLIENT_ID"):
from dotenv import load_dotenv

load_dotenv()

# Set up logging based on the environment variable LOG_LEVEL
Expand All @@ -21,43 +20,51 @@
case _:
logging.basicConfig(level=logging.ERROR)

# Get hostname and port from environment variables with defaults
hostname = os.environ.get("SMTP_RELAY_HOSTNAME", "0.0.0.0")
port = int(os.environ.get("SMTP_RELAY_PORT", "25"))
# Get hostname and port from environment variables with defaults
hostname = os.environ.get("SMTP_RELAY_HOSTNAME", "0.0.0.0")
port = int(os.environ.get("SMTP_RELAY_PORT", "25"))

# Ensure required environment variables are set
required_env_vars = ["CLIENT_ID", "CLIENT_SECRET", "AUTHORITY"]
for var in required_env_vars:
if not os.environ.get(var):
logging.error(f"Environment variable {var} is required.")
sys.exit(1)

# Initialize the SMTP server controller with Microsoft Graph handler
controller = Controller(
MicrosoftGraphHandler(),
hostname=hostname,
port=port,
require_starttls=False,
auth_require_tls=False,
auth_required=False,
)
# Initialize the SMTP server controller with Microsoft Graph handler
controller = Controller(
MicrosoftGraphHandler(),
hostname=hostname,
port=port,
require_starttls=False,
auth_require_tls=False,
auth_required=False,
)

# Create an asyncio Event to signal server shutdown
stop_event = asyncio.Event()
# Create an asyncio Event to signal server shutdown
stop_event = asyncio.Event()

def stop():
"""
Signal the event loop to stop by setting the stop event.
"""
stop_event.set()
def stop():
"""
Signal the event loop to stop by setting the stop event.
"""
stop_event.set()

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()

# Register signal handlers for graceful shutdown
for sig in ("SIGINT", "SIGTERM"):
loop.add_signal_handler(getattr(signal, sig), stop)
# Register signal handlers for graceful shutdown
for sig in ("SIGINT", "SIGTERM"):
loop.add_signal_handler(getattr(signal, sig), stop)

try:
controller.start()
logging.info(f"Started SMTP service on {hostname}:{port}")

try:
# Run the event loop until a stop signal is received
loop.run_until_complete(stop_event.wait())
finally:
# Stop the SMTP server and close the event loop
controller.stop()
loop.close()
logging.info("SMTP server stopped.")
# Run the event loop until a stop signal is received
loop.run_until_complete(stop_event.wait())
except Exception as e:
logging.error(f"Error occurred: {e}")
finally:
# Stop the SMTP server and close the event loop
controller.stop()
loop.close()
logging.info("SMTP server stopped.")
6 changes: 5 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ ENV SMTP_RELAY_HOSTNAME=0.0.0.0
ENV SMTP_RELAY_PORT=25

# Run the Python application
CMD ["python", "./server_threaded.py"]
# Use ENTRYPOINT to ensure the script runs
ENTRYPOINT ["python"]

# Use CMD to provide default arguments to ENTRYPOINT
CMD ["server_threaded.py"]

0 comments on commit 66dd199

Please sign in to comment.