Skip to content

Commit cb60d2c

Browse files
committed
feat: replaced sequential server posting to async
1 parent eff3f8e commit cb60d2c

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gunicorn==20.0.4
1010

1111
# Posting
1212
requests
13+
aiohttp
1314

1415
# Polling
1516
easysnmp==0.2.5

switchmap/poller/async_poll.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pprint import pprint
66
import os
77
import time
8+
import aiohttp
89

910
# Import app libraries
1011
from switchmap import API_POLLER_POST_URI
@@ -58,14 +59,14 @@ async def devices(max_concurrent_devices=None):
5859
# Semaphore to limit concurrent devices
5960
device_semaphore = asyncio.Semaphore(max_concurrent_devices)
6061

61-
tasks = [
62-
device(argument, device_semaphore, post=True) for argument in arguments
63-
]
64-
65-
# Execute all devices concurrently
66-
start_time = time.time()
67-
results = await asyncio.gather(*tasks, return_exceptions=True)
68-
end_time = time.time()
62+
async with aiohttp.ClientSession() as session:
63+
tasks = [
64+
device(argument, device_semaphore, session, post=True) for argument in arguments
65+
]
66+
# Execute all devices concurrently
67+
start_time = time.time()
68+
results = await asyncio.gather(*tasks, return_exceptions=True)
69+
end_time = time.time()
6970

7071
# Process results and log summary
7172
success_count = sum(1 for r in results if r is True)
@@ -83,7 +84,7 @@ async def devices(max_concurrent_devices=None):
8384
log.log2warning(1403, log_message)
8485

8586

86-
async def device(poll_meta, device_semaphore, post=True):
87+
async def device(poll_meta, device_semaphore, session,post=True):
8788
"""Poll each device asynchoronously.
8889
8990
Args:
@@ -137,13 +138,20 @@ async def device(poll_meta, device_semaphore, post=True):
137138
data = _device.process()
138139
data["misc"]["zone"] = zone
139140

140-
#! do a little research on aiohttp
141141
if post:
142-
rest.post(API_POLLER_POST_URI, data, config)
143-
log_message = (
144-
f"Successfully polled and posted data for {hostname}"
145-
)
146-
log.log2debug(1407, log_message)
142+
try:
143+
async with session.post(API_POLLER_POST_URI, json=data) as res:
144+
if res.status == 200:
145+
log_message = f"Successfully polled and posted data for {hostname}"
146+
log.log2debug(1407, log_message)
147+
else:
148+
log_message = f"Failed to post data for {hostname}, status={res.status}"
149+
log.log2warning(1414, log_message)
150+
except aiohttp.ClientError as e:
151+
log_message = f"HTTP error posting data for {hostname}: {e}"
152+
log.log2exception(1415, log_message)
153+
return False
154+
147155
else:
148156
pprint(data)
149157

@@ -192,8 +200,9 @@ async def cli_device(hostname):
192200
log.log2info(1410, log_message)
193201

194202
# Poll each zone occurrence
203+
semaphore = asyncio.Semaphore(1)
195204
tasks = [
196-
device(argument, asyncio.Semaphore(1), post=False)
205+
device(argument, semaphore, post=False)
197206
for argument in arguments
198207
]
199208
results = await asyncio.gather(*tasks, return_exceptions=True)

switchmap/poller/snmp/async_snmp_info.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ async def misc(self):
7272

7373
# Get vendor information
7474
sysobjectid = await self.snmp_object.sysobjectid()
75-
vendor = iana_enterprise.Query(sysobjectid=sysobjectid)
76-
data["IANAEnterpriseNumber"] = vendor.enterprise()
77-
75+
if sysobjectid:
76+
vendor = iana_enterprise.Query(sysobjectid=sysobjectid)
77+
data["IANAEnterpriseNumber"] = vendor.enterprise()
78+
else:
79+
data["IANAEnterpriseNumber"] = None
80+
7881
return data
7982

8083
async def system(self):

0 commit comments

Comments
 (0)