Most scheduled tasks and many Windows services can move to Azure Functions, but not by copying them across. A job that runs for a few minutes, works on data it can reach over the network and doesn't depend on the server it lives on is a good fit. A job that runs for hours, holds state in memory, or relies on COM components, the registry or a mapped drive needs refactoring first, or a different home in Azure.
What kinds of background job are there?
Background jobs on Windows servers usually come in two shapes.
Scheduled tasks run on a timer through Windows Task Scheduler: a console application that imports a supplier's price file at 6am, sends overdue invoice reminders each morning or rebuilds a report table overnight.
Windows services run continuously. Some poll a folder or a database table every minute. Some listen on a queue such as MSMQ. Some do both, plus a few other things added over the years, because the service was already installed and running.
Both kinds tend to be undocumented, run under a service account with more rights than they need, and are noticed only when they stop.
Why move background jobs to Azure Functions?
The server is usually the reason. Jobs often sit on an ageing Windows server that nobody wants to rebuild, and they are what stops it being switched off. Moving them to Functions removes that dependency, and brings:
- Triggers that replace your own polling code. A job can run when a file lands in storage, a message arrives on a Service Bus queue or an HTTP request comes in, rather than checking every minute.
- Scaling that follows the work. A queue-triggered function can process a backlog with several instances in parallel, then scale back down.
- Logging and alerting through Application Insights and Azure Monitor, rather than a text file on a server that someone has to remote into.
- Deployment through a pipeline, the same way as your other code.
Which background jobs suit Azure Functions?
A job is a good candidate when it:
- Finishes in minutes, or can be split into pieces that each do.
- Starts from something Functions can trigger on: a schedule, a queue message, a new file or blob, an HTTP call or an Event Grid event.
- Reads and writes data over the network, through databases, APIs and storage, rather than the local disk.
- Holds no state in memory between runs.
- Can run safely more than once on the same input, because retries happen.
A job is a poor candidate, at least as it stands, when it:
- Runs for hours without a natural place to split it.
- Keeps a long-lived connection open, such as a socket to a piece of hardware or a session with an old mainframe.
- Depends on software installed on the server, such as a printer driver, Office or a vendor's COM component.
- Needs to reach systems only available on the local network, with no plan for connecting Azure to them.
Poor candidates still have options. Azure Container Apps jobs and WebJobs on App Service suit long-running work. Jobs that need Windows software installed, such as a COM component or printer driver, belong in a Windows container or on a virtual machine until that dependency is replaced.
What are the Azure Functions timeout limits?
How long a function can run depends on the hosting plan. On the Consumption plan, a function times out after five minutes by default and can be configured to run for up to ten. The Flex Consumption, Premium and Dedicated plans allow much longer runs, though on a Dedicated plan Always On must be switched on. Whatever the plan, an HTTP-triggered function has to respond within 230 seconds or the caller gets a timeout, so long work behind an HTTP call should be handed to a queue.
Even on a plan without a hard limit, long single runs are fragile. Instances can be recycled during platform updates or scale-in, and a job that is halfway through a two-hour loop loses its place. Design for that by splitting the work:
- A timer function finds what needs doing and puts one message per item on a queue.
- A queue-triggered function processes each item. Failures retry per item, and items that keep failing are moved to a dead-letter queue, or a poison queue if you use Storage queues.
- If the process needs steps in order, waits or a final step once everything is done, Durable Functions keeps track of progress and resumes after a restart.
The overnight job that loops through 50,000 customer records becomes many small, independent units of work, and one bad record no longer stops the run.
Which Windows dependencies cause problems?
Functions runs in a managed environment, not on a server you control. Before moving a job, check whether it uses any of these:
- Local file paths and mapped drives. Replace them with Blob Storage or Azure Files.
- Settings in the registry or machine-wide config files belong in app settings, with secrets in Key Vault.
- COM components and installed software are the hardest. They may need a Windows container, a virtual machine or a replacement library.
- Domain service accounts give way to managed identities for Azure resources. On-premises systems that expect Windows authentication need a different approach, often an API in front of them.
- MSMQ has no place in Functions. Service Bus queues replace it, with dead-lettering built in and duplicate detection available on the Standard and Premium tiers.
- Timer triggers use UTC by default. A job that must run at 2am UK time all year needs the WEBSITE_TIME_ZONE app setting, which the Flex Consumption and Linux Consumption plans don't support, or a schedule that allows for British Summer Time.
- A job that talks to an on-premises SQL Server needs a route to it, through a VNet connected to your network or a hybrid connection. The Consumption plan supports neither.
Does the code have to be upgraded to modern .NET first?
Not always. The Functions isolated worker model supports .NET Framework 4.8.1 on Windows, so code written for 4.8 can usually move with a retarget rather than an upgrade. It needs a Windows plan: Flex Consumption runs on Linux only and can't host it. That is useful for getting off a server quickly, but the long-term home is modern .NET, where the newest Functions features and libraries land. If the job's code is shared with a larger application, upgrading the shared libraries first usually makes both moves easier. Our guide to upgrading .NET Framework to modern .NET covers that in stages.
How do you move background jobs to Azure Functions safely?
- List every scheduled task and Windows service on the server, with what each one does, when it runs and what it touches. Expect to find some nobody knew about.
- Separate the business logic from the hosting code. Pull the useful work out of the service's main loop or console entry point into a class library that can be tested on its own.
- Pick the trigger. Replace polling with a queue, blob or Event Grid trigger where you can; keep a timer only where the work is genuinely time-based.
- Remove the Windows dependencies listed above, one at a time, testing as you go.
- Make processing safe to repeat, so a retry doesn't send the same email or post the same invoice twice.
- Add logging with business identifiers and set alerts on failures and on runs that don't happen.
- Run the old and new versions side by side where possible, with the new one writing to a test target or in a read-only mode, and compare results.
- Switch over, keep the old job disabled but in place for a short period, then remove it and, when the last job has gone, the server.
What mistakes should you avoid?
- Lifting a Windows service's polling loop into a timer function that runs every minute. It works, but it carries the old design's problems with it.
- Ignoring overruns. A timer function won't start a new run while the last one is still going, so a job that takes longer than its interval quietly falls behind.
- Treating the Consumption plan's time limit as something to fix later.
- Moving a job without its monitoring. On the old server someone may have noticed the log file stop growing; in Azure, nobody will unless you set an alert.
For the choice between code and workflows for the integration side of a job, see Logic Apps or Azure Functions.
We move background jobs off ageing Windows servers and onto Azure, as part of wider modernisation or on their own. Read more about our legacy modernisation service and our work on Azure integration.
Email [email protected] with your brief.