-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from johncronan/dev
add celery; implement timed_completion in form admin
- Loading branch information
Showing
38 changed files
with
397 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os | ||
from celery import Celery | ||
|
||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') | ||
|
||
app = Celery('formative') | ||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
app.autodiscover_tasks() |
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,5 +1,5 @@ | ||
import multiprocessing | ||
|
||
bind = ':8000' | ||
timeout = 180 | ||
timeout = 45 | ||
workers = multiprocessing.cpu_count() * 2 + 1 |
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,59 @@ | ||
from django.apps import apps | ||
from django.core import mail | ||
from django.template import Template | ||
from django.utils import timezone | ||
from celery import shared_task | ||
import time | ||
|
||
from .models import Form | ||
from .utils import send_email, submission_link | ||
|
||
|
||
EMAILS_PER_SECOND = 10 | ||
|
||
@shared_task | ||
def send_email_for_submissions(model_name, id_values, subject_str, content_str): | ||
model = apps.get_model(f'formative.{model_name}') | ||
queryset = model.objects.filter(pk__in=id_values) | ||
|
||
subject, content = Template(subject_str), Template(content_str) | ||
iterator = queryset.iterator() | ||
batch, form, last_time, done, n = [], None, None, False, 0 | ||
while not done: | ||
submission = next(iterator, None) | ||
if not submission: done = True | ||
else: batch.append(submission) | ||
|
||
if len(batch) == EMAILS_PER_SECOND or done: | ||
if last_time: | ||
this_time = time.time() | ||
remaining = last_time + 1 - this_time | ||
if remaining > 0: time.sleep(remaining) | ||
last_time = time.time() | ||
|
||
with mail.get_connection() as conn: | ||
for sub in batch: | ||
if not form: form = sub._get_form() | ||
context = { | ||
'submission': sub, 'form': form, | ||
'submission_link': submission_link(sub, form) | ||
} | ||
if sub._submitted: sub._update_context(form, context) | ||
n += 1 | ||
send_email(content, sub._email, subject, | ||
context=context, connection=conn) | ||
batch = [] | ||
return n | ||
|
||
@shared_task | ||
def timed_complete_form(form_id, datetime_val): | ||
try: form = Form.objects.get(id=form_id) | ||
except Form.DoesNotExist: return | ||
|
||
if form.timed_completion() != datetime_val: | ||
return False # it gets rescheduled on value change, so don't do anything | ||
|
||
form.status = Form.Status.COMPLETED | ||
form.completed = timezone.now() | ||
form.save() | ||
return True |
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
Oops, something went wrong.