django websub订户
websubsub的Python项目详细描述
Django WebSub订户。
安装
pip install websubsub
将websubsub.apps.WebsubsubConfig添加到^{tt2}列表中$ 在您的settings.py:
INSTALLED_APPS = [ ..., 'websubsub.apps.WebsubsubConfig' ]
将settings.py中的SITE_URL设置设置为 您的项目站点,例如https://example.com。它将习惯于 生成完整的回调URL。
在settings.py中设置DUMBLOCK_REDIS_URL设置。redis锁 用于确保订阅/取消订阅任务与 中心和本地数据库。
SITE_URL = 'http://example.com' DUMBLOCK_REDIS_URL= 'redis://redishost:6379'
添加websubsub.tasks.refresh_subscriptions和 websubsub.tasks.retry_failed到芹菜节拍时间表。如果你定义 它在settings.py:
CELERY_BEAT_SCHEDULE = { 'websub_refresh': { 'task': 'websubsub.tasks.refresh_subscriptions', 'schedule': 3600 # Hourly }, 'websub_retry': { 'task': 'websubsub.tasks.retry_failed', 'schedule': 600 # Every 10 minutes }, }
用法
创建websub回调
创建芹菜任务处理程序,通常在tasks.py:
from celery import shared_task @shared_task def news_task(data): print('got news!')
回调url应该以uuid结尾。在中注册处理程序的URL urls.py:
from websubsub.views import WssView from .tasks import news_task, reports_task urlpatterns = [ path('/websubcallback/news/<uuid:id>', WssView.as_view(news_task), name='webnews') path('/websubcallback/reports/<uuid:id>', WssView.as_view(reports_task), name='webreports') ]
订阅
您可以随时创建订阅,也可以使用静态订阅。
在代码中创建订阅:
from websubsub.models import Subscription Subscription.create(topic='mytopic', urlname='webnews', hub='http://example.com')
这将在数据库中创建订阅对象并安排芹菜 要使用中心订阅的任务。
静态订阅
[待办事项]不确定这是处理静态订阅的最佳方式。
静态订阅可以在settings.py中定义,它们是 然后通过管理命令实现 ./manage.py websubscribe_static。
在settings.py:
中添加静态订阅WEBSUBS_HUBS = { 'http://example.com': { 'subscriptions': [ # (topic, urlname) pairs ('mytopic', 'webnews'), ... ] } }
执行./manage.py websubscribe_static
退订
取消订阅现有订阅,调用 Subscription.unsubscribe()方法:
from websubsub.models import Subscription Subscription.objects.get(pk=4).unsubscribe()
发现
未实施
设置
SITE_URL-例如:https://example.com。必修的。将习惯于 生成完整的回调URL。
DUMBLOCK_REDIS_URL-例如:redis://redishost:6379。必修的。威尔 用于锁定原子任务。
WEBSUBS_DEFAULT_HUB_URL
WEBSUBS_MAX_CONNECT_RETRIES
WEBSUBS_MAX_HUB_ERROR_RETRIES
WEBSUBS_MAX_VERIFY_RETRIES
WEBSUBS_VERIFY_WAIT_TIME-之前应该经过多少秒 未验证的订阅被视为失败。之后, websubsub.tasks.retry_failed()任务将能够重试 再次订阅进程。
测试
pip install -r tests/requirements.txt py.test