Re: [yocto] [layerindex-web][PATCH v2] Asynchronous email notifications, task execution

2017-08-24 Thread Paul Eggleton
Hi Diana,

On Thursday, 24 August 2017 12:58:38 PM NZST Diana Thayer wrote:
> This patch adds asynchronous task execution using a Celery backend
> and RabbitMQ task queue.
> 
> It updates the README to reflect the installation and configuration
> of a basic RabbitMQ setup, adds a 'tasks.py' file to contain task
> definitions, updates the 'edit_layer_view' function to send
> emails to administrators about new and updated layers asynchronously,
> and modifies the 'settings.py' to include a default configuration
> for a RabbitMQ connection.

This summarises what the patch does, which is useful, but it misses out the
most important bit - why we're making this change, i.e. to allow layer
submission process to proceed even in the event that sending the notification
email fails, and establishing an asynchronous execution mechanism that we can
use in future e.g. for triggering parse operations from the web UI. If you
could also include a reference to the bug in the form [YOCTO #11197] that
would be great.

Please also add your Signed-off-by which is missing.

One other comment below.

>
> --- a/layerindex/views.py
> +++ b/layerindex/views.py
> @@ -19,7 +19,6 @@ from layerindex.forms import EditLayerForm, 
> LayerMaintainerFormSet, EditNoteForm
>  from django.db import transaction
>  from django.contrib.auth.models import User, Permission
>  from django.db.models import Q, Count, Sum
> -from django.core.mail import EmailMessage
>  from django.template.loader import get_template
>  from django.template import Context
>  from django.utils.decorators import method_decorator
> @@ -28,6 +27,7 @@ from django.contrib import messages
>  from reversion.models import Revision
>  from . import utils
>  from . import simplesearch
> +from . import tasks
>  import settings
>  from django.dispatch import receiver
>  import reversion
> @@ -163,7 +163,7 @@ def edit_layer_view(request, template_name, 
> branch='master', slug=None):
>  # Send email
>  plaintext = get_template('layerindex/submitemail.txt')
>  perm = Permission.objects.get(codename='publish_layer')
> -users = User.objects.filter(Q(groups__permissions=perm) 
> | Q(user_permissions=perm) ).distinct()
> +users = User.objects.filter(Q(groups__permissions=perm) 
> | Q(user_permissions=perm) | Q(is_superuser=True)).distinct()

I forgot to mention this earlier - can you make this a separate patch? It's not
directly related. (It was mostly intentional, but I can see that it's a 
permission
and therefore it can be argued that a superuser should logically have it, so
I'm OK with it.)

Thanks,
Paul

-- 

Paul Eggleton
Intel Open Source Technology Centre
-- 
___
yocto mailing list
yocto@yoctoproject.org
https://lists.yoctoproject.org/listinfo/yocto


[yocto] [layerindex-web][PATCH v2] Asynchronous email notifications, task execution

2017-08-23 Thread Diana Thayer
This patch adds asynchronous task execution using a Celery backend
and RabbitMQ task queue.

It updates the README to reflect the installation and configuration
of a basic RabbitMQ setup, adds a 'tasks.py' file to contain task
definitions, updates the 'edit_layer_view' function to send
emails to administrators about new and updated layers asynchronously,
and modifies the 'settings.py' to include a default configuration
for a RabbitMQ connection.
---
 README  | 10 +-
 TODO|  1 -
 layerindex/tasks.py | 24 
 layerindex/views.py |  7 +++
 requirements.txt|  1 +
 settings.py |  4 
 6 files changed, 41 insertions(+), 6 deletions(-)
 create mode 100644 layerindex/tasks.py

diff --git a/README b/README
index 62f739d..c7f7409 100644
--- a/README
+++ b/README
@@ -14,6 +14,7 @@ In order to make use of this application you will need:
 * Python 3.4+
 * Django 1.8.x - tested with 1.8.17; newer versions may work, but
   the application has not been tested with 1.9 or newer.
+* RabbitMQ 3.6.x - tested with 3.6.10.
 * For production usage, a web server set up to host Django applications
   (not needed for local-only testing)
 * A database supported by Django (SQLite, MySQL, etc.). Django takes
@@ -41,7 +42,9 @@ Setup instructions:
 1. Edit settings.py to specify a database, EMAIL_HOST, SECRET_KEY and
other settings specific to your installation. Ensure you set
LAYER_FETCH_DIR to an absolute path to a location with sufficient
-   space for fetching layer repositories.
+   space for fetching layer repositories. Modify RABBIT_BROKER
+   and RABBIT_BACKEND to reflect the settings used by your RabbitMQ
+   server.
 
 2. Run the following commands within the layerindex-web directory to
initialise the database:
@@ -64,6 +67,11 @@ Setup instructions:
production you need to use a proper web server and have DEBUG set
to False.
 
+   3.1. In order to process asynchronous tasks like sending email,
+you will need to run a Celery worker:
+
+celery -A layerindex.tasks worker --loglevel=info
+
 4. You'll need to add at least the openembedded-core layer to the
database, or some equivalent that contains conf/bitbake.conf for
the base system configuration. To add this, follow these steps:
diff --git a/TODO b/TODO
index 186219f..29986ac 100644
--- a/TODO
+++ b/TODO
@@ -27,7 +27,6 @@ Other
 * Show layer type in layer detail?
 * Usage links in list page?
 * Subdirs in list page?
-* Prevent SMTP failures from breaking submission process
 * Query backend service i.e. special URL to query information for external 
apps/scripts
 * Add comparison to duplicates page
 * Create simple script to check for unlisted layer subdirectories in all repos
diff --git a/layerindex/tasks.py b/layerindex/tasks.py
new file mode 100644
index 000..de80804
--- /dev/null
+++ b/layerindex/tasks.py
@@ -0,0 +1,24 @@
+from celery import Celery
+from django.core.mail import EmailMessage
+from . import utils
+import os
+import time
+
+try:
+import settings
+except ImportError:
+# not in a full django env, so settings is inaccessible.
+# setup django to access settings.
+utils.setup_django()
+import settings
+
+tasks = Celery('layerindex',
+broker=settings.RABBIT_BROKER,
+backend=settings.RABBIT_BACKEND)
+
+@tasks.task
+def send_email(subject, text_content, from_email=settings.DEFAULT_FROM_EMAIL, 
to_emails=[]):
+# We seem to need to run this within the task
+utils.setup_django()
+msg = EmailMessage(subject, text_content, from_email, to_emails)
+msg.send()
diff --git a/layerindex/views.py b/layerindex/views.py
index 1661cb3..8eac866 100644
--- a/layerindex/views.py
+++ b/layerindex/views.py
@@ -19,7 +19,6 @@ from layerindex.forms import EditLayerForm, 
LayerMaintainerFormSet, EditNoteForm
 from django.db import transaction
 from django.contrib.auth.models import User, Permission
 from django.db.models import Q, Count, Sum
-from django.core.mail import EmailMessage
 from django.template.loader import get_template
 from django.template import Context
 from django.utils.decorators import method_decorator
@@ -28,6 +27,7 @@ from django.contrib import messages
 from reversion.models import Revision
 from . import utils
 from . import simplesearch
+from . import tasks
 import settings
 from django.dispatch import receiver
 import reversion
@@ -163,7 +163,7 @@ def edit_layer_view(request, template_name, 
branch='master', slug=None):
 # Send email
 plaintext = get_template('layerindex/submitemail.txt')
 perm = Permission.objects.get(codename='publish_layer')
-users = User.objects.filter(Q(groups__permissions=perm) | 
Q(user_permissions=perm) ).distinct()
+users = User.objects.filter(Q(groups__permissions=perm) | 
Q(user_permissions=perm) | Q(is_superuser=True)).distinct()
 for user in users: