Skip to content

Commit

Permalink
βž• Add SMTP connections test
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Jan 16, 2025
1 parent 088c36d commit 3ba0803
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM dunglas/frankenphp:${frankenphp_version}-php${php_version} AS base
WORKDIR /laravel
SHELL ["/bin/bash", "-eou", "pipefail", "-c"]

ENV SERVER_NAME=:80
ENV SERVER_NAME=:8000
ENV OCTANE_SERVER=frankenphp
ARG user=laravel

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
| `CONTAINER_WORKER_TIMEOUT` | `300` | worker |
| `TEST_DB_CONNECTION` | `true` | `*` |
| `TEST_CACHE_CONNECTION` | `true` | `*` |
| `TEST_CONNECTION_TIMEOUT` | `20` | `*` |
| `TEST_CONNECTION_TIMEOUT` | `10` | `*` |
34 changes: 34 additions & 0 deletions src/common/test_smtp_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require 'vendor/autoload.php';

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;

$app = require_once 'bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

try {
$dsn = sprintf(
'smtp://%s:%s@%s:%d',
config('mail.mailers.smtp.username'),
config('mail.mailers.smtp.password'),
config('mail.mailers.smtp.host'),
config('mail.mailers.smtp.port')
);

if (config('mail.mailers.smtp.encryption') === 'tls') {
$dsn .= '?encryption=tls';
}

$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);

$transport->start();

exit(0);
} catch (Exception $e) {
echo 'SMTP connection error: ' . $e->getMessage();
exit(1);
}
33 changes: 26 additions & 7 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/bin/bash

set -e
set -euo pipefail
trap 'echo "Error on line $LINENO"' ERR

: "${CONTAINER_MODE:=app}"
: "${CONTAINER_PORT:=8000}"
: "${CONTAINER_MANUAL_SETUP:=}"
: "${CONTAINER_MODE:=app}"
: "${CONTAINER_WORKER_DELAY:=10}"
: "${CONTAINER_WORKER_SLEEP:=5}"
: "${CONTAINER_WORKER_TIMEOUT:=300}"
: "${CONTAINER_WORKER_TRIES:=3}"

: "${TEST_DB_CONNECTION:=true}"
: "${TEST_CACHE_CONNECTION:=true}"
: "${TEST_CONNECTION_TIMEOUT:=20}"
: "${TEST_SMTP_CONNECTION:=false}"
: "${TEST_CONNECTION_TIMEOUT:=10}"

: "${APP_ENV:=production}"
: "${APP_DEBUG:=false}"
Expand All @@ -21,6 +24,9 @@ ARTISAN="php -d variables_order=EGPCS /laravel/artisan"
_test_connection() {
local count=0
local type="${1}"
local status

echo "πŸ§ͺ Testing ${type} connection..."

while [ "$count" -lt "$TEST_CONNECTION_TIMEOUT" ]; do
php -f "/common/test_${type}_connection.php" > /dev/null 2>&1
Expand All @@ -44,14 +50,20 @@ _test_connections() {
if [ "$TEST_DB_CONNECTION" != "true" ]; then
echo "⏭ Skipping database connection test..."
else
_test_connection "db"
_test_connection "database"
fi

if [ "$TEST_CACHE_CONNECTION" != "true" ]; then
echo "⏭ Skipping cache connection test..."
else
_test_connection "cache"
fi

if [ "$TEST_SMTP_CONNECTION" != "true" ]; then
echo "⏭ Skipping SMTP connection test..."
else
_test_connection "smtp"
fi
}

_migrate() {
Expand All @@ -71,13 +83,20 @@ _setup() {
if [ -d "/laravel/app/public/storage" ]; then
echo "βœ… Storage already linked..."
else
echo "πŸ” Linking the storage..."
echo "πŸ—‚οΈ Linking the storage..."
${ARTISAN} storage:link
fi

echo "βš™οΈ Creating config cache..."
${ARTISAN} config:cache
${ARTISAN} events:cache

echo "πŸƒ Creating event cache..."
${ARTISAN} event:cache

echo "🚏 Creating route cache..."
${ARTISAN} route:cache

echo "πŸ–ΌοΈ Creating view cache..."
${ARTISAN} view:cache
}

Expand All @@ -97,7 +116,7 @@ _run() {
--delay="$CONTAINER_WORKER_DELAY"
;;
horizon)
echo "Running horizon..."
echo "🌀️ Running horizon..."
exec ${ARTISAN} horizon
;;
scheduler)
Expand Down

0 comments on commit 3ba0803

Please sign in to comment.