Martino
Martino

Reputation: 11

I'm using Flask and APScheduler, but the next day's schedule doesn't work

I am currently using APScheduler integrated with the Flask app. When a user registers a schedule through the Flask app, it registers with APScheduler's background scheduler. The Flask app is running Gunicorn.

The issue is as follows.
1. After registering the schedule, the first schedule works normally.
2. The next day schedule doesn't work. The schedule function contains print statements and loggers, but none of them are output.
3. As an unusual feature, the Gunicorn WORKER TIMEOUT log is output and the app is restarted. After that, a log such as "schedule was missed by 00:00" is output intermittently.
4. The number of Gunicorn worker is 1.

# my__app/setup.py
app = None
sched = None

class customFlask(Flask):
    """
    Flask Setting
    """

    def __init__(self, **kwargs):
        Flask.__init__(self, __name__, **kwargs)
        base.app = self
        self.config.from_envvar("SETTINGS")

        base.db = _BaseSQLAlchemy(base.app, session_options={"autoflush": False})
        Migrate(base.app, base.db)

def init(**kwargs):
    """
    Setting Flask (call CorFlask)
    """
    global app, sched
    if not app:
        app = customFlask(**kwargs)
    sched = BackgroundScheduler(
        jobstores={"default": SQLAlchemyJobStore(url=SQLALCHEMY_DATABASE_URI, tablename="apscheduler_jobs")}
    )


    return app, sched
# my_app/__init__.py
app, sched = init(instance_path=os.path.dirname(__file__) + "/../instance")
core_bp = Blueprint("core", "core_bp", template_folder=os.path.dirname(__file__) + "/templates")

with app.app_context():
    # (...skip...)

    sched.start()

a suspected thing
1. The background_scheduler was executed within the app_context clause of the Flask app.
2. The scheduler does not have a worker to work with because there is only one Gunicorn worker

Upvotes: 1

Views: 30

Answers (0)

Related Questions