farahats9/sqlalchemy-celery-beat

Confusing README

miguelvalente opened this issue · 2 comments

Hey I was following the instructions in the README.md and they threw me for a spin.
I'll admit that partially is a skill issue on my end but following the code you are lead to the following example:

session_manager = SessionManager()
beat_dburi = 'sqlite:///test.db'
engine, Session = session_manager.create_session(beat_dburi)
session = Session()

schedule = session.query(IntervalSchedule).filter_by(every=10, period=Period.SECONDS).first()
if not schedule:
    schedule = IntervalSchedule(every=10, period=Period.SECONDS)
    session.add(schedule)
    session.commit()

task = PeriodicTask(
    schedule_model=schedule,
    name='Importing contacts',
    task='main.add',
    args='[60, 9]', 
)
session.add(task)
session.commit()

When you try to run this it fails. The version bellow works.

session_manager = SessionManager()
beat_dburi = 'sqlite:///test.db'
session = session_manager.session_factory(beat_dburi)

with session_cleanup(session):

    schedule = session.query(IntervalSchedule).filter_by(every=10, period=Period.SECONDS).first()
    if not schedule:
        schedule = IntervalSchedule(every=20, period=Period.SECONDS)
        session.add(schedule)
        session.commit()

    task = PeriodicTask(
        schedule_model=schedule,
        name='Importing contacts',
        task='main.add',
        args='[60, 9]', 
    )
    session.add(task)
    session.commit()

Thank you for pointing this out, you should always use session_factory to create the session specially when the tables are not created because it will create them for you.
I will update the readme example

No problem, and thanks.