You need to implement a bash script that handles the restart of a Rails web application and optionally restarts a delayed job process. This script will be invoked by another script that verifies its functionality.
Functionality to Build:
Create a script that accepts three parameters:
The first is the path to the PID file of the Rails web server process.
The second is a timeout value in seconds to wait for the process to terminate before forcefully killing it.
The third is a signal number (usually 1 for SIGHUP), which should be sent to gracefully restart the process.
The script should handle the following:
If the Rails process is running (as indicated by the existence of the PID file), send the specified signal to the process to initiate a graceful restart.
If the process does not terminate within the timeout period, forcefully kill the process and then attempt to restart it.
Additionally, if the environment variable ENABLE_DELAYED_JOB is set to 1, the script should repeat similar logic for restarting the delayed job process using the provided PID file and timeout values.
Expected Inputs:
The script will accept inputs as command-line arguments:
$1: Path to PID file for the Rails web server (e.g., $RAILS_ROOT_DIR/tmp/puma.pid)
$2: Timeout in seconds (e.g., 30)
$3: Signal number (e.g., 1)
Expected Outputs:
The script should exit with code 0 on successful restart.
If there is an error during the restart process, the script should exit with a non-zero code (specifically 1 for the web server and 2 for the delayed job).
File Location and Naming:
The implemented script should be named restart_webapp.sh and located in the /app/ directory.
Make sure to handle error checking appropriately and document any assumptions made in the code.
Tests
The following test script will be used to verify your solution:
bash
#!/bin/bash
echo '--- RESTART APP START ---'
#---------------------------------------------------------------------
# rails 再起動
$SCRIPTS_ROOT_DIR/restart_webapp_rails.sh $RAILS_ROOT_DIR/tmp/puma.pid 30 1
if [ $? -ne 0 ]; then
exit 1
fi
if [ "$ENABLE_DELAYED_JOB" = "1" ]; then
#---------------------------------------------------------------------
# delayed_job 再起動
$SCRIPTS_ROOT_DIR/restart_webapp_delayed_job.sh $RAILS_ROOT_DIR/tmp/pids/delayed_job.pid 60 1
if [ $? -ne 0 ]; then
exit 2
fi
fi
echo '--- RESTART APP FINISHED ---'