Skip to content

Commit

Permalink
add slice expiry email template
Browse files Browse the repository at this point in the history
  • Loading branch information
kthare10 committed Sep 18, 2024
1 parent ec9c083 commit 14a202e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
28 changes: 28 additions & 0 deletions fabric_cf/actor/core/util/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@
from email.mime.text import MIMEText


# Function to load the email template from a file and replace placeholders
from typing import Tuple


def load_and_update_template(*, template_path: str, user: str, slice_name: str, hours_left: float) -> Tuple[str, str]:
"""
Load the Email body from the template
@param template_path: location of the template
@param user: user name
@param slice_name: slice name
@param hours_left: hours left
@return: Subject and Body
"""
with open(template_path, 'r') as template_file:
email_template = template_file.read()

# Replace placeholders with actual values
email_content = email_template.replace('<User>', user) \
.replace('<slice_name>', slice_name) \
.replace('<hours_left>', str(hours_left))

# Extract the subject and the body from the email content
subject_line = email_content.split('\n')[0].replace("Subject: ", "")
email_body = "\n".join(email_content.split('\n')[1:])

return subject_line, email_body


def send_email(*, smtp_config: dict, to_email: str, subject: str, body: str):
"""
Send Email to a user
Expand Down
1 change: 1 addition & 0 deletions fabric_cf/orchestrator/config.orchestrator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ smtp:
smtp_password:
from_email: fabric_cf
reply_to_email: [email protected]
template_path: ./slice_expiration_template.txt

database:
db-user: fabric
Expand Down
21 changes: 21 additions & 0 deletions fabric_cf/orchestrator/slice_expiration_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Subject: Urgent: Your Slice <slice_name> Will Expire Soon

Dear <User>,

We hope your experience with FABRIC has been productive.

This is a reminder that your slice <slice_name> is scheduled to expire in <hours_left> hours. Once expired, the resources associated with your slice will be automatically released, which may result in the loss of any unsaved data or unfinished experiments.

If you need additional time for your work, we encourage you to extend the slice before it expires. Alternatively, please ensure that you capture all necessary results from your experiments to avoid data loss.

Here are a few steps you can take:
- **Extend the Slice**: If your experiment requires more time, you can extend your slice duration from the FABRIC portal.
- **Save Results**: If you're finished with your experiment, please download and store any critical results before the slice expires.

If you have any questions or need assistance, feel free to reach out to us. We're here to support you!

Thank you for using FABRIC, and we look forward to your continued success.

Best regards,
The FABRIC Team
https://portal.fabric-testbed.net/
11 changes: 6 additions & 5 deletions tools/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from fabric_cf.actor.core.manage.kafka.kafka_mgmt_message_processor import KafkaMgmtMessageProcessor
from fabric_cf.actor.core.plugins.db.actor_database import ActorDatabase
from fabric_cf.actor.core.util.id import ID
from fabric_cf.actor.core.util.smtp import send_email
from fabric_cf.actor.core.util.smtp import send_email, load_and_update_template
from fabric_cf.actor.fim.fim_helper import FimHelper


Expand Down Expand Up @@ -288,15 +288,16 @@ def send_slice_expiry_email_warnings(self):

now = datetime.now(timezone.utc)

subject = "Test Email from Fabric Testbed"
body = "This is a test email."

for s in slices:
s.get_owner().get_email()
if s.get_lease_end():
diff = s.get_lease_end() - now
if diff > timedelta(hours=24) or diff > timedelta(hours=12) or diff > timedelta(hours=6):
if diff < timedelta(hours=12):
try:
subject, body = load_and_update_template(template_path=self.smtp_config.get("template_path"),
user=s.get_owner().get_name(),
slice_name=f"{s.get_slice_name()}/{s.get_slice_id()}",
hours_left=diff.total_seconds()/60)
send_email(smtp_config=self.smtp_config, to_email=s.get_owner().get_email(),
subject=subject, body=body)
except smtplib.SMTPAuthenticationError as e:
Expand Down

0 comments on commit 14a202e

Please sign in to comment.