-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
Copy pathairbyte.py
162 lines (142 loc) · 6.38 KB
/
airbyte.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""This module contains a Airbyte Job sensor."""
from __future__ import annotations
import time
import warnings
from typing import TYPE_CHECKING, Any, Literal, Sequence
from airflow.configuration import conf
from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.providers.airbyte.hooks.airbyte import AirbyteHook
from airflow.providers.airbyte.triggers.airbyte import AirbyteSyncTrigger
from airflow.sensors.base import BaseSensorOperator
if TYPE_CHECKING:
from airflow.utils.context import Context
class AirbyteJobSensor(BaseSensorOperator):
"""
Check for the state of a previously submitted Airbyte job.
:param airbyte_job_id: Required. Id of the Airbyte job
:param airbyte_conn_id: Optional. The name of the Airflow connection to get
:param deferrable: Run sensor in the deferrable mode.
connection information for Airbyte. Defaults to "airbyte_default".
:param api_version: Optional. Airbyte API version. Defaults to "v1".
:param api_type: Optional. The type of Airbyte API to use. Either "config" or "cloud". Defaults to "config".
"""
template_fields: Sequence[str] = ("airbyte_job_id",)
ui_color = "#6C51FD"
def __init__(
self,
*,
airbyte_job_id: int,
deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False),
airbyte_conn_id: str = "airbyte_default",
api_version: str = "v1",
api_type: Literal["config", "cloud"] = "config",
**kwargs,
) -> None:
if deferrable:
if "poke_interval" not in kwargs:
# TODO: Remove once deprecated
if "polling_interval" in kwargs:
kwargs["poke_interval"] = kwargs["polling_interval"]
warnings.warn(
"Argument `poll_interval` is deprecated and will be removed "
"in a future release. Please use `poke_interval` instead.",
AirflowProviderDeprecationWarning,
stacklevel=2,
)
else:
kwargs["poke_interval"] = 5
if "timeout" not in kwargs:
kwargs["timeout"] = 60 * 60 * 24 * 7
super().__init__(**kwargs)
self.deferrable = deferrable
self.airbyte_conn_id = airbyte_conn_id
self.airbyte_job_id = airbyte_job_id
self.api_version = api_version
self.api_type = api_type
def poke(self, context: Context) -> bool:
hook = AirbyteHook(
airbyte_conn_id=self.airbyte_conn_id, api_version=self.api_version, api_type=self.api_type
)
job = hook.get_job(job_id=self.airbyte_job_id)
if self.api_type == "config":
status = job.json()["job"]["status"]
else:
status = job.json()["status"]
if status == hook.FAILED:
message = f"Job failed: \n{job}"
raise AirflowException(message)
elif status == hook.CANCELLED:
message = f"Job was cancelled: \n{job}"
raise AirflowException(message)
elif status == hook.SUCCEEDED:
self.log.info("Job %s completed successfully.", self.airbyte_job_id)
return True
elif status == hook.ERROR:
self.log.info("Job %s attempt has failed.", self.airbyte_job_id)
self.log.info("Waiting for job %s to complete.", self.airbyte_job_id)
return False
def execute(self, context: Context) -> Any:
"""Submit a job which generates a run_id and gets deferred."""
if not self.deferrable:
super().execute(context)
else:
hook = AirbyteHook(
airbyte_conn_id=self.airbyte_conn_id, api_version=self.api_version, api_type=self.api_type
)
job = hook.get_job(job_id=(int(self.airbyte_job_id)))
if self.api_type == "config":
state = job.json()["job"]["status"]
else:
state = job.json()["status"]
end_time = time.time() + self.timeout
self.log.info("Airbyte Job Id: Job %s", self.airbyte_job_id)
if state in (hook.RUNNING, hook.PENDING, hook.INCOMPLETE):
self.defer(
timeout=self.execution_timeout,
trigger=AirbyteSyncTrigger(
api_type=self.api_type,
conn_id=self.airbyte_conn_id,
job_id=self.airbyte_job_id,
end_time=end_time,
poll_interval=60,
),
method_name="execute_complete",
)
elif state == hook.SUCCEEDED:
self.log.info("%s completed successfully.", self.task_id)
return
elif state == hook.ERROR:
raise AirflowException(f"Job failed:\n{job}")
elif state == hook.CANCELLED:
raise AirflowException(f"Job was cancelled:\n{job}")
else:
raise AirflowException(
f"Encountered unexpected state `{state}` for job_id `{self.airbyte_job_id}"
)
def execute_complete(self, context: Context, event: Any = None) -> None:
"""
Invoke this callback when the trigger fires; return immediately.
Relies on trigger to throw an exception, otherwise it assumes execution was
successful.
"""
if event["status"] == "error":
raise AirflowException(event["message"])
self.log.info("%s completed successfully.", self.task_id)
return None