Compare commits
90
Commits
056fe85d7c
...
v1.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aeee735661 | ||
|
|
c89cdd2653 | ||
|
|
375048b959 | ||
|
|
118933af10 | ||
|
|
1939449a8f | ||
|
|
89cb193fe3 | ||
|
|
a3a692f2be | ||
|
|
be69763f48 | ||
|
|
d13e069287 | ||
|
|
bc0d2aced7 | ||
|
|
d872cfde43 | ||
|
|
7a396a88fb | ||
|
|
238188f4a5 | ||
|
|
63975169c0 | ||
|
|
387986e2d8 | ||
|
|
d5bcf04e61 | ||
|
|
12ccd1e586 | ||
|
|
e3fadbc22c | ||
|
|
ecfddfc69e | ||
|
|
caeace7907 | ||
|
|
a456ccc536 | ||
|
|
ae26182e23 | ||
|
|
652fe9af3a | ||
|
|
1760fc8136 | ||
|
|
cffcac8b37 | ||
|
|
907958484c | ||
|
|
9a1724742a | ||
|
|
20023ca9a9 | ||
|
|
478d258a31 | ||
|
|
eb8b1456ab | ||
|
|
4c99b6a1af | ||
|
|
0becddf81f | ||
|
|
233c0ef977 | ||
|
|
11e3c866de | ||
|
|
ce43e1fa38 | ||
|
|
9c9722615f | ||
|
|
9ad983cc1c | ||
|
|
10821f94df | ||
|
|
550551c4a0 | ||
|
|
5709c7152a | ||
|
|
a8b27d8f21 | ||
|
|
2cd449e130 | ||
|
|
91b22a781c | ||
|
|
6f7201054b | ||
|
|
697973a454 | ||
|
|
77c749cfb8 | ||
|
|
aa358bd361 | ||
|
|
3ecea06979 | ||
|
|
6a48b900c8 | ||
|
|
4fff9100e2 | ||
|
|
a8dae35cd0 | ||
|
|
b62fb0c4fa | ||
|
|
2d9b06be5d | ||
|
|
5280cd1d8b | ||
|
|
e0ac9bb0cc | ||
|
|
c3203f024b | ||
|
|
65621ed484 | ||
|
|
29dd4543c3 | ||
|
|
ef416de1a7 | ||
|
|
d16c9aa5bc | ||
|
|
455d82d0f0 | ||
|
|
182a2f70b6 | ||
|
|
a81e2a89c9 | ||
|
|
2041d42e76 | ||
|
|
c7c3631d8d | ||
|
|
2cdc184a1b | ||
|
|
2394bb856d | ||
|
|
65a693706a | ||
|
|
1f716aeb21 | ||
|
|
2889601bc3 | ||
|
|
1341fc5597 | ||
|
|
0b4413ce9c | ||
|
|
a387e5894f | ||
|
|
9668646a05 | ||
|
|
e6f0242775 | ||
|
|
35e40d7b98 | ||
|
|
0a2fc865b1 | ||
|
|
212f8179d5 | ||
|
|
7d5d2f52bd | ||
|
|
ed70b9808b | ||
|
|
cf1aab7caf | ||
|
|
e39c85327f | ||
|
|
3f6e15c6e1 | ||
|
|
3c1d93726e | ||
|
|
4ccb357f7c | ||
|
|
cfbf544110 | ||
|
|
5e5fb31767 | ||
|
|
d4e5728015 | ||
|
|
a9818001d5 | ||
|
|
b51460b696 |
@@ -0,0 +1,30 @@
|
||||
name: pipeline
|
||||
run-name: ${{ gitea.actor }} is running script
|
||||
runs-on: ubuntu-latest
|
||||
on:
|
||||
push:
|
||||
branches-ignore:
|
||||
- "release"
|
||||
image: python:3.10
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up Python 3.10
|
||||
uses: https://github.com/actions/setup-python@v3
|
||||
with:
|
||||
python-version: "3.10"
|
||||
architecture: "x64"
|
||||
- name: Install Packages
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8 mypy pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: PEP8 check
|
||||
run: flake8 ./code --benchmark
|
||||
- name: Type check
|
||||
run: |
|
||||
python3 -m pip install types-python-dateutil
|
||||
mypy ./code
|
||||
@@ -1,26 +0,0 @@
|
||||
name: Python application
|
||||
run-name: ${{ gitea.actor }} is building the application
|
||||
on: [push]
|
||||
image: python:3.10
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up Python 3.10
|
||||
uses: https://github.com/actions/setup-python@v3
|
||||
with:
|
||||
python-version: "3.10"
|
||||
architecture: "x64"
|
||||
- name: Install Dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8 pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
flake8 ./code
|
||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
||||
# flake8 ./code --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||
@@ -0,0 +1,54 @@
|
||||
name: Docker
|
||||
run-name: Build and Publish Service
|
||||
runs-on: ubuntu-latest
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up Python 3.10
|
||||
uses: https://github.com/actions/setup-python@v3
|
||||
with:
|
||||
python-version: "3.10"
|
||||
architecture: "x64"
|
||||
- name: Install Packages
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8 mypy pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: PEP8 check
|
||||
run: flake8 ./code --benchmark
|
||||
- name: Type check
|
||||
run: |
|
||||
python3 -m pip install types-python-dateutil
|
||||
mypy ./code
|
||||
publish:
|
||||
name: Publish
|
||||
runs-on: ubuntu-latest
|
||||
needs: [test]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
-
|
||||
name: Set Environment Variables
|
||||
id: vars
|
||||
env:
|
||||
repository: ${{gitea.repository}}
|
||||
ref_name: ${{gitea.ref_name}}
|
||||
run: |
|
||||
echo $repository
|
||||
echo ::set-output name=OWNER::${repository#/*}
|
||||
echo ::set-output name=PROJECT::${repository#*/}
|
||||
echo $ref_name
|
||||
echo ::set-output name=TAG::${ref_name#refs/*/}
|
||||
-
|
||||
name: Push Image
|
||||
uses: docker/build-push-action@v2
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
tags: 192.168.1.2:5000/${{gitea.repository}}:${{steps.vars.outputs.TAG}}
|
||||
@@ -1,3 +1,21 @@
|
||||
# bandwidth_probing
|
||||
|
||||
Service that runs in a container and measures network bandwidth at a specified interval. The results are sent to a MongoDB database. Logs are sent to discord channel.
|
||||
|
||||
## Environment Variables
|
||||
|
||||
```python
|
||||
LOGGER_LEVEL: Literal['debug', 'info', 'warning', 'error', 'critical'] = 'debug'
|
||||
USER_ID: int = 0 # id number 0 is reserved for testing
|
||||
REPLICATES: int = 3 # number of replicates to be measured per event
|
||||
TRIGGER_INTERVAL_SECONDS: float = 3600 # seconds between events
|
||||
DATA_QUEUE_FLUSH_INTERVAL_SECONDS: float = 600 # seconds between attempts to push data stuck in data queue
|
||||
|
||||
DB_IP_ADDRESS: str = '192.168.1.2'
|
||||
DB_NAME: str = 'bandwidth_probing'
|
||||
DB_COLLECTION_NAME: str = 'data'
|
||||
|
||||
DISCORD_SERVICE_NAME: str = 'bandwidth_probing
|
||||
DISCORD_WEBHOOK_URL: str = 'https://discord.com/api/webhooks/1127970367047225354/wNkoRex4OncMw11OCJg6atR_xgHS2VhLrku4jYIUw81Kr4sutdD3Tt-XAltG0UQy14rZ'
|
||||
DISCORD_LOGGER_LEVEL: Literal['debug', 'info', 'warning', 'error', 'critical'] = 'warning'
|
||||
```
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
from speedtest import Speedtest
|
||||
from dotenv import load_dotenv
|
||||
from typing import List
|
||||
import logging
|
||||
|
||||
|
||||
def measure():
|
||||
@@ -13,6 +13,7 @@ def measure():
|
||||
s.download(threads=None)
|
||||
s.upload(threads=None)
|
||||
s.results.share()
|
||||
logging.debug('finished')
|
||||
return s.results.dict()
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
from cron_converter import Cron
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import dateutil
|
||||
import time
|
||||
import functools
|
||||
import logging
|
||||
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
SCHEDULE = config.get('cron', 'schedule')
|
||||
TZ = config.get('cron', 'tz')
|
||||
|
||||
|
||||
def cronjob(
|
||||
schedule: str = SCHEDULE,
|
||||
tz: str = TZ
|
||||
):
|
||||
"""
|
||||
decorator for running function as a cronjob.
|
||||
"""
|
||||
logging.info(f'started cronjob with schedule {schedule} and tz {tz}')
|
||||
|
||||
def decorator_cronjob(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_cronjob(*args, **kwargs):
|
||||
scheduler = Cron(schedule).schedule(timezone_str=tz)
|
||||
trigger_time = scheduler.next()
|
||||
while True:
|
||||
while trigger_time > datetime.now(tz=dateutil.tz.gettz(tz)):
|
||||
# waiting loop
|
||||
time.sleep(1)
|
||||
# actual execution
|
||||
try:
|
||||
func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
logging.warning(f'failed executing {func}: {e}')
|
||||
raise
|
||||
else:
|
||||
# update trigger time
|
||||
trigger_time = scheduler.next()
|
||||
return wrapper_cronjob
|
||||
return decorator_cronjob
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@cronjob(schedule='* * * * *', tz=TZ)
|
||||
def test_loop(msg: str):
|
||||
print(msg)
|
||||
|
||||
test_loop('hello world')
|
||||
+31
-12
@@ -1,31 +1,50 @@
|
||||
from pymongo import MongoClient
|
||||
from dotenv import load_dotenv
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
import logging
|
||||
import os
|
||||
|
||||
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
DB_IP_ADDRESS = config.get('database', 'db_ip_address')
|
||||
DB_NAME = config.get('database', 'db_name')
|
||||
DB_COLLECTION_NAME = config.get('database', 'db_collection_name')
|
||||
|
||||
|
||||
def connect(
|
||||
ip_addr: str | None = None,
|
||||
db_name: str | None = None,
|
||||
collection_name: str | None = None
|
||||
ip_addr: str = DB_IP_ADDRESS,
|
||||
db_name: str = DB_NAME,
|
||||
collection_name: str = DB_COLLECTION_NAME
|
||||
):
|
||||
"""
|
||||
Connect to MongoDB database and return collection.
|
||||
"""
|
||||
# ensure variables are set
|
||||
if ip_addr is None:
|
||||
ip_addr = os.getenv('DB_IP_ADDRESS')
|
||||
if db_name is None:
|
||||
db_name = os.getenv('DB_NAME')
|
||||
if collection_name is None:
|
||||
collection_name = os.getenv('DB_COLLECTION_NAME')
|
||||
# load environment variables
|
||||
load_dotenv()
|
||||
ip_addr = os.getenv(
|
||||
'DB_IP_ADDRESS',
|
||||
default=DB_IP_ADDRESS
|
||||
)
|
||||
db_name = os.getenv(
|
||||
'DB_NAME',
|
||||
default=DB_NAME
|
||||
)
|
||||
collection_name = os.getenv(
|
||||
'DB_COLLECTION_NAME',
|
||||
default=DB_COLLECTION_NAME
|
||||
)
|
||||
# connect to database
|
||||
client = MongoClient(ip_addr)
|
||||
client: MongoClient = MongoClient(ip_addr)
|
||||
db = client[db_name]
|
||||
collection = db[collection_name]
|
||||
logging.debug('finished')
|
||||
return collection
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_dotenv()
|
||||
collection = connect()
|
||||
print(collection)
|
||||
print(collection)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
[main]
|
||||
logger_level=debug
|
||||
user_id=0
|
||||
replicates=3
|
||||
queue_flush_interval_seconds=600
|
||||
|
||||
[database]
|
||||
db_ip_address=192.168.1.2
|
||||
db_name=bandwidth_probing
|
||||
db_collection_name=data
|
||||
|
||||
[discord]
|
||||
webhook_url=https://discord.com/api/webhooks/1127970367047225354/wNkoRex4OncMw11OCJg6atR_xgHS2VhLrku4jYIUw81Kr4sutdD3Tt-XAltG0UQy14rZ
|
||||
service_name=bandwidth_probing
|
||||
logger_level=warning
|
||||
|
||||
[cron]
|
||||
schedule=* * * * *
|
||||
tz=Europe/Paris
|
||||
+47
-31
@@ -1,55 +1,71 @@
|
||||
from discord_logging.handler import DiscordHandler
|
||||
from dotenv import load_dotenv
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
import logging
|
||||
import os
|
||||
|
||||
|
||||
def initialise_app():
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
LOGGER_LEVEL = config.get('main', 'logger_level')
|
||||
DISCORD_SERVICE_NAME = config.get('discord', 'service_name')
|
||||
DISCORD_WEBHOOK_URL = config.get('discord', 'webhook_url')
|
||||
DISCORD_LOGGER_LEVEL = config.get('discord', 'logger_level')
|
||||
|
||||
|
||||
def initialise_app(
|
||||
logger_level: str = LOGGER_LEVEL,
|
||||
discord_service_name: str = DISCORD_SERVICE_NAME,
|
||||
discord_webhook_url: str = DISCORD_WEBHOOK_URL,
|
||||
discord_logger_level: str = DISCORD_LOGGER_LEVEL
|
||||
) -> None:
|
||||
"""
|
||||
Convienience function that ensures eveything is ready before running main loop.
|
||||
Convienience function that ensures eveything is ready
|
||||
before running the main loop.
|
||||
"""
|
||||
# load environment variables from file
|
||||
# load environment variables
|
||||
load_dotenv()
|
||||
assert (
|
||||
'USER_ID' in os.environ
|
||||
), (
|
||||
'environment variable USER_ID must be specified'
|
||||
)
|
||||
logger_level = os.getenv(
|
||||
'LOGGER_LEVEL',
|
||||
default=LOGGER_LEVEL
|
||||
)
|
||||
discord_service_name = os.getenv(
|
||||
'DISCORD_SERVICE_NAME',
|
||||
default=DISCORD_SERVICE_NAME
|
||||
)
|
||||
discord_webhook_url = os.getenv(
|
||||
'DISCORD_WEBHOOK_URL',
|
||||
default=DISCORD_WEBHOOK_URL
|
||||
)
|
||||
discord_logger_level = os.getenv(
|
||||
'DISCORD_LOGGER_LEVEL',
|
||||
default=DISCORD_LOGGER_LEVEL
|
||||
)
|
||||
# setup logging
|
||||
fmt = '|%(asctime)s| %(levelname)s %(filename)s:%(funcName)s:%(message)s'
|
||||
datefmt = '%Y-%m-%d %H:%M:%S'
|
||||
logger_level = os.getenv('LOGGER_LEVEL', default=None)
|
||||
if logger_level is None:
|
||||
logger_level = 'info'
|
||||
logging.info(f'environment variable LOGGER_LEVEL not set. Using default (info)')
|
||||
level = getattr(logging, logger_level.upper())
|
||||
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
|
||||
# setup logging to discord
|
||||
discord_url = os.getenv('DISCORD_URL', default=None)
|
||||
assert discord_url is not None
|
||||
logger = logging.getLogger()
|
||||
discord_handler = DiscordHandler(
|
||||
service_name = 'bandwidth_probing',
|
||||
webhook_url = discord_url,
|
||||
service_name=discord_service_name,
|
||||
webhook_url=discord_webhook_url,
|
||||
)
|
||||
discord_handler.setFormatter(logging.Formatter('%(message)s'))
|
||||
discord_handler.setLevel(logging.WARNING)
|
||||
level = getattr(logging, discord_logger_level.upper())
|
||||
discord_handler.setLevel(level=level)
|
||||
logger.addHandler(discord_handler)
|
||||
# ensure environment variables are set
|
||||
necessary_list = [
|
||||
'DB_IP_ADDRESS',
|
||||
'DB_NAME',
|
||||
'DB_COLLECTION_NAME'
|
||||
]
|
||||
for var in necessary_list:
|
||||
assert var in os.environ, f'environment variable {var} not set.'
|
||||
# set defaults if not yet handled
|
||||
default_dict = {
|
||||
'USER_ID': 0,
|
||||
'REPLICATES': 3,
|
||||
'TRIGGER_INTERVAL_SECONDS': 60
|
||||
}
|
||||
for k, v in default_dict.items():
|
||||
if not k in os.environ:
|
||||
logging.info(f'environment variable {k} not set. Using default ({v}).')
|
||||
os.environ[k] = str(v)
|
||||
logging.debug('finished')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
initialise_app()
|
||||
logging.warning('test message')
|
||||
logging.warning('test message')
|
||||
|
||||
+124
-32
@@ -1,51 +1,143 @@
|
||||
import time
|
||||
import logging
|
||||
import time
|
||||
import os
|
||||
import json
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
from queue import Queue
|
||||
from initialise_app import initialise_app
|
||||
from cron import cronjob
|
||||
from bandwidth import measure as measure_bandwidth
|
||||
from database import connect
|
||||
|
||||
def event():
|
||||
logging.info('started event')
|
||||
# get env vars
|
||||
replicates = int(os.getenv('REPLICATES'))
|
||||
# setup database connection
|
||||
|
||||
# load default values
|
||||
config = ConfigParser()
|
||||
config.read(Path(__file__).parent / 'defaults.ini')
|
||||
REPLICATES = int(config.get('main', 'replicates'))
|
||||
QUEUE_FLUSH_INTERVAL_SECONDS = float(
|
||||
config.get(
|
||||
'main',
|
||||
'queue_flush_interval_seconds'
|
||||
)
|
||||
)
|
||||
SCHEDULE = config.get('cron', 'schedule')
|
||||
TZ = config.get('cron', 'tz')
|
||||
|
||||
|
||||
def flush_data(
|
||||
data_queue: Queue,
|
||||
user_id: int
|
||||
) -> None:
|
||||
# connect to database
|
||||
db = connect()
|
||||
# run event
|
||||
for rep_num in range(replicates):
|
||||
logging.info(f'running replicate {rep_num}')
|
||||
# do measurement
|
||||
try:
|
||||
res = measure_bandwidth()
|
||||
except Exception as e:
|
||||
logging.error(f'failed to measure bandwidth: {e}')
|
||||
continue
|
||||
# upload result to database
|
||||
# stop early
|
||||
queue_size = data_queue.qsize()
|
||||
if queue_size == 0:
|
||||
logging.debug('queue already empty')
|
||||
logging.debug('finished')
|
||||
return
|
||||
# flush queue
|
||||
for i in range(queue_size):
|
||||
# get data
|
||||
data = data_queue.get()
|
||||
# prepare payload
|
||||
payload_dict = {
|
||||
'user_id': os.getenv('USER_ID'),
|
||||
'data': res
|
||||
'user_id': user_id,
|
||||
'data': data
|
||||
}
|
||||
# send to database
|
||||
try:
|
||||
db_id = db.insert_one(payload_dict).inserted_id
|
||||
except Exception as e:
|
||||
logging.error(f'failed sending results to database: {e}')
|
||||
data_queue.put(data) # put data back in queue
|
||||
logging.error('failed sending data to database')
|
||||
logging.debug(
|
||||
'failed pushing to database:\n'
|
||||
f'{json.dumps(payload_dict, indent=4)}\n'
|
||||
f'with error: {e}'
|
||||
)
|
||||
continue
|
||||
logging.debug(f'data sent to database received db_id: {db_id}')
|
||||
else:
|
||||
logging.debug(f'data sent to database received id: {db_id}')
|
||||
logging.debug('finished')
|
||||
|
||||
|
||||
def event(
|
||||
data_queue: Queue,
|
||||
user_id: int,
|
||||
replicates: int = REPLICATES
|
||||
) -> None:
|
||||
# run event
|
||||
for rep_num in range(replicates):
|
||||
logging.debug(f'running replicate {rep_num+1} of {replicates}')
|
||||
# do measurement
|
||||
try:
|
||||
data = measure_bandwidth()
|
||||
except Exception as e:
|
||||
logging.error(f'failed to measure bandwidth: {e}')
|
||||
continue
|
||||
# add data to queue
|
||||
data_queue.put(data)
|
||||
# polite pause
|
||||
time.sleep(1)
|
||||
logging.info('finished event')
|
||||
# flush queue
|
||||
flush_data(
|
||||
data_queue=data_queue,
|
||||
user_id=user_id
|
||||
)
|
||||
logging.debug('finished')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# setup
|
||||
initialise_app()
|
||||
trigger_time = time.time()
|
||||
while True:
|
||||
if time.time() >= trigger_time:
|
||||
# set new trigger time
|
||||
trigger_time += int(os.getenv('TRIGGER_INTERVAL_SECONDS'))
|
||||
# run event
|
||||
try:
|
||||
event()
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
time.sleep(1)
|
||||
data_queue: Queue = Queue()
|
||||
user_id = int(os.getenv('USER_ID', default=0))
|
||||
replicates = int(os.getenv('REPLICATES', default=REPLICATES))
|
||||
queue_flush_interval = float(
|
||||
os.getenv(
|
||||
'QUEUE_FLUSH_INTERVAL_SECONDS',
|
||||
default=QUEUE_FLUSH_INTERVAL_SECONDS
|
||||
)
|
||||
)
|
||||
schedule = os.getenv(
|
||||
key='SCHEDULE',
|
||||
default=SCHEDULE
|
||||
)
|
||||
tz = os.getenv(
|
||||
key='TZ',
|
||||
default=TZ
|
||||
)
|
||||
|
||||
@cronjob(schedule=schedule, tz=tz)
|
||||
def main_loop(
|
||||
data_queue: Queue,
|
||||
user_id: int,
|
||||
replicates: int = REPLICATES
|
||||
) -> None:
|
||||
# run event
|
||||
for rep_num in range(replicates):
|
||||
logging.debug(f'running replicate {rep_num+1} of {replicates}')
|
||||
# do measurement
|
||||
try:
|
||||
data = measure_bandwidth()
|
||||
except Exception as e:
|
||||
logging.error(f'failed to measure bandwidth: {e}')
|
||||
continue
|
||||
# add data to queue
|
||||
data_queue.put(data)
|
||||
# polite pause
|
||||
time.sleep(1)
|
||||
# flush queue
|
||||
flush_data(
|
||||
data_queue=data_queue,
|
||||
user_id=user_id
|
||||
)
|
||||
logging.debug('finished')
|
||||
# start main loop
|
||||
main_loop(
|
||||
data_queue=data_queue,
|
||||
user_id=user_id,
|
||||
replicates=replicates
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user