Skip to content

Commit

Permalink
Merge branch 'drafts/async-worker' into releases/0.12.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Feb 21, 2018
2 parents af331be + f3b895c commit ef60910
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 26 deletions.
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ services:
ports:
- 9009:9009

worker:
restart: always
image: demoshop
environment:
- DJANGO_SHOP_TUTORIAL
- POSTGRES_DB=myshop_$DJANGO_SHOP_TUTORIAL
- DJANGO_SETTINGS_MODULE=myshop.settings
env_file:
- example/docker-files/environ
command: su django -c /web/worker.py
volumes:
- shopmedia:/web/workdir
depends_on:
- postgresdb
- redishost
networks:
- shopnet

networks:
shopnet:

Expand Down
16 changes: 16 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ Some recipes on how to perform certain tasks in **django-SHOP**.
howto/handling-discounts
howto/handling-taxes


Django/Python compatibility table
=================================

=========== === === ==== ==== === === === === ===
django-SHOP Django Python
----------- ------------------------- ------------------
\ 1.8 1.9 1.10 1.11 2.0 2.7 3.4 3.5 3.6
=========== === === ==== ==== === === === === ===
0.10.x ✓ ✓ ⨯ ⨯ ⨯ ✓ ✓ ✓ ⨯
0.11.x ⨯ ✓ ✓ ⨯ ⨯ ✓ ✓ ✓ ✓
0.12.x ⨯ ⨯ ⨯ ✓ ⨯ ✓ ✓ ✓ ✓
0.13.x ⨯ ⨯ ⨯ ✓ ⨯ ✓ ✓ ✓ ✓
=========== === === ==== ==== === === === === ===


Development and Community
=========================

Expand Down
1 change: 1 addition & 0 deletions example/docker-files/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ADD example/myshop /web/myshop
ADD requirements /web/requirements
COPY example/wsgi.py /web/wsgi.py
COPY example/manage.py /web/manage.py
COPY example/worker.py /web/worker.py
COPY example/package.json /web/package.json
COPY example/package-lock.json /web/package-lock.json
COPY example/docker-files/uwsgi.ini /web/uwsgi.ini
Expand Down
1 change: 1 addition & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
django-redis-cache==1.7.1
django-redis-sessions==0.6.1
redis==2.10.6
schedule==0.5.0
stripe==1.37.0
djangoshop-stripe<0.13
26 changes: 0 additions & 26 deletions example/run_jobs.py

This file was deleted.

37 changes: 37 additions & 0 deletions example/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
import redis
import schedule
import time


if __name__ == '__main__':
from django import setup
from django.conf import settings
from django.core.management import call_command
from django.utils import timezone

# initialize Django
setup()

# schedule jobs
schedule.every().minute.do(call_command, 'send_queued_mail')
rebuild_at = timezone.now() + timezone.timedelta(minutes=6)
schedule.every().hour.at(rebuild_at.strftime('*:%M')).do(call_command, 'rebuild_index', interactive=False)
schedule.every().sunday.do(call_command, 'shopcustomers', delete_expired=True)

if hasattr(settings, 'SESSION_REDIS'):
redis_con = dict((key, settings.SESSION_REDIS[key]) for key in ['host', 'port', 'db', 'socket_timeout'])
pool = redis.ConnectionPool(**redis_con)
r = redis.Redis(connection_pool=pool)
pubsub = r.pubsub()
pubsub.subscribe('django-SHOP')
else:
pubsub = type('PubSub', (), {'get_message': lambda s: None})()

while True:
message = pubsub.get_message()
if message:
if message['data'] == 'send_queued_mail':
call_command('send_queued_mail')
schedule.run_pending()
time.sleep(1)
2 changes: 2 additions & 0 deletions shop/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from shop.conf import app_settings
from shop.models.order import BaseOrder
from shop.models.fields import ChoiceEnum, ChoiceEnumField
from shop.signals import email_queued


class Email(OriginalEmail):
Expand Down Expand Up @@ -194,3 +195,4 @@ def order_event_notification(sender, instance=None, target=None, **kwargs):
attachments[notiatt.attachment.original_filename] = notiatt.attachment.file.file
mail.send(recipient, template=template, context=context,
attachments=attachments, render_on_delivery=True)
email_queued()
21 changes: 21 additions & 0 deletions shop/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

try:
import redis
except ImportError:
redis = None
from django.conf import settings
from django.dispatch import Signal


customer_recognized = Signal(providing_args=['customer', 'request'])

if redis and hasattr(settings, 'SESSION_REDIS'):
redis_con = dict((key, settings.SESSION_REDIS[key]) for key in ['host', 'port', 'db', 'socket_timeout'])
pool = redis.ConnectionPool(**redis_con)
redis_con = redis.Redis(connection_pool=pool)
else:
redis_con = type('Redis', (), {'publish': lambda *args: None})()


def email_queued():
"""
If SESSION_REDIS is configured, inform a separately running worker engine, that
emails are ready for delivery. Call this function every time an email has been
handled over to the Post-Office.
"""
redis_con.publish('django-SHOP', 'send_queued_mail')
5 changes: 5 additions & 0 deletions shop/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from shop.models.cart import CartModel
from shop.models.customer import CustomerModel
from shop.rest.auth import PasswordResetSerializer, PasswordResetConfirmSerializer
from shop.signals import email_queued


class AuthFormsView(GenericAPIView):
Expand Down Expand Up @@ -130,6 +131,10 @@ def post(self, request, *args, **kwargs):
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.save()

# trigger async email queue
email_queued()

# Return the success message with OK HTTP status
msg = _("Instructions on how to reset the password have been sent to '{email}'.")
response_data = {'reset_form': {
Expand Down

0 comments on commit ef60910

Please sign in to comment.