db_callback_request.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. from importlib import import_module
  20. from typing import TYPE_CHECKING
  21. from sqlalchemy import Column, Integer, String
  22. from airflow.models.base import Base
  23. from airflow.utils import timezone
  24. from airflow.utils.sqlalchemy import ExtendedJSON, UtcDateTime
  25. if TYPE_CHECKING:
  26. from airflow.callbacks.callback_requests import CallbackRequest
  27. class DbCallbackRequest(Base):
  28. """Used to handle callbacks through database."""
  29. __tablename__ = "callback_request"
  30. id = Column(Integer(), nullable=False, primary_key=True)
  31. created_at = Column(UtcDateTime, default=timezone.utcnow, nullable=False)
  32. priority_weight = Column(Integer(), nullable=False)
  33. callback_data = Column(ExtendedJSON, nullable=False)
  34. callback_type = Column(String(20), nullable=False)
  35. processor_subdir = Column(String(2000), nullable=True)
  36. def __init__(self, priority_weight: int, callback: CallbackRequest):
  37. self.created_at = timezone.utcnow()
  38. self.priority_weight = priority_weight
  39. self.processor_subdir = callback.processor_subdir
  40. self.callback_data = callback.to_json()
  41. self.callback_type = callback.__class__.__name__
  42. def get_callback_request(self) -> CallbackRequest:
  43. module = import_module("airflow.callbacks.callback_requests")
  44. callback_class = getattr(module, self.callback_type)
  45. # Get the function (from the instance) that we need to call
  46. from_json = getattr(callback_class, "from_json")
  47. return from_json(self.callback_data)