-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtasks.py
More file actions
35 lines (30 loc) · 1.11 KB
/
tasks.py
File metadata and controls
35 lines (30 loc) · 1.11 KB
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
# Create your tasks here
import time
import logging
from django.core import signals
from datetime import timedelta
from django.utils.timezone import now
from celery import shared_task
from .models.resource import Resource
logger = logging.getLogger(__name__)
@shared_task
def retrieve_resource(data):
signals.request_started.send(sender=data['task_id'])
try:
resource = Resource.objects.get(uuid=data['resource_id'])
if not resource.retrieve():
t = time.time() - resource.created.timestamp()
if t < 3600:
retrieve_resource.apply_async(
args=(data, ),
eta=now() + timedelta(seconds=30))
elif t < 3600 * 12:
retrieve_resource.apply_async(
args=(data, ),
eta=now() + timedelta(seconds=1800))
else:
resource.detach_resource()
except Resource.DoesNotExist:
logger.info("retrieve task not found resource: {}".format(data['resource_id'])) # noqa
finally:
signals.request_finished.send(sender=data['task_id'])