You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/04/01 09:00:56 UTC

[GitHub] [airflow] ashb commented on a change in pull request #15125: Add latest only decorator

ashb commented on a change in pull request #15125:
URL: https://github.com/apache/airflow/pull/15125#discussion_r605491675



##########
File path: airflow/example_dags/example_latest_only_decorator.py
##########
@@ -0,0 +1,58 @@
+#
+# 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.
+
+"""Example of the `latest_only` decorator"""
+
+from airflow import DAG
+from airflow.models.baseoperator import BaseOperator
+from airflow.operators.bash import BashOperator
+from airflow.utils.dates import days_ago
+from airflow.utils.decorators import latest_only
+
+
+# [START example]
+class MyLatestOnlyOperator(BaseOperator):  # pylint: disable=missing-class-docstring
+    def __init__(self, latest_only=True, **kwargs):  # pylint: disable=redefined-outer-name
+        self.latest_only = latest_only
+        if self.latest_only:
+            self.message = 'I will skip all but the latest execution'
+        else:
+            self.message = 'I will never skip'
+        super().__init__(**kwargs)
+
+    @latest_only
+    def execute(self, context):
+        print(self.message)
+
+
+class MyBashOnlyOperator(BashOperator):  # pylint: disable=missing-class-docstring
+    @latest_only
+    def execute(self, context):
+        print('I will skip all but the latest execution')
+        super().execute(context=context)

Review comment:
       It would be nice if this could work as
   
   ```
        subclass_example = latest_only(BashOperator(task_id='latest_only_bash_task', bash_command='echo "hello world"'))
   ```
   
   or similar, but I'm not sure if that is realistically achievable.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org