farahats9 / sqlalchemy-celery-beat

Celery Periodic Tasks backed by the SQLAlchemy
MIT License
43 stars 6 forks source link

Confusing README #4

Closed miguelvalente closed 6 months ago

miguelvalente commented 6 months ago

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()
farahats9 commented 6 months ago

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

mvalenteprosus commented 6 months ago

No problem, and thanks.