Fix timer cancellation in PacketConsumer and simplify migration

- 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 <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-07-21 08:48:57 -05:00
parent 8906e218dd
commit dc0e49780b
No known key found for this signature in database
2 changed files with 4 additions and 39 deletions

View file

@ -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}}

View file

@ -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