From dc0e49780b10bd301bdeb66c0a03e05272ed7801 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 21 Jul 2025 08:48:57 -0500 Subject: [PATCH] Fix timer cancellation in PacketConsumer and simplify migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add nil check before canceling timer in PacketConsumer to prevent errors when timer reference doesn't exist (fixes test failures) - Simplify packet counter reset migration by removing unsupported event triggers, keeping only manual reset function - All tests now passing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- lib/aprsme/packet_consumer.ex | 4 +- ..._add_truncate_reset_for_packet_counter.exs | 39 +------------------ 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/lib/aprsme/packet_consumer.ex b/lib/aprsme/packet_consumer.ex index 0d7c36d..780b92a 100644 --- a/lib/aprsme/packet_consumer.ex +++ b/lib/aprsme/packet_consumer.ex @@ -73,7 +73,7 @@ defmodule Aprsme.PacketConsumer do end # Cancel and restart timer - Process.cancel_timer(timer) + if timer, do: Process.cancel_timer(timer) new_timer = Process.send_after(self(), :process_batch, state.batch_timeout) {:noreply, [], %{state | batch: [], timer: new_timer}} @@ -82,7 +82,7 @@ defmodule Aprsme.PacketConsumer do process_batch(new_batch) # Cancel and restart timer - Process.cancel_timer(timer) + if timer, do: Process.cancel_timer(timer) new_timer = Process.send_after(self(), :process_batch, state.batch_timeout) {:noreply, [], %{state | batch: [], timer: new_timer}} diff --git a/priv/repo/migrations/20250719202358_add_truncate_reset_for_packet_counter.exs b/priv/repo/migrations/20250719202358_add_truncate_reset_for_packet_counter.exs index 7031ba5..2aa0ff8 100644 --- a/priv/repo/migrations/20250719202358_add_truncate_reset_for_packet_counter.exs +++ b/priv/repo/migrations/20250719202358_add_truncate_reset_for_packet_counter.exs @@ -2,38 +2,7 @@ defmodule Aprsme.Repo.Migrations.AddTruncateResetForPacketCounter do use Ecto.Migration def up do - # Create a function to reset the packet counter sequence - execute """ - CREATE OR REPLACE FUNCTION reset_packet_counter_on_truncate() - RETURNS event_trigger AS $$ - DECLARE - cmd RECORD; - BEGIN - FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands() - LOOP - IF cmd.command_tag = 'TRUNCATE TABLE' AND cmd.object_identity = 'public.packets' THEN - -- Reset the sequence to 0 - PERFORM setval('packet_count_seq', 0, false); - - -- Also update the deprecated packet_counters table for backward compatibility - UPDATE packet_counters - SET count = 0, updated_at = NOW() - WHERE counter_type = 'total_packets'; - END IF; - END LOOP; - END; - $$ LANGUAGE plpgsql; - """ - - # Create an event trigger for TRUNCATE operations - execute """ - CREATE EVENT TRIGGER reset_packet_counter_trigger - ON ddl_command_end - WHEN TAG IN ('TRUNCATE TABLE') - EXECUTE FUNCTION reset_packet_counter_on_truncate(); - """ - - # Also create a manual reset function that can be called if needed + # Create a manual reset function that can be called when needed execute """ CREATE OR REPLACE FUNCTION reset_packet_counter() RETURNS void AS $$ @@ -89,11 +58,7 @@ defmodule Aprsme.Repo.Migrations.AddTruncateResetForPacketCounter do end def down do - # Drop the event trigger - execute "DROP EVENT TRIGGER IF EXISTS reset_packet_counter_trigger;" - - # Drop the reset functions - execute "DROP FUNCTION IF EXISTS reset_packet_counter_on_truncate();" + # Drop the reset function execute "DROP FUNCTION IF EXISTS reset_packet_counter();" # Restore the original get_packet_count function