-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding celery to manage clone imprinting as a background task
- Loading branch information
Showing
9 changed files
with
157 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ | |
# Register blueprints | ||
from app import routes | ||
app.register_blueprint(routes.clones.bp) | ||
|
||
from app import logging |
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,25 @@ | ||
""" | ||
Logging | ||
------------------------- | ||
Sets up a mail handler for logging errors. | ||
""" | ||
|
||
from app import app | ||
|
||
cfg = app.config | ||
|
||
# Email error messages. | ||
if not app.debug: | ||
import logging | ||
from logging.handlers import SMTPHandler | ||
mail_handler = SMTPHandler( | ||
(cfg['MAIL_HOST'], cfg['MAIL_PORT']), | ||
cfg['MAIL_USER'], | ||
cfg['MAIL_TARGETS'], | ||
'CLONES IN TROUBLE', | ||
(cfg['MAIL_USER'], cfg['MAIL_PASS']), | ||
secure=()) | ||
mail_handler.setLevel(logging.ERROR) | ||
app.logger.addHandler(mail_handler) | ||
|
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 was deleted.
Oops, something went wrong.
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,55 @@ | ||
""" | ||
Tasks | ||
------------------------- | ||
Configure worker tasks. | ||
""" | ||
|
||
from app.models import Clone | ||
from celery import Celery | ||
import config | ||
|
||
# For sending mail. | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
celery = Celery() | ||
celery.config_from_object(config) | ||
|
||
|
||
@celery.task | ||
def imprint(username): | ||
""" | ||
Imprint a user's Twitter activity onto the clone. | ||
""" | ||
c = Clone.objects.get(username=username) | ||
c.imprint() | ||
c.imprinting = False | ||
c.save() | ||
notify.delay('Imprinting complete', 'Imprinting complete for user {0}'.format(username)) | ||
|
||
|
||
@celery.task | ||
def notify(subject, body): | ||
""" | ||
Send an e-mail notification. | ||
""" | ||
from_addr = config.MAIL_USER | ||
|
||
# Construct the message. | ||
msg = MIMEMultipart() | ||
msg['From'] = from_addr | ||
msg['Subject'] = subject | ||
msg.attach(MIMEText(body, 'plain')) | ||
|
||
# Connect to the mail server. | ||
server = smtplib.SMTP(config.MAIL_HOST, config.MAIL_PORT) | ||
server.starttls() | ||
server.login(from_addr, config.MAIL_PASS) | ||
|
||
for target in config.MAIL_TARGETS: | ||
msg['To'] = target | ||
server.sendmail(from_addr, target, msg.as_string()) | ||
|
||
server.quit() |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
CSRF_ENABLED = True | ||
SECRET_KEY = 'some-passphrase' | ||
MONGODB_SETTINGS = {'DB': 'youtwo'} | ||
MONGODB_SETTINGS = { | ||
'DB': 'youtwo', | ||
'HOST': 'localhost' | ||
|
||
# If necessary: | ||
#'USERNAME': 'username', | ||
#'PASSWORD': 'pw' | ||
} | ||
|
||
MAIL_HOST = 'smtp.gmail.com' | ||
MAIL_PORT = 587 | ||
|
@@ -27,3 +34,44 @@ | |
'VB', | ||
'RB' # adverbs | ||
] | ||
|
||
# Celery config. | ||
# Broker (message queue) url. | ||
BROKER_URL = 'amqp://guest@localhost:5672//' | ||
|
||
# Try connecting ad infinitum. | ||
BROKER_CONNECTION_MAX_RETRIES = None | ||
|
||
# Result backend. | ||
CELERY_RESULT_BACKEND = 'mongodb' | ||
CELERY_MONGODB_BACKEND_SETTINGS = { | ||
'host': 'localhost', | ||
'port': 27017, | ||
'database': 'celery', | ||
'taskmeta_collection': 'my_taskmeta' # Collection name to use for task output | ||
} | ||
|
||
# What modules to import on start. | ||
# Note that in production environments you will want to | ||
# remove the 'tests' tasks module. | ||
CELERY_IMPORTS = ('app.tasks',) | ||
|
||
# Send emails on errors | ||
CELERY_SEND_TASK_ERROR_EMAILS = True | ||
ADMINS = [ | ||
('Francis Tseng', '[email protected]') | ||
] | ||
|
||
SERVER_EMAIL = '[email protected]' | ||
EMAIL_HOST = 'smtp.gmail.com' | ||
EMAIL_PORT = 587 | ||
EMAIL_HOST_USER = '[email protected]' | ||
EMAIL_HOST_PASSWORD = 'your-pass' | ||
EMAIL_USE_TLS = True | ||
|
||
# Setting a maximum amount of tasks per worker | ||
# so the worker processes get regularly killed | ||
# (to reclaim memory). Not sure if this is the best | ||
# approach, but see: | ||
# https://github.com/publicscience/argos/issues/112 | ||
CELERYD_MAX_TASKS_PER_CHILD=100 |
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