-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
58 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
46 changes: 46 additions & 0 deletions
46
airbyte-ci/connectors/metadata_service/orchestrator/orchestrator/sensors/metadata.py
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,46 @@ | ||
from dagster import sensor, RunRequest, SkipReason, SensorDefinition, SensorEvaluationContext, build_resources, DefaultSensorStatus | ||
from orchestrator.utils.dagster_helpers import deserialize_composite_etag_cursor, serialize_composite_etag_cursor | ||
|
||
|
||
def metadata_updated_sensor(job, resources_def) -> SensorDefinition: | ||
""" | ||
This sensor is responsible for polling the metadata folder in GCS for new or updated metadata files. | ||
If it notices that the etags have changed, it will trigger the given job. | ||
""" | ||
|
||
@sensor( | ||
name=f"{job.name}_on_metadata_updated", | ||
job=job, | ||
minimum_interval_seconds=30, | ||
default_status=DefaultSensorStatus.STOPPED, | ||
) | ||
def metadata_updated_sensor_definition(context: SensorEvaluationContext): | ||
context.log.info("Starting gcs_metadata_updated_sensor") | ||
|
||
with build_resources(resources_def) as resources: | ||
context.log.info("Got resources for gcs_metadata_updated_sensor") | ||
|
||
etag_cursor_raw = context.cursor or None | ||
etag_cursor = deserialize_composite_etag_cursor(etag_cursor_raw) | ||
etag_cursor_set = set(etag_cursor) | ||
|
||
context.log.info(f"Old etag cursor: {etag_cursor}") | ||
|
||
metadata_folder_blobs = resources.metadata_folder_blobs | ||
## TODO, yaml vs yml | ||
new_etag_cursors = [blob.etag for blob in metadata_folder_blobs if blob.name.endswith("metadata.yaml")] | ||
new_etag_cursor_set = set(new_etag_cursors) | ||
context.log.info(f"New etag cursor: {new_etag_cursor_set}") | ||
|
||
# Note: ETAGs are GCS's way of providing a version number for a file | ||
# Another option would be to use the last modified date or MD5 hash | ||
if etag_cursor_set == new_etag_cursor_set: | ||
context.log.info("No new updated_metadata_files in GCS bucket") | ||
return SkipReason("No new updated_metadata_files in GCS bucket") | ||
|
||
serialized_new_etag_cursor = serialize_composite_etag_cursor(new_etag_cursors) | ||
context.update_cursor(serialized_new_etag_cursor) | ||
context.log.info("New updated_metadata_files in GCS bucket") | ||
return RunRequest(run_key="updated_metadata_files") | ||
|
||
return metadata_updated_sensor_definition |
13 changes: 12 additions & 1 deletion
13
airbyte-ci/connectors/metadata_service/orchestrator/orchestrator/utils/dagster_helpers.py
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 |
---|---|---|
@@ -1,12 +1,23 @@ | ||
from dagster import MetadataValue, Output | ||
import pandas as pd | ||
from typing import NewType | ||
from typing import Optional, List | ||
|
||
OutputDataFrame = Output[pd.DataFrame] | ||
CURSOR_SEPARATOR = ":" | ||
|
||
|
||
def output_dataframe(result_df: pd.DataFrame) -> Output[pd.DataFrame]: | ||
""" | ||
Returns a Dagster Output object with a dataframe as the result and a markdown preview. | ||
""" | ||
return Output(result_df, metadata={"count": len(result_df), "preview": MetadataValue.md(result_df.to_markdown())}) | ||
|
||
|
||
def deserialize_composite_etag_cursor(etag_cursor: Optional[str]) -> List[str]: | ||
if etag_cursor is None: | ||
return [] | ||
|
||
return etag_cursor.split(CURSOR_SEPARATOR) | ||
|
||
def serialize_composite_etag_cursor(etags: List[str]): | ||
return CURSOR_SEPARATOR.join(etags) |