Module 7 - ACA Scheduled Jobs with Dapr Cron Binding¶
Module Duration
60 minutes
Curious about Azure Container Apps jobs?
There is a new kid on the block. Azure Container Apps jobs became generally available in late August 2023. This workshop is not yet updated to account for this new type of container app. Stay tuned for updates!
Objective¶
In this module, we will accomplish three objectives:
- Learn how the Cron binding can trigger actions.
- Add a Cron binding to the Backend Background Processor.
- Deploy updated Background Processor and API projects to Azure.
Module Sections¶
-
From the VS Code Terminal tab, open developer command prompt or PowerShell terminal in the project folder
TasksTracker.ContainerApps
(root): -
Restore the previously-stored variables by executing the local script. The output informs you how many variables have been set.
1. The Cron Binding¶
In the preceding module, we discussed how Dapr bindings can simplify the integration process with external systems by facilitating the handling of events and the invocation of external resources.
In this module we will focus on a special type of Dapr input binding named Cron Binding.
The Cron binding doesn't subscribe to events coming from an external system. Instead, this binding can be used to trigger application code in our service periodically based on a configurable interval. The binding provides a simple way to implement a background worker to wake up and do some work at a regular interval, without the need to implement an endless loop with a configurable delay. We intend to utilize this binding for a specific use case, wherein it will be triggered once daily at a particular time (12:05 am), and search for tasks that have a due date matching the previous day of its execution and are still pending. Once the service identifies tasks that meet these criteria, it will designate them as overdue tasks and save the revised status on Azure Cosmos DB.
Contrasting the binding to Azure Container Apps jobs, we do not need a separate container app and can integrate this binding into our existing backend service.
2. Updating the Backend Background Processor Project¶
2.1 Add Cron Binding Configuration¶
To set up the Cron binding, we add a component file that specifies the code that requires triggering and the intervals at which it should occur.
To accomplish this, create a new file called dapr-scheduled-cron.yaml within the components folder and insert the following code:
Curious to learn more about above yaml file configuration?
The actions performed above are as follows:
- Added a new input binding of type
bindings.cron
. - Provided the name
ScheduledTasksManager
for this binding. This means that an HTTP POST endpoint on the URL/ScheduledTasksManager
should be added as it will be invoked when the job is triggered based on the Cron interval. - Setting the interval for this Cron job to be triggered once a day at 12:05am. For full details and available options on how to set this value, visit the Cron binding specs..
2.2 Add the Endpoint Which Will be Invoked by Cron Binding¶
Let's add an endpoint which will be triggered when the Cron configuration is met. This endpoint will contain the routine needed to run at a regular interval.
Add a new file under the Controllers folder in the project TasksTracker.Processor.Backend.Svc as shown below:
Here, we have added a new action method called CheckOverDueTasksJob
, which includes the relevant business logic that will be executed by the Cron job configuration at specified intervals.
This action method must be of the POST
type, allowing it to be invoked when the job is triggered in accordance with the Cron interval.
2.3 Update the Backend Web API Project¶
Now we need to add two new methods which are used by the scheduled job.
Update these files under the Services folder in the project TasksTracker.TasksManager.Backend.Api as highlighted below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
Add a new file in a new Utilities folder in the project TasksTracker.TasksManager.Backend.Api as shown below:
Curious to learn more about above code?
What we've implemented here is the following:
- Method
GetYesterdaysDueTasks
will query the Cosmos DB state store using Dapr State API to lookup all the yesterday's task which are not completed yet. Remember that Cron job is configured to run each day at 12:05am so we are interested to check only the day before when the service runs. We initially made this implementation simple. There might be some edge cases not handled with the current implementation. - Method
MarkOverdueTasks
will take list of all tasks which passed the due date and set the flagIsOverDue
totrue
.
Add the new methods to the fake implementation for class FakeTasksManager.cs
so the project TasksTracker.TasksManager.Backend.Api builds successfully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
2.4 Add Action Methods to Backend Web API project¶
As you've seen previously, we are using a Dapr Service-to-Service invocation API to call methods api/overduetasks
and api/overduetasks/markoverdue
in the Backend Web API from the Backend Background Processor.
Add a new file under the Controllers folder in the project TasksTracker.TasksManager.Backend.Api as shown below:
2.5 Add Cron Binding Configuration Matching ACA Specs¶
Add a new file folder aca-components. This file will be used when updating the Azure Container App Env and enable this binding.
Note
The name of the binding is not part of the file metadata. We are going to set the name of the binding to the value ScheduledTasksManager
when we update the Azure Container Apps Env.
3. Deploy the Backend Background Processor and the Backend API Projects to Azure Container Apps¶
3.1 Build the Backend Background Processor and the Backend API App Images and Push them to ACR¶
To prepare for deployment to Azure Container Apps, we must build and deploy both application images to ACR, just as we did before. We can use the same PowerShell console use the following code (make sure you are on directory TasksTracker.ContainerApps):
3.2 Add the Cron Dapr Component to ACA Environment¶
3.3 Deploy New Revisions of the Backend API and Backend Background Processor to ACA¶
As we did before, we need to update the Azure Container App hosting the Backend API & Backend Background Processor with a new revision so our code changes are available for the end users. To accomplish this run the PowerShell script below:
Note
The service ScheduledTasksManager
which will be triggered by the Cron job on certain intervals is hosted in the ACA service ACA-Processor Backend
. In the future module we are going to scale this ACA ACA-Processor Backend
to multiple replicas/instances.
It is highly recommended that background periodic jobs are hosted in a container app with one single replica, you don't want your background periodic job to run on multiple replicas trying to do the same thing. This, in fact, would be a limitation that could call for a separate Azure Container App jobs implementation as we typically want or app/API/service to scale.
Success
With those changes in place and deployed, from the Azure portal, you can open the log streams of the container app hosting the ACA-Processor-Backend
and check the logs generated when the Cron job is triggered,
you should see logs similar to the below image
Note
Keep in mind though that you won't be able to see the results instantaneously as the cron job searches for tasks that have a due date matching the previous day of its execution and are still pending.
-
Navigate to the root and persist the module to Git.
Review¶
In this module, we have accomplished three objectives:
- Learned how the Cron binding can trigger actions.
- Added a Cron binding to the Backend Background Processor.
- Deployed updated Background Processor and API projects to Azure.