Migrate Azure Scheduler jobs to Azure Functions


Migrate a code-based Azure Scheduler job to Azure Functions


Recently, I migrated a scheduled job from .NET Framework 4.6 to .NET Core and from Azure App Service to Azure Functions. This was part of a larger effort to move to .NET Core, and the retirement of a dependency of Azure Scheduler jobs

While there is nothing wrong with .NET Framework 4.6 or Azure App service, the project was moving toward separate, smaller, quicker deployments for the various projects instead of a single monolithic deployment.


Existing scheduled job: Add feature flags

Add the ability to control you job with a feature flag.
  1. In your Azure App service, create a feature flag for every scheduled job. This can be as simple as an App Configuration setting with a value or you can use Azure App Configuration. 
  2. Add code for the feature flag and make sure it has a default value if it isn't detected in the environment. 
  3. Add logging statements to report your runtime feature flag value. 
  4. Deploy the code to Azure App service and make sure you can stop and start the scheduled job with the feature flag before continuing. 

string FeatureFlagFindRetweets = Environment.GetEnvironmentVariable("FeatureFlagScheduledJobFindRetweets");
if (!String.IsNullOrEmpty(FeatureFlagFindRetweets) && FeatureFlagFindRetweets.ToLower() == "false")
{
    log.WriteLine($"FindRetweet executed at: {DateTime.Now} feature flag disabled");
    return;
}
log.WriteLine($"FindRetweet executed at {DateTime.Now} feature flag enabled");

New Azure Function: Timer trigger

Create a new Azure Function app to replace the scheduled job and add the same feature flags

  1. Create an Azure Function app locally with Visual Studio 2021 and the latest .NET Core you intend to support.
  2. Create a timer trigger and add the same code inside the function as your original scheduled job including the logging.  
  3. Add the same feature flags you use in the original scheduled job. While Azure Functions has an easy way to disable a function, a feature flag allows for greater flexibility in the future. 
  4. Add any dependencies the original project used with NuGet Manager. 
  5. Build your function. 
  6. You probably have a few build errors because the .NET versions are different and there may be breaking changes. These are usually minor issues that take a little investigation. Common issues I've come across are:
    • HTTP Client library changes - used to integrated with other APIs
    • JSON (de)serialization library changed from NewtonSoft to System.Text.Json
    • Entity Framework changes - or whatever database library you use. 
    • Authentication library changes
  7. Fix your build issues but don't change any of the logic of the code yet. This is a straight migration. 
  8. Once your project builds, migrate your tests and perform the same dependency migrations. 
  9. Run your tests and verify your new timer trigger logic still succeeds.
  10. Deploy your timer trigger with your existing deployment infrastructure making sure the feature flags are not enabled. 

Disable Azure Schedule job, enable Azure Function Timer trigger

Azure App Service and Azure Functions both allow you to control the Configuration Settings from a variety of methods include Azure CLI, PowerShell, Azure SDKs, and the Azure portal. 
  1. Determine how you want to automatically deploy configuration changes. For this initial switch over, in a low priority job, you could change these feature flags manually in the Azure portal. For more critical jobs, you should automate the task and include it as part of your deployment pipeline. 
  2. Disable the original scheduler jobs and verify with logging that the job executed but didn't continue past the check of the feature flag. 
  3. Enable the timer trigger and verify with logging that the job executed and did continue past the feature flag check. 

Have a better method? 

Let me know @dfberry

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

How to convert SVG data to a Png Image file Using InkScape

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation