All Products
Search
Document Center

Microservices Engine:Gracefully shut down jobs and tasks executed in an application

Last Updated:Jan 22, 2024

This topic describes how to gracefully shut down jobs and tasks executed in an application.

Background information

In actual business scenarios, scheduled tasks are continuously executed in application processes at fixed intervals. When an application is restarted during release, the application process must be temporarily shut down. If you close the application, the ongoing scheduled tasks will be interrupted, which may cause incomplete data or other issues. To prevent this situation, SchedulerX allows you to implement a graceful shutdown for scheduled tasks. You must wait until the running tasks are complete before you safely close the application.

Limits

The version of the SchedulerX agent must be 1.10.8 or later.

How to configure

Different from real-time online business scenarios, offline scheduled tasks may take a long time to execute, but it is unsuitable for application processes to wait indefinitely when they are being shut down. You can configure the following content to set the time for delayed shutdown of an application that has running tasks.

# The graceful shutdown modes. Valid values: WAIT_ALL and WAIT_RUNNING. 
# If you do not specify the spring.schedulerx2.graceShutdownMode parameter, graceful shutdown is disabled. By default, graceful shutdown is disabled. 
spring.schedulerx2.graceShutdownMode=WAIT_ALL
# The shutdown grace period. Unit: seconds. By default, no shutdown grace period is specified. 
spring.schedulerx2.graceShutdownTimeout=10

Graceful shutdown mode

Description

WAIT_ALL

In this mode, an application exits only after all received jobs and tasks are complete. (Recommended)

WAIT_RUNNING

In this mode, an application exits after running jobs and tasks to which threads are allocated are complete. Jobs and tasks in queue are dropped.

Important

When you execute a script to stop an application, we recommend that you do not directly run the kill -9 command. Otherwise, graceful shutdown does not work. To shut down an application, we recommend that you first run the kill -15 command, monitor the application for a period of time, and then run the kill -9 command to forcibly shut down the application. We recommend that you set a proper shutdown grace period based on the characteristics of the application and involved jobs and tasks to prevent a slow release process.