Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AIRFLOW-4067] Telegram hook/operator to post messages to telegram channels #4891

Closed
wants to merge 11 commits into from

Conversation

dchaplinsky
Copy link

@dchaplinsky dchaplinsky commented Mar 10, 2019

Make sure you have checked all steps below.

Jira

  • My PR addresses the following Airflow Jira issues and references them in the PR title. For example, "[AIRFLOW-XXX] My Airflow PR"
    • https://issues.apache.org/jira/browse/AIRFLOW-XXX
    • In case you are fixing a typo in the documentation you can prepend your commit with [AIRFLOW-XXX], code changes always need a Jira issue.
    • In case you are proposing a fundamental code change, you need to create an Airflow Improvement Proposal(AIP).

Description

  • Telegram operator allows to post templatable messages to telegram private/public channels using bot token. It uses airflow connections to store credentials and channel id.

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:

Commits

  • My commits all reference Jira issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • When adding new operators/hooks/sensors, the autoclass documentation generation needs to be added.
    • All the public functions and the classes in the PR contain docstrings that explain what it does
    • If you implement backwards incompatible changes, please leave a note in the Updating.md so we can assign it to a appropriate release

Code Quality

  • Passes flake8

@XD-DENG
Copy link
Member

XD-DENG commented Mar 10, 2019

Hi @dchaplinsky , any code change (including adding new code) should come with a JIRA ticket. Only pure doc change can be exempted.

@dchaplinsky
Copy link
Author

@XD-DENG , just to clarify. If I want to add something to contrib, not the core, I should create a ticket in Jira first?

@XD-DENG
Copy link
Member

XD-DENG commented Mar 10, 2019

@dchaplinsky yes, it should cover all code changes in this repository, including /contrib. You can refer to the history of commits relating to airflow/contrib (https://github.com/apache/airflow/commits/master/airflow/contrib)

telegram_client.call("POST", self.api_params)


class TelegramAPIPostOperator(TelegramAPIOperator):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? Operators are built by compositions. The complex logic is in the hook. Inheritance does not bring benefits here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about reference implementation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be difficult to make changes to the old code and keep it backwards compatible. However, if you have time, you can make a change, but remember to add a note in the file updating.md. Let us wait for the opinions of other people now.

for retry in range(max_retries):
try:
return func(*args, **kwargs)
except (telegram.error.TimedOut,) as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only this error? What if an internal server error happens? Then you also need to retry.

@dchaplinsky dchaplinsky changed the title Telegram hook/operator to post messages to telegram channels [AIRFLOW-4067] Telegram hook/operator to post messages to telegram channels Mar 11, 2019
mik-laj and others added 2 commits March 11, 2019 14:17
Co-Authored-By: dchaplinsky <chaplinsky.dmitry@gmail.com>
Co-Authored-By: dchaplinsky <chaplinsky.dmitry@gmail.com>
@XD-DENG
Copy link
Member

XD-DENG commented Mar 11, 2019

@dchaplinsky please update your PR description based on the original template (ref: #4890)

You may find it tedious. But this make it easier for folks to understand what you're trying to propose in this PR, and easily link to JIRA.

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@mik-laj
Copy link
Member

mik-laj commented Mar 11, 2019

@dchaplinsky Look at: https://github.com/apache/airflow/blob/master/docs/conf.py#L39-L91
You only need to add a library here and the documentation will build correctly.
You should add class information to the code.rst file. If you have more time then you can write a guide in the howto / operator directory, but this is not required.

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@mik-laj
Copy link
Member

mik-laj commented Mar 11, 2019

I am now working on a new look for the documents from this section. I will be happy if you follow my new structure. http://purring-scent.surge.sh/howto/operator/gcp/natural_language.html This is not required. This is just my little request.

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@dchaplinsky
Copy link
Author

@mik-laj , please look into code.

@XD-DENG , I'll update ticket description later tonight or tomorrow.
Still not sure about howto section to the docs tho, but will try.

Retry calling the decorated function using an exponential backoff.
(с) Eliot aka saltycrane, https://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/

Args:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use a reStructuredText docstring style.

try:
return f(*args, **kwargs)
except exceptions as e:
msg = "{}, Retrying in {} seconds...".format(e, mdelay)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should avoid formatting string before passing to logger.
Reference: #4804

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@mik-laj
Copy link
Member

mik-laj commented Mar 11, 2019

@dchaplinsky All elements from this module should be treated as public, because we want to have automatically generated documentation
Reference: #4788

@mik-laj
Copy link
Member

mik-laj commented Mar 11, 2019

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 11, 2019 via email

@dchaplinsky
Copy link
Author

@XD-DENG please check ticket and PR description

@dchaplinsky
Copy link
Author

@mik-laj , can we finish with code review part? I'd appreciate an answer on my question above, about retry decorator.

self.log.info(self.connection.send_message(**params))


__all__ = [TelegramHook]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
__all__ = [TelegramHook]
__all__ = ["TelegramHook"]

This should be a list of module names, not modules.

However, I would like to point out that we do not use the all variable in the whole project. I think that this is a good rule because it forces you to think about the whole project, not just small fragments. In the future, when Airflow will be divided into modules and interfaces between libraries will be important, we can think about changing this rule. Now we have one code base and we must think about its overall good.

Why is the decorator only in this module, not in the entire Airflow? What if another operator needs this decorator? Copy code? Move code? If we copy to another place, what will we do with the documentation? Can we change it? Did we respect code style of author?

@zhongjiajie What are you thinking?

@mik-laj
Copy link
Member

mik-laj commented Mar 14, 2019

This is not enough reason to change these rules.I think that the code should be rewritten. We have rules that should be followed. If we accept this code, it will be rewritten in the future. We should avoid additional work in the future. Now this change is easier to make.

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 14, 2019 via email

@dchaplinsky
Copy link
Author

@mik-laj , please review

"Cannot get chat_id: " "No valid chat_id nor telegram_conn_id supplied."
)

@tenacity.retry(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

"disable_web_page_preview": True,
}
params.update(api_params)
self.log.info(self.connection.send_message(**params))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full response should be at the debug level. At the info level, only the message that there is an attempt to send a message and that the message was sent.

@mik-laj
Copy link
Member

mik-laj commented Mar 18, 2019

I really like the solution with the tenacity library. ;-) Awesome!

mik-laj and others added 2 commits March 18, 2019 04:18
Co-Authored-By: dchaplinsky <chaplinsky.dmitry@gmail.com>
@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 18, 2019 via email

@dchaplinsky
Copy link
Author

@mik-laj can you check it now?

@mik-laj
Copy link
Member

mik-laj commented Mar 24, 2019

I have a suggestion. To add it to your repository execute a command:

curl https://termbin.com/wymk | git am

Can you also add unit tests?

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 24, 2019 via email

@dchaplinsky
Copy link
Author

dchaplinsky commented Mar 24, 2019 via email

@stale
Copy link

stale bot commented Sep 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Sep 3, 2019
@stale stale bot closed this Sep 11, 2019
@GreenGrassBlueOcean
Copy link

Hi are there any plans to work further on this pull request? I would love to include this operator for notifications in my dags. Much easier than receiving tons of emails and sorting these emails out.

@dchaplinsky
Copy link
Author

dchaplinsky commented Jun 5, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stale PRs per the .github/workflows/stale.yml policy file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants