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.
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()