Home > front end >  Python Airflow custom sensor - which methods to implement
Python Airflow custom sensor - which methods to implement

Time:08-27

I'm trying to write a custom sensor that inherits BaseSensorOperator and SSHOperator. in the sensor body I override poke and execute method.

  class mysensor (BaseSensorOperator,SSHOperator):
      template_fields = ("my_param")
      def __init__ (
          self,
          my_param = None,
          *args,
          **kawags):
      self.my_param = my_param
      super().__init__(
          command = "some bash command",
          ssh_conn_id = "some connection"
          *args,
          **kwargs)

def poke(self, context):
    ... (implemented poke)
def execute():
    ...(implemented execute)
          

CodePudding user response:

Custom sensors are required to implement only the poke function. You should not override the execute function (unless you really know what you are doing). The execute function is implemented in BaseSensorOperator and that is what gives sensors their capabilities.

In your case you should not use SSHOperator, you should use SSHHook directly.

Your Sensor should look like:

from airflow.providers.ssh.hooks.ssh import SSHHook

class Mysensor(BaseSensorOperator):
    ...

    def poke(self,context):
      hook = SSHHook(...)
      # add your sensor logic here
  • Related