# Path: mass_flask_scheduling/tasks/schedule_analyses.py
# def schedule_analyses():
#     with app.app_context():
#         instance_dict = _prepare_instance_dict()
#         analysis_requests = AnalysisRequest.objects().no_dereference()
#         requests_scheduled = 0
#         requests_not_scheduled = 0
#         for request in analysis_requests:
#             if _schedule_analysis_request(request, instance_dict):
#                 request.delete()
#                 requests_scheduled += 1
#             else:
#                 requests_not_scheduled += 1


import os
from flask import Blueprint
from flask_apscheduler import APScheduler
from mass_flask_scheduling.tasks import schedule_analyses


scheduling_blueprint = Blueprint('mass_flask_scheduling', __name__, template_folder='templates', static_folder='static')
scheduler = APScheduler()


@scheduling_blueprint.record_once
def on_load(state):
    if 'MASS_TESTING' in state.app.config and state.app.config['MASS_TESTING'] is True:
        print('Not starting scheduler when MASS_TESTING=True')
        return
    elif not state.app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        scheduler.init_app(state.app)

# Path: mass_flask_scheduling/tasks/schedule_analyses.py
# def schedule_analyses():
#     with app.app_context():
#         instance_dict = _prepare_instance_dict()
#         analysis_requests = AnalysisRequest.objects().no_dereference()
#         requests_scheduled = 0
#         requests_not_scheduled = 0
#         for request in analysis_requests:
#             if _schedule_analysis_request(request, instance_dict):
#                 request.delete()
#                 requests_scheduled += 1
#             else:
#                 requests_not_scheduled += 1


# Path: mass_flask_scheduling/config.py
import os
from flask import Blueprint
from flask_apscheduler import APScheduler
from mass_flask_scheduling.tasks import schedule_analyses



scheduling_blueprint = Blueprint('mass_flask_scheduling', __name__, template_folder='templates', static_folder='static')
scheduler = APScheduler()


@scheduling_blueprint.record_once
def on_load(state):
    if 'MASS_TESTING' in state.app.config and state.app.config['MASS_TESTING'] is True:
        print('Not starting scheduler when MASS_TESTING=True')
        return
    elif not state.app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        scheduler.init_app(state.app)

        scheduler.add_job('schedule_analyses', schedule_analyses, trigger='interval', seconds=state.app.config['SCHEDULE_ANALYSES_INTERVAL'])