forked from port-labs/ocean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] ocean core next resync (port-labs#835)
# Description **What:** - Integrated functionality to send the state of the integration on each sync. - Implemented prediction of the next sync date. **Why:** - To ensure that the integration state is consistently updated and monitored. - To enhance the accuracy and reliability of the next sync date prediction by utilizing server data for SaaS applications, rather than relying solely on environment variables. **How:** - Updated the sync logic to include the current integration state in the payload sent to our monitoring system. - Modified the sync prediction mechanism for SaaS applications to use data from our servers, providing more accurate and context-aware predictions. ## Type of change Please leave one option from the following and delete the rest: - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] New Integration (non-breaking change which adds a new integration) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Non-breaking change (fix of existing functionality that will not change current behavior) - [ ] Documentation (added/updated documentation) --------- Co-authored-by: Shalev Avhar <[email protected]> Co-authored-by: Tom Tankilevitch <[email protected]>
- Loading branch information
1 parent
f96ea28
commit d54b97b
Showing
16 changed files
with
360 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .updater import ResyncStateUpdater | ||
|
||
__all__ = [ | ||
"ResyncStateUpdater", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import datetime | ||
from typing import Any, Literal | ||
from port_ocean.clients.port.client import PortClient | ||
from port_ocean.utils.misc import IntegrationStateStatus | ||
from port_ocean.utils.time import get_next_occurrence | ||
|
||
|
||
class ResyncStateUpdater: | ||
def __init__(self, port_client: PortClient, scheduled_resync_interval: int | None): | ||
self.port_client = port_client | ||
self.initiated_at = datetime.datetime.now(tz=datetime.timezone.utc) | ||
self.scheduled_resync_interval = scheduled_resync_interval | ||
|
||
# This is used to differ between integration changes that require a full resync and state changes | ||
# So that the polling event-listener can decide whether to perform a full resync or not | ||
# TODO: remove this once we separate the state from the integration | ||
self.last_integration_state_updated_at: str = "" | ||
|
||
def _calculate_next_scheduled_resync( | ||
self, | ||
interval: int | None = None, | ||
custom_start_time: datetime.datetime | None = None, | ||
) -> str | None: | ||
if interval is None: | ||
return None | ||
return get_next_occurrence( | ||
interval * 60, custom_start_time or self.initiated_at | ||
).isoformat() | ||
|
||
async def update_before_resync( | ||
self, | ||
interval: int | None = None, | ||
custom_start_time: datetime.datetime | None = None, | ||
) -> None: | ||
_interval = interval or self.scheduled_resync_interval | ||
nest_resync = self._calculate_next_scheduled_resync( | ||
_interval, custom_start_time | ||
) | ||
state: dict[str, Any] = { | ||
"status": IntegrationStateStatus.Running.value, | ||
"lastResyncEnd": None, | ||
"lastResyncStart": datetime.datetime.now( | ||
tz=datetime.timezone.utc | ||
).isoformat(), | ||
"nextResync": nest_resync, | ||
"intervalInMinuets": _interval, | ||
} | ||
|
||
integration = await self.port_client.update_integration_state( | ||
state, should_raise=False | ||
) | ||
if integration: | ||
self.last_integration_state_updated_at = integration["resyncState"][ | ||
"updatedAt" | ||
] | ||
|
||
async def update_after_resync( | ||
self, | ||
status: Literal[ | ||
IntegrationStateStatus.Completed, IntegrationStateStatus.Failed | ||
] = IntegrationStateStatus.Completed, | ||
interval: int | None = None, | ||
custom_start_time: datetime.datetime | None = None, | ||
) -> None: | ||
_interval = interval or self.scheduled_resync_interval | ||
nest_resync = self._calculate_next_scheduled_resync( | ||
_interval, custom_start_time | ||
) | ||
state: dict[str, Any] = { | ||
"status": status.value, | ||
"lastResyncEnd": datetime.datetime.now( | ||
tz=datetime.timezone.utc | ||
).isoformat(), | ||
"nextResync": nest_resync, | ||
"intervalInMinuets": _interval, | ||
} | ||
|
||
integration = await self.port_client.update_integration_state( | ||
state, should_raise=False | ||
) | ||
if integration: | ||
self.last_integration_state_updated_at = integration["resyncState"][ | ||
"updatedAt" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.