Skip to content

Commit 827f7c2

Browse files
authored
fix: Refactor archive_to_dlq logic and fix minor issues
2 parents 757ed58 + 2033e1e commit 827f7c2

5 files changed

Lines changed: 50 additions & 18 deletions

File tree

migrations/001_initial.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ BEGIN
104104
END;
105105
$$;
106106

107+
-- pgmq_read_by_id(queue_name, msg_id)
108+
CREATE OR REPLACE FUNCTION pgmq_read_by_id(queue_name text, msg_id bigint)
109+
RETURNS JSONB
110+
LANGUAGE plpgsql
111+
AS $$
112+
DECLARE
113+
msg RECORD;
114+
BEGIN
115+
SELECT * FROM pgmq.read(queue_name, msg_id) INTO msg;
116+
RETURN CASE WHEN msg IS NULL THEN NULL ELSE json_build_object(
117+
'msg_id', msg.msg_id,
118+
'message', msg.message,
119+
'read_ct', msg.read_ct,
120+
'enqueued_at', msg.enqueued_at,
121+
'first_received_at', msg.first_received_at,
122+
'next_msg_scheduled_for', msg.next_msg_scheduled_for
123+
) END;
124+
END;
125+
$$;
126+
107127
-- App deployments (tracks deployed applications)
108128
CREATE TABLE IF NOT EXISTS app_deployments (
109129
id BIGSERIAL PRIMARY KEY,

netengine/core/pgmq_client.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,29 @@ async def archive_to_dlq(self, queue_name: str, msg_id: int, reason: str) -> Non
5252
return
5353

5454
envelope = EventEnvelope(**json.loads(msg["message"]))
55-
updated = EventEnvelope(
56-
event_id=envelope.event_id,
57-
correlation_id=envelope.correlation_id,
58-
event_type=envelope.event_type,
59-
emitted_by=envelope.emitted_by,
60-
emitted_at=envelope.emitted_at,
61-
payload={**envelope.payload, "dlq_reason": reason},
62-
parent_event_id=envelope.parent_event_id,
63-
retry_count=envelope.retry_count + 1,
64-
)
65-
6655
await self.delete(queue_name, msg_id)
67-
if updated.retry_count >= MAX_RETRIES:
68-
await self.send(f"{queue_name}_dlq", updated)
56+
57+
if envelope.retry_count + 1 >= MAX_RETRIES:
58+
dlq_envelope = EventEnvelope(
59+
event_id=envelope.event_id,
60+
correlation_id=envelope.correlation_id,
61+
event_type=envelope.event_type,
62+
emitted_by=envelope.emitted_by,
63+
emitted_at=envelope.emitted_at,
64+
payload={**envelope.payload, "dlq_reason": reason},
65+
parent_event_id=envelope.parent_event_id,
66+
retry_count=envelope.retry_count + 1,
67+
)
68+
await self.send(f"{queue_name}_dlq", dlq_envelope)
6969
else:
70-
await self.send(queue_name, updated)
70+
requeue_envelope = EventEnvelope(
71+
event_id=envelope.event_id,
72+
correlation_id=envelope.correlation_id,
73+
event_type=envelope.event_type,
74+
emitted_by=envelope.emitted_by,
75+
emitted_at=envelope.emitted_at,
76+
payload=envelope.payload,
77+
parent_event_id=envelope.parent_event_id,
78+
retry_count=envelope.retry_count + 1,
79+
)
80+
await self.send(queue_name, requeue_envelope)

netengine/errors.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Any
22

3-
from omegaconf import DictConfig as Config
4-
53

64
class BaseNetEngineException(Exception):
75
def __init__(
@@ -12,7 +10,7 @@ def __init__(
1210
) -> None:
1311
self._msg = message
1412
self._code: int | str | None = None
15-
self._log_rules: Config | dict[str, Any] = {
13+
self._log_rules: dict[str, Any] = {
1614
"log_on_init": True,
1715
"at_lvl": "TRACE",
1816
"with_msg": message,

netengine/handlers/dns.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ async def add_zone_record(
792792
logger.debug(f"Zone file flushed to disk: {zone_file_path}")
793793
# Signal CoreDNS to reload zones (SIGUSR1)
794794
await self._reload_coredns(context)
795+
else:
796+
logger.warning(
797+
f"Zone directory does not exist, disk flush skipped: {zone_file_path.parent}"
798+
)
795799

796800
logger.info(f"Zone record updated: {zone} {record_type} {name} -> {value} (TTL: {ttl})")
797801

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def dev_sandbox_spec() -> NetEngineSpec:
6161
@pytest.fixture(autouse=True)
6262
def isolated_runtime_state_file(tmp_path, monkeypatch):
6363
"""Keep tests from reading or writing the repository-root runtime state file."""
64-
monkeypatch.setenv("NETENGINE_STATE_FILE", str(tmp_path / "netengines_state.json"))
64+
monkeypatch.setenv("NETENGINE_STATE_FILE", str(tmp_path / "netengine_state.json"))
6565

6666

6767
@pytest.fixture

0 commit comments

Comments
 (0)