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 adopt files left by a previous run whose tracking state was lost,
so the retention policies apply to them as well. Pass it a pattern
that matches only this sink's own files if the output directory is
shared with other writers.
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
|
Attributes:
| Name | Type | Description |
|---|---|---|
files_reaped |
int
|
Running count of files deleted by the retention policies, including any deleted while adopting leftovers at startup. Sinks can snapshot this to report reap counts in their own periodic summaries. |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
clean_up_directory(directory, pattern, *, recursive=False)
¶
Adopt files in directory matching pattern and apply retention policies.
This is useful after a restart when the in-memory file cache has been lost. Matching files are added to the file cache in modification-time order and the retention policies applied, so leftovers from a previous run behave exactly like files this run wrote itself: files already past retention_time are deleted immediately, younger ones age out as the run proceeds, and max_files counts them toward its limit.
When several processes write to one directory, give a pattern specific enough to match only this sink's own files, so that a restarting sink does not sweep away another's output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
Directory to scan. |
required |
pattern
|
str
|
Glob matched against file names, e.g. |
required |
recursive
|
bool
|
Whether to descend into subdirectories. Default False. |
False
|
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.