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