Skip to content

Commit

Permalink
Use celery to send emails in background
Browse files Browse the repository at this point in the history
  • Loading branch information
evantey14 committed May 8, 2017
1 parent c24ae2c commit 1a4d362
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ Now, on your local machine, you should be able to navigate to
`http://localhost:5000/admin` and login with the username "admin" with the
password "admin".

If you'd like to enable sending emails, you'll also need to run a celery worker
with:

```bash
celery -A gavel:celery worker
```

**While developing, you should keep `vagrant rsync-auto` running on the host
machine so that whenever you change any files, they're automatically synced
over to the VM.** When the app running in the VM detects changed files, it'll
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
worker: celery -A gavel:celery worker
web: python initialize.py && gunicorn gavel:app
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ gavel` (unless you're using a different database name). Before you use the app,
you need to initialize the database by running `python initialize.py`. **Note
that Gavel does not preserve database schema compatibility between versions.**

In order to send emails, you'll need to install Redis.

When testing, you can run the app with `python runserver.py`. In production,
you should use something like [Gunicorn][gunicorn] to serve this. You can run
the app with `gunicorn -b :<PORT> -w <number of workers> gavel:app`.

For sending emails, you'll also need to start a celery worker with `celery -A
gavel:celery worker`.

## Configuration

Before starting the app, copy `config.template.yaml` to `config.yaml` and set
Expand Down
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Vagrant.configure(2) do |config|
apt-get install -y \
postgresql-9.4 postgresql-server-dev-9.4 \
redis-server \
python3-dev python3-pip
pip3 install --upgrade pip
Expand Down
13 changes: 12 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{
"addons": [
"heroku-postgresql:hobby-dev"
"heroku-postgresql:hobby-dev",
"heroku-redis:hobby-dev"
],
"formation": {
"web": {
"quantity": 1,
"size": "free"
},
"worker": {
"quantity": 1,
"size": "free"
}
},
"description": "An awesome judging system for hackathons",
"env": {
"ADMIN_PASSWORD": "change-this-before-deploying",
Expand Down
4 changes: 4 additions & 0 deletions config.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ secret_key: null
# defaults to 'postgresql://localhost/gavel'
db_uri: null

# can also be specified as the 'REDIS_URL' or 'BROKER_URI' environment variable
# defaults to 'redis://localhost:6379/0'
broker_uri: null

# can also be specified as the 'PORT' environment variable
# defaults to '5000'
port: null
Expand Down
5 changes: 5 additions & 0 deletions gavel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
)
assets.register('scss_all', scss)

from celery import Celery
app.config['CELERY_BROKER_URL'] = settings.BROKER_URI
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

from gavel.models import db
db.app = app
db.init_app(app)
Expand Down
2 changes: 1 addition & 1 deletion gavel/controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,4 @@ def email_invite_links(annotators):
body = '\n\n'.join(utils.get_paragraphs(raw_body))
emails.append((annotator.email, settings.EMAIL_SUBJECT, body))

utils.send_emails(emails)
utils.send_emails.delay(emails)
1 change: 1 addition & 0 deletions gavel/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def _list(item):
BASE_URL = c.get('base_url', 'BASE_URL')
ADMIN_PASSWORD = c.get('admin_password', 'ADMIN_PASSWORD')
DB_URI = c.get('db_uri', ['DATABASE_URL', 'DB_URI'], default='postgresql://localhost/gavel')
BROKER_URI = c.get('broker_uri', ['REDIS_URL', 'BROKER_URI'], default='redis://localhost:6379/0')
SECRET_KEY = c.get('secret_key', 'SECRET_KEY')
PORT = int(c.get('port', 'PORT', default=5000))
MIN_VIEWS = int(c.get('min_views', 'MIN_VIEWS', default=2))
Expand Down
2 changes: 2 additions & 0 deletions gavel/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from gavel import celery
import gavel.settings as settings
import gavel.crowd_bt as crowd_bt
import gavel.constants as constants
Expand Down Expand Up @@ -50,6 +51,7 @@ def get_paragraphs(message):
paragraphs = [i.replace('\n', ' ') for i in paragraphs if i]
return paragraphs

@celery.task
def send_emails(emails):
'''
Send a batch of emails.
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
appdirs==1.4.3
celery==4.0.2
click==6.7
Flask==0.12
Flask-Assets==0.12
Expand All @@ -15,6 +16,7 @@ psycopg2==2.7.1
pyparsing==2.2.0
pyScss==1.3.5
PyYAML==3.12
redis==2.10.5
requests==2.13.0
scipy==0.19.0
six==1.10.0
Expand Down

0 comments on commit 1a4d362

Please sign in to comment.