MSE XXL-JOB adds graceful shutdown functionality while maintaining compatibility with the open-source version. When an application shuts down, it first notifies the scheduling server to stop dispatching new tasks, waits for existing tasks to complete, and then safely shuts down the application, ensuring business continuity during restarts. This topic describes how to enable the graceful shutdown feature based on Alibaba Cloud Task Scheduling XXL-JOB to help you handle actual business restart or shutdown scenarios.
Overview of scheduled task graceful shutdown
In actual business scenarios, scheduled tasks within application processes continuously run at fixed intervals. When an application restarts during deployment, running scheduled tasks are forcibly interrupted, which may lead to incomplete data and a sharp decrease in scheduling success rates, ultimately resulting in business data damage. The following issues may occur:
Task execution interruption: When a task is running and the application process shuts down, business processing is interrupted, which may lead to incomplete business data.
Task scheduling failure: During the restart process, the scheduler distributes tasks to nodes that are shutting down, causing scheduling failures that affect overall processing efficiency.
Therefore, in scenarios where scheduled task scheduling is used, graceful shutdown of scheduled tasks is necessary to ensure smooth business operation during rolling deployments and restarts.
Implementing graceful shutdown based on open-source XXL-JOB
Prerequisites
The engine version is 2.1.0 or later. For more information, see XXL-JOB engine version.
The dependency related to the SchedulerX plugin package is added to the pom.xml file of the client. For more information, see XXL-JOB plugin version.
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx3-plugin-xxljob</artifactId> <version>Latest version</version> </dependency>
Procedure
Configure and enable a complete graceful shutdown solution in different business forms and deployment scenarios. The procedure consists of two steps:
Step 1: Perform initial integration with executors by using different frameworks
Different deployment modes of business applications require different methods for initial integration.
Mode 1: SpringBoot business application (recommended)
If you integrate a business application with XXL-Job executors by using Spring Boot, the system can automatically perform initial integration for graceful shutdown. Perform the following steps:
Add the SchedulerX plugin Maven dependency. For more information, see XXL-JOB plugin version.
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx3-plugin-xxljob</artifactId> <version>Latest version</version> </dependency>
Add the application configuration parameter and enable graceful shutdown. For detailed parameter descriptions, see Configuration parameter description.
# Configure graceful shutdown xxl.job.executor.shutdownMode=WAIT_ALL
Mode 2: Spring business application
If your business application is a web application started through the Spring framework, in addition to adding POM dependency
and application startup parameters
(refer to Mode 1: SpringBoot business application (recommended)), you also need to initialize the configuration XxlJobExecutorEnhancerInitializer
by adding the following configuration to web.xml:
<web-app>
<context-param>
<!-- Spring ApplicationContextInitializer is used to improve the capabilities of XXL-JOB executors. -->
<param-name>globalInitializerClasses</param-name>
<param-value>com.aliyun.schedulerx.xxljob.enhance.XxlJobExecutorEnhancerInitializer</param-value>
</context-param>
</web-app>
Mode 3: Frameless Java business application
If you integrate a business application with XXL-JOB executors in the frameless mode, as mentioned in the use cases of XXL-JOB executors, when you start the business application in pure Java, you can perform initial integration for graceful shutdown by using custom code. First, the business application still needs to add POM dependency
and application startup parameters
(refer to Mode 1: SpringBoot business application (recommended)). The following is a reference example:
Before starting the Executor, add:
EnhancerLoader.load(xxlJobProp)
to load the enhancement functionality.Before starting the Executor, add:
Runtime.getRuntime().addShutdownHook(...)
to add a shutdown Hook implementation for the current application.
Step 2: Application shutdown processing
Self-built deployment, shutdown using kill -15
In a self-built CD process, there is usually an application process stop node. This node can build a stop.sh
script for application process termination. The script content must contain the logic for the graceful shutdown of the application. The following code shows the sample script content for application process shutdown.
K8s containerized deployment, shutdown using PreStop
The lifecycle management feature of Kubernetes pods can automatically implement graceful shutdown. You can also use the preStop hook to implement the graceful shutdown logic by running exec to execute scripts and using HTTP requests.
Invalid modification: If the application process is the main process PID 1 in a container, the system automatically sends a SIGTERM signal to the main process to gracefully shut down the application.
Custom preStop: If there are complex multi-process relationships within the container, you can configure a preStop script to customize the application process termination using
kill -15 PID
or by calling a pre-setstop.sh
script to terminate the application process.
The terminationGracePeriodSeconds
parameter of the Pod controls the maximum waiting time for graceful shutdown (default is 30s). Configure it reasonably according to business needs.
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"]
Automatic integration with Alibaba Cloud application release platform
Stay tuned.
Configuration parameter description
You must configure the following parameter to enable graceful shutdown for your application. This parameter provides two modes for you to implement graceful shutdown.
# Graceful shutdown mode, WAIT_ALL: wait for all; WAIT_RUNNING: wait for running.
# If you do not configure this parameter, the original logic of XXL-JOB is applied so that graceful shutdown is disabled by default.
xxl.job.executor.shutdownMode=WAIT_ALL
Shutdown mode | Description |
Wait for all ( | In this mode, an application exits only after all jobs, including running jobs and jobs in queue, are complete. This mode is recommended. |
Wait for running ( | In this mode, an application exits after running jobs to which threads are allocated in the application are complete. Jobs in queue are dropped. |