Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ with monitors as (
extract(epoch from current_timestamp - last_heartbeat) :: int as time_since_last_heartbeat
from "Monitors"
where
enabled and (queued or running)
enabled and
(queued or running) and
coalesce(queued_at < current_timestamp - ($1 :: int) * interval '1 second', true) and
coalesce(running_at < current_timestamp - ($1 :: int) * interval '1 second', true)
)
select id
from monitors
Expand Down
11 changes: 10 additions & 1 deletion tests/components/controller/procedures/test_monitors_stuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def get_time(reference: str) -> datetime | None:
values = {
"now": now(),
"ten_seconds_ago": now() - timedelta(seconds=11),
"five_minutes_ago": now() - timedelta(seconds=301),
}
return values.get(reference)

Expand All @@ -28,19 +27,25 @@ async def test_configuration():
@pytest.mark.parametrize("enabled", [False, True])
@pytest.mark.parametrize("queued", [False, True])
@pytest.mark.parametrize("running", [False, True])
@pytest.mark.parametrize("queued_at", [None, "now", "ten_seconds_ago"])
@pytest.mark.parametrize("running_at", [None, "now", "ten_seconds_ago"])
@pytest.mark.parametrize("last_heartbeat", [None, "now", "ten_seconds_ago"])
async def test_monitors_stuck(
caplog,
sample_monitor: Monitor,
enabled,
queued,
running,
queued_at,
running_at,
last_heartbeat,
):
"""'monitors_stuck' should fix monitors that are stuck"""
sample_monitor.enabled = enabled
sample_monitor.queued = queued
sample_monitor.running = running
sample_monitor.queued_at = get_time(queued_at) # type:ignore[assignment]
sample_monitor.running_at = get_time(running_at) # type:ignore[assignment]
sample_monitor.last_heartbeat = get_time(last_heartbeat) # type:ignore[assignment]
await sample_monitor.save()

Expand All @@ -52,6 +57,10 @@ async def test_monitors_stuck(
triggered = False
elif last_heartbeat is None:
triggered = False
elif queued_at == "now":
triggered = False
elif running_at == "now":
triggered = False
elif last_heartbeat == "now":
triggered = False
else:
Expand Down