Switched to an eta= based async task
This commit is contained in:
parent
1e2c147f7f
commit
536ec3871c
12
server.py
12
server.py
|
@ -2,8 +2,9 @@ import logging
|
||||||
import uuid
|
import uuid
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
from flask import Flask, abort, request, Response, send_file
|
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from flask import Flask, abort, request, Response, send_file
|
||||||
import pypandoc
|
import pypandoc
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
@ -15,7 +16,6 @@ celery.conf.broker_url = 'redis://redis:6379/0'
|
||||||
celery.conf.worker_concurrency = 2
|
celery.conf.worker_concurrency = 2
|
||||||
@celery.on_after_configure.connect
|
@celery.on_after_configure.connect
|
||||||
def setup_periodic_tasks(sender, **kwargs):
|
def setup_periodic_tasks(sender, **kwargs):
|
||||||
sender.add_periodic_task(10.0, clean_up.s(), name="Cleanup")
|
|
||||||
app.logger.info('Cleanup periodic task setup')
|
app.logger.info('Cleanup periodic task setup')
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,6 +33,9 @@ def convert():
|
||||||
|
|
||||||
job_name = uuid.uuid4().hex
|
job_name = uuid.uuid4().hex
|
||||||
job_dir = tempfile.mkdtemp(suffix='-' + job_name)
|
job_dir = tempfile.mkdtemp(suffix='-' + job_name)
|
||||||
|
later = datetime.utcnow() + timedelta(hours=1)
|
||||||
|
clean_up.apply_async((job_dir, ), eta=later)
|
||||||
|
|
||||||
input_file = request.files['input']
|
input_file = request.files['input']
|
||||||
input_base, input_ext = input_file.filename.rsplit('.')
|
input_base, input_ext = input_file.filename.rsplit('.')
|
||||||
input_path = os.path.join(job_dir, 'input.' + input_ext)
|
input_path = os.path.join(job_dir, 'input.' + input_ext)
|
||||||
|
@ -43,7 +46,7 @@ def convert():
|
||||||
for output in outputs:
|
for output in outputs:
|
||||||
output_filename = '{0}.{1}'.format(input_base, output)
|
output_filename = '{0}.{1}'.format(input_base, output)
|
||||||
output_path = os.path.join(job_dir, output_filename)
|
output_path = os.path.join(job_dir, output_filename)
|
||||||
logger.info('Converting {0} to {1}'.format(input_path, output_path))
|
app.logger.info('Converting {0} to {1}'.format(input_path, output_path))
|
||||||
pypandoc.convert(input_path, output, outputfile=output_path)
|
pypandoc.convert(input_path, output, outputfile=output_path)
|
||||||
results.append({
|
results.append({
|
||||||
'input': input_path,
|
'input': input_path,
|
||||||
|
@ -56,5 +59,6 @@ def convert():
|
||||||
|
|
||||||
|
|
||||||
@celery.task()
|
@celery.task()
|
||||||
def clean_up():
|
def clean_up(path):
|
||||||
|
app.logger.info('Deleting work dir %s', path)
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue