ramitkataria commented on code in PR #54796: URL: https://github.com/apache/airflow/pull/54796#discussion_r2430783242
########## airflow-core/src/airflow/models/callback.py: ########## @@ -0,0 +1,177 @@ +# 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. +from __future__ import annotations + +import logging +from enum import Enum +from importlib import import_module +from typing import TYPE_CHECKING + +import uuid6 +from sqlalchemy import Column, ForeignKey, Integer, String, Text +from sqlalchemy.orm import relationship +from sqlalchemy_utils import UUIDType + +from airflow._shared.timezones import timezone +from airflow.models import Base +from airflow.utils.sqlalchemy import ExtendedJSON, UtcDateTime + +if TYPE_CHECKING: + from sqlalchemy.orm import Session + + from airflow.callbacks.callback_requests import CallbackRequest + from airflow.triggers.base import TriggerEvent + +log = logging.getLogger(__name__) + + +class CallbackState(str, Enum): + """All possible states of callbacks.""" + + PENDING = "pending" + QUEUED = "queued" + RUNNING = "running" + SUCCESS = "success" + FAILED = "failed" + + +class CallbackType(str, Enum): + """ + Types of Callbacks. + + Used for figuring out what class to instantiate while deserialization. + """ + + TRIGGERER = "triggerer" + EXECUTOR = "executor" + DAG_PROCESSOR = "dag_processor" + + +class CallbackFetchMethod(str, Enum): + """Methods used to fetch callback at runtime.""" + + # For future use once Dag Processor callbacks (on_success_callback/on_failure_callback) get moved to executors + DAG_ATTRIBUTE = "dag_attribute" + + # For deadline callbacks since they import callbacks through the import path + IMPORT_PATH = "import_path" + + +class Callback(Base): + """Base class for callbacks.""" + + __tablename__ = "callback" + + id = Column(UUIDType(binary=False), primary_key=True, default=uuid6.uuid7) + + # This is used by SQLAlchemy to be able to deserialize DB rows to subclasses + __mapper_args__ = { + "polymorphic_identity": "callback", + "polymorphic_on": "type", + } + type = Column(String(20), nullable=False) + + # Method used to fetch the callback, of type: CallbackFetchMethod + fetch_method = Column(String(20), nullable=True) + + # Used by subclasses to store information about how to run the callback + data = Column(ExtendedJSON) + + # State of the Callback of type: CallbackState + state = Column(String(10)) + + # Return value of the callback if successful, otherwise exception details + output = Column(Text) + + # Used for prioritization (not currently implemented). Higher weight -> higher priority + priority_weight = Column(Integer, nullable=False) + + # Creation time of the callback + created_at = Column(UtcDateTime, default=timezone.utcnow, nullable=False) + + # Used for callbacks of type CallbackType.TRIGGERER + trigger_id = Column(Integer, ForeignKey("trigger.id"), nullable=True) + trigger = relationship("Trigger", back_populates="callback", uselist=False) + + def __init__(self, priority_weight: int = 1): + self.state = CallbackState.PENDING + self.priority_weight = priority_weight + self.created_at = timezone.utcnow() + + def queue(self): + self.state = CallbackState.QUEUED + + @staticmethod + def create_from_sdk_def(callback_def) -> Callback: Review Comment: I had them earlier but removed after discussion here: https://github.com/apache/airflow/pull/54796#discussion_r2304116137 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
