From fc7cafecd2a18b5970e2efb3e351586caa0d2503 Mon Sep 17 00:00:00 2001
From: Steven Winship <39765413+stevenwinship@users.noreply.github.com>
Date: Wed, 12 Jun 2024 16:51:50 -0400
Subject: [PATCH] adding smtp monitor restart scripts

---
 .gitignore                                      |  1 +
 .../source/developerGuide/localEnvironment.md   | 17 +++++++++++++++++
 environment/monitoring/monitor-smtp.service     |  6 ++++++
 environment/monitoring/monitor-smtp.timer       |  5 +++++
 environment/monitoring/smtp-monitor.sh          | 11 +++++++++++
 5 files changed, 40 insertions(+)
 create mode 100644 environment/monitoring/monitor-smtp.service
 create mode 100644 environment/monitoring/monitor-smtp.timer
 create mode 100755 environment/monitoring/smtp-monitor.sh

diff --git a/.gitignore b/.gitignore
index c6ddcdb..9b355cb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 .DS_Store
 .DS_Store?
 .vscode
+.idea
 
 environment/data
 environment/config.inc.php
diff --git a/docs/Sphinx-guides/source/developerGuide/localEnvironment.md b/docs/Sphinx-guides/source/developerGuide/localEnvironment.md
index e0389fd..c6efce3 100644
--- a/docs/Sphinx-guides/source/developerGuide/localEnvironment.md
+++ b/docs/Sphinx-guides/source/developerGuide/localEnvironment.md
@@ -78,6 +78,23 @@ smtp_port = 25
 smtp_suppress_cert_check = On
 ```
 
+#### Setting up a systemd service and timer to restart the SMTP service automatically if it goes down:
+
+Copy ppr-ojs/environment/monitoring/monitor-smtp.service and monitor-smtp.timer to /etc/systemd/system/ on the machine the smtp server is running
+
+Copy ppr-ojs/environment/monitoring/smtp-monitor.sh to your home directory on the machine the smtp server is running and give it execution permissions
+
+The default directory is /home/core/smtp-monitor.sh but can be changes by editing the monitor-smtp.service file
+
+Start the service:
+```
+sudo systemctl start monitor-smtp.timer
+```
+A log will be generated and can be viewed by:
+```
+cat /tmp/smtp-monitor
+```
+
 ## Clean the data directories
 To start the OJS application fresh, you will need to clean the DB and OJS files within the data directory.
 
diff --git a/environment/monitoring/monitor-smtp.service b/environment/monitoring/monitor-smtp.service
new file mode 100644
index 0000000..df0cdaa
--- /dev/null
+++ b/environment/monitoring/monitor-smtp.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Monitors SMTP docker container and restarts if not running
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/sh -c '/home/core/smtp-monitor.sh >> /tmp/smtp-monitor'
diff --git a/environment/monitoring/monitor-smtp.timer b/environment/monitoring/monitor-smtp.timer
new file mode 100644
index 0000000..14d7fe0
--- /dev/null
+++ b/environment/monitoring/monitor-smtp.timer
@@ -0,0 +1,5 @@
+[Unit]
+Description=Run monitor-smtp.service every 10 minutes
+
+[Timer]
+OnCalendar=*:0/10
diff --git a/environment/monitoring/smtp-monitor.sh b/environment/monitoring/smtp-monitor.sh
new file mode 100755
index 0000000..bb6e6a2
--- /dev/null
+++ b/environment/monitoring/smtp-monitor.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+CONTAINER_NAME=iqss-smtp-ojs
+
+if docker ps | grep -q $CONTAINER_NAME; then
+  echo "$(date -u) running"
+else
+  echo "$(date -u) not running"
+  #docker start $CONTAINER_NAME
+  docker run --rm -itd --name $CONTAINER_NAME -p 1080:1080 -p 1025:1025 --env MAILDEV_SMTP_PORT=1025 --env MAILDEV_WEB_PORT=1080 --env MAILDEV_MAIL_DIRECTORY=/mail --mount type=tmpfs,destination=/mail maildev/maildev:2.0.5
+fi