1
1
""" Server metrics upload.
2
2
"""
3
- # -*- coding: utf-8 -*-
4
-
3
+ import asyncio
5
4
import os
6
5
import platform
7
6
import time
12
11
from aleph_message .status import MessageStatus
13
12
14
13
from aleph .sdk .chains .ethereum import get_fallback_account
15
- from aleph .sdk .client import AuthenticatedAlephClientSync , AuthenticatedAlephHttpClient
14
+ from aleph .sdk .client import AuthenticatedAlephHttpClient
16
15
from aleph .sdk .conf import settings
17
16
17
+ # -*- coding: utf-8 -*-
18
+
18
19
19
20
def get_sysinfo ():
20
21
uptime = int (time .time () - psutil .boot_time ())
@@ -53,10 +54,12 @@ def get_cpu_cores():
53
54
return [c ._asdict () for c in psutil .cpu_times_percent (0 , percpu = True )]
54
55
55
56
56
- def send_metrics (
57
- session : AuthenticatedAlephClientSync , metrics
57
+ async def send_metrics (
58
+ session : AuthenticatedAlephHttpClient , metrics
58
59
) -> Tuple [AlephMessage , MessageStatus ]:
59
- return session .create_aggregate (key = "metrics" , content = metrics , channel = "SYSINFO" )
60
+ return await session .create_aggregate (
61
+ key = "metrics" , content = metrics , channel = "SYSINFO"
62
+ )
60
63
61
64
62
65
def collect_metrics ():
@@ -68,17 +71,17 @@ def collect_metrics():
68
71
}
69
72
70
73
71
- def main ():
74
+ async def main ():
72
75
account = get_fallback_account ()
73
- with AuthenticatedAlephHttpClient (
76
+ async with AuthenticatedAlephHttpClient (
74
77
account = account , api_server = settings .API_HOST
75
78
) as session :
76
79
while True :
77
80
metrics = collect_metrics ()
78
- message , status = send_metrics (session , metrics )
81
+ message , status = await send_metrics (session , metrics )
79
82
print ("sent" , message .item_hash )
80
83
time .sleep (10 )
81
84
82
85
83
86
if __name__ == "__main__" :
84
- main ()
87
+ asyncio . run ( main () )
0 commit comments