port-ocean 0.9.14__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of port-ocean might be problematic. Click here for more details.

Files changed (136) hide show
  1. {port_ocean-0.9.14 → port_ocean-0.10.0}/PKG-INFO +1 -1
  2. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/client.py +17 -0
  3. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/config/settings.py +3 -3
  4. port_ocean-0.10.0/port_ocean/core/event_listener/base.py +84 -0
  5. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/event_listener/http.py +1 -1
  6. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/event_listener/kafka.py +8 -4
  7. port_ocean-0.10.0/port_ocean/core/event_listener/once.py +159 -0
  8. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/event_listener/polling.py +14 -10
  9. port_ocean-0.10.0/port_ocean/core/handlers/resync_state_updater/__init__.py +5 -0
  10. port_ocean-0.10.0/port_ocean/core/handlers/resync_state_updater/updater.py +84 -0
  11. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/models.py +5 -2
  12. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/utils.py +3 -2
  13. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/ocean.py +25 -7
  14. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/misc.py +7 -1
  15. port_ocean-0.10.0/port_ocean/utils/time.py +54 -0
  16. {port_ocean-0.9.14 → port_ocean-0.10.0}/pyproject.toml +2 -1
  17. port_ocean-0.9.14/port_ocean/core/event_listener/base.py +0 -47
  18. port_ocean-0.9.14/port_ocean/core/event_listener/once.py +0 -64
  19. {port_ocean-0.9.14 → port_ocean-0.10.0}/LICENSE.md +0 -0
  20. {port_ocean-0.9.14 → port_ocean-0.10.0}/README.md +0 -0
  21. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/__init__.py +0 -0
  22. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/bootstrap.py +0 -0
  23. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/__init__.py +0 -0
  24. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cli.py +0 -0
  25. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/__init__.py +0 -0
  26. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/defaults/__init___.py +0 -0
  27. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/defaults/clean.py +0 -0
  28. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/defaults/dock.py +0 -0
  29. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/defaults/group.py +0 -0
  30. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/list_integrations.py +0 -0
  31. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/main.py +0 -0
  32. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/new.py +0 -0
  33. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/pull.py +0 -0
  34. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/sail.py +0 -0
  35. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/commands/version.py +0 -0
  36. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/__init__.py +0 -0
  37. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/cookiecutter.json +0 -0
  38. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/extensions.py +0 -0
  39. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/hooks/post_gen_project.py +0 -0
  40. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/.dockerignore +0 -0
  41. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/.gitignore +0 -0
  42. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/.port/resources/.gitignore +0 -0
  43. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/.port/spec.yaml +0 -0
  44. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/CHANGELOG.md +0 -0
  45. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/Dockerfile +0 -0
  46. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/Makefile +0 -0
  47. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/README.md +0 -0
  48. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/changelog/.gitignore +0 -0
  49. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/debug.py +0 -0
  50. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/main.py +0 -0
  51. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/poetry.toml +0 -0
  52. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/pyproject.toml +0 -0
  53. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/sonar-project.properties +0 -0
  54. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/tests/__init__.py +0 -0
  55. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/cookiecutter/{{cookiecutter.integration_slug}}/tests/test_sample.py +0 -0
  56. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/cli/utils.py +0 -0
  57. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/__init__.py +0 -0
  58. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/__init__.py +0 -0
  59. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/authentication.py +0 -0
  60. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/mixins/__init__.py +0 -0
  61. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/mixins/blueprints.py +0 -0
  62. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/mixins/entities.py +0 -0
  63. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/mixins/integrations.py +0 -0
  64. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/mixins/migrations.py +0 -0
  65. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/retry_transport.py +0 -0
  66. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/types.py +0 -0
  67. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/clients/port/utils.py +0 -0
  68. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/config/__init__.py +0 -0
  69. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/config/base.py +0 -0
  70. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/config/dynamic.py +0 -0
  71. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/consumers/__init__.py +0 -0
  72. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/consumers/kafka_consumer.py +0 -0
  73. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/context/__init__.py +0 -0
  74. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/context/event.py +0 -0
  75. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/context/ocean.py +0 -0
  76. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/context/resource.py +0 -0
  77. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/__init__.py +0 -0
  78. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/defaults/__init__.py +0 -0
  79. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/defaults/clean.py +0 -0
  80. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/defaults/common.py +0 -0
  81. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/defaults/initialize.py +0 -0
  82. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/event_listener/__init__.py +0 -0
  83. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/event_listener/factory.py +0 -0
  84. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/__init__.py +0 -0
  85. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/base.py +0 -0
  86. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/__init__.py +0 -0
  87. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/base.py +0 -0
  88. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/port/__init__.py +0 -0
  89. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/port/applier.py +0 -0
  90. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/port/get_related_entities.py +0 -0
  91. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entities_state_applier/port/order_by_entities_dependencies.py +0 -0
  92. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entity_processor/__init__.py +0 -0
  93. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entity_processor/base.py +0 -0
  94. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/entity_processor/jq_entity_processor.py +0 -0
  95. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/port_app_config/__init__.py +0 -0
  96. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/port_app_config/api.py +0 -0
  97. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/port_app_config/base.py +0 -0
  98. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/handlers/port_app_config/models.py +0 -0
  99. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/__init__.py +0 -0
  100. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/base.py +0 -0
  101. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/__init__.py +0 -0
  102. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/events.py +0 -0
  103. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/handler.py +0 -0
  104. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/sync.py +0 -0
  105. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/sync_raw.py +0 -0
  106. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/integrations/mixins/utils.py +0 -0
  107. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/core/ocean_types.py +0 -0
  108. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/__init__.py +0 -0
  109. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/api.py +0 -0
  110. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/base.py +0 -0
  111. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/clients.py +0 -0
  112. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/context.py +0 -0
  113. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/core.py +0 -0
  114. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/port_defaults.py +0 -0
  115. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/exceptions/utils.py +0 -0
  116. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/helpers/__init__.py +0 -0
  117. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/helpers/async_client.py +0 -0
  118. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/helpers/retry.py +0 -0
  119. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/log/__init__.py +0 -0
  120. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/log/handlers.py +0 -0
  121. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/log/logger_setup.py +0 -0
  122. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/log/sensetive.py +0 -0
  123. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/middlewares.py +0 -0
  124. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/py.typed +0 -0
  125. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/run.py +0 -0
  126. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/sonar-project.properties +0 -0
  127. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/tests/__init__.py +0 -0
  128. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/tests/test_sample.py +0 -0
  129. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/__init__.py +0 -0
  130. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/async_http.py +0 -0
  131. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/async_iterators.py +0 -0
  132. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/cache.py +0 -0
  133. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/queue_utils.py +0 -0
  134. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/repeat.py +0 -0
  135. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/utils/signal.py +0 -0
  136. {port_ocean-0.9.14 → port_ocean-0.10.0}/port_ocean/version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: port-ocean
3
- Version: 0.9.14
3
+ Version: 0.10.0
4
4
  Summary: Port Ocean is a CLI tool for managing your Port projects.
5
5
  Home-page: https://app.getport.io
6
6
  Keywords: ocean,port-ocean,port
@@ -13,6 +13,7 @@ from port_ocean.clients.port.utils import (
13
13
  get_internal_http_client,
14
14
  )
15
15
  from port_ocean.exceptions.clients import KafkaCredentialsNotFound
16
+ from typing import Any
16
17
 
17
18
 
18
19
  class PortClient(
@@ -75,3 +76,19 @@ class PortClient(
75
76
  handle_status_code(response)
76
77
 
77
78
  return response.json()["organization"]["id"]
79
+
80
+ async def update_integration_state(
81
+ self, state: dict[str, Any], should_raise: bool = True, should_log: bool = True
82
+ ) -> dict[str, Any]:
83
+ if should_log:
84
+ logger.debug(f"Updating integration resync state with: {state}")
85
+ response = await self.client.patch(
86
+ f"{self.api_url}/integration/{self.integration_identifier}/resync-state",
87
+ headers=await self.auth.headers(),
88
+ json=state,
89
+ )
90
+ handle_status_code(response, should_raise, should_log)
91
+ if response.is_success and should_log:
92
+ logger.info("Integration resync state updated successfully")
93
+
94
+ return response.json().get("integration", {})
@@ -76,7 +76,7 @@ class IntegrationConfiguration(BaseOceanSettings, extra=Extra.allow):
76
76
  integration: IntegrationSettings = Field(
77
77
  default_factory=lambda: IntegrationSettings(type="", identifier="")
78
78
  )
79
- runtime: Runtime = "OnPrem"
79
+ runtime: Runtime = Runtime.OnPrem
80
80
 
81
81
  @root_validator()
82
82
  def validate_integration_config(cls, values: dict[str, Any]) -> dict[str, Any]:
@@ -100,8 +100,8 @@ class IntegrationConfiguration(BaseOceanSettings, extra=Extra.allow):
100
100
  return values
101
101
 
102
102
  @validator("runtime")
103
- def validate_runtime(cls, runtime: Literal["OnPrem", "Saas"]) -> Runtime:
104
- if runtime == "Saas":
103
+ def validate_runtime(cls, runtime: Runtime) -> Runtime:
104
+ if runtime == Runtime.Saas:
105
105
  spec = get_spec_file()
106
106
  if spec is None:
107
107
  raise ValueError(
@@ -0,0 +1,84 @@
1
+ from abc import abstractmethod
2
+ from typing import TypedDict, Callable, Any, Awaitable
3
+
4
+ from pydantic import Extra
5
+
6
+ from port_ocean.config.base import BaseOceanModel
7
+ from port_ocean.utils.signal import signal_handler
8
+ from port_ocean.context.ocean import ocean
9
+ from port_ocean.utils.misc import IntegrationStateStatus
10
+
11
+
12
+ class EventListenerEvents(TypedDict):
13
+ """
14
+ A dictionary containing event types and their corresponding event handlers.
15
+ """
16
+
17
+ on_resync: Callable[[dict[Any, Any]], Awaitable[None]]
18
+
19
+
20
+ class BaseEventListener:
21
+ def __init__(
22
+ self,
23
+ events: EventListenerEvents,
24
+ ):
25
+ self.events = events
26
+
27
+ async def start(self) -> None:
28
+ signal_handler.register(self._stop)
29
+ await self._start()
30
+
31
+ @abstractmethod
32
+ async def _start(self) -> None:
33
+ pass
34
+
35
+ def _stop(self) -> None:
36
+ """
37
+ Can be used for event listeners that need cleanup before exiting.
38
+ """
39
+ pass
40
+
41
+ async def _before_resync(self) -> None:
42
+ """
43
+ Can be used for event listeners that need to perform some action before resync.
44
+ """
45
+ await ocean.app.resync_state_updater.update_before_resync()
46
+
47
+ async def _after_resync(self) -> None:
48
+ """
49
+ Can be used for event listeners that need to perform some action after resync.
50
+ """
51
+ await ocean.app.resync_state_updater.update_after_resync()
52
+
53
+ async def _on_resync_failure(self, e: Exception) -> None:
54
+ """
55
+ Can be used for event listeners that need to handle resync failures.
56
+ """
57
+ await ocean.app.resync_state_updater.update_after_resync(
58
+ IntegrationStateStatus.Failed
59
+ )
60
+
61
+ async def _resync(
62
+ self,
63
+ resync_args: dict[Any, Any],
64
+ ) -> None:
65
+ """
66
+ Triggers the "on_resync" event.
67
+ """
68
+ await self._before_resync()
69
+ try:
70
+ await self.events["on_resync"](resync_args)
71
+ await self._after_resync()
72
+ except Exception as e:
73
+ await self._on_resync_failure(e)
74
+ raise e
75
+
76
+
77
+ class EventListenerSettings(BaseOceanModel, extra=Extra.allow):
78
+ type: str
79
+
80
+ def to_request(self) -> dict[str, Any]:
81
+ """
82
+ Converts the Settings object to a dictionary representation (request format).
83
+ """
84
+ return {"type": self.type}
@@ -64,6 +64,6 @@ class HttpEventListener(BaseEventListener):
64
64
 
65
65
  @target_channel_router.post("/resync")
66
66
  async def resync() -> None:
67
- await self.events["on_resync"]({})
67
+ await self._resync({})
68
68
 
69
69
  ocean.app.fast_api_app.include_router(target_channel_router)
@@ -99,9 +99,13 @@ class KafkaEventListener(BaseEventListener):
99
99
  return False
100
100
 
101
101
  integration_identifier = after.get("identifier")
102
- if integration_identifier == self.integration_identifier and (
103
- "change.log" in topic
104
- ):
102
+ if integration_identifier != self.integration_identifier:
103
+ return False
104
+
105
+ if after.get("updatedAt") == after.get("resyncState", {}).get("updatedAt"):
106
+ return False
107
+
108
+ if "change.log" in topic:
105
109
  return msg_value.get("changelogDestination", {}).get("type", "") == "KAFKA"
106
110
 
107
111
  return False
@@ -122,7 +126,7 @@ class KafkaEventListener(BaseEventListener):
122
126
 
123
127
  if "change.log" in topic and message is not None:
124
128
  try:
125
- await self.events["on_resync"](message)
129
+ await self._resync(message)
126
130
  except Exception as e:
127
131
  _type, _, tb = sys.exc_info()
128
132
  logger.opt(exception=(_type, None, tb)).error(
@@ -0,0 +1,159 @@
1
+ import datetime
2
+ import signal
3
+ from typing import Literal, Any
4
+
5
+ from loguru import logger
6
+
7
+ from port_ocean.core.event_listener.base import (
8
+ BaseEventListener,
9
+ EventListenerEvents,
10
+ EventListenerSettings,
11
+ )
12
+ from port_ocean.utils.repeat import repeat_every
13
+ from port_ocean.context.ocean import ocean
14
+ from port_ocean.utils.time import convert_str_to_utc_datetime, convert_to_minutes
15
+ from port_ocean.utils.misc import IntegrationStateStatus
16
+
17
+
18
+ class OnceEventListenerSettings(EventListenerSettings):
19
+ """
20
+ Once event listener configuration settings.
21
+ This class inherits from `EventListenerSettings`, which provides a foundation for creating event listener settings.
22
+ """
23
+
24
+ type: Literal["ONCE"]
25
+
26
+ def to_request(self) -> dict[str, Any]:
27
+ return {}
28
+
29
+
30
+ class OnceEventListener(BaseEventListener):
31
+ """
32
+ Once event listener.
33
+
34
+ This event listener is used to trigger a resync immediately.
35
+
36
+ Parameters:
37
+ events (EventListenerEvents): A dictionary containing event types and their corresponding event handlers.
38
+ event_listener_config (OnceEventListenerSettings): The event listener configuration settings.
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ events: EventListenerEvents,
44
+ event_listener_config: OnceEventListenerSettings,
45
+ ):
46
+ super().__init__(events)
47
+ self.event_listener_config = event_listener_config
48
+ self.cached_integration: dict[str, Any] | None = None
49
+
50
+ async def get_current_integration_cached(self) -> dict[str, Any]:
51
+ if self.cached_integration:
52
+ return self.cached_integration
53
+
54
+ self.cached_integration = await ocean.port_client.get_current_integration()
55
+ return self.cached_integration
56
+
57
+ async def get_saas_resync_initialization_and_interval(
58
+ self,
59
+ ) -> tuple[int | None, datetime.datetime | None]:
60
+ """
61
+ Get the scheduled resync interval and the last updated time of the integration config for the saas application.
62
+ interval is the saas configured resync interval time.
63
+ start_time is the last updated time of the integration config.
64
+ return: (interval, start_time)
65
+ """
66
+ if not ocean.app.is_saas():
67
+ return (None, None)
68
+
69
+ try:
70
+ integration = await self.get_current_integration_cached()
71
+ except Exception as e:
72
+ logger.exception(f"Error occurred while getting current integration {e}")
73
+ return (None, None)
74
+
75
+ interval_str = (
76
+ integration.get("spec", {})
77
+ .get("appSpec", {})
78
+ .get("scheduledResyncInterval")
79
+ )
80
+
81
+ if not interval_str:
82
+ logger.error(
83
+ "Unexpected: scheduledResyncInterval not found for Saas integration, Cannot predict the next resync"
84
+ )
85
+ return (None, None)
86
+
87
+ last_updated_saas_integration_config_str = integration.get(
88
+ "statusInfo", {}
89
+ ).get("updatedAt")
90
+
91
+ # we use the last updated time of the integration config as the start time since in saas application the interval is configured by the user from the portal
92
+ if not last_updated_saas_integration_config_str:
93
+ logger.error(
94
+ "Unexpected: updatedAt not found for Saas integration, Cannot predict the next resync"
95
+ )
96
+ return (None, None)
97
+
98
+ return (
99
+ convert_to_minutes(interval_str),
100
+ convert_str_to_utc_datetime(last_updated_saas_integration_config_str),
101
+ )
102
+
103
+ async def _before_resync(self) -> None:
104
+ if not ocean.app.is_saas():
105
+ # in case of non-saas, we still want to update the state before and after the resync
106
+ await super()._before_resync()
107
+ return
108
+
109
+ (interval, start_time) = (
110
+ await self.get_saas_resync_initialization_and_interval()
111
+ )
112
+ await ocean.app.resync_state_updater.update_before_resync(interval, start_time)
113
+
114
+ async def _after_resync(self) -> None:
115
+ if not ocean.app.is_saas():
116
+ # in case of non-saas, we still want to update the state before and after the resync
117
+ await super()._after_resync()
118
+ return
119
+
120
+ (interval, start_time) = (
121
+ await self.get_saas_resync_initialization_and_interval()
122
+ )
123
+ await ocean.app.resync_state_updater.update_after_resync(
124
+ IntegrationStateStatus.Completed, interval, start_time
125
+ )
126
+
127
+ async def _on_resync_failure(self, e: Exception) -> None:
128
+ if not ocean.app.is_saas():
129
+ # in case of non-saas, we still want to update the state before and after the resync
130
+ await super()._after_resync()
131
+ return
132
+
133
+ (interval, start_time) = (
134
+ await self.get_saas_resync_initialization_and_interval()
135
+ )
136
+ await ocean.app.resync_state_updater.update_after_resync(
137
+ IntegrationStateStatus.Failed, interval, start_time
138
+ )
139
+
140
+ async def _start(self) -> None:
141
+ """
142
+ Starts the resync process, and exits the application once finished.
143
+ """
144
+
145
+ # we use the `repeat_every` decorator to make sure the resync will be triggered, but won't stuck the application
146
+ # from finishing the startup process which is required to close the application gracefully
147
+ @repeat_every(seconds=0, max_repetitions=1)
148
+ async def resync_and_exit() -> None:
149
+ logger.info("Once event listener started")
150
+ try:
151
+ await self._resync({})
152
+ except Exception:
153
+ # we catch all exceptions here to make sure the application will exit gracefully
154
+ logger.exception("Error occurred while resyncing")
155
+ logger.info("Once event listener finished")
156
+ logger.info("Exiting application")
157
+ signal.raise_signal(signal.SIGINT)
158
+
159
+ await resync_and_exit()
@@ -49,7 +49,16 @@ class PollingEventListener(BaseEventListener):
49
49
  ):
50
50
  super().__init__(events)
51
51
  self.event_listener_config = event_listener_config
52
- self._last_updated_at = None
52
+
53
+ def should_resync(self, last_updated_at: str) -> bool:
54
+ _last_updated_at = (
55
+ ocean.app.resync_state_updater.last_integration_state_updated_at
56
+ )
57
+
58
+ if _last_updated_at is None:
59
+ return self.event_listener_config.resync_on_start
60
+
61
+ return _last_updated_at != last_updated_at
53
62
 
54
63
  async def _start(self) -> None:
55
64
  """
@@ -69,17 +78,12 @@ class PollingEventListener(BaseEventListener):
69
78
  integration = await ocean.app.port_client.get_current_integration()
70
79
  last_updated_at = integration["updatedAt"]
71
80
 
72
- should_resync = (
73
- self._last_updated_at is not None
74
- or self.event_listener_config.resync_on_start
75
- ) and self._last_updated_at != last_updated_at
76
-
77
- if should_resync:
81
+ if self.should_resync(last_updated_at):
78
82
  logger.info("Detected change in integration, resyncing")
79
- self._last_updated_at = last_updated_at
80
- running_task: Task[Any] = get_event_loop().create_task(
81
- self.events["on_resync"]({}) # type: ignore
83
+ ocean.app.resync_state_updater.last_integration_state_updated_at = (
84
+ last_updated_at
82
85
  )
86
+ running_task: Task[Any] = get_event_loop().create_task(self._resync({}))
83
87
  signal_handler.register(running_task.cancel)
84
88
 
85
89
  await running_task
@@ -0,0 +1,5 @@
1
+ from .updater import ResyncStateUpdater
2
+
3
+ __all__ = [
4
+ "ResyncStateUpdater",
5
+ ]
@@ -0,0 +1,84 @@
1
+ import datetime
2
+ from typing import Any, Literal
3
+ from port_ocean.clients.port.client import PortClient
4
+ from port_ocean.utils.misc import IntegrationStateStatus
5
+ from port_ocean.utils.time import get_next_occurrence
6
+
7
+
8
+ class ResyncStateUpdater:
9
+ def __init__(self, port_client: PortClient, scheduled_resync_interval: int | None):
10
+ self.port_client = port_client
11
+ self.initiated_at = datetime.datetime.now(tz=datetime.timezone.utc)
12
+ self.scheduled_resync_interval = scheduled_resync_interval
13
+
14
+ # This is used to differ between integration changes that require a full resync and state changes
15
+ # So that the polling event-listener can decide whether to perform a full resync or not
16
+ # TODO: remove this once we separate the state from the integration
17
+ self.last_integration_state_updated_at: str = ""
18
+
19
+ def _calculate_next_scheduled_resync(
20
+ self,
21
+ interval: int | None = None,
22
+ custom_start_time: datetime.datetime | None = None,
23
+ ) -> str | None:
24
+ if interval is None:
25
+ return None
26
+ return get_next_occurrence(
27
+ interval * 60, custom_start_time or self.initiated_at
28
+ ).isoformat()
29
+
30
+ async def update_before_resync(
31
+ self,
32
+ interval: int | None = None,
33
+ custom_start_time: datetime.datetime | None = None,
34
+ ) -> None:
35
+ _interval = interval or self.scheduled_resync_interval
36
+ nest_resync = self._calculate_next_scheduled_resync(
37
+ _interval, custom_start_time
38
+ )
39
+ state: dict[str, Any] = {
40
+ "status": IntegrationStateStatus.Running.value,
41
+ "lastResyncEnd": None,
42
+ "lastResyncStart": datetime.datetime.now(
43
+ tz=datetime.timezone.utc
44
+ ).isoformat(),
45
+ "nextResync": nest_resync,
46
+ "intervalInMinuets": _interval,
47
+ }
48
+
49
+ integration = await self.port_client.update_integration_state(
50
+ state, should_raise=False
51
+ )
52
+ if integration:
53
+ self.last_integration_state_updated_at = integration["resyncState"][
54
+ "updatedAt"
55
+ ]
56
+
57
+ async def update_after_resync(
58
+ self,
59
+ status: Literal[
60
+ IntegrationStateStatus.Completed, IntegrationStateStatus.Failed
61
+ ] = IntegrationStateStatus.Completed,
62
+ interval: int | None = None,
63
+ custom_start_time: datetime.datetime | None = None,
64
+ ) -> None:
65
+ _interval = interval or self.scheduled_resync_interval
66
+ nest_resync = self._calculate_next_scheduled_resync(
67
+ _interval, custom_start_time
68
+ )
69
+ state: dict[str, Any] = {
70
+ "status": status.value,
71
+ "lastResyncEnd": datetime.datetime.now(
72
+ tz=datetime.timezone.utc
73
+ ).isoformat(),
74
+ "nextResync": nest_resync,
75
+ "intervalInMinuets": _interval,
76
+ }
77
+
78
+ integration = await self.port_client.update_integration_state(
79
+ state, should_raise=False
80
+ )
81
+ if integration:
82
+ self.last_integration_state_updated_at = integration["resyncState"][
83
+ "updatedAt"
84
+ ]
@@ -1,11 +1,14 @@
1
1
  from dataclasses import dataclass, field
2
- from typing import Any, Literal
2
+ from enum import Enum
3
+ from typing import Any
3
4
 
4
5
  from pydantic import BaseModel
5
6
  from pydantic.fields import Field
6
7
 
7
8
 
8
- Runtime = Literal["OnPrem", "Saas"]
9
+ class Runtime(Enum):
10
+ Saas = "Saas"
11
+ OnPrem = "OnPrem"
9
12
 
10
13
 
11
14
  class Entity(BaseModel):
@@ -35,12 +35,13 @@ def is_same_entity(first_entity: Entity, second_entity: Entity) -> bool:
35
35
 
36
36
 
37
37
  async def validate_integration_runtime(
38
- port_client: PortClient, requested_runtime: Runtime
38
+ port_client: PortClient,
39
+ requested_runtime: Runtime,
39
40
  ) -> None:
40
41
  logger.debug("Validating integration runtime")
41
42
  current_integration = await port_client.get_current_integration(should_raise=False)
42
43
  current_runtime = current_integration.get("installationType", "OnPrem")
43
- if current_integration and current_runtime != requested_runtime:
44
+ if current_integration and current_runtime != requested_runtime.value:
44
45
  raise IntegrationRuntimeException(
45
46
  f"Invalid Runtime! Requested to run existing {current_runtime} integration in {requested_runtime} runtime."
46
47
  )
@@ -9,6 +9,8 @@ from loguru import logger
9
9
  from pydantic import BaseModel
10
10
  from starlette.types import Scope, Receive, Send
11
11
 
12
+ from port_ocean.core.handlers.resync_state_updater import ResyncStateUpdater
13
+ from port_ocean.core.models import Runtime
12
14
  from port_ocean.clients.port.client import PortClient
13
15
  from port_ocean.config.settings import (
14
16
  IntegrationConfiguration,
@@ -24,6 +26,7 @@ from port_ocean.middlewares import request_handler
24
26
  from port_ocean.utils.repeat import repeat_every
25
27
  from port_ocean.utils.signal import signal_handler
26
28
  from port_ocean.version import __integration_version__
29
+ from port_ocean.utils.misc import IntegrationStateStatus
27
30
 
28
31
 
29
32
  class Ocean:
@@ -63,16 +66,27 @@ class Ocean:
63
66
  integration_class(ocean) if integration_class else BaseIntegration(ocean)
64
67
  )
65
68
 
69
+ self.resync_state_updater = ResyncStateUpdater(
70
+ self.port_client, self.config.scheduled_resync_interval
71
+ )
72
+
73
+ def is_saas(self) -> bool:
74
+ return self.config.runtime == Runtime.Saas
75
+
66
76
  async def _setup_scheduled_resync(
67
77
  self,
68
78
  ) -> None:
69
- def execute_resync_all() -> None:
70
- loop = asyncio.new_event_loop()
71
- asyncio.set_event_loop(loop)
72
-
79
+ async def execute_resync_all() -> None:
80
+ await self.resync_state_updater.update_before_resync()
73
81
  logger.info("Starting a new scheduled resync")
74
- loop.run_until_complete(self.integration.sync_raw_all())
75
- loop.close()
82
+ try:
83
+ await self.integration.sync_raw_all()
84
+ await self.resync_state_updater.update_after_resync()
85
+ except Exception as e:
86
+ await self.resync_state_updater.update_after_resync(
87
+ IntegrationStateStatus.Failed
88
+ )
89
+ raise e
76
90
 
77
91
  interval = self.config.scheduled_resync_interval
78
92
  if interval is not None:
@@ -83,7 +97,11 @@ class Ocean:
83
97
  seconds=interval * 60,
84
98
  # Not running the resync immediately because the event listener should run resync on startup
85
99
  wait_first=True,
86
- )(lambda: threading.Thread(target=execute_resync_all).start())
100
+ )(
101
+ lambda: threading.Thread(
102
+ target=lambda: asyncio.run(execute_resync_all())
103
+ ).start()
104
+ )
87
105
  await repeated_function()
88
106
 
89
107
  async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
@@ -1,3 +1,4 @@
1
+ from enum import Enum
1
2
  import inspect
2
3
  from importlib.util import spec_from_file_location, module_from_spec
3
4
  from pathlib import Path
@@ -5,11 +6,16 @@ from time import time
5
6
  from types import ModuleType
6
7
  from typing import Callable, Any
7
8
  from uuid import uuid4
8
-
9
9
  import tomli
10
10
  import yaml
11
11
 
12
12
 
13
+ class IntegrationStateStatus(Enum):
14
+ Running = "running"
15
+ Failed = "failed"
16
+ Completed = "completed"
17
+
18
+
13
19
  def get_time(seconds_precision: bool = True) -> float:
14
20
  """Return current time as Unix/Epoch timestamp, in seconds.
15
21
  :param seconds_precision: if True, return with seconds precision as integer (default).
@@ -0,0 +1,54 @@
1
+ import datetime
2
+ from loguru import logger
3
+
4
+
5
+ def convert_str_to_utc_datetime(time_str: str) -> datetime.datetime | None:
6
+ """
7
+ Convert a string representing time to a datetime object.
8
+ :param time_str: a string representing time in the format "2021-09-01T12:00:00Z"
9
+ """
10
+ aware_date = datetime.datetime.fromisoformat(time_str)
11
+ if time_str.endswith("Z"):
12
+ aware_date = datetime.datetime.fromisoformat(time_str.replace("Z", "+00:00"))
13
+ return aware_date.astimezone(datetime.timezone.utc)
14
+
15
+
16
+ def convert_to_minutes(s: str) -> int:
17
+ minutes_per_unit = {"s": 1 / 60, "m": 1, "h": 60, "d": 1440, "w": 10080}
18
+ try:
19
+ return int(int(s[:-1]) * minutes_per_unit[s[-1]])
20
+ except Exception:
21
+ logger.error(f"Failed converting string to minutes, {s}")
22
+ raise ValueError(
23
+ f"Invalid format. Expected a string ending with {minutes_per_unit.keys()}"
24
+ )
25
+
26
+
27
+ def get_next_occurrence(
28
+ interval_seconds: int,
29
+ start_time: datetime.datetime,
30
+ now: datetime.datetime | None = None,
31
+ ) -> datetime.datetime:
32
+ """
33
+ Predict the next occurrence of an event based on interval, start time, and current time.
34
+
35
+ :param interval_minutes: Interval between occurrences in minutes.
36
+ :param start_time: Start time of the event as a datetime object.
37
+ :param now: Current time as a datetime object.
38
+ :return: The next occurrence time as a datetime object.
39
+ """
40
+
41
+ if now is None:
42
+ now = datetime.datetime.now(tz=datetime.timezone.utc)
43
+ # Calculate the total seconds elapsed since the start time
44
+ elapsed_seconds = (now - start_time).total_seconds()
45
+
46
+ # Calculate the number of intervals that have passed
47
+ intervals_passed = int(elapsed_seconds // interval_seconds)
48
+
49
+ # Calculate the next occurrence time
50
+ next_occurrence = start_time + datetime.timedelta(
51
+ seconds=(intervals_passed + 1) * interval_seconds
52
+ )
53
+
54
+ return next_occurrence
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "port-ocean"
3
- version = "0.9.14"
3
+ version = "0.10.0"
4
4
  description = "Port Ocean is a CLI tool for managing your Port projects."
5
5
  readme = "README.md"
6
6
  homepage = "https://app.getport.io"
@@ -75,6 +75,7 @@ towncrier = "^23.6.0"
75
75
  yamllint = "^1.32.0"
76
76
  types-python-dateutil = "^2.9.0.20240316"
77
77
  pytest-xdist = "^3.6.1"
78
+ pre-commit = "^3.7.1"
78
79
 
79
80
  [tool.towncrier]
80
81
  directory = "changelog"