-
Notifications
You must be signed in to change notification settings - Fork 23
Send notifications if sync has an error #125
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
289a77e
add notification
JanCaha 9937786
move function
JanCaha c16102b
add tests
JanCaha a6360ce
only send email if previous error message was not the same as current
JanCaha cb6ecd0
add notification setting example
JanCaha 087390e
fix typo
JanCaha ee6ca5c
fix error messages
JanCaha 8aded71
password and user are optional
JanCaha be36fcf
new config
JanCaha 664b83d
move smtp functions to separate file
JanCaha 08d77c6
use new functions
JanCaha 3f3522f
update tests
JanCaha acde9c7
add imports
JanCaha a41dbc9
update usage
JanCaha 20199b5
use simple EmailMessage
JanCaha e9b8eaa
add option to send tes email
JanCaha 9a2431b
fix error messages
JanCaha 20ee1b8
add minimal email interval
JanCaha 0b3c505
add time difference between emails
JanCaha 712860c
fix message
JanCaha 9a6f026
fix message
JanCaha c8b1d3a
fix message
JanCaha a289efa
zero port
JanCaha 9192e9a
fix typo in variable
JanCaha 7bb5759
catch errors and simplify into single function
JanCaha 18d1a3a
fix typo
JanCaha 18c22a3
add docstrings
JanCaha 3d997ab
email sending should fail without crashing the deamon
JanCaha 63716b3
restyle imports
JanCaha e64ef03
notification description in use.md
JanCaha 873bfaf
drop unused imports
JanCaha c4ab272
drop unused import
JanCaha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import datetime | ||
| import smtplib | ||
| import typing | ||
| from email.message import EmailMessage | ||
|
|
||
| from dynaconf import Dynaconf | ||
|
|
||
wonder-sk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def create_connection_and_log_user(config: Dynaconf) -> typing.Union[smtplib.SMTP_SSL, smtplib.SMTP]: | ||
| """Create connection and log user to the SMTP server using the configuration in config.""" | ||
| if "smtp_port" in config.notification: | ||
| port = config.notification.smtp_port | ||
| else: | ||
| port = 0 | ||
|
|
||
| if "use_ssl" in config.notification and config.notification.use_ssl: | ||
| host = smtplib.SMTP_SSL(config.notification.smtp_server, port) | ||
| else: | ||
| host = smtplib.SMTP(config.notification.smtp_server, port) | ||
|
|
||
| if "use_tls" in config.notification and config.notification.use_tls: | ||
| host.starttls() | ||
| if config.notification.smtp_username and config.notification.smtp_password: | ||
| host.login(config.notification.smtp_username, config.notification.smtp_password) | ||
|
|
||
| return host | ||
|
|
||
|
|
||
| def should_send_another_email( | ||
| current_time: datetime.datetime, last_email_sent: typing.Optional[datetime.datetime], config: Dynaconf | ||
| ) -> bool: | ||
| """Checks if another email should be sent. The time difference to last sent email needs to larger them | ||
| minimal interval specified in config or 4 hours if no value was specified.""" | ||
| if last_email_sent is None: | ||
| return True | ||
| min_time_diff = config.notification.minimal_email_interval if "minimal_email_interval" in config.notification else 4 | ||
| time_diff = current_time - last_email_sent | ||
| return time_diff.seconds > (min_time_diff * 3600) | ||
|
|
||
|
|
||
| def send_email(error: str, last_email_send: typing.Optional[datetime.datetime], config: Dynaconf) -> None: | ||
| """Sends email with provided error using the settings in config.""" | ||
| current_time = datetime.datetime.now() | ||
|
|
||
| if should_send_another_email(current_time, last_email_send, config): | ||
| msg = EmailMessage() | ||
| msg["Subject"] = config.notification.email_subject | ||
| msg["From"] = config.notification.email_sender | ||
| msg["To"] = ", ".join(config.notification.email_recipients) | ||
|
|
||
| date_time = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") | ||
|
|
||
| error = f"{date_time}: {error}" | ||
|
|
||
| msg.set_content(error) | ||
|
|
||
| sender_email = config.notification.email_sender | ||
| if "smtp_username" in config.notification: | ||
| sender_email = config.notification.smtp_username | ||
|
|
||
| try: | ||
| smtp_conn = create_connection_and_log_user(config) | ||
| smtp_conn.sendmail(sender_email, config.notification.email_recipients, msg.as_string()) | ||
| smtp_conn.quit() | ||
| except: | ||
| pass | ||
|
|
||
|
|
||
| def can_send_email(config: Dynaconf) -> bool: | ||
| """Checks if notification settings is in the config and emails can be send.""" | ||
| if "notification" in config: | ||
| return True | ||
| return False | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.