-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for new runtime and base class
The idea here is to split the responsibilities of the current base class into two: A runtime and an extractor. The runtime is responsible for parsing command line arguments, loading config files and so on, before spawning the extractor in a separate process. The runtime will automatically restart the extractor if it crashes, but can also be asked by the extractor to restart it - for example after a config change. The extractor class is then only responsible for running the extractor application itself, making it much cleaner.
- Loading branch information
Showing
9 changed files
with
399 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ __pycache__/ | |
# Local test files | ||
test.py | ||
test.yaml | ||
local-test.yaml | ||
pyrightconfig.json | ||
|
||
# Tokens, etc | ||
|
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
Empty file.
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,31 @@ | ||
""" | ||
Example of how you would build an extractor with the new base class | ||
""" | ||
|
||
from cognite.extractorutils.unstable.configuration.models import ExtractorConfig | ||
|
||
from .base import Extractor | ||
from .runtime import Runtime | ||
|
||
|
||
class MyConfig(ExtractorConfig): | ||
parameter_one: int | ||
parameter_two: str | ||
|
||
|
||
class MyExtractor(Extractor[MyConfig]): | ||
NAME = "Test extractor" | ||
EXTERNAL_ID = "test-extractor" | ||
DESCRIPTION = "Test of the new runtime" | ||
VERSION = "1.0.0" | ||
CONFIG_TYPE = MyConfig | ||
|
||
def run(self) -> None: | ||
self.logger.info("Started!") | ||
if not self.cancellation_token.wait(10): | ||
raise ValueError("Oops") | ||
|
||
|
||
if __name__ == "__main__": | ||
runtime = Runtime(MyExtractor) | ||
runtime.run() |
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 enum import Enum | ||
|
||
|
||
class RuntimeMessage(Enum): | ||
RESTART = 1 |
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,119 @@ | ||
import logging | ||
from multiprocessing import Queue | ||
from threading import RLock, Thread | ||
from types import TracebackType | ||
from typing import Generic, Literal, Optional, Type, TypeVar, Union | ||
|
||
try: | ||
from typing import Self | ||
except ImportError: | ||
from typing_extensions import Self | ||
|
||
from cognite.extractorutils.threading import CancellationToken | ||
from cognite.extractorutils.unstable.configuration.models import ConnectionConfig, ExtractorConfig | ||
from cognite.extractorutils.unstable.core._messaging import RuntimeMessage | ||
|
||
ConfigType = TypeVar("ConfigType", bound=ExtractorConfig) | ||
ConfigRevision = Union[Literal["local"], int] | ||
|
||
|
||
class Extractor(Generic[ConfigType]): | ||
NAME: str | ||
EXTERNAL_ID: str | ||
DESCRIPTION: str | ||
VERSION: str | ||
|
||
CONFIG_TYPE: Type[ConfigType] | ||
|
||
def __init__( | ||
self, | ||
connection_config: ConnectionConfig, | ||
application_config: ConfigType, | ||
current_config_revision: ConfigRevision, | ||
) -> None: | ||
self.cancellation_token = CancellationToken() | ||
self.cancellation_token.cancel_on_interrupt() | ||
|
||
self.connection_config = connection_config | ||
self.application_config = application_config | ||
self.current_config_revision = current_config_revision | ||
|
||
self.cognite_client = self.connection_config.get_cognite_client(f"{self.EXTERNAL_ID}-{self.VERSION}") | ||
|
||
self._checkin_lock = RLock() | ||
self._runtime_messages: Optional[Queue[RuntimeMessage]] = None | ||
|
||
self.logger = logging.getLogger(f"{self.EXTERNAL_ID}.main") | ||
|
||
def _set_runtime_message_queue(self, queue: Queue) -> None: | ||
self._runtime_messages = queue | ||
|
||
def _run_checkin(self) -> None: | ||
def checkin() -> None: | ||
body = {"externalId": self.connection_config.extraction_pipeline} | ||
|
||
with self._checkin_lock: | ||
res = self.cognite_client.post( | ||
f"/api/v1/projects/{self.cognite_client.config.project}/odin/checkin", | ||
json=body, | ||
headers={"cdf-version": "alpha"}, | ||
) | ||
new_config_revision = res.json().get("lastConfigRevision") | ||
|
||
if new_config_revision and new_config_revision != self.current_config_revision: | ||
self.restart() | ||
|
||
while not self.cancellation_token.is_cancelled: | ||
try: | ||
checkin() | ||
except Exception: | ||
self.logger.exception("Error during checkin") | ||
self.cancellation_token.wait(10) | ||
|
||
def restart(self) -> None: | ||
if self._runtime_messages: | ||
self._runtime_messages.put(RuntimeMessage.RESTART) | ||
self.cancellation_token.cancel() | ||
|
||
@classmethod | ||
def init_from_runtime( | ||
cls, | ||
connection_config: ConnectionConfig, | ||
application_config: ConfigType, | ||
current_config_revision: ConfigRevision, | ||
) -> Self: | ||
return cls(connection_config, application_config, current_config_revision) | ||
|
||
def start(self) -> None: | ||
self.cognite_client.post( | ||
f"/api/v1/projects/{self.cognite_client.config.project}/odin/extractorinfo", | ||
json={ | ||
"externalId": self.connection_config.extraction_pipeline, | ||
"activeConfigRevision": self.current_config_revision, | ||
"extractor": { | ||
"version": self.VERSION, | ||
"externalId": self.EXTERNAL_ID, | ||
}, | ||
}, | ||
headers={"cdf-version": "alpha"}, | ||
) | ||
Thread(target=self._run_checkin, name="ExtractorCheckin", daemon=True).start() | ||
|
||
def stop(self) -> None: | ||
self.cancellation_token.cancel() | ||
|
||
def __enter__(self) -> Self: | ||
self.start() | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_val: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> bool: | ||
self.stop() | ||
return exc_val is None | ||
|
||
def run(self) -> None: | ||
raise NotImplementedError() |
Oops, something went wrong.