Integrating Flask-APScheduler with flask-migrate and flask Script

762 views Asked by At

I am not clear on how to integrate my models, using flask-script, and scheduled jobs on the same database. There appears to be some work under the hood with Flask-apscheduler where it creates the database for you.

When I run python manage.py db init It will create a database with an apscheduled_jobs table. Now if I run python manage.py db migrate I get the error:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: apscheduler_jobs.id [SQL: 'INSERT INTO apscheduler_jobs (id, next_run_time, job_state) VALUES (?, ?, ?)'] [parameters: ('job1', 1505317109.041658, <memory at 0x000001AD6754E288>)]

Followed by:

apscheduler.jobstores.base.ConflictingIdError: 'Job identifier (job1) conflicts with an existing job'

The layout of my project looks something like:

enter image description here

In intel\_init__.py looks like:

from flask import Flask
from flask_apscheduler import APScheduler
from .models import db


app = Flask(__name__)
app.config.from_object('config.Config')
db.init_app(app)

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

manage.py

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from intel_app import app, db

migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

EDIT: I moved schedule.start() from intel\_init__.py to the run.py module, it will work just once, for it I stop the app and then restart the app again, I get the same errors as above.

0

There are 0 answers