コード例 #1
0
def submit_attachment(task, attachment):
    """Sends an attachment's file to the conversion service"""
    from indico_conversion.plugin import ConversionPlugin
    if ConversionPlugin.settings.get('maintenance'):
        task.retry(countdown=900)
    url = ConversionPlugin.settings.get('server_url')
    payload = {
        'attachment_id': attachment.id
    }
    data = {
        'converter': 'pdf',
        'urlresponse': url_for_plugin('conversion.callback', _external=True),
        'dirresponse': secure_serializer.dumps(payload, salt='pdf-conversion')
    }
    file = attachment.file
    name, ext = os.path.splitext(file.filename)
    # we know ext is safe since it's based on a whitelist. the name part may be fully
    # non-ascii so we sanitize that to a generic name if necessary
    filename = secure_filename(name, 'attachment') + ext
    with file.open() as fd:
        try:
            response = requests.post(url, data=data, files={'uploadedfile': (filename, fd, file.content_type)})
            response.raise_for_status()
            if 'ok' not in response.text:
                raise requests.RequestException('Unexpected response from server: {}'.format(response.text))
        except requests.RequestException as exc:
            attempt = task.request.retries + 1
            try:
                delay = DELAYS[task.request.retries] if not config.DEBUG else 1
            except IndexError:
                # like this we can safely bump MAX_TRIES manually if necessary
                delay = DELAYS[-1]
            try:
                task.retry(countdown=delay, max_retries=(MAX_TRIES - 1))
            except MaxRetriesExceededError:
                ConversionPlugin.logger.error('Could not submit attachment %d (attempt %d/%d); giving up [%s]',
                                              attachment.id, attempt, MAX_TRIES, exc)
                cache.delete(unicode(attachment.id))
            except Retry:
                ConversionPlugin.logger.warning('Could not submit attachment %d (attempt %d/%d); retry in %ds [%s]',
                                                attachment.id, attempt, MAX_TRIES, delay, exc)
                raise
        else:
            ConversionPlugin.logger.info('Submitted %r', attachment)
コード例 #2
0
def submit_attachment(task, attachment):
    """Sends an attachment's file to the conversion service"""
    from indico_conversion.plugin import ConversionPlugin
    if ConversionPlugin.settings.get('maintenance'):
        task.retry(countdown=900)
    url = ConversionPlugin.settings.get('server_url')
    payload = {'attachment_id': attachment.id}
    data = {
        'converter': 'pdf',
        'urlresponse': url_for_plugin('conversion.callback', _external=True),
        'dirresponse': secure_serializer.dumps(payload, salt='pdf-conversion')
    }
    file = attachment.file
    with file.open() as fd:
        try:
            response = requests.post(
                url,
                data=data,
                files={'uploadedfile': (file.filename, fd, file.content_type)})
            response.raise_for_status()
            if 'ok' not in response.text:
                raise requests.RequestException(
                    'Unexpected response from server: {}'.format(
                        response.text))
        except requests.RequestException as exc:
            attempt = task.request.retries + 1
            delay = (DELAYS +
                     [0])[task.request.retries] if not config.DEBUG else 1
            try:
                task.retry(countdown=delay, max_retries=(MAX_TRIES - 1))
            except MaxRetriesExceededError:
                ConversionPlugin.logger.error(
                    'Could not submit attachment %d (attempt %d/%d); giving up [%s]',
                    attachment.id, attempt, MAX_TRIES, exc)
                cache.delete(unicode(attachment.id))
            except Retry:
                ConversionPlugin.logger.warning(
                    'Could not submit attachment %d (attempt %d/%d); retry in %ds [%s]',
                    attachment.id, attempt, MAX_TRIES, delay, exc)
                raise
        else:
            ConversionPlugin.logger.info('Submitted %r', attachment)