1
0
Fork 0

Move to redis and fix a bunch of things

This commit is contained in:
Mouse Reeve 2020-03-31 16:31:33 -07:00
parent ca26a712c3
commit bb04a40044
14 changed files with 286 additions and 50 deletions

View file

@ -1,9 +0,0 @@
''' we need this file to initialize celery '''
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)

View file

@ -1,24 +0,0 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fedireads.settings')
app = Celery('fedireads')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))

View file

@ -13,6 +13,7 @@ import requests
from fedireads import models, outgoing
from fedireads import status as status_builder
from fedireads.remote_user import get_or_create_remote_user
from fedireads import tasks
@csrf_exempt
@ -256,31 +257,16 @@ def handle_create(activity):
def handle_favorite(activity):
''' approval of your good good post '''
try:
status_id = activity['object'].split('/')[-1]
status = models.Status.objects.get(id=status_id)
liker = get_or_create_remote_user(activity['actor'])
except (models.Status.DoesNotExist, models.User.DoesNotExist):
return HttpResponseNotFound()
if not liker.local:
status_builder.create_favorite_from_activity(liker, activity)
status_builder.create_notification(
status.user,
'FAVORITE',
related_user=liker,
related_status=status,
)
print('hiii!')
tasks.handle_incoming_favorite.delay(activity)
return HttpResponse()
def handle_unfavorite(activity):
''' approval of your good good post '''
try:
favorite_id = activity['object']['id']
fav = status_builder.get_favorite(favorite_id)
except models.Favorite.DoesNotExist:
favorite_id = activity['object']['id']
fav = status_builder.get_favorite(favorite_id)
if not fav:
return HttpResponseNotFound()
fav.delete()

View file

@ -5,6 +5,13 @@ from environs import Env
env = Env()
# celery
CELERY_BROKER = env('CELERY_BROKER')
CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -21,12 +28,6 @@ DOMAIN = env('DOMAIN')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*'])
OL_URL = env('OL_URL')
# celery/rebbitmq
CELERY_BROKER_URL = env('CELERY_BROKER')
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'amqp'
# Application definition
INSTALLED_APPS = [

42
fedireads/tasks.py Normal file
View file

@ -0,0 +1,42 @@
''' background tasks '''
from celery import Celery
import os
from fedireads import models
from fedireads import status as status_builder
from fedireads.outgoing import get_or_create_remote_user
from fedireads import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fr_celery.settings')
app = Celery(
'tasks',
broker=settings.CELERY_BROKER,
)
@app.task
def handle_incoming_favorite(activity):
''' ugh '''
print('here we go')
try:
status_id = activity['object'].split('/')[-1]
print(status_id)
status = models.Status.objects.get(id=status_id)
liker = get_or_create_remote_user(activity['actor'])
except (models.Status.DoesNotExist, models.User.DoesNotExist):
print('gonna return')
return
print('got the status okay')
if not liker.local:
status_builder.create_favorite_from_activity(liker, activity)
status_builder.create_notification(
status.user,
'FAVORITE',
related_user=liker,
related_status=status,
)
print('done')