PyTech Warsaw - 06.12.2018
Przemek Lewandowski
@haxoza
PyWaw (pywaw.org)
PyLightWaw (pylight.org)
You know a little bit about:
A single unit of work to be executed asynchronously.
@app.task
def send_welcome_email(user_id, context):
user = User.objects.get(id=user_id)
email_address = user.get_email()
WelcomeEmail(context=context).send(to=email_address)
@app.task
def process_document(document_id):
document = Document.objects.get(pk=document_id)
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
@app.task
def process_document(document_id):
document = Document.objects.get(pk=document_id)
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration
sentry_sdk.init(integrations=[CeleryIntegration()])
@app.task
def process_document(document_id):
document = Document.objects.get(pk=document_id)
try:
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
except Exception as e:
document.status = Document.Status.failed
document.save()
raise
@app.task
def process_document(document_id):
document = Document.objects.get(pk=document_id)
try:
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
except Exception as e:
document.set_status_failed()
raise
class DocumentManager(models.Manager):
@transaction.atomic
def create_and_process(self, document):
from .tasks import process_document
document.save()
transaction.on_commit(
lambda: process_document.delay(document.pk),
)
return document
@app.task(
autoretry_for=(RequestException,),
retry_kwargs={'max_retries': 5},
)
def process_document(document_id):
document = Document.objects.get(pk=document_id)
try:
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
except Exception as e:
document.set_status_failed()
raise
@app.task(
autoretry_for=(RequestException,),
retry_kwargs={'max_retries': 5},
)
def process_document(document_id):
document = Document.objects.get(pk=document_id)
try:
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
except Exception as e:
document.set_status_failed()
raise
CELERY_ROUTES = {
'tasks.process_document': {'queue': 'processing_queue'},
'tasks.send_welcome_email': {'queue': 'quick_queue'},
}
logger = get_task_logger(__name__)
@app.task(
autoretry_for=(RequestException,),
retry_kwargs={'max_retries': 5},
)
def process_document(document_id):
document = Document.objects.get(pk=document_id)
try:
process_document_service = ProcessDocumentService.create(
language=document.language,
logger=logger,
)
process_document_service.process(document)
logger.info("Processing succeed: {0}".format(repr(document)))
except Exception as e:
document.set_status_failed()
logger.exception("Processing failed: {0}".format(repr(document)))
raise
Processing options:
@app.task
def process_document(document_id):
document = Document.objects.get(pk=document_id)
process_document_service = ProcessDocumentService.create(
document.language,
)
process_document_service.process(document)
@app.task
def process_document():
documents = Document.objects.filter(
status=Document.Status.new
)
process_document_service = ProcessDocumentService.create()
for document in documents:
process_document_service.process(document)