Quickstart for SQLAlchemy usersΒΆ
News supports SQLAlchemy out of the box. Subscribing web contents using News with SQLAlchemy is as simple as following.
1. Define models
First off, you need to describe how your subscription schedule and web contents should look like to your database.
# models.py
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from news.models.sqlalchemy import (
create_default_news,
create_default_schedule
)
base = declarative_base()
class User(base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
Schedule = create_default_schedule(user_model=User, base=base)
News = create_default_news(schedule_model=Schedule, base=base)
2. Define a backend
Backend serves as an interface between your models and other news
components. Any backend constructors will produce singleton backend objects
based on combination of supplied models.
# backend.py
from news.backends import SQLAlchemyBackend
from .models import Schedule, News
backend = SQLAlchemyBackend(schedule_model=Schedule, news_model=News)
3. Define a scheduler
news usese celery as an task queue for distributing cover tasks over
multiple workers. You will need to define your own celery instance and supply
it to the scheduler.
# celery.py
from celery import Celery
celery = Celery()
# scheduler.py
from news.scheduler import Scheduler
from .backend import backend
from .celery import celery
scheduler = Scheduler(backend=backend, celery=celery)
if __name__ == '__main__':
scheduler.start()
4. Register the scheduler’s celery task
Celery workers should know what kind of tasks they can work on. Scheduler’s
make_task() task factory method creates it’s news cover task.
# tasks.py
from .scheduler import scheduler
scheduler_task = scheduler.make_task()
5. Populate your schedules
To get web contents periodically, you first need to subscribe an url.
# populate.py from .models import User, Schedule from .extensions import session if __name__ == '__main__': owner = User.query.first() schedule = Schedule(url='http://httpbin.org', owner=owner) session.add(schedule) session.commit()
6. Launch celery workers and the scheduler
Launch celery workers and the scheduler on their each process!
$ celery worker
$ python scheduler.py