I have a very simple program. What it does is listen to a Postgres channel, when a notification arrives it does some work and then continues to listen again.
It can happen that a notification is somehow corrupted. Then the program should just crash. I have systemd setup to restart it and it is no big deal.
spawn do begin PG.connect_listen(ENV["POSTGRES"], "new_measurements") do |n| server_id, file_system_id, measurement_id = n.payload.split(",") capacity = pg.scalar("SELECT capacity FROM measurements WHERE id = $1", measurement_id).as(Int32) if capacity > 90 pp "panic!" end end ensure pg.close end end sleep
The exception is for instance thrown at the
pg.scalar query. But I really want to catch all exceptions with an ensure and then crash/exit the program.
When an exception occurs now the program just hangs. It enters the ensure clause and then hangs on the
sleep call? I don’t really know.
sleep are necessary otherwise the program will exit immediately without processing notifications.
I’m pretty new to Crystal and I’m not sure what the canonical way of handling this is.