Skip to content

Commit ffd84c6

Browse files
committed
Use the same now for all in merge check
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 88304ad commit ffd84c6

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
## Bug Fixes
1616

1717
* The merge by type class now uses the correct logger path.
18+
* The merge by type was made more robust under heavy load, making sure to use the same `now` for all dispatches that are checked.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies = [
4040
# plugins.mkdocstrings.handlers.python.import)
4141
"frequenz-sdk >= 1.0.0-rc2100, < 1.0.0-rc2200",
4242
"frequenz-channels >= 1.6.1, < 2.0.0",
43-
"frequenz-client-dispatch >= 0.11.0, < 0.12.0",
43+
"frequenz-client-dispatch >= 0.11.1, < 0.12.0",
4444
"frequenz-client-common >= 0.3.2, < 0.4.0",
4545
"frequenz-client-base >= 0.11.0, < 0.12.0",
4646
]

src/frequenz/dispatch/_dispatch.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,29 @@ def started(self) -> bool:
4343
Returns:
4444
True if the dispatch is started, False otherwise.
4545
"""
46+
now = datetime.now(tz=timezone.utc)
47+
return self.started_at(now)
48+
49+
def started_at(self, now: datetime) -> bool:
50+
"""Check if the dispatch has started.
51+
52+
A dispatch is considered started if the current time is after the start
53+
time but before the end time.
54+
55+
Recurring dispatches are considered started if the current time is after
56+
the start time of the last occurrence but before the end time of the
57+
last occurrence.
58+
59+
Params:
60+
now: time to use as now
61+
62+
Returns:
63+
True if the dispatch is started
64+
"""
4665
if self.deleted:
4766
return False
4867

49-
return super().started
68+
return super().started_at(now)
5069

5170
# noqa is needed because of a bug in pydoclint that makes it think a `return` without a return
5271
# value needs documenting

src/frequenz/dispatch/_merge_strategies.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
from collections.abc import Mapping
8+
from datetime import datetime, timezone
89
from sys import maxsize
910
from typing import Any
1011

@@ -41,12 +42,14 @@ def filter(
4142
Keeps stop events only if no other dispatches matching the
4243
strategy's criteria are running.
4344
"""
44-
if dispatch.started:
45+
now = datetime.now(tz=timezone.utc)
46+
47+
if dispatch.started_at(now):
4548
_logger.debug("Keeping start event %s", dispatch.id)
4649
return True
4750

4851
other_dispatches_running = any(
49-
existing_dispatch.started
52+
existing_dispatch.started_at(now)
5053
for existing_dispatch in dispatches.values()
5154
if (
5255
self.identity(existing_dispatch) == self.identity(dispatch)

0 commit comments

Comments
 (0)