Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create systemd file so the app can run as a Linux system daemon. #2737 #2754

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions examples/linux/systemd/systemd-talawa-api.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

# READ ALL STEPS BEFORE PROCEEDING
#
# 0) Change the daemon_directory setting in your configuration file to
# /var/run/talawa-api
# 1) Copy this file to one of these directories depending on your Linux version
# i. RedHat variants: /usr/lib/systemd/system/
# ii. Debian/Ubuntu variants: /lib/systemd/system/
# 2) Edit the CODEROOT path to be the full path of the Talawa API's root directory
# 3) Edit the TALAWA_API_CONFIGDIR path to be the full path of the Talawa API's configuration directory
# This defaults to /etc/ directory of the Talawa API codebase
# 4) Edit the User and Group to match the POSIX user you want the daemon
# to run as.
# 4a) Set appropriate permissions:
# sudo chown -R talawa:talawa /etc/talawa
# sudo chmod 750 /etc/talawa
# 4b) If using SELinux, set the correct context:
# sudo semanage fcontext -a -t bin_t "/home/talawa/talawa-api/main.py"
# sudo restorecon -v /home/talawa/talawa-api/main.py
# 5) Run the command "sudo systemctl daemon-reload". This needs to be run only once
# 6) Run the command "sudo systemctl start talawa-api.service" to start
# 7) Run the command "sudo systemctl stop talawa-api.service" to stop
# 8) Run the command "sudo systemctl restart talawa-api.service" to restart
# 9) Run the command "sudo systemctl enable talawa-api.service" to make
# talawa-api start automatically on boot

[Unit]
Description=Talawa API Service
After=network.target

Comment on lines +27 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add essential service dependencies

The Unit section should include additional dependencies and documentation:

 [Unit]
 Description=Talawa API Service
+Documentation=https://github.com/talawa-api/
 After=network.target
+After=postgresql.service redis.service
+Requires=postgresql.service
+Wants=redis.service

This ensures proper service startup order and documents the service's dependencies.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[Unit]
Description=Talawa API Service
After=network.target
[Unit]
Description=Talawa API Service
Documentation=https://github.com/talawa-api/
After=network.target
After=postgresql.service redis.service
Requires=postgresql.service
Wants=redis.service

Documentation=https://github.com/talawa-api/
After=postgresql.service redis.service
Requires=postgresql.service
Wants=redis.service

[Service]
User=talawa
Group=talawa

Environment=CODEROOT=/home/talawa/talawa-api
Environment=TALAWA_API_CONFIGDIR=/etc/talawa
Environment=VIRTUAL_ENV=$CODEROOT/venv/bin
Environment=PATH=$VIRTUAL_ENV:$PATH
Environment=START=--start "STOP=--stop --force" "RESTART=--restart --force"
EnvironmentFile=/etc/examples/linux/systemd/talawa-api.env

ExecStart=${VIRTUAL_ENV}/python3 ${CODEROOT}/main.py ${START}
ExecStop=${VIRTUAL_ENV}/python3 ${CODEROOT}/main.py ${STOP}
ExecReload=${VIRTUAL_ENV}/python3 ${CODEROOT}/main.py ${RESTART}
Comment on lines +47 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

The systemd service file references a non-existent main.py script

The verification reveals that main.py does not exist in the repository, yet it's referenced in the systemd service file. This script is expected to handle start, stop, and restart operations but is missing from the codebase.

  • examples/linux/systemd/systemd-talawa-api.service references a non-existent main.py
  • The script is expected to support command-line arguments ${START}, ${STOP}, and ${RESTART}
🔗 Analysis chain

Verify main.py supports the specified command-line arguments

Let's verify that the main.py script exists and supports the specified command-line arguments (--start, --stop, --restart).

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify main.py exists and supports the required command-line arguments

# Check if main.py exists
echo "Checking for main.py..."
if ! fd "main.py$" ; then
  echo "Error: main.py not found"
  exit 1
fi

# Check for command-line argument handling
echo "Checking command-line argument handling..."
rg -A 5 "argparse|ArgumentParser|add_argument" "main.py" || echo "Warning: No argument parsing found in main.py"

# Look for start/stop/restart handling
echo "Checking start/stop/restart handling..."
rg -i "def (start|stop|restart)" "main.py" || echo "Warning: No start/stop/restart functions found in main.py"

Length of output: 678


RemainAfterExit=yes
GuessMainPID=yes
Type=simple
RuntimeDirectory=talawa

# Restart policy
Restart=always
RestartSec=3

# Security settings
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=full
ProtectHome=read-only

# Resource limits
LimitNOFILE=65535
LimitNPROC=4096

[Install]
WantedBy=multi-user.target
7 changes: 7 additions & 0 deletions examples/linux/systemd/talawa-api.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CODEROOT=/home/talawa/talawa-api
TALAWA_API_CONFIGDIR=/etc/talawa
VIRTUAL_ENV=${CODEROOT}/venv/bin
PATH=${VIRTUAL_ENV}:${PATH}
START=--start
STOP=--stop --force
RESTART=--restart --force
Loading