Skip to content

Commit bae09b0

Browse files
committed
flag as processed after request forwarded to next service
1 parent 103667a commit bae09b0

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

collector/host_inventory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def worker(_: str, source_id: str, dest: str, b64_identity: str) -> None:
109109
prometheus_metrics.METRICS['posts'].inc()
110110
try:
111111
utils.retryable('post', dest, json=data, headers=headers)
112+
utils.set_processed(account_id)
112113
prometheus_metrics.METRICS['post_successes'].inc()
113114
except utils.RetryFailedError as exception:
114115
LOGGER.error(

collector/utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
REDIS = redis.Redis(**json.loads(REDIS_ENV), password=REDIS_PASSWORD)
1818

1919

20-
def processed(account_id: str) -> bool:
20+
def processed(key: str) -> bool:
2121
"""If an account has been processed within the window."""
22-
if REDIS.incr(account_id) == 1:
23-
REDIS.expire(account_id, PROCESS_WINDOW)
24-
return False
25-
return True
22+
return REDIS.get(key)
23+
24+
25+
def set_processed(key: str) -> None:
26+
"""Flag an account as processed with TTL."""
27+
REDIS.set(key, 1, ex=PROCESS_WINDOW)
2628

2729

2830
class RetryFailedError(requests.HTTPError):

tests/collector/test_host_inventory.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,25 @@ class TestRedisLookup:
112112
def test_not_processed_yet(self, mocker):
113113
"""When account not processed before."""
114114
redis = mocker.MagicMock()
115-
redis.incr.return_value = 1
115+
redis.get.return_value = None
116116
mocker.patch.object(collector.utils, 'REDIS', redis)
117117
assert not collector.utils.processed('abc')
118-
redis.expire.assert_called_once_with(
119-
'abc',
120-
collector.utils.PROCESS_WINDOW
121-
)
122118

123119
def test_processed_before(self, mocker):
124120
"""When account processed before."""
125121
redis = mocker.MagicMock()
126-
redis.incr.return_value = 2
122+
redis.get.return_value = 1
127123
mocker.patch.object(collector.utils, 'REDIS', redis)
128124
assert collector.utils.processed('abc')
125+
126+
def test_set_processed(self, mocker):
127+
"""Flag an account as processed before."""
128+
redis = mocker.MagicMock()
129+
redis.set.return_value = True
130+
mocker.patch.object(collector.utils, 'REDIS', redis)
131+
collector.utils.set_processed('abc')
132+
redis.set.assert_called_once_with(
133+
'abc',
134+
1,
135+
ex=collector.utils.PROCESS_WINDOW,
136+
)

0 commit comments

Comments
 (0)