ergon-framework-python 0.3.2__tar.gz → 0.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. {ergon_framework_python-0.3.2/src/ergon_framework_python.egg-info → ergon_framework_python-0.3.3}/PKG-INFO +1 -1
  2. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/pyproject.toml +1 -1
  3. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/async_service.py +58 -3
  4. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/mixins/consumer.py +124 -41
  5. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3/src/ergon_framework_python.egg-info}/PKG-INFO +1 -1
  6. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/LICENSE +0 -0
  7. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/README.md +0 -0
  8. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/setup.cfg +0 -0
  9. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/__init__.py +0 -0
  10. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/__init__.py +0 -0
  11. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/docker-compose.telemetry.yml +0 -0
  12. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/grafana.yaml +0 -0
  13. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/loki.yaml +0 -0
  14. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/otel-collector-config.yaml +0 -0
  15. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/prometheus.yaml +0 -0
  16. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/_observability/tempo.yaml +0 -0
  17. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/connectors/__init__.py +0 -0
  18. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/main.py +0 -0
  19. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/__init__.py +0 -0
  20. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/constants.py +0 -0
  21. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/__init__.py +0 -0
  22. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/config.py +0 -0
  23. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/exceptions.py +0 -0
  24. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/helpers.py +0 -0
  25. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/schemas.py +0 -0
  26. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/example_task/task.py +0 -0
  27. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/exceptions.py +0 -0
  28. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/helpers.py +0 -0
  29. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/schemas.py +0 -0
  30. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/bootstrap/src/__project__/tasks/settings.py +0 -0
  31. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/cli.py +0 -0
  32. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/__init__.py +0 -0
  33. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/_lazy.py +0 -0
  34. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/connector.py +0 -0
  35. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/__init__.py +0 -0
  36. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/_client.py +0 -0
  37. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/async_connector.py +0 -0
  38. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/__init__.py +0 -0
  39. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/adapters.py +0 -0
  40. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/async_connector.py +0 -0
  41. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/connector.py +0 -0
  42. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/models.py +0 -0
  43. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/__init__.py +0 -0
  44. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/activity.py +0 -0
  45. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/addresses.py +0 -0
  46. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/attachments.py +0 -0
  47. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/messages.py +0 -0
  48. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/records.py +0 -0
  49. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/channels/services/service.py +0 -0
  50. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/connector.py +0 -0
  51. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/models.py +0 -0
  52. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/utils.py +0 -0
  53. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/__init__.py +0 -0
  54. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/_mode.py +0 -0
  55. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/_operations.py +0 -0
  56. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/async_connector.py +0 -0
  57. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/connector.py +0 -0
  58. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/models.py +0 -0
  59. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/ergon_platform/workflows/utils.py +0 -0
  60. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/excel/__init__.py +0 -0
  61. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/excel/connector.py +0 -0
  62. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/excel/models.py +0 -0
  63. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/excel/service.py +0 -0
  64. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/__init__.py +0 -0
  65. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/async_auth_service.py +0 -0
  66. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/async_connector.py +0 -0
  67. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/async_service.py +0 -0
  68. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/auth_service.py +0 -0
  69. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/connector.py +0 -0
  70. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/models.py +0 -0
  71. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/service.py +0 -0
  72. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/nylas/utils.py +0 -0
  73. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/__init__.py +0 -0
  74. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/async_connector.py +0 -0
  75. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/async_service.py +0 -0
  76. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/connector.py +0 -0
  77. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/models.py +0 -0
  78. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/service.py +0 -0
  79. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/outlook/utils.py +0 -0
  80. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/__init__.py +0 -0
  81. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/async_connector.py +0 -0
  82. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/async_service.py +0 -0
  83. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/connector.py +0 -0
  84. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/models.py +0 -0
  85. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/service.py +0 -0
  86. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/pipefy/version.py +0 -0
  87. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/postgres/__init__.py +0 -0
  88. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/postgres/async_connector.py +0 -0
  89. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/postgres/async_service.py +0 -0
  90. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/postgres/models.py +0 -0
  91. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/__init__.py +0 -0
  92. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/async_connector.py +0 -0
  93. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/connector.py +0 -0
  94. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/helper.py +0 -0
  95. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/models.py +0 -0
  96. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/rabbitmq/service.py +0 -0
  97. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/__init__.py +0 -0
  98. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/async_connector.py +0 -0
  99. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/async_service.py +0 -0
  100. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/connector.py +0 -0
  101. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/models.py +0 -0
  102. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/sqs/service.py +0 -0
  103. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/connector/transaction.py +0 -0
  104. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/py.typed +0 -0
  105. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/service/__init__.py +0 -0
  106. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/service/service.py +0 -0
  107. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/__init__.py +0 -0
  108. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/base.py +0 -0
  109. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/exceptions.py +0 -0
  110. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/helpers.py +0 -0
  111. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/liveness.py +0 -0
  112. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/manager.py +0 -0
  113. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/mixins/__init__.py +0 -0
  114. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/mixins/metrics.py +0 -0
  115. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/mixins/producer.py +0 -0
  116. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/policies.py +0 -0
  117. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/runner.py +0 -0
  118. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/shutdown.py +0 -0
  119. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/task/utils.py +0 -0
  120. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/telemetry/__init__.py +0 -0
  121. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/telemetry/_resource.py +0 -0
  122. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/telemetry/logging.py +0 -0
  123. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/telemetry/metrics.py +0 -0
  124. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/telemetry/tracing.py +0 -0
  125. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/utils/__init__.py +0 -0
  126. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon/utils/env.py +0 -0
  127. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon_framework_python.egg-info/SOURCES.txt +0 -0
  128. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon_framework_python.egg-info/dependency_links.txt +0 -0
  129. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon_framework_python.egg-info/entry_points.txt +0 -0
  130. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon_framework_python.egg-info/requires.txt +0 -0
  131. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/src/ergon_framework_python.egg-info/top_level.txt +0 -0
  132. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/tests/test_optional_connectors.py +0 -0
  133. {ergon_framework_python-0.3.2 → ergon_framework_python-0.3.3}/tests/test_smoke.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ergon-framework-python
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Ergon internal task-oriented project bootstrapper
5
5
  Author-email: Ergondata Technologies <anza.vossos@protonmail.com>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ergon-framework-python"
3
- version = "0.3.2"
3
+ version = "0.3.3"
4
4
  description = "Ergon internal task-oriented project bootstrapper"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -97,6 +97,10 @@ class AsyncRabbitMQService:
97
97
  self._consumer_state = "disconnected"
98
98
  self._consumer_epoch = 0
99
99
  self._in_flight_message_ids: set[int] = set()
100
+ # Deliveries handed to the caller on a consume channel that has since
101
+ # been torn down. The broker has requeued them, so a later ack/nack can
102
+ # no longer reach it and says nothing about the channel in use now.
103
+ self._retired_message_ids: set[int] = set()
100
104
  # AMQP is push-based: one long-lived basic.consume registration per
101
105
  # queue feeds this local buffer. Broker QoS bounds the number of
102
106
  # unacknowledged deliveries, so fetches only drain memory.
@@ -259,9 +263,17 @@ class AsyncRabbitMQService:
259
263
  self._consumer_config_signature = None
260
264
  while not self._delivery_buffer.empty():
261
265
  try:
262
- self._delivery_buffer.get_nowait()
266
+ _, delivery = self._delivery_buffer.get_nowait()
263
267
  except asyncio.QueueEmpty:
264
268
  break
269
+ if delivery is not None:
270
+ # Never handed to the caller, so nobody will settle it.
271
+ self._in_flight_message_ids.discard(id(delivery.get("_message")))
272
+
273
+ def _retire_in_flight_deliveries(self) -> None:
274
+ """Remember the deliveries the caller still holds from the torn-down channel."""
275
+ self._retired_message_ids.update(self._in_flight_message_ids)
276
+ self._in_flight_message_ids.clear()
265
277
 
266
278
  async def _teardown_consume_channel(self, reason: str = "explicit teardown") -> None:
267
279
  """Deterministically tear down the consume channel.
@@ -279,7 +291,7 @@ class AsyncRabbitMQService:
279
291
  self._invalidate_consume_channel(reason)
280
292
  self._consumer_epoch += 1
281
293
  self._consumer_state = "channel_dead"
282
- self._in_flight_message_ids.clear()
294
+ self._retire_in_flight_deliveries()
283
295
  self._wake_pending_fetches()
284
296
  await self._close_channel_safely(channel, reason)
285
297
 
@@ -310,7 +322,7 @@ class AsyncRabbitMQService:
310
322
  self._invalidate_consume_channel(reason)
311
323
  self._consumer_epoch += 1
312
324
  self._consumer_state = "channel_dead"
313
- self._in_flight_message_ids.clear()
325
+ self._retire_in_flight_deliveries()
314
326
  self._wake_pending_fetches()
315
327
  if channel.is_closed:
316
328
  return
@@ -568,6 +580,9 @@ class AsyncRabbitMQService:
568
580
  ) -> None:
569
581
  if delivery_epoch != self._consumer_epoch:
570
582
  return
583
+ # A new object can reuse the id of a retired delivery
584
+ # that was dropped without being settled.
585
+ self._retired_message_ids.discard(id(message))
571
586
  self._in_flight_message_ids.add(id(message))
572
587
  self._last_fetch_ts = time.time()
573
588
  self._consumer_state = "delivering"
@@ -702,11 +717,37 @@ class AsyncRabbitMQService:
702
717
 
703
718
  # ---------- Ack / Nack ----------
704
719
 
720
+ @staticmethod
721
+ def _already_settled(message: AbstractIncomingMessage) -> bool:
722
+ """True once the message has been acked, nacked or rejected.
723
+
724
+ A task may settle a delivery before its work ends, for example to
725
+ acknowledge it at pickup when the work outlasts the broker's
726
+ acknowledgement timeout and the task keeps its own record of it. The
727
+ settlement that follows at the end of the transaction then has nothing
728
+ left to do. aio-pika raises ``MessageProcessError`` for it, which is
729
+ indistinguishable from a dead channel, so it is answered here.
730
+ """
731
+ return getattr(message, "processed", False) is True
732
+
705
733
  async def ack(self, message: AbstractIncomingMessage) -> None:
706
734
  # Imported lazily to avoid a circular import between
707
735
  # ergon.connector and ergon.task at package init time.
708
736
  from ...task import exceptions as task_exceptions
709
737
 
738
+ if self._already_settled(message):
739
+ self._retired_message_ids.discard(id(message))
740
+ logger.debug("Ack skipped: delivery_tag=%s is already settled", getattr(message, "delivery_tag", None))
741
+ return
742
+ if id(message) in self._retired_message_ids:
743
+ # Delivered on a channel that is gone. The channel in use now is
744
+ # not the one that failed, so it stays up.
745
+ self._retired_message_ids.discard(id(message))
746
+ raise task_exceptions.AckOnDeadChannelError(
747
+ delivery_tag=getattr(message, "delivery_tag", None),
748
+ queue=getattr(message, "routing_key", None),
749
+ )
750
+
710
751
  self._consumer_state = "acking"
711
752
  try:
712
753
  # Bound the ack so a half-open socket is detected in seconds rather
@@ -719,6 +760,7 @@ class AsyncRabbitMQService:
719
760
  self._consumer_state = "polling"
720
761
  except _DEAD_CHANNEL_TIMEOUT_EXCEPTIONS as exc:
721
762
  await self._teardown_consume_channel(f"ack failed: {exc!r}")
763
+ self._retired_message_ids.discard(id(message))
722
764
  raise task_exceptions.AckOnDeadChannelError(
723
765
  delivery_tag=getattr(message, "delivery_tag", None),
724
766
  queue=getattr(message, "routing_key", None),
@@ -728,6 +770,17 @@ class AsyncRabbitMQService:
728
770
  async def nack(self, message: AbstractIncomingMessage, requeue: bool = True) -> None:
729
771
  from ...task import exceptions as task_exceptions
730
772
 
773
+ if self._already_settled(message):
774
+ self._retired_message_ids.discard(id(message))
775
+ logger.debug("Nack skipped: delivery_tag=%s is already settled", getattr(message, "delivery_tag", None))
776
+ return
777
+ if id(message) in self._retired_message_ids:
778
+ self._retired_message_ids.discard(id(message))
779
+ raise task_exceptions.NackOnDeadChannelError(
780
+ delivery_tag=getattr(message, "delivery_tag", None),
781
+ queue=getattr(message, "routing_key", None),
782
+ )
783
+
731
784
  self._consumer_state = "acking"
732
785
  try:
733
786
  async with timeout_after(self.client.ack_timeout):
@@ -737,6 +790,7 @@ class AsyncRabbitMQService:
737
790
  self._consumer_state = "polling"
738
791
  except _DEAD_CHANNEL_TIMEOUT_EXCEPTIONS as exc:
739
792
  await self._teardown_consume_channel(f"nack failed: {exc!r}")
793
+ self._retired_message_ids.discard(id(message))
740
794
  raise task_exceptions.NackOnDeadChannelError(
741
795
  delivery_tag=getattr(message, "delivery_tag", None),
742
796
  queue=getattr(message, "routing_key", None),
@@ -799,5 +853,6 @@ class AsyncRabbitMQService:
799
853
  await self._close_connection_safely(connection, "service close")
800
854
 
801
855
  self._in_flight_message_ids.clear()
856
+ self._retired_message_ids.clear()
802
857
  self._consumer_state = "closed"
803
858
  logger.info("RabbitMQ connection closed")
@@ -17,6 +17,12 @@ from . import producer
17
17
  logger = logging.getLogger(__name__)
18
18
  tracer = telemetry.tracing.get_tracer(__name__)
19
19
 
20
+ # The shortest time between two fetches of a continuous loop while its
21
+ # transactions run and the source has nothing. It only bounds a source that
22
+ # answers an empty fetch at once; a fetch that already waited this long for a
23
+ # delivery is followed by the next one with no pause.
24
+ _BUSY_EMPTY_FETCH_INTERVAL = 0.1
25
+
20
26
 
21
27
  def _wrap_handler_failure(result: Any) -> exceptions.TransactionException:
22
28
  """Normalise a handler failure value into a TransactionException.
@@ -574,6 +580,12 @@ class AsyncConsumerMixin(ABC):
574
580
  _consumer_in_flight: set[int]
575
581
  _consumer_active_stale_after: float | None
576
582
  _consumer_fetch_stale_after: float
583
+ # The continuous loop fetches while transactions run, so a fetch and a
584
+ # transaction each keep their own clock: a fetch that keeps returning must
585
+ # not hide a transaction that never ends, and a transaction that starts or
586
+ # ends must not hide a fetch that never returns.
587
+ _consumer_last_transaction_progress_monotonic: float
588
+ _consumer_fetch_started_monotonic: float | None
577
589
 
578
590
  def _mark_consumer_progress(self, state: str) -> None:
579
591
  self._consumer_liveness_state = state
@@ -589,12 +601,15 @@ class AsyncConsumerMixin(ABC):
589
601
  else:
590
602
  in_flight.discard(id(transaction))
591
603
  self._mark_consumer_progress("processing" if in_flight else "polling")
604
+ self._consumer_last_transaction_progress_monotonic = self._consumer_last_progress_monotonic
592
605
 
593
606
  def liveness_snapshot(self) -> liveness.TaskLivenessSnapshot:
594
607
  """Evaluate consumer-loop progress without treating an idle queue as dead."""
595
608
  now = time.monotonic()
596
609
  state = getattr(self, "_consumer_liveness_state", "starting")
597
610
  last_progress = getattr(self, "_consumer_last_progress_monotonic", now)
611
+ last_transaction_progress = getattr(self, "_consumer_last_transaction_progress_monotonic", last_progress)
612
+ fetch_started = getattr(self, "_consumer_fetch_started_monotonic", None)
598
613
  in_flight = len(getattr(self, "_consumer_in_flight", set()))
599
614
  active_stale_after = getattr(self, "_consumer_active_stale_after", None)
600
615
  fetch_stale_after = getattr(self, "_consumer_fetch_stale_after", 120.0)
@@ -604,11 +619,11 @@ class AsyncConsumerMixin(ABC):
604
619
  "connectors": {},
605
620
  }
606
621
 
607
- if in_flight and active_stale_after is not None and now - last_progress > active_stale_after:
622
+ if in_flight and active_stale_after is not None and now - last_transaction_progress > active_stale_after:
608
623
  return liveness.TaskLivenessSnapshot(
609
624
  healthy=False,
610
625
  state="processing_stalled",
611
- reason=f"No transaction progress for {now - last_progress:.1f}s",
626
+ reason=f"No transaction progress for {now - last_transaction_progress:.1f}s",
612
627
  details=details,
613
628
  )
614
629
 
@@ -620,6 +635,14 @@ class AsyncConsumerMixin(ABC):
620
635
  details=details,
621
636
  )
622
637
 
638
+ if fetch_started is not None and now - fetch_started > fetch_stale_after:
639
+ return liveness.TaskLivenessSnapshot(
640
+ healthy=False,
641
+ state="fetch_stalled",
642
+ reason=f"Fetch has not completed for {now - fetch_started:.1f}s",
643
+ details=details,
644
+ )
645
+
623
646
  for name, conn in self.connectors.items():
624
647
  health_fn = getattr(conn, "health", None)
625
648
  if not callable(health_fn):
@@ -683,14 +706,18 @@ class AsyncConsumerMixin(ABC):
683
706
  ) -> tuple[bool, List[connector.Transaction]]:
684
707
  fetch_size = policy.batch.size if batch_size is None else batch_size
685
708
  self._mark_consumer_progress("fetching")
709
+ self._consumer_fetch_started_monotonic = self._consumer_last_progress_monotonic
686
710
  logger.info(f"Fetch handler started for batch size {fetch_size}", extra=policy.extra)
687
711
  fetch_start = time.perf_counter()
688
- success, result = await helpers.run_fn_async(
689
- fn=lambda: conn.fetch_transactions_async(fetch_size, **policy.extra),
690
- retry=policy.retry,
691
- trace_name=f"{self.__class__.__name__}.fetch_transactions",
692
- trace_attrs={"batch_size": fetch_size},
693
- )
712
+ try:
713
+ success, result = await helpers.run_fn_async(
714
+ fn=lambda: conn.fetch_transactions_async(fetch_size, **policy.extra),
715
+ retry=policy.retry,
716
+ trace_name=f"{self.__class__.__name__}.fetch_transactions",
717
+ trace_attrs={"batch_size": fetch_size},
718
+ )
719
+ finally:
720
+ self._consumer_fetch_started_monotonic = None
694
721
  # Record fetch metrics
695
722
  fetched_count = len(result) if success and result else 0
696
723
  mixin_metrics.record_consumer_fetch(
@@ -914,6 +941,8 @@ class AsyncConsumerMixin(ABC):
914
941
  conn = self._resolve_connector(policy.fetch.connector_name)
915
942
  self._consumer_in_flight = set()
916
943
  self._mark_consumer_progress("starting")
944
+ self._consumer_last_transaction_progress_monotonic = self._consumer_last_progress_monotonic
945
+ self._consumer_fetch_started_monotonic = None
917
946
  supervision = getattr(getattr(self, "task_config", None), "supervision", None)
918
947
  transaction_timeout = policy.transaction_runtime.timeout
919
948
  processing_override = getattr(supervision, "processing_stale_after", None)
@@ -966,8 +995,24 @@ class AsyncConsumerMixin(ABC):
966
995
  nonlocal batch_number, empty_count, processed
967
996
 
968
997
  concurrency = policy.loop.concurrency.value
998
+ limit = policy.loop.limit
969
999
  submitted = 0
970
1000
  in_flight: set[asyncio.Task] = set()
1001
+ # The fetch runs beside the transactions, not between them. A
1002
+ # loop that waited for a transaction to end before it fetched
1003
+ # again left every delivery that arrived in the meantime
1004
+ # unstarted for as long as that transaction ran, and did not
1005
+ # see the broker close its channel until then.
1006
+ fetch_task: asyncio.Task | None = None
1007
+ fetch_started = 0.0
1008
+ fetch_is_stale = False
1009
+ # After an empty fetch with transactions in flight, the next
1010
+ # fetch waits for this pause or for a transaction to end,
1011
+ # whichever comes first.
1012
+ pause_task: asyncio.Task | None = None
1013
+ # A non-streaming loop drains its source: after an empty fetch
1014
+ # it fetches again only once a transaction has ended.
1015
+ fetch_after_completion = False
971
1016
 
972
1017
  async def reap(done: set[asyncio.Task]) -> None:
973
1018
  nonlocal processed
@@ -979,41 +1024,54 @@ class AsyncConsumerMixin(ABC):
979
1024
  finally:
980
1025
  processed += 1
981
1026
 
982
- async def wait_for_completion() -> None:
983
- done, _ = await asyncio.wait(
984
- in_flight,
985
- return_when=asyncio.FIRST_COMPLETED,
986
- )
987
- in_flight.difference_update(done)
988
- await reap(done)
989
- self._mark_consumer_progress("polling")
990
-
991
1027
  try:
992
1028
  while True:
993
- if policy.loop.limit is not None and submitted >= policy.loop.limit:
994
- if in_flight:
995
- await wait_for_completion()
996
- continue
1029
+ free_slots = concurrency - len(in_flight)
1030
+ if limit is not None:
1031
+ free_slots = min(free_slots, limit - submitted)
1032
+
1033
+ if fetch_task is None and pause_task is None and not fetch_after_completion and free_slots > 0:
1034
+ logger.debug(
1035
+ "Fetching transactions for %d free continuous-consumer slot(s) with fetch policy: %s",
1036
+ free_slots,
1037
+ policy.fetch.model_dump_json(indent=2),
1038
+ )
1039
+ fetch_started = time.monotonic()
1040
+ fetch_is_stale = False
1041
+ fetch_task = asyncio.create_task(
1042
+ self._handle_fetch(conn, policy.fetch, batch_size=free_slots),
1043
+ )
1044
+
1045
+ waiting: set[asyncio.Task] = set(in_flight)
1046
+ if fetch_task is not None:
1047
+ waiting.add(fetch_task)
1048
+ if pause_task is not None:
1049
+ waiting.add(pause_task)
1050
+ if not waiting:
1051
+ # The limit is reached and nothing is in flight.
997
1052
  break
998
1053
 
999
- free_slots = concurrency - len(in_flight)
1000
- if free_slots == 0:
1001
- await wait_for_completion()
1002
- continue
1054
+ done, _ = await asyncio.wait(waiting, return_when=asyncio.FIRST_COMPLETED)
1003
1055
 
1004
- if policy.loop.limit is not None:
1005
- free_slots = min(free_slots, policy.loop.limit - submitted)
1056
+ finished = done & in_flight
1057
+ if finished:
1058
+ in_flight.difference_update(finished)
1059
+ await reap(finished)
1060
+ fetch_after_completion = False
1061
+ if fetch_task is not None:
1062
+ fetch_is_stale = True
1063
+ if pause_task is not None:
1064
+ pause_task.cancel()
1065
+ pause_task = None
1006
1066
 
1007
- logger.debug(
1008
- "Fetching transactions for %d free continuous-consumer slot(s) with fetch policy: %s",
1009
- free_slots,
1010
- policy.fetch.model_dump_json(indent=2),
1011
- )
1012
- success, result = await self._handle_fetch(
1013
- conn,
1014
- policy.fetch,
1015
- batch_size=free_slots,
1016
- )
1067
+ if pause_task is not None and pause_task in done:
1068
+ pause_task = None
1069
+
1070
+ if fetch_task is None or fetch_task not in done:
1071
+ continue
1072
+
1073
+ success, result = fetch_task.result()
1074
+ fetch_task = None
1017
1075
 
1018
1076
  if not success:
1019
1077
  logger.error("Fetch failed → %s", result)
@@ -1056,8 +1114,28 @@ class AsyncConsumerMixin(ABC):
1056
1114
  submitted += 1
1057
1115
  continue
1058
1116
 
1117
+ if fetch_is_stale:
1118
+ # A transaction ended while this fetch was out, so
1119
+ # its empty answer may be older than what that
1120
+ # transaction left in the source: ask again before
1121
+ # waiting or stopping.
1122
+ continue
1123
+
1059
1124
  if in_flight:
1060
- await wait_for_completion()
1125
+ if not policy.loop.streaming:
1126
+ fetch_after_completion = True
1127
+ continue
1128
+ # Keep fetching while the transactions run. A
1129
+ # source that waits for a delivery (RabbitMQ) has
1130
+ # already spent its poll timeout in the fetch; one
1131
+ # that answers at once is asked again after the
1132
+ # empty-fetch backoff, and never in a tight loop.
1133
+ pause = max(
1134
+ policy.fetch.empty.backoff,
1135
+ _BUSY_EMPTY_FETCH_INTERVAL - (time.monotonic() - fetch_started),
1136
+ )
1137
+ if pause > 0:
1138
+ pause_task = asyncio.create_task(asyncio.sleep(pause))
1061
1139
  continue
1062
1140
 
1063
1141
  self._mark_consumer_progress("idle")
@@ -1078,10 +1156,15 @@ class AsyncConsumerMixin(ABC):
1078
1156
  )
1079
1157
  empty_count += 1
1080
1158
  except BaseException:
1081
- for task in in_flight:
1159
+ pending = set(in_flight)
1160
+ if fetch_task is not None:
1161
+ pending.add(fetch_task)
1162
+ if pause_task is not None:
1163
+ pending.add(pause_task)
1164
+ for task in pending:
1082
1165
  task.cancel()
1083
- if in_flight:
1084
- await asyncio.gather(*in_flight, return_exceptions=True)
1166
+ if pending:
1167
+ await asyncio.gather(*pending, return_exceptions=True)
1085
1168
  raise
1086
1169
 
1087
1170
  elapsed_time = time.perf_counter() - start_time
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ergon-framework-python
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Ergon internal task-oriented project bootstrapper
5
5
  Author-email: Ergondata Technologies <anza.vossos@protonmail.com>
6
6
  License-Expression: MIT