Add DateTimeSensor#9697
Merged
Merged
Conversation
55bd420 to
b31a86b
Compare
6 tasks
turbaszek
reviewed
Jul 22, 2020
Member
There was a problem hiding this comment.
Would you mind adding a test for poke method?
22quinn
commented
Jul 22, 2020
|
|
||
| def poke(self, context: Dict) -> bool: | ||
| self.log.info("Checking if the time (%s) has come", self.target_time) | ||
| return timezone.utcnow() > timezone.parse(self.target_time) |
Contributor
Author
There was a problem hiding this comment.
I'm wondering if it should be changed to>=? Same applies to TimeSensor and TimeDeltaSensor. The impact of changing to >= is that the task can succeed one poke interval earlier in some cases.
Contributor
Author
There was a problem hiding this comment.
I was just thinking >= is more mathematically correct. But now I realize it's extremely rare to have = because utcnow() has microsecond precision. So nvm
turbaszek
approved these changes
Jul 22, 2020
Member
|
@zikun before we merge, I think it would be helpful to add some info from PR description to docstring: A major advantage of this sensor is idempotence for the ``target_time``.
It handles some cases for which ``TimeSensor`` and ``TimeDeltaSensor`` are not suited.
**Example**: 1 ::
If a task needs to wait for 11am on each ``execution_date``. Using
``TimeSensor`` or ``TimeDeltaSensor``, all backfill tasks started at 1am have to wait for 10 hours.
This is unnecessary, e.g. a backfill task with ``{{ ds }} = '1970-01-01'`` does not need to wait
because ``1970-01-01T11:00:00`` has already passed.
**Example**: 2 ::
If a DAG is scheduled to run at 23:00 daily, but one of the tasks is required
to run at 01:00 next day, using ``TimeSensor`` will return ``True`` immediately because 23:00 > 01:00.
Instead, we can use ``DateTimeSensor``:
.. code-block:: python
DateTimeSensor(
task_id='wait_for_0100',
target_time='{{ next_execution_date.tomorrow().replace(hour=1) }}',
dag=dag
)Hope this is ok formatted rst but I'm not sure |
Contributor
Author
|
I tried on rst online viewer. It should work. |
turbaszek
approved these changes
Jul 23, 2020
kaxil
pushed a commit
that referenced
this pull request
Aug 14, 2020
* Add DateTimeSensor (cherry picked from commit 243b704)
kaxil
pushed a commit
that referenced
this pull request
Aug 15, 2020
* Add DateTimeSensor (cherry picked from commit 243b704)
kaxil
pushed a commit
that referenced
this pull request
Aug 15, 2020
* Add DateTimeSensor (cherry picked from commit 243b704)
cfei18
pushed a commit
to cfei18/incubator-airflow
that referenced
this pull request
Mar 5, 2021
* Add DateTimeSensor (cherry picked from commit 243b704)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to #9609
A major advantage of this new sensor is idempotence for the
target_time.It handles some cases for which
TimeSensorandTimeDeltaSensorare not suited.Example 1) If a task needs to wait for 11am on each
execution_date. UsingTimeSensororTimeDeltaSensor, all backfill tasks started at 1am have to wait for 10 hours. This is unnecessary, e.g. a backfill task with{{ ds }} = '1970-01-01'does not need to wait because1970-01-01T11:00:00has already passed.Example 2) If a DAG is scheduled to run at 23:00 daily, but one of the tasks is required to run at 01:00 next day, using
TimeSensorwill returnTrueimmediately because 23:00 > 01:00. Instead, we can useDateTimeSensor:On another topic - I think
TimeSensor,TimeDeltaSensor, and thisDateTimeSensorshould probably be put in the same filesensors/time.py. What does everyone think? But that will be a breaking change. Will have to do it in a separate PR.Make sure to mark the boxes below before creating PR: [x]
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.
Read the Pull Request Guidelines for more information.