Techno Blender
Digitally Yours.

Easily build an Event Reminder with Cloud Workflows and Cloud Scheduler | by Marc Djohossou | Apr, 2023

0 35


Photo by Imants Kaziļuns on Unsplash

Building an event reminder, like Facebook anniversary reminder can seem more laborious than it really is. In this article, I elaborate on an easy, yet efficient way to build a birthday reminder application. Please, read through to unlock your access to the repository containing the full working code. Following are the main topics covered:

Introduction to Cloud Workflows and Cloud Run

Build the Birthday Identification Service

Build the Email Notification Service

Build the Birthday Reminder Service

Summing Up

Introduction to Cloud Workflows and Cloud Run

Cloud Workflows is a Google Cloud service that lets you orchestrate a series of HTTP-Based services. Those services can be internal (when they belong to a Google Cloud domain) or external. In additional to its attractive cost, Cloud Workflows has some unique and interesting features to offer like Wait and Callback that lets you wait for up to 1 year for an event to happen.

Cloud Run is used to run containerized applications at scale. As a serverless offer it does not require creating a cluster or a Virtual Machine, which accelerate building and deploying applications. Although Cloud Run tends to be used often for websites and Rest APIs, it can be used for a broad range of tasks including lightweight data processing or task automation. On that note, this article uses a data processing Cloud Run service to identify people that will be soon celebrating their birthday and a task automation Cloud Run service that sends reminder emails.

Build the Birthday Identification Service

Let’s imagine that we have been given the details of a group of people whose anniversary should be reminded in advance to a few people, maybe their closest friends.

Image By Author, birthdays table — List of people whom birthday to identify

Looking at the first line of the table above, we see that Mr Smith was born a 25th of December. Then, the goal might be to automatically notify Mr Smith’s friends via email one week ahead (so that they have enough time to arrange the surprise party 😀).

Now there are many places where the birthdays table might be stored. As a Google Cloud user, it makes sense to store the birthdays details in a BigQuery table. Another really good candidate is Google Sheets, especially if the number of people in the birthdays table is not too high.

With Google Sheets as the storage solution, the birthday identification service will then look like this:

Image By Author, Birthday Identification Service Architecture — Sheets Logo from flaticon¹

This can then be easily translated in Python Code

import os

from flask import Flask, make_response, request, jsonify

from find_anniversary import AnniversaryFinder

app = Flask(__name__)

@app.route("/anniversaries", methods=['GET'])
def get_anniversaries():
_args = request.args
sheet_id = _args.get('sheetId')
emails = _args.get('emails').split(';')
anniversary_finder = AnniversaryFinder(sheet_id)
ann = anniversary_finder.find_anniversary()
resp = {'emails': emails, 'anniversaries': ann}
return make_response(resp)

Note how the birthday identification service takes 2 input parameters:

It reads the birthdays table, filters the table to keep only the next week lucky-ones and returns a python dictionary containing the details of the detected birthdays and the list of people to notify.

result = {
'emails': ['[email protected]', '[email protected]'],
'anniversaries': [
{'lastName': 'Smith',
'firstName': 'Mark',
'date': '25th of december',
'mobile': '+1-541-235-2000'}]
}

Now we can give the service a name and deploy it to have a Cloud Run service ready to identify next birthdays from the birthdays table:

Image By Author, Birthday Identification Service Deployed to Cloud Run

Note: This article does not go into the specifics of how to identify the upcoming birthdays or how to deploy the birthday identification service.

Build the Email Notification Service

Once the nearing birthdays are identified, it’s time to communicate the good news. In a social network context for example, only the direct connections of the “birthday boy / girl” will get to be notified.

The email notification Cloud Run service takes as input a json payload containing:

  • The list of birthday boy / girl and their details (identified previously)
  • The list of the people to notify
@app.route("/emails", methods=['POST'])
def send_email():
req = request.get_json(silent=True, force=True)
emails = req['emails']
anniversaries = req['anniversaries']

mail = SendEMail()
with open('anniversaries_email_template.html') as _file:
template_email = Template(_file.read())

content = template_email.render(anniversaries=anniversaries)
mail.send(emails, content)
return make_response(req)

The actual email sending can be achieved in multiple ways. Personally, I’ve had success using Sendgrid or Gmail SMPT server via the Python Package smtplib.

Image By Author, Email Notification Service Deployed to Cloud Run

At this point, we have built a service that is able to identify birthdays and another service which is able to send email notifications. Yet, there are 2 missing ingredients for a working birthday reminder application:

  • A solution that is able to run the two services sequentially on a schedule
  • A solution that is able to skip running the email notification service if no birthday has been identified.

And this is where Cloud Workflows comes into play

Note: This article does not dive into the details of how to send the emails or how to deploy the email notification service.

Build the Birthday Reminder Service

As the birthday identification service and the email notification service are up and running, all that is left is how to connect them and recurrently start the birthday reminder application on a schedule.

Let’s say we want the birthday reminder application to execute every Saturday. Then we need to do the following:

  • Activate the Cloud Workflows API

In Cloud Console search bar, type Workflows and click on the Worflows API link. Then click on the ENABLE button to activate the API and wait for a few seconds.

Image By Author, Looking for Cloud Workflows API
Image By Author, Activate the Cloud Workflows API
  • Create the Birthday Reminder Workflow

Once the Workflows API is enabled, click on the CREATE button and start filling the details for the birthday workflow creation. Please, note that the workflow service account should have the Cloud Run Invoker IAM Role (or a broader role) to be able to trigger the birthday identification and the email notification services

Image By Author, Birthday Reminder Workflow Creation, Part 1

Click on the ADD NEW TRIGGER button and configure a Cloud Scheduler. Please, make sure to define a json document in the Workflow’s argument containing the birthdays table Google Sheets ID and the emails to notify. In addition, you’ll want to pick a service account with the Workflows Invoker IAM Role (or broader) to run the scheduler job.

Image By Author, Birthday Reminder Workflow Creation, Part 2

Click the CONTINUE button and add the workflow definition yaml file. Then click on DEPLOY and wait a few seconds for the workflow to be created

Image By Author, Birthday Reminder Workflow Creation, Part 3

Do you remember the 2 reasons why Cloud Workflows is actually needed for putting together the birthday reminder application ? It provides:

  • A way to recurrently run the birthday identification and the email notification services, one after the other
  • The capacity to skip the execution of the notification if there were no birthdays to celebrate.

This skipping capacity is implemented by using a switch step in which the number of upcoming birthdays is checked. If there is no birthdays, the notification email service is not executed.

main:
params: [input]
steps:
- findAnniversaries:
call: http.get
args:
url: https://anniversaryfinder-kfddqdergq-ew.a.run.app/anniversaries
auth:
type: OIDC
query:
sheetId: ${input.sheetId}
emails: ${input.emails}
result: anniversaries
- sendEmailsOrNot:
switch:
- condition: ${len(anniversaries.body.anniversaries) > 0}
next: sendEmails
- condition: ${len(anniversaries.body.anniversaries) == 0}
next: doNotSendEmails
- sendEmails:
call: http.post
args:
url: https://emailsender-kfddqdergq-ew.a.run.app/emails
auth:
type: OIDC
body: ${anniversaries.body}
- doNotSendEmails:
return: "No anniversaries"

Summing Up

This writing describes a simple way of building any event reminder application using Cloud Scheduler and Cloud Workflows. The former is responsible of triggering the reminding while the latter orchestrates HTTP services that hold the logic of what needs to be reminded. Typically, we created a workflow that orchestrates 2 Cloud Run services and used a Cloud Workflows switch step to early stop the workflow when needed.

If this sparked your interest, you’ll find the full working code in this gitlab repository. Please, request an access by sending an email at [email protected] and I’ll be happy to add you to the repository.

Thank you for your time and see you soon with another interesting article.

[1] https://www.flaticon.com/free-icon/sheets_281761?term=google+sheets&page=1&position=1&origin=tag&related_id=281761


Photo by Imants Kaziļuns on Unsplash

Building an event reminder, like Facebook anniversary reminder can seem more laborious than it really is. In this article, I elaborate on an easy, yet efficient way to build a birthday reminder application. Please, read through to unlock your access to the repository containing the full working code. Following are the main topics covered:

Introduction to Cloud Workflows and Cloud Run

Build the Birthday Identification Service

Build the Email Notification Service

Build the Birthday Reminder Service

Summing Up

Introduction to Cloud Workflows and Cloud Run

Cloud Workflows is a Google Cloud service that lets you orchestrate a series of HTTP-Based services. Those services can be internal (when they belong to a Google Cloud domain) or external. In additional to its attractive cost, Cloud Workflows has some unique and interesting features to offer like Wait and Callback that lets you wait for up to 1 year for an event to happen.

Cloud Run is used to run containerized applications at scale. As a serverless offer it does not require creating a cluster or a Virtual Machine, which accelerate building and deploying applications. Although Cloud Run tends to be used often for websites and Rest APIs, it can be used for a broad range of tasks including lightweight data processing or task automation. On that note, this article uses a data processing Cloud Run service to identify people that will be soon celebrating their birthday and a task automation Cloud Run service that sends reminder emails.

Build the Birthday Identification Service

Let’s imagine that we have been given the details of a group of people whose anniversary should be reminded in advance to a few people, maybe their closest friends.

Image By Author, birthdays table — List of people whom birthday to identify

Looking at the first line of the table above, we see that Mr Smith was born a 25th of December. Then, the goal might be to automatically notify Mr Smith’s friends via email one week ahead (so that they have enough time to arrange the surprise party 😀).

Now there are many places where the birthdays table might be stored. As a Google Cloud user, it makes sense to store the birthdays details in a BigQuery table. Another really good candidate is Google Sheets, especially if the number of people in the birthdays table is not too high.

With Google Sheets as the storage solution, the birthday identification service will then look like this:

Image By Author, Birthday Identification Service Architecture — Sheets Logo from flaticon¹

This can then be easily translated in Python Code

import os

from flask import Flask, make_response, request, jsonify

from find_anniversary import AnniversaryFinder

app = Flask(__name__)

@app.route("/anniversaries", methods=['GET'])
def get_anniversaries():
_args = request.args
sheet_id = _args.get('sheetId')
emails = _args.get('emails').split(';')
anniversary_finder = AnniversaryFinder(sheet_id)
ann = anniversary_finder.find_anniversary()
resp = {'emails': emails, 'anniversaries': ann}
return make_response(resp)

Note how the birthday identification service takes 2 input parameters:

It reads the birthdays table, filters the table to keep only the next week lucky-ones and returns a python dictionary containing the details of the detected birthdays and the list of people to notify.

result = {
'emails': ['[email protected]', '[email protected]'],
'anniversaries': [
{'lastName': 'Smith',
'firstName': 'Mark',
'date': '25th of december',
'mobile': '+1-541-235-2000'}]
}

Now we can give the service a name and deploy it to have a Cloud Run service ready to identify next birthdays from the birthdays table:

Image By Author, Birthday Identification Service Deployed to Cloud Run

Note: This article does not go into the specifics of how to identify the upcoming birthdays or how to deploy the birthday identification service.

Build the Email Notification Service

Once the nearing birthdays are identified, it’s time to communicate the good news. In a social network context for example, only the direct connections of the “birthday boy / girl” will get to be notified.

The email notification Cloud Run service takes as input a json payload containing:

  • The list of birthday boy / girl and their details (identified previously)
  • The list of the people to notify
@app.route("/emails", methods=['POST'])
def send_email():
req = request.get_json(silent=True, force=True)
emails = req['emails']
anniversaries = req['anniversaries']

mail = SendEMail()
with open('anniversaries_email_template.html') as _file:
template_email = Template(_file.read())

content = template_email.render(anniversaries=anniversaries)
mail.send(emails, content)
return make_response(req)

The actual email sending can be achieved in multiple ways. Personally, I’ve had success using Sendgrid or Gmail SMPT server via the Python Package smtplib.

Image By Author, Email Notification Service Deployed to Cloud Run

At this point, we have built a service that is able to identify birthdays and another service which is able to send email notifications. Yet, there are 2 missing ingredients for a working birthday reminder application:

  • A solution that is able to run the two services sequentially on a schedule
  • A solution that is able to skip running the email notification service if no birthday has been identified.

And this is where Cloud Workflows comes into play

Note: This article does not dive into the details of how to send the emails or how to deploy the email notification service.

Build the Birthday Reminder Service

As the birthday identification service and the email notification service are up and running, all that is left is how to connect them and recurrently start the birthday reminder application on a schedule.

Let’s say we want the birthday reminder application to execute every Saturday. Then we need to do the following:

  • Activate the Cloud Workflows API

In Cloud Console search bar, type Workflows and click on the Worflows API link. Then click on the ENABLE button to activate the API and wait for a few seconds.

Image By Author, Looking for Cloud Workflows API
Image By Author, Activate the Cloud Workflows API
  • Create the Birthday Reminder Workflow

Once the Workflows API is enabled, click on the CREATE button and start filling the details for the birthday workflow creation. Please, note that the workflow service account should have the Cloud Run Invoker IAM Role (or a broader role) to be able to trigger the birthday identification and the email notification services

Image By Author, Birthday Reminder Workflow Creation, Part 1

Click on the ADD NEW TRIGGER button and configure a Cloud Scheduler. Please, make sure to define a json document in the Workflow’s argument containing the birthdays table Google Sheets ID and the emails to notify. In addition, you’ll want to pick a service account with the Workflows Invoker IAM Role (or broader) to run the scheduler job.

Image By Author, Birthday Reminder Workflow Creation, Part 2

Click the CONTINUE button and add the workflow definition yaml file. Then click on DEPLOY and wait a few seconds for the workflow to be created

Image By Author, Birthday Reminder Workflow Creation, Part 3

Do you remember the 2 reasons why Cloud Workflows is actually needed for putting together the birthday reminder application ? It provides:

  • A way to recurrently run the birthday identification and the email notification services, one after the other
  • The capacity to skip the execution of the notification if there were no birthdays to celebrate.

This skipping capacity is implemented by using a switch step in which the number of upcoming birthdays is checked. If there is no birthdays, the notification email service is not executed.

main:
params: [input]
steps:
- findAnniversaries:
call: http.get
args:
url: https://anniversaryfinder-kfddqdergq-ew.a.run.app/anniversaries
auth:
type: OIDC
query:
sheetId: ${input.sheetId}
emails: ${input.emails}
result: anniversaries
- sendEmailsOrNot:
switch:
- condition: ${len(anniversaries.body.anniversaries) > 0}
next: sendEmails
- condition: ${len(anniversaries.body.anniversaries) == 0}
next: doNotSendEmails
- sendEmails:
call: http.post
args:
url: https://emailsender-kfddqdergq-ew.a.run.app/emails
auth:
type: OIDC
body: ${anniversaries.body}
- doNotSendEmails:
return: "No anniversaries"

Summing Up

This writing describes a simple way of building any event reminder application using Cloud Scheduler and Cloud Workflows. The former is responsible of triggering the reminding while the latter orchestrates HTTP services that hold the logic of what needs to be reminded. Typically, we created a workflow that orchestrates 2 Cloud Run services and used a Cloud Workflows switch step to early stop the workflow when needed.

If this sparked your interest, you’ll find the full working code in this gitlab repository. Please, request an access by sending an email at [email protected] and I’ll be happy to add you to the repository.

Thank you for your time and see you soon with another interesting article.

[1] https://www.flaticon.com/free-icon/sheets_281761?term=google+sheets&page=1&position=1&origin=tag&related_id=281761

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment