forked from kiri-art/docker-diffusers-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.py
More file actions
109 lines (84 loc) · 2.59 KB
/
send.py
File metadata and controls
109 lines (84 loc) · 2.59 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
import json
import os
import datetime
import time
import requests
import hashlib
from requests_futures.sessions import FuturesSession
print()
environ = os.environ.copy()
for key in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "HF_AUTH_TOKEN"]:
if environ.get(key, None):
environ[key] = "XXX"
print(environ)
print()
def get_now():
return round(time.time() * 1000)
send_url = os.getenv("SEND_URL")
if send_url == "":
send_url = None
sign_key = os.getenv("SIGN_KEY")
if sign_key == "":
sign_key = None
futureSession = FuturesSession()
with open("/proc/self/mountinfo") as file:
line = file.readline().strip()
while line:
if "/docker/containers/" in line:
container_id = line.split("/docker/containers/")[
-1
] # Take only text to the right
container_id = container_id.split("/")[0] # Take only text to the left
break
line = file.readline().strip()
init_used = False
def clearSession(force=False):
global session
global init_used
if init_used or force:
session = {"_ctime": get_now()}
else:
init_used = True
def getTimings():
timings = {}
for key in session.keys():
if key == "_ctime":
continue
start = session[key].get("start", None)
done = session[key].get("done", None)
if start and done:
timings.update({key: session[key]["done"] - session[key]["start"]})
else:
timings.update({key: -1})
return timings
def send(type: str, status: str, payload: dict = {}):
now = get_now()
if status == "start":
session.update({type: {"start": now, "last_time": now}})
elif status == "done":
session[type].update({"done": now, "diff": now - session[type]["start"]})
else:
session[type]["last_time"] = now
data = {
"type": type,
"status": status,
"container_id": container_id,
"time": now,
"t": now - session["_ctime"],
"tsl": now - session[type]["last_time"],
"payload": payload,
}
if send_url:
input = json.dumps(data, separators=(",", ":")) + sign_key
sig = hashlib.md5(input.encode("utf-8")).hexdigest()
data["sig"] = sig
print(datetime.datetime.now(), data)
if send_url:
futureSession.post(send_url, json=data)
# try:
# requests.post(send_url, json=data) # , timeout=0.0000000001)
# except requests.exceptions.ReadTimeout:
# except requests.exceptions.RequestException as error:
# print(error)
# pass
clearSession(True)