Skip to content

Commit

Permalink
fix: Handle exceptions when processing messages and keep running.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjw committed Nov 9, 2022
1 parent 95f8fa8 commit a3295fe
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions bin/prom433
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import paho.mqtt.client as mqtt
import traceback
import sys

from prom433 import get_arguments, serve, prometheus
Expand All @@ -25,14 +26,28 @@ def on_connect(client, userdata, flags, rc):

client.subscribe("rtl_433/+/events")


def safe_on_message(on_message):
def safe_on_message(client, userdata, msg):
try:
return on_message(client, userdata, msg)
except Exception as e:
sys.stderr.write(f"Exception processing message: {msg}\n")

traceback.print_exc()

return safe_on_message


def main():
args = get_arguments(sys.argv[1:])

client = mqtt.Client()

client.on_connect = on_connect
client.on_message=lambda client, userdata, message: \
prometheus(message.payload.decode("utf-8"))
client.on_message = \
safe_on_message(lambda client, userdata, message: \
prometheus(message.payload.decode("utf-8")))

client.connect(args.mqtt)

Expand Down

0 comments on commit a3295fe

Please sign in to comment.