-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtasks.py
More file actions
190 lines (177 loc) · 5.41 KB
/
tasks.py
File metadata and controls
190 lines (177 loc) · 5.41 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Create your tasks here
import time
import uuid
import json
import logging
import requests
from datetime import timedelta
from typing import List, Dict
from django.core import signals
from django.conf import settings
from django.utils.timezone import now
from celery import shared_task
from api.models.resource import Resource
logger = logging.getLogger(__name__)
@shared_task
def retrieve_resource(resource):
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
if not resource.retrieve():
t = time.time() - resource.created.timestamp()
if t < 3600:
retrieve_resource.apply_async(
args=(resource, ),
eta=now() + timedelta(seconds=30))
elif t < 3600 * 12:
retrieve_resource.apply_async(
args=(resource, ),
eta=now() + timedelta(seconds=1800))
else:
resource.detach_resource()
except Resource.DoesNotExist:
logger.exception("retrieve task not found resource: {}".format(resource.id)) # noqa
finally:
signals.request_finished.send(sender=task_id)
@shared_task
def measure_config(config: List[Dict[str, str]]):
"""
[
{
"app_id": "test",
"owner_id": "test",
"container_type": web,
"cpu": "1",
"memory": "2G",
"timestamp": 1609231998.9103732
}
]
"""
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
requests.post(
url="%s/measurements/config/" % settings.WORKFLOW_MANAGER_URL,
headers={
'Content-Type': 'application/json',
'Authorization': 'token %s' % settings.WORKFLOW_MANAGER_TOKEN
},
data=json.dumps(config)
)
except Exception as e:
logger.exception("write influxdb point fail: {}".format(e))
finally:
signals.request_finished.send(sender=task_id)
@shared_task
def measure_volumes(volumes: List[Dict[str, str]]):
"""
[
{
"name": "disk",
"app_id": "test",
"owner_id": "test",
"size": "100G",
"timestamp": "1609231998.9103732"
}
]
"""
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
requests.post(
url="%s/measurements/volumes/" % settings.WORKFLOW_MANAGER_URL,
headers={
'Content-Type': 'application/json',
'Authorization': 'token %s' % settings.WORKFLOW_MANAGER_TOKEN
},
data=json.dumps(volumes)
)
except Exception as e:
logger.exception("write influxdb point fail: {}".format(e))
finally:
signals.request_finished.send(sender=task_id)
@shared_task
def measure_networks(networks: List[Dict[str, str]]):
"""
[
{
"app_id": "test",
"owner_id": "test",
"pod_name": "django2test-web-xxxxxx",
"rx_bytes": "10000",
"tx_bytes": "200000",
"timestamp": "1609231998.9103732"
}
]
"""
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
requests.post(
url="%s/measurements/networks/" % settings.WORKFLOW_MANAGER_URL,
headers={
'Content-Type': 'application/json',
'Authorization': 'token %s' % settings.WORKFLOW_MANAGER_TOKEN
},
data=json.dumps(networks)
)
except Exception as e:
logger.exception("write influxdb point fail: {}".format(e))
finally:
signals.request_finished.send(sender=task_id)
@shared_task
def measure_instances(instances: List[Dict[str, str]]):
"""
[
{
"app_id": "test",
"owner_id": "test",
"container_type": "web",
"container_count": 1,
"timestamp": "1609231998.9103732"
}
]
"""
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
requests.post(
url="%s/measurements/instances/" % settings.WORKFLOW_MANAGER_URL,
headers={
'Content-Type': 'application/json',
'Authorization': 'token %s' % settings.WORKFLOW_MANAGER_TOKEN
},
data=json.dumps(instances)
)
except Exception as e:
logger.exception("write influxdb point fail: {}".format(e))
finally:
signals.request_finished.send(sender=task_id)
@shared_task
def measure_resources(resources: List[Dict[str, str]]):
"""
[
{
"name": "test1",
"app_id": "redis",
"owener_id": "test",
"plan": "redis:small",
"timestamp": "1609231998.9103732"
}
]
"""
task_id = uuid.uuid4().hex
signals.request_started.send(sender=task_id)
try:
requests.post(
url="%s/measurements/resources/" % settings.WORKFLOW_MANAGER_URL,
headers={
'Content-Type': 'application/json',
'Authorization': 'token %s' % settings.WORKFLOW_MANAGER_TOKEN
},
data=json.dumps(resources)
)
except Exception as e:
logger.exception("write influxdb point fail: {}".format(e))
finally:
signals.request_finished.send(sender=task_id)