Skip to content

IFC-1702: Events retention period - #8398

Closed
solababs wants to merge 10 commits into
developfrom
sb-17022026-events-retention-duration-ifc-1702
Closed

solababs wants to merge 10 commits into
developfrom
sb-17022026-events-retention-duration-ifc-1702

Conversation

@solababs

@solababs solababs commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Why

Prefect's internal event store grows unbounded by default, which can lead to increased memory/disk usage on long-running Infrahub workers. There is currently no way to configure how long Prefect events are retained.

Goal: Expose a configurable retention period for Prefect events so anyone can control how long workflow event data is kept.

Non-goals: This does not change any other Prefect lifecycle or cleanup behavior.

Closes #6906

What changed

  • New config setting: Added worker_events_retention_period (integer, days, default 7, minimum 1) to WorkflowSettings.

  • Wire it into the worker: The new setting is passed to Prefect via PREFECT_EVENTS_RETENTION_PERIOD in the worker's temporary_settings block, formatted as "d" (e.g. "7d").

How to review

Check new Field definition on WorkflowSettings
Check passing the setting to Prefect's temporary_settings

How to test

# Unit tests (verify no regressions in config/worker)
uv run invoke backend.test-unit

# Manual: override the setting and confirm it takes effect
INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD=14 uv run infrahub worker start
# Check Prefect logs/settings to confirm PREFECT_EVENTS_RETENTION_PERIOD=14d

Impact & rollout

  • Backward compatibility:
    No breaking changes. The new setting has a sensible default (7 days); existing deployments require no configuration changes.

  • Performance:
    Should improve long-running worker performance by bounding event store growth.

  • Config/env changes:
    New optional env var INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD (integer, days).

  • Deployment notes:
    Safe to deploy without coordination. The setting takes effect on worker restart.

Checklist

  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change)
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)

Summary by CodeRabbit

  • New Features

    • Added a configurable worker event retention period for workflows (default 7 days, minimum 1 day).
  • Documentation

    • Added configuration reference and environment variable to override the worker event retention period; updated local development setup to reflect the new setting.

@solababs
solababs requested a review from a team as a code owner February 17, 2026 07:43
@github-actions github-actions Bot added the group/backend API server, GraphQL, task worker (Prefect), database, Python code label Feb 17, 2026
@coderabbitai

coderabbitai Bot commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Added worker_events_retention_period (int, default 7, min 1) to WorkflowSettings. That value is applied as Prefect's PREFECT_EVENTS_RETENTION_PERIOD (formatted "<N>d") in the InfrahubAsync worker setup and in create_infrahub_prefect. Introduced env var INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD (default 7), added PREFECT_EVENTS_RETENTION_PERIOD="7d" to the development Dockerfile, and updated docs and changelog.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a configurable events retention period feature.
Description check ✅ Passed The description is well-structured with clear explanations of the problem, solution, testing approach, and backward compatibility considerations.
Linked Issues check ✅ Passed The PR fully addresses issue #6906 by implementing a configurable retention period for Prefect events, allowing operators to control activity log retention rather than being limited to the 7-day default.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the configurable events retention period feature; no unrelated modifications detected across config, worker, documentation, and changelog files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codspeed

codspeed Bot commented Feb 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 13 untouched benchmarks


Comparing sb-17022026-events-retention-duration-ifc-1702 (c057093) with develop (54bca7a)1

Open in CodSpeed

Footnotes

  1. No successful run was found on develop (2146176) during the generation of this report, so 54bca7a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@solababs
solababs requested a review from fatih-acar February 17, 2026 07:50

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
changelog/6906.added.md (1)

1-1: Enhance the changelog entry for clarity and completeness.

The entry could be improved in several ways:

  1. Markdown formatting: Use backticks around the environment variable name for better readability.
  2. Completeness: The entry only mentions the environment variable but not the underlying configuration field (WorkflowSettings.worker_events_retention_period) that it overrides.
  3. Purpose: Explain what this setting controls (Prefect's event retention period to prevent unbounded growth of the event store).
  4. Constraints: Mention the minimum value of 1 day.

Since the referenced markdown guidelines (dev/guidelines/markdown.md) were not provided, please verify this entry complies with the project's changelog formatting standards.

📝 Suggested improvement
-Added new optional env var INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD (integer, days) with default of 7d.
+Added configurable retention period for Prefect workflow events via `WorkflowSettings.worker_events_retention_period` (default: 7 days, minimum: 1 day). Can be overridden using the optional environment variable `INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD`. This prevents unbounded growth of Prefect's internal event store.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@changelog/6906.added.md` at line 1, Update the changelog entry to use
markdown backticks for the environment variable name
(`INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD`), state that it overrides
the `WorkflowSettings.worker_events_retention_period` configuration field,
explain that it controls Prefect's worker event retention period (to prevent
unbounded growth of the event store), and include the default value (7 days)
plus the minimum allowed value (1 day); ensure the phrasing matches the
project's changelog style (brief, declarative sentence) and formatting
conventions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@changelog/6906.added.md`:
- Line 1: Update the changelog entry to use markdown backticks for the
environment variable name (`INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD`),
state that it overrides the `WorkflowSettings.worker_events_retention_period`
configuration field, explain that it controls Prefect's worker event retention
period (to prevent unbounded growth of the event store), and include the default
value (7 days) plus the minimum allowed value (1 day); ensure the phrasing
matches the project's changelog style (brief, declarative sentence) and
formatting conventions.

Comment thread backend/infrahub/config.py Outdated
description="Threshold for caching flow run counts (0 to always cache, higher values to disable)",
)
worker_events_retention_period: int = Field(
default=7, ge=1, description="Specify the number of days to retain events for (days)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (days) can be removed
days is already specified earlier in the description

@solababs
solababs requested review from a team as code owners February 18, 2026 06:28
@github-actions github-actions Bot added the type/documentation Improvements or additions to documentation label Feb 18, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/infrahub/config.py (1)

425-427: LGTM! Optional: improve description wording.

The field definition is correct. Optionally, the description "Specify the number of days to retain events for" (ends with a dangling preposition) reads better as "Specify the number of days to retain worker events" — this also propagates to the auto-generated docs.

✏️ Proposed description wording improvement
     worker_events_retention_period: int = Field(
-        default=7, ge=1, description="Specify the number of days to retain events for"
+        default=7, ge=1, description="Specify the number of days to retain worker events"
     )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/infrahub/config.py` around lines 425 - 427, Update the Field
description for worker_events_retention_period to a clearer phrasing: replace
"Specify the number of days to retain events for" with something like "Specify
the number of days to retain worker events" so the auto-generated docs read
cleanly; locate the Field declaration for worker_events_retention_period in
config.py and update its description argument accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/infrahub/prefect_server/app.py`:
- Around line 45-49: The code sets PREFECT_EVENTS_RETENTION_PERIOD by
concatenating events_retention_days with "d" but does not validate the fallback
env var INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD; change the fallback to
parse and validate using int(), e.g. attempt to int(os.environ.get(..., "7"))
and on ValueError fall back to 7 (or config.SETTINGS value), then set
os.environ["PREFECT_EVENTS_RETENTION_PERIOD"] = f"{validated_days}d"; apply the
same int-casting/validation fix in places that construct
PREFECT_EVENTS_RETENTION_PERIOD (e.g. the code that formats
events_retention_days in infrahub_async.py and any Dockerfile environment
construction).

---

Duplicate comments:
In `@development/Dockerfile`:
- Around line 90-91: Update the Dockerfile environment default for
PREFECT_EVENTS_RETENTION_PERIOD to use ISO 8601 duration format by replacing the
string "7d" with "P7D" so it matches the runtime change in app.py; locate the
PREFECT_EVENTS_RETENTION_PERIOD entry in the Dockerfile and set
PREFECT_EVENTS_RETENTION_PERIOD="P7D".

---

Nitpick comments:
In `@backend/infrahub/config.py`:
- Around line 425-427: Update the Field description for
worker_events_retention_period to a clearer phrasing: replace "Specify the
number of days to retain events for" with something like "Specify the number of
days to retain worker events" so the auto-generated docs read cleanly; locate
the Field declaration for worker_events_retention_period in config.py and update
its description argument accordingly.

Comment thread backend/infrahub/prefect_server/app.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@backend/infrahub/prefect_server/app.py`:
- Around line 47-49: The code assigns events_retention_days via
int(os.environ.get("INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD", "7"))
which can raise ValueError for non-numeric input and allows zero/negative
values; change this to parse the env var safely (catch ValueError or use a
helper to safe-int) and enforce a minimum of 1 before setting
os.environ["PREFECT_EVENTS_RETENTION_PERIOD"]; reference the variable/events key
names events_retention_days, INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD
and the env output PREFECT_EVENTS_RETENTION_PERIOD when implementing the
validation and fallback logic.

Comment thread changelog/6906.added.md Outdated
@@ -0,0 +1 @@
Added new optional env var INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD (integer, days) with default of 7d.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this should be with default of 7 b/c the user does not include the d in their env var, we add that before sending to Prefect, right?

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 2 weeks and has been marked as stale.

If you're still working on this, please:

  • Push new commits, or
  • Leave a comment to indicate this PR is still active

The stale label will be removed automatically when there's new activity.

@github-actions github-actions Bot added the stale Marks stale issues and pull requests label Mar 20, 2026
Comment thread development/Dockerfile Outdated
PREFECT_API_DATABASE_TIMEOUT=60 \
PREFECT_FLOWS_HEARTBEAT_FREQUENCY=30
PREFECT_FLOWS_HEARTBEAT_FREQUENCY=30 \
PREFECT_EVENTS_RETENTION_PERIOD="7d"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised we have to add this to the Dockerfile.

@BeArchiTek BeArchiTek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you run the invoke task to update the env variable in the docker compose files so INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD is present

@github-actions github-actions Bot removed the stale Marks stale issues and pull requests label Apr 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 2 weeks and has been marked as stale.

If you're still working on this, please:

  • Push new commits, or
  • Leave a comment to indicate this PR is still active

The stale label will be removed automatically when there's new activity.

@github-actions github-actions Bot added the stale Marks stale issues and pull requests label Jun 23, 2026
saltas888 and others added 2 commits August 12, 2026 12:09
…-1702

Resolve development/Dockerfile conflict by taking develop's restructured
version and dropping the PREFECT_EVENTS_RETENTION_PERIOD ENV line: the
prefect server app sets it at startup from config, so a Dockerfile
default is redundant.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Add INFRAHUB_WORKFLOW_WORKER_EVENTS_RETENTION_PERIOD to docker-compose.yml
  via 'invoke release.gen-config-env --update-docker-file'
- Reword the setting description and regenerate the configuration reference
- Clarify the changelog fragment

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@saltas888
saltas888 requested a review from a team as a code owner August 12, 2026 09:14
@saltas888
saltas888 requested a review from BeArchiTek August 12, 2026 10:37
@saltas888

Copy link
Copy Markdown
Contributor

Superseded, I'd suggest we close this.

This targets PREFECT_EVENTS_RETENTION_PERIOD, which is the old event retention config. The direction we settled on is Prefect's DB vacuum service, which already ships in the version we pin, with per-event-type retention overrides on top of the global period.

Worth flagging that the vacuum service doesn't solve #6906 on its own either. Overrides can only shorten retention, never extend it (effective retention is min(override, global)), and the global sweep deletes everything past the global period with no exclusion for overridden types. So a short global with a long infrahub.* override doesn't work, the global has to be the longest retention we want and the shortening has to target Prefect's internal events instead.

Tracked in IFC-2934. Thanks for the work here @solababs, the config surface is still roughly what we'll want, just applied at a different layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

group/backend API server, GraphQL, task worker (Prefect), database, Python code stale Marks stale issues and pull requests type/documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants