G'day mates. I'm trying to add a cron job to call my cron_job_notes_recall
function periodically but Flask gives an error:
No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
Ok, I followed the Flask Factory approach and everything is being initialized in init.py hence I decided to start my job here. I am pretty sure that the function works properly because I put it into a function as a route and call it. I suppose the problem might be caused by my database (?). The function simply makes a query to DB which is initialized above afterward send an e-mail if the query returned something. Do You have any idea where my cron job should be initialized ?
init.py
# Globally accessible libraries
db = SQLAlchemy()
login_manager = LoginManager()
sched = BlockingScheduler()
def create_app(test_config=False):
"""Initialize the core application."""
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
app = Flask(__name__, instance_relative_config=False)
if not test_config:
app.config.from_object('config.Config')
pprint(app.__dict__)
else:
app.config.from_object('config.TestConfig')
app.config.update(
BCRYPT_LOG_ROUNDS=4,
HASH_ROUNDS=1,
LOGIN_DISABLED=True,
WTF_CSRF_ENABLED=False,
SECRET_KEY=os.urandom(25),
TESTING=True
)
db.init_app(app)
login_manager.init_app(app)
with app.app_context():
# Include our Routes
from flask_bootstrap import Bootstrap
from . import routes
from . import auth
# Register Blueprints
app.register_blueprint(auth.auth_bp)
Bootstrap(app)
db.create_all()
# HERE IS MY CRON JOB
sched.add_job(cron_job_notes_recall, 'interval', seconds=1)
sched.start()
try:
return app, logging
except: