Skip to content

Commit 9beb1b7

Browse files
committed
Sets timestap for interval comparision just once
No more fetching new now() timestamp for every check. Hard to test for this, but could have once in a blue moon caused a missed archive due to the timestamp changing during iteration.
1 parent e153b60 commit 9beb1b7

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/borg/archiver/prune_cmd.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def quarterly_3monthly_period_func(a):
100100
)
101101

102102

103-
def prune_split(archives, rule, n_or_interval, kept_because=None):
103+
def prune_split(archives, rule, n_or_interval, base_timestamp, kept_because=None):
104104
if isinstance(n_or_interval, int):
105105
n, interval = n_or_interval, None
106106
else:
@@ -119,7 +119,7 @@ def prune_split(archives, rule, n_or_interval, kept_because=None):
119119
period = period_func(a)
120120
if period != last:
121121
last = period
122-
if a.id not in kept_because and (interval is None or a.ts >= datetime.now().astimezone() - interval):
122+
if a.id not in kept_because and (interval is None or a.ts >= base_timestamp - interval):
123123
keep.append(a)
124124
kept_because[a.id] = (rule, len(keep))
125125
if len(keep) == n:
@@ -180,11 +180,12 @@ def do_prune(self, args, repository, manifest):
180180
# (<rulename>, <how many archives were kept by this rule so far >)
181181
kept_because = {}
182182

183+
base_timestamp = datetime.now().astimezone()
183184
# find archives which need to be kept because of the various time period rules
184185
for rule in PRUNING_PATTERNS.keys():
185186
num_or_interval = getattr(args, rule, None)
186187
if num_or_interval is not None:
187-
keep += prune_split(archives, rule, num_or_interval, kept_because)
188+
keep += prune_split(archives, rule, num_or_interval, base_timestamp, kept_because)
188189

189190
to_delete = set(archives) - set(keep)
190191
with Cache(repository, manifest, iec=args.iec) as cache:

src/borg/testsuite/helpers_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def subset(lst, ids):
494494
MockArchive(datetime(2017, 10, 1, 10, 0, 5, tzinfo=None), 13),
495495
]
496496
kept_because = {}
497-
keep = prune_split(archives, rule, num_to_keep, kept_because)
497+
keep = prune_split(archives, rule, num_to_keep, datetime.now().astimezone(), kept_because)
498498

499499
assert set(keep) == subset(archives, expected_ids)
500500
for item in keep:
@@ -517,7 +517,7 @@ def subset(lst, ids):
517517

518518
# Keep oldest when retention target can't otherwise be met
519519
kept_because = {}
520-
keep = prune_split(archives, "yearly", 3, kept_because)
520+
keep = prune_split(archives, "yearly", 3, datetime.now().astimezone(), kept_because)
521521

522522
assert set(keep) == subset(archives, [1, 3, 4])
523523
assert kept_because[1][0] == "yearly[oldest]"
@@ -526,7 +526,7 @@ def subset(lst, ids):
526526

527527
# Otherwise, prune it
528528
kept_because = {}
529-
keep = prune_split(archives, "yearly", 2, kept_because)
529+
keep = prune_split(archives, "yearly", 2, datetime.now().astimezone(), kept_because)
530530

531531
assert set(keep) == subset(archives, [3, 4])
532532
assert kept_because[3][0] == "yearly"
@@ -537,7 +537,7 @@ def test_prune_split_no_archives():
537537
archives = []
538538

539539
kept_because = {}
540-
keep = prune_split(archives, "yearly", 3, kept_because)
540+
keep = prune_split(archives, "yearly", 3, datetime.now().astimezone(), kept_because)
541541

542542
assert keep == []
543543
assert kept_because == {}

0 commit comments

Comments
 (0)