added use of default values
This commit is contained in:
+34
-7
@@ -1,15 +1,34 @@
|
|||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from configparser import ConfigParser
|
||||||
|
from pathlib import Path
|
||||||
from initialise_app import initialise_app
|
from initialise_app import initialise_app
|
||||||
from bandwidth import measure as measure_bandwidth
|
from bandwidth import measure as measure_bandwidth
|
||||||
from database import connect
|
from database import connect
|
||||||
|
|
||||||
|
|
||||||
def event():
|
# load default values
|
||||||
logging.info('started event')
|
config = ConfigParser()
|
||||||
# get env vars
|
config.read(Path(__file__).parent / 'defaults.ini')
|
||||||
replicates = int(os.getenv('REPLICATES'))
|
REPLICATES = int(config.get('main', 'replicates'))
|
||||||
|
USER_ID = int(config.get('main', 'user_id'))
|
||||||
|
TRIGGER_INTERVAL_SECONDS = float(
|
||||||
|
config.get(
|
||||||
|
'main',
|
||||||
|
'trigger_interval_seconds'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def event(
|
||||||
|
replicates: int = REPLICATES,
|
||||||
|
user_id: int = USER_ID
|
||||||
|
):
|
||||||
|
logging.debug('started event')
|
||||||
|
# load environment variables
|
||||||
|
replicates = int(os.getenv('REPLICATES', default=REPLICATES))
|
||||||
|
user_id = int(os.getenv('USER_ID', default=USER_ID))
|
||||||
# setup database connection
|
# setup database connection
|
||||||
db = connect()
|
db = connect()
|
||||||
# run event
|
# run event
|
||||||
@@ -23,7 +42,7 @@ def event():
|
|||||||
continue
|
continue
|
||||||
# upload result to database
|
# upload result to database
|
||||||
payload_dict = {
|
payload_dict = {
|
||||||
'user_id': os.getenv('USER_ID'),
|
'user_id': user_id,
|
||||||
'data': res
|
'data': res
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
@@ -33,7 +52,7 @@ def event():
|
|||||||
continue
|
continue
|
||||||
logging.debug(f'data sent to database received db_id: {db_id}')
|
logging.debug(f'data sent to database received db_id: {db_id}')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
logging.info('finished event')
|
logging.debug('finished')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@@ -42,10 +61,18 @@ if __name__ == '__main__':
|
|||||||
while True:
|
while True:
|
||||||
if time.time() >= trigger_time:
|
if time.time() >= trigger_time:
|
||||||
# set new trigger time
|
# set new trigger time
|
||||||
trigger_time += int(os.getenv('TRIGGER_INTERVAL_SECONDS'))
|
trigger_interval = float(
|
||||||
|
os.getenv(
|
||||||
|
'TRIGGER_INTERVAL_SECONDS',
|
||||||
|
default=TRIGGER_INTERVAL_SECONDS
|
||||||
|
)
|
||||||
|
)
|
||||||
|
trigger_time += trigger_interval
|
||||||
# run event
|
# run event
|
||||||
try:
|
try:
|
||||||
event()
|
event()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
|
else:
|
||||||
|
logging.info('finished event')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user