Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/services/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ async def get_state(self, integration_id: str, action_id: str, source_id: str =
async def set_state(self, integration_id: str, action_id: str, state: dict, source_id: str = "no-source"):
for attempt in stamina.retry_context(on=redis.RedisError, attempts=5, wait_initial=1.0, wait_max=30, wait_jitter=3.0):
with attempt:
await self.db_client.set(
await self.db_client.setex(
f"integration_state.{integration_id}.{action_id}.{source_id}",
7*86400,
json.dumps(state, default=str)
)
Comment on lines 23 to 30
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see that in some integrations we want the state data to expire. It'd be better to make that feature optional and let the caller choose whether to use it. We could add a "ttl" or "expiration" param defaulting to None. Then when set we use setex, else we use set. Then update the integrations needing this to set the ttl.

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.

The arguments for set include , ex: Union[int, datetime.timedelta, NoneType] = None so we can continue using the same generalized set method.

set(name: Union[bytes, str, memoryview], value: Union[bytes, memoryview, str, int, float], ex: Union[int, datetime.timedelta, NoneType] = None, ...)


Expand Down
12 changes: 8 additions & 4 deletions app/services/tests/test_state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ async def test_set_integration_state(mocker, mock_redis, integration_v2):
state=state
)

mock_redis.Redis.return_value.set.assert_called_once_with(
mock_redis.Redis.return_value.setex.assert_called_once_with(
f"integration_state.{integration_id}.pull_observations.no-source",
7*86400,
'{"last_execution": "' + execution_timestamp + '"}'
)

Expand Down Expand Up @@ -62,8 +63,9 @@ async def test_delete_integration_state(mocker, mock_redis, integration_v2):
state=state
)

mock_redis.Redis.return_value.set.assert_called_once_with(
mock_redis.Redis.return_value.setex.assert_called_once_with(
f"integration_state.{integration_id}.pull_observations.no-source",
7*86400,
'{"last_execution": "' + execution_timestamp + '"}'
)

Expand Down Expand Up @@ -94,8 +96,9 @@ async def test_set_source_state(mocker, mock_redis, integration_v2, mock_integra
state=mock_integration_state
)

mock_redis.Redis.return_value.set.assert_called_once_with(
mock_redis.Redis.return_value.setex.assert_called_once_with(
f"integration_state.{integration_id}.pull_observations.{source_id}",
7*86400,
json.dumps(mock_integration_state, default=str)
)

Expand Down Expand Up @@ -134,8 +137,9 @@ async def test_delete_state_source_state(mocker, mock_redis, integration_v2, moc
state=mock_integration_state
)

mock_redis.Redis.return_value.set.assert_called_once_with(
mock_redis.Redis.return_value.setex.assert_called_once_with(
f"integration_state.{integration_id}.pull_observations.{source_id}",
7*86400,
json.dumps(mock_integration_state, default=str)
)

Expand Down