sgnts.sinks.retention
¶
Mixin that adds file retention policies to sinks that write files.
FileRetentionMixin
dataclass
¶
Bases: HasLogger
flowchart TD
sgnts.sinks.retention.FileRetentionMixin[FileRetentionMixin]
sgnts.sinks.retention.HasLogger[HasLogger]
sgnts.sinks.retention.HasLogger --> sgnts.sinks.retention.FileRetentionMixin
click sgnts.sinks.retention.FileRetentionMixin href "" "sgnts.sinks.retention.FileRetentionMixin"
click sgnts.sinks.retention.HasLogger href "" "sgnts.sinks.retention.HasLogger"
Mixin that adds file retention policies to any sink that writes files.
Provides count-based and time-based retention, which can be used
independently or combined. After each file write, the sink calls
:meth:track_file to register the path; cleanup runs automatically.
Call :meth:clean_up_directory during startup (e.g. in configure)
to sweep stale files left by a previous run whose tracking state was
lost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_files
|
int | None
|
Keep only the N most recent files. Older files are deleted
when the count is exceeded. Disabled when |
None
|
retention_time
|
float | None
|
Retention time in seconds. Files whose mtime is older than
this are deleted. Can be combined with max_files. Disabled
when |
None
|
Source code in src/sgnts/sinks/retention.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
clean_up_directory(directory, suffix)
¶
Remove files in directory matching suffix that exceed retention_time.
This is useful after a restart when the in-memory file cache has been lost. Only time-based retention applies here — count-based cleanup requires creation-order knowledge that a directory scan cannot reliably provide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
Directory to scan. |
required |
suffix
|
str
|
File suffix to match (e.g. |
required |
Source code in src/sgnts/sinks/retention.py
clean_up_files()
¶
Remove tracked files that exceed retention policies.
Returns:
| Type | Description |
|---|---|
int
|
Number of files deleted. |
Source code in src/sgnts/sinks/retention.py
track_file(path)
¶
Register a written file and run cleanup if policies are set.