Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
494 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import django.dispatch | ||
|
||
search_index_update = django.dispatch.Signal( | ||
providing_args=['sender', 'instance']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,103 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import logging | ||
|
||
from django.db import transaction | ||
from django.core.mail import send_mail | ||
from django.apps import apps | ||
from django.core.management import call_command | ||
|
||
from .conf import settings | ||
from . import signals | ||
|
||
try: | ||
# TODO: remove this try block. | ||
from celery.decorators import task | ||
except ImportError: | ||
task = None | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
if not hasattr(settings, 'BROKER_URL'): | ||
def task(f): | ||
f.delay = f | ||
return f | ||
# XXX support custom task manager __import__('foo.task')? | ||
def task_manager(tm): | ||
if tm == 'celery': | ||
from celery import shared_task | ||
def task(t): | ||
t = shared_task(t) | ||
def _task(*args, **kwargs): | ||
return t.delay(*args, **kwargs) | ||
return _task | ||
return task | ||
if tm == 'huey': | ||
from huey.contrib.djhuey import db_task | ||
return db_task() | ||
assert tm is None | ||
return lambda t: t | ||
|
||
task = task_manager(settings.ST_TASK_MANAGER) | ||
|
||
@task | ||
def send_notification(): | ||
pass | ||
|
||
def periodic_task_manager(tm): | ||
if tm == 'huey': | ||
from huey import crontab | ||
from huey.contrib.djhuey import db_periodic_task | ||
def periodic_task(hours): | ||
return db_periodic_task(crontab( | ||
minute='0', hour='*/{}'.format(hours))) | ||
return periodic_task | ||
assert tm in ('celery', None) | ||
def fake_periodic_task(*args, **kwargs): | ||
return task_manager(tm) | ||
return fake_periodic_task | ||
|
||
@task | ||
def backup_database(): | ||
pass | ||
periodic_task = periodic_task_manager(settings.ST_TASK_MANAGER) | ||
|
||
|
||
@task | ||
def search_index_update(): | ||
pass | ||
def delayed_task(t): | ||
t = task(t) # wrap at import time | ||
def delayed_task_inner(*args, **kwargs): | ||
transaction.on_commit(lambda: t(*args, **kwargs)) | ||
return delayed_task_inner | ||
|
||
|
||
@delayed_task | ||
def send_email(subject, message, from_email, recipients): | ||
# Avoid retrying this task. It's better to log the exception | ||
# here instead of possibly spamming users on retry | ||
# We send to one recipient at the time, because otherwise | ||
# it'll likely get flagged as spam, or it won't reach the | ||
# the recipient at all | ||
for recipient in recipients: | ||
try: | ||
send_mail( | ||
subject=subject, | ||
message=message, | ||
from_email=from_email, | ||
recipient_list=[recipient]) | ||
except OSError as err: | ||
logger.exception(err) | ||
return # bail out | ||
|
||
|
||
@delayed_task | ||
def search_index_update(topic_pk): | ||
# Indexing is too expensive; bail if | ||
# there's no dedicated task manager | ||
if settings.ST_TASK_MANAGER is None: | ||
return | ||
Topic = apps.get_model('spirit_topic.Topic') | ||
signals.search_index_update.send( | ||
sender=Topic, | ||
instance=Topic.objects.get(pk=topic_pk)) | ||
|
||
@task | ||
|
||
@periodic_task(hours=settings.ST_SEARCH_INDEX_UPDATE_HOURS) | ||
def full_search_index_update(): | ||
age = settings.ST_SEARCH_INDEX_UPDATE_HOURS | ||
call_command("update_index", age=age) | ||
|
||
|
||
@delayed_task | ||
def clean_sessions(): | ||
pass | ||
|
||
|
||
@delayed_task | ||
def backup_database(): | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Generated by Django 3.0.7 on 2020-09-11 17:59 | ||
|
||
from django.db import migrations, models | ||
import spirit.core.utils.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tests', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='TaskResultModel', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('result', models.CharField(max_length=255)), | ||
], | ||
), | ||
migrations.AlterField( | ||
model_name='autoslugbadpopulatefrommodel', | ||
name='slug', | ||
field=spirit.core.utils.models.AutoSlugField(allow_unicode=True, populate_from='bad'), | ||
), | ||
migrations.AlterField( | ||
model_name='autoslugdefaultmodel', | ||
name='slug', | ||
field=spirit.core.utils.models.AutoSlugField(allow_unicode=True, default='foo'), | ||
), | ||
migrations.AlterField( | ||
model_name='autoslugpopulatefrommodel', | ||
name='slug', | ||
field=spirit.core.utils.models.AutoSlugField(allow_unicode=True, populate_from='title'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.db import models | ||
|
||
|
||
class TaskResultModel(models.Model): | ||
|
||
result = models.CharField(max_length=255) |
Oops, something went wrong.