Use scoped_session correctly.

This commit is contained in:
Patrick Jentsch 2019-08-12 19:34:24 +02:00
parent faf54e57fa
commit a5687a3f1b

View File

@ -14,8 +14,7 @@ class Scheduler(BackgroundScheduler):
def init_app(self, app):
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
self.session_factory = sessionmaker(bind=engine)
self.Session = scoped_session(self.session_factory)
self.Session = scoped_session(sessionmaker(bind=engine))
if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
self.add_job(self.check_jobs, id='jobs', seconds=10, trigger='interval')
self.start()
@ -24,15 +23,14 @@ class Scheduler(BackgroundScheduler):
from .models import Job
from . import swarm
Session = scoped_session(self.session_factory)
session = Session()
session = self.Session()
jobs = session.query(Job)
submitted_jobs = jobs.filter_by(status='submitted').all()
foo_jobs = jobs.filter(Job.status != 'complete',
Job.status != 'failed',
Job.status != 'submitted').all()
for job in submitted_jobs:
swarm.run(job.id)
swarm.run(job)
job.status = 'scheduled'
for job in foo_jobs:
'''
@ -44,4 +42,4 @@ class Scheduler(BackgroundScheduler):
job.end_date = datetime.utcnow()
service.remove()
session.commit()
Session.remove()
self.Session.remove()