Graceful shutdown ensures that running SchedulerX jobs finish before the application process exits during a restart or redeployment, preventing incomplete data processing.
Graceful shutdown diagram
Prerequisites
The SchedulerX client version must be 1.10.8 or later.
Configure graceful shutdown
SchedulerX supports graceful shutdown for jobs in standalone mode and distributed mode. Because scheduled jobs may take a long time to complete, set a timeout to prevent application processes from waiting indefinitely.
Add the following properties to your Spring Boot configuration:
# The graceful shutdown mode. WAIT_ALL: Wait for all jobs to complete. WAIT_RUNNING: Wait for running jobs to complete.
# If you do not configure this parameter, the graceful shutdown feature is disabled. By default, the graceful shutdown feature is disabled.
spring.schedulerx2.graceShutdownMode=WAIT_ALL
# The timeout period for graceful shutdown. Unit: seconds. By default, no timeout period for graceful shutdown is specified.
spring.schedulerx2.graceShutdownTimeout=10
# Specifies whether to enable the port required by the HTTP service. Default value: false.
spring.schedulerx2.httpServerEnable=true
# The port over which you can call the shutdown-purposed HTTP interface. Default port: 51886.
spring.schedulerx2.httpServerPort=52333
Shutdown modes
|
Mode |
Behavior |
|
|
The application exits only after all jobs and tasks in the application complete. |
|
|
The application exits after running jobs and tasks to which threads are allocated complete. Jobs and tasks in queue are dropped. |
Trigger graceful shutdown
Use one of the following three methods to trigger graceful shutdown.
Method 1: Send a SIGTERM signal (kill -15)
SchedulerX SDKs register a JVM shutdown hook that triggers graceful shutdown automatically. Send a SIGTERM signal to the application process:
kill -15 <Process ID>
The process waits for running jobs to complete based on the configured shutdown mode and timeout, then exits.
Do not run kill -9 directly. This signal forces immediate termination and bypasses graceful shutdown. Instead, run kill -15 first, monitor the application for a period, and then run kill -9 only if the process has not exited. Set an appropriate graceful shutdown timeout based on your job characteristics to prevent an extremely slow release process.
Method 2: Use the Spring Boot shutdown event
For Spring Boot applications, use the Actuator to trigger graceful shutdown through the Spring container shutdown event.
-
Add the Actuator dependency to your
pom.xmlfile:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-
Enable the shutdown endpoint:
management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=shutdown
-
Call the shutdown endpoint:
curl -X GET http://NodeIP:Port/actuator/shutdown
Method 3: Use the SchedulerX HTTP interface
Enable the SchedulerX built-in HTTP server to accept shutdown requests from external systems.
-
Add the following configuration:
# Enable the HTTP service interface. Default: false.
spring.schedulerx2.httpServerEnable=true
# HTTP shutdown interface port. Default: 51886.
spring.schedulerx2.httpServerPort=51886
Call the following HTTP endpoint to trigger a graceful shutdown:
curl -X GET http://NodeIP:51886/schedulerx/worker/shutdown
Integrate graceful shutdown into deployment workflows
Add graceful shutdown to your release process to prevent scheduled jobs from being interrupted during application restarts.
Integrate into a self-built continuous delivery (CD) process
Add graceful shutdown logic to the stop step of your CD pipeline by using any of the three trigger methods.
Sample CD process:
The following script demonstrates how to stop an application process with graceful shutdown:
# After you start the application, the process ID is written into the app.pid file.
PID="{Application deployment path}/app.pid"
FORCE=1
if [ -f ${PID} ]; then
TARGET_PID=`cat ${PID}`
kill -15 ${TARGET_PID}
loop=1
while(( $loop<=5 ))
do
# health: Specifies whether the current application process ends. You can configure the health field based on your business requirements.
health
if [ $? == 0 ]; then
echo "check $loop times, current app has not stop yet."
sleep 5s
let "loop++"
else
FORCE=0
break
fi
done
if [ $FORCE -eq 1 ]; then
echo "App(pid:${TARGET_PID}) stop timeout, forced termination."
kill -9 ${TARGET_PID}
if
rm -rf ${PID}
echo "App(pid:${TARGET_PID}) stopped successful."
fi
Integrate into a Kubernetes PreStop hook
Use the Kubernetes PreStop lifecycle hook to trigger graceful shutdown before a pod is terminated. Two approaches are available: exec scripts and HTTP requests.
Exec script approach
Run a stop script or send a SIGTERM signal directly:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app-image:latest
lifecycle:
preStop:
exec:
# command: ["/bin/sh", "-c", "kill -15 PID && sleep 30"]
command: ["/bin/sh", "-c", "Script path /stop.sh"]
HTTP request approach
Call the shutdown endpoint through an HTTP GET request:
-
For Spring Boot applications with Actuator enabled, set the path to
/actuator/shutdown. -
For non-Spring applications, enable the SchedulerX HTTP server and set the path to
/schedulerx/worker/shutdown.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app-image:latest
lifecycle:
preStop:
httpGet:
path: /schedulerx/worker/shutdown
port: 51886
scheme: HTTP
Integrate with Alibaba Cloud deployment platforms
If you deploy through Enterprise Distributed Application Service (EDAS) or Microservices Engine (MSE) with Container Service for Kubernetes (ACK), the platform handles SchedulerX graceful shutdown automatically after you enable the feature. For more information, see Gracefully shut down Spring Cloud applications and Graceful shutdown.