Closed obriat closed 3 years ago
I will take a look.
@obriat I just uploaded a fix. switched to .error instead of .exception to hide the extra information. https://pypi.org/project/locust-influxdb-listener/0.0.5/
It didn't fix my issue, I'm a python newbie so maybe my approach is not the right one I want to wait for influxdb to be ready and just log it if it can't be reach but without stopping my locsut test.
@events.init.add_listener
def locust_init(environment, **kwargs):
...
influxDBSettings = InfluxDBSettings(
influx_host=influx_host,
influx_port='8086',
user='****',
pwd='****',
database='locustidb'
)
# try 5 times for a total of 55s
for i in range(5):
try:
InfluxDBListener(env=environment, influxDbSettings=influxDBSettings)
except:
wait = i * i
log("Try " + i + ": Could not connect to influxdb, wait for" + wait + " seconds and try again.")
time.sleep(wait)
continue
else:
log("Influxdb is available")
break
else:
log("Influxdb is not available")
@obriat The exception is currently handled by the listener. So you don't have to catch it in your end here.
Make you you update to version 0.0.5, which has the fix for the extra log information....
You're right, my package wasn't up to date. I also see my other mistake, I took the logger.exception for a real exception. So do you know how I can determine if InfluxDBListener is connected ? The object doesn't seem to return any data or state, it just logs the error.
I will make it so it returns a true in case of success.. @obriat
Here a suggestion: #12.
It turns out that __init__
should not return anything, so I used a state parameter.
I think I find the right process by using the ping
method and catching its exception:
influxDBSettings = InfluxDBSettings(
influx_host='influx_host',
influx_port='8086',
user='admin',
pwd='pass',
database='locustidb'
)
for i in range(1, 6):
try:
# start listener with the given configuration
influx = InfluxDBListener(env=environment, influxDbSettings=influxDBSettings)
influx.influxdb_client.ping()
except:
# InfluxDBListener does not throw exception but influx.influxdb_client.ping() do.
wait = i * i
print("Try n°" + str(i) + ": Could not connect to influxdb, wait for " + str(
wait) + " seconds and try again.")
time.sleep(wait) # sleep the script for x seconds and....#
else:
print("Influx (version: " + influx.influxdb_client.ping() + ") is available")
break # ...break out of the loop#
else:
print("Influx is not available!")
Perfect.
I initiate InfluxDBListener on @events.init.add_listener and sometimes the InfluxDB is not up, so python throw me exceptions even if I try/catch the function call. I noticed that the call to influxdb_client in /locust_influxdb_listener/init.py", line 55 should be catched and return None. Am I missing something here ?