How to gracefully stop apscheduler docker container¶
By default, the apscheduler process will not gracefully exit when you exec docker stop
command. But why?
In fact, when your exec docker stop
, docker server will send a SIGTERM
signal to the main process in docker container. When the main process is a apscheduler process, it will not handle this signal.
Let apscheduler handle SIGTERM
signal¶
Code Example:
import signal
from apscheduler.schedulers.blocking import BlockingScheduler
def my_task():
print('pretend to do something...')
def exit_gracefully(signum, frame):
print('shutdown scheduler gracefully')
scheduler.shutdown()
scheduler = BlockingScheduler()
scheduler.add_job(my_task, 'interval', hours=1, max_instances=1)
# register a SIGTERM signal handler here
signal.signal(signal.SIGTERM, exit_gracefully)
scheduler.start()
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/apscheduler_docker_stop
Posted on 2022-03-29
Mail to author