File size: 4,902 Bytes
8f59bb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | from django.core.cache import cache
from celery.task import Task
try:
from celery.utils.log import get_task_logger
except ImportError:
from celery.log import get_task_logger
from queued_storage.conf import settings
from queued_storage.signals import file_transferred
from queued_storage.utils import import_attribute
logger = get_task_logger(name=__name__)
class Transfer(Task):
"""
The default task. Transfers a file to a remote location.
The actual transfer is implemented in the remote backend.
To use a different task, pass it into the backend:
.. code-block:: python
from queued_storage.backends import QueuedS3BotoStorage
s3_delete_storage = QueuedS3BotoStorage(
task='queued_storage.tasks.TransferAndDelete')
# later, in model definition:
image = models.ImageField(storage=s3_delete_storage)
The result should be ``True`` if the transfer was successful,
or ``False`` if unsuccessful. In the latter case the task will be
retried.
You can subclass the :class:`~queued_storage.tasks.Transfer` class
to customize the behaviour, to do something like this:
.. code-block:: python
from queued_storage.tasks import Transfer
class TransferAndNotify(Transfer):
def transfer(self, *args, **kwargs):
result = super(TransferAndNotify, self).transfer(*args, **kwargs)
if result:
# call the (imaginary) notify function with the result
notify(result)
return result
"""
#: The number of retries if unsuccessful (default: see
#: :attr:`~queued_storage.conf.settings.QUEUED_STORAGE_RETRIES`)
max_retries = settings.QUEUED_STORAGE_RETRIES
#: The delay between each retry in seconds (default: see
#: :attr:`~queued_storage.conf.settings.QUEUED_STORAGE_RETRY_DELAY`)
default_retry_delay = settings.QUEUED_STORAGE_RETRY_DELAY
def run(self, name, cache_key,
local_path, remote_path,
local_options, remote_options, **kwargs):
"""
The main work horse of the transfer task. Calls the transfer
method with the local and remote storage backends as given
with the parameters.
:param name: name of the file to transfer
:type name: str
:param local_path: local storage class to transfer from
:type local_path: str
:param local_options: options of the local storage class
:type local_options: dict
:param remote_path: remote storage class to transfer to
:type remote_path: str
:param remote_options: options of the remote storage class
:type remote_options: dict
:param cache_key: cache key to set after a successful transfer
:type cache_key: str
:rtype: task result
"""
local = import_attribute(local_path)(**local_options)
remote = import_attribute(remote_path)(**remote_options)
result = self.transfer(name, local, remote, **kwargs)
if result is True:
cache.set(cache_key, True)
file_transferred.send(sender=self.__class__,
name=name, local=local, remote=remote)
elif result is False:
args = [name, cache_key, local_path,
remote_path, local_options, remote_options]
self.retry(args=args, kwargs=kwargs)
else:
raise ValueError("Task '%s' did not return True/False but %s" %
(self.__class__, result))
return result
def transfer(self, name, local, remote, **kwargs):
"""
Transfers the file with the given name from the local to the remote
storage backend.
:param name: The name of the file to transfer
:param local: The local storage backend instance
:param remote: The remote storage backend instance
:returns: `True` when the transfer succeeded, `False` if not. Retries
the task when returning `False`
:rtype: bool
"""
try:
remote.save(name, local.open(name))
return True
except Exception as e:
logger.error("Unable to save '%s' to remote storage. "
"About to retry." % name)
logger.exception(e)
return False
class TransferAndDelete(Transfer):
"""
A :class:`~queued_storage.tasks.Transfer` subclass which deletes the
file with the given name using the local storage if the transfer
was successful.
"""
def transfer(self, name, local, remote, **kwargs):
result = super(TransferAndDelete, self).transfer(name, local,
remote, **kwargs)
if result:
local.delete(name)
return result
|