Designing background processing
1 hr 30 mins
Front Stage plans to become an international company with offices in multiple countries. The company wants to send a Happy Equinox Day email to each customer shortly after midnight customer local time. When an equinox occurs, daytime and nighttime are of the same duration all over the world. Equinoxes occur twice yearly according to the following schedule.
Front Stage wants the emails to be processed as efficiently as possible to minimize the impact on processing during regular business hours.
The following table provides the credentials you need to complete the exercise.
When you begin, identify the following main challenges and document your approach.
- Calculate the time for sending the email.
- Send the email according to the time calculation.
Although not part of this exercise, you can build out a complete solution by addressing these additional challenges:
- Map a customer address to the appropriate time zone.
- Develop a way to retrieve and store the equinox dates.
- Orchestrate the relationships between the configured rules.
1 Identify design options
As you develop your implementation, rely on rules in Pega Platform™. Consider the following approaches to address the requirements:
- Option 1: Create a single-node job scheduler with spun-off queue processors for each customer time zone.
- Option 2:Create a job scheduler for each known customer time zone. Configure each job scheduler to run at the appropriate time for the given time zone. The job scheduler queries for customers within the indicated time zone, sending each an email.
- Option 3: Create advanced agents that equal or exceed the number of known customer time zones multiplied by the number of nodes. Allow the master agent to generate agent schedules. Remove the unneeded agent schedule. Modify the remaining agent schedules by setting the start time to when emails go out for a given time zone. Each agent schedules queries for customers within the indicated time zone, sending each an email.
2 Evaluate design options
|Create a single-node job scheduler
|Create job scheduler per time zone
|Create advanced agents per time zone
3 Recommend the best design option
The single-node job scheduler with spun-off queue processors for each customer time zone is the recommended approach due to better performance.
4 Tasks to perform
1. Calculate the time for sending the email
First, determine the date that the equinox occurs in each time zone. Although the equinox occurs simultaneously worldwide, the actual date may differ depending on each time zone's geographical location. For example, if the equinox occurs on September 22 at 20:02 GMT, the equinox occurs on September 22 for those located in time zones in and west of GMT +1. For locations east of GMT +1, the equinox occurs on September 23. You need this information to send the email on the morning of the correct date.
Next, calculate the time in GMT to send an email to each customer to deliver an email to that customer shortly after midnight (customer local time) on the day of the equinox. Use GMT offset math to solve the problem. The DateTime library for Pega Platform™ does not contain any function that returns GMT offset within a particular time zone at a particular GMT-expressed DateTime. The function named ToZoneId simplifies the calculation. The function is created in a library ZonedDateTime which imports java.time.* and java.time.format.* .
The function is shown in the following image.
The BackgroundProcEx case type populates a page list of FSG-Examples-Data-TimeZoneInfo instances seeded with different times. Four rule-declare-expressions then fire against the PageList. The page context for the rule-declare-expressions is .TimeZoneList().
Note: The .GMTTime property and associated rule-declare-expression are not required as part of a solution. They are used for verification. The value must always match Top.DateTimeString
The following image is an illustration of the calculated times. IDL stands for International Date Line.
To send a Happy Equinox email to customers in Honolulu, Hawaii, USA, so that the email arrives shortly after midnight (local time) the morning of September 22, 2021, Front Stage sends the email on September 22, 2021, 10:00 GMT. Four hours later, the same difference between two time zones, the Happy Equinox Day email is sent to customers in Sydney, Australia.
Sydney and Honolulu are four time zones apart. As a result, midnight in Sydney occurs four hours later than midnight in Honolulu. The difference is that, after local midnight occurs in Sydney, the calendar date becomes one day greater than the calendar date in Honolulu.
2. Send the email according to the time calculation
Two job schedulers are defined in the solution: VernalEquinoxGreetings and AutumnalEquinoxGreetings. These two job schedulers have been configured to execute yearly on March 19, 2020, and Sept 21, 2020, respectively.
The LaunchTimeZoneQueueProcessors activity that is called by each job scheduler receives the equinox type, VERNAL or AUTUMNAL, and for simplicity, the DateTimeString when the equinox occurs. Ideally, there is a database table from which the DateTimeString can be queried based on the current year and equinox type.
The activity loops through distinct time zones identified by the D_SamplePersonTimeZoneList list data page. For each time zone, the activity creates and populates an FSG-Examples-Work-BackgroundProcEx page named CaseInfo. That page is then used as the step page when invoking the Queue-For-Processing method. The Queue-For-Processing method sees that the configured, dedicated queue processor was configured as delayed; this causes the Queue-For-Process method to display the Date time for processing field. If the queue processor is not configured as delayed, the Date time for processing field is not displayed.
Note: An alternative approach that entails unnecessary risk is having the LaunchTimeZoneQueueProcessor's activity spin-off a new BackgroundProcEx case for each time zone. Each spun-off BackgroundProcEx case avoids a disastrous runaway infinite loop by detecting that a page named CaseInfo exists on the Clipboard. Seeing the CaseInfo page on the Clipboard, the spun-off case changes to the Run in background alternate stage. Within the Run in background stage, the DateTimeString and the TimeZoneInfo properties are copied from the top-level CaseInfo page into the BackgroundProcEx case before the Run in background flow shape. TheRun in background shape tells the delayed SendTimeZoneGreetings queue processor to run at .TimeZoneInfo.MidnightGMT.
The SendTimeZoneGreetings queue processor activity of the same name obtains and iterates each SamplePerson in D_SamplePersonList[TimeZone:Param.TimeZone].pxResults.
Instead of sending a real email that errors out, the activity uses the Log-Message method in a step to output the timezone and WorkPartyUri values to the PegaRULES.log.