1

I am having an issue getting my background task to fire off at the scheduled interval using a production webserver (IIS).

My app works correctly with the built in Werkzeug web server, but when I deploy to IIS the background task does not fire every 5 minutes. Instead, it will run 5 minutes after the webpage is loaded. Is this because I have the scheduler initialized in the __init__.py?

__init__.py

import os
from flask import Flask
from flask_apscheduler import APScheduler
from apscheduler.schedulers.background import BackgroundScheduler

app = Flask(__name__)

app.config.from_object('app.config.BaseConfig')


# Setup the database
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

scheduler = APScheduler(BackgroundScheduler())
scheduler.init_app(app)
scheduler.start()

# Import the views
from app.views import main, error

config.py

import logging
import os

SCHEDULER_JOBS = [
    {
        'id': 'read',
        'func': 'app.read_licenses:read',
        'trigger': 'interval',
        'minutes': 5
    }
]


class BaseConfig(object):
    """Base configuration"""
    JOBS = SCHEDULER_JOBS
    SCHEDULER_JOB_DEFAULTS = {
        'coalesce': False,
        'max_instances': 1
    }
    # use sqlite by default
    SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'sqlite:///app.db')
    SCHEDULER_API_ENABLED = True

web.config

<configuration>
 <system.webServer>
   <handlers>
    <add name="Python FastCGI"
       path="*"
       verb="*"
       modules="FastCgiModule"
       scriptProcessor="c:\inetpub\wwwroot\myapp\venv\scripts\python.exe|c:\inetpub\wwwroot\myapp\venv\lib\site-packages\wfastcgi.py"
       resourceType="Unspecified"
       requireAccess="Script" />
   </handlers>
 </system.webServer>
 <appSettings>
   <!-- Required settings -->
   <add key="WSGI_HANDLER" value="app.app" />
   <add key="PYTHONPATH" value="C:\inetpub\wwwroot\myapp" />

    <!-- Optional settings -->
    <add key="WSGI_LOG" value="C:\iis-logs\myapp.log" />
 </appSettings>
 </configuration>
its30
  • 253
  • 3
  • 17
  • could you please share did you change anything when you migrate site from Werkzeug server to iis? in my opinion when you use APScheduler in init then it will be loaded upon app creation. you could refer this [link](https://github.com/viniciuschiele/flask-apscheduler/blob/master/COMMON-ISSUES.md) for more detail. – Jalpa Panchal Jan 16 '20 at 06:06
  • @JalpaPanchal the only thing that is different is the webserver. Outside of the apscheduler problem, the application behaves normally on IIS. – its30 Jan 16 '20 at 12:58
  • @JalpaPanchal I added the web.config as the only difference – its30 Jan 16 '20 at 14:05
  • you could try to put the scheduler in another file try to test it. as I said when you use APScheduler in init then it will be loaded upon app creation. – Jalpa Panchal Jan 17 '20 at 02:12
  • i have the same problem. the app work perfectly but the Apscheduler not work. – nickb84 Nov 19 '21 at 08:05

0 Answers0