Notifications
Notifications is another feature of Daeploy. When a notification is raised it can be viewed
on the dashboard at http://your-host or sent to an email address. They have several use-cases,
such as catching unexpected behaviour of your program or as alarms to act upon.
To add a notification we use the notify()
function.
It can be placed in a conditional statement to send a notification if it’s true.
Let’s say you want to add a notification to the daeploy init
service when
someone tries to greet the world, because that would take a lot of time.
Then we add an if-statement to check the input and send the notification.
After the notification is sent we raise a HTTPException
with the status code 403 - Forbidden, to show the user that their request
was rejected. With this added, the hello()
will look like this:
import logging
from daeploy import service
from daeploy.communication import notify, Severity
from daeploy.exceptions import HTTPException
logger = logging.getLogger(__name__)
@service.entrypoint
def hello(name: str) -> str:
if name.lower() == "world":
message = "Trying to greet world. Too time consuming!"
notify(msg=message, severity=Severity.WARNING)
raise HTTPException(403, detail=message)
logger.info(f"Greeting someone with the name: {name}")
return f"Hello {name}"
if __name__ == "__main__":
service.run()
Email Notifications
The notifications sent by the notify()
call above will only
show up on the dashboard, but it is also possible to send them as emails as well. For this,
the manager has to be configured with an email and an SMTP server. Please refer to the
documentation on the manager configuration: Email notifications.
To send the emails we use the emails
keyword argument of the notify()
function that takes a list of emails as input. So to send an email notification we could write:
notify(
msg="Trying to greet world. Too time consuming!",
severity=Severity.WARNING,
emails=["your@email.com"],
)
Tip
To make the email recipients dynamically changeable, you can add
the emails as a parameter with add_parameter()
.