Does Docker have the ability to run background tasks like Azure Webjobs?

Viewed 1587

I've got a sorta-legacy R&D app which is a monolith website and then a few webjobs which are used for some background processing.

Now, I've been experimenting moving all of this over to Docker + Microservices (note: not because Microservices are the 'new hot stuff' but because our application suits getting split up into more manageable pieces/services.)

It was easy slicing the website up into Gateway API (or BFF Api's) + microservices. But I'm not sure how to handle the webjob migration. The webjobs are (currently) Azure Queue timer and trigger based.

Everything is running under: - Docker (on linux containers) - ASP.NET Core 2.1

Anyone have any suggestions what other ways I can migrate the WebJobs to a Docker container of something?

I know Hangfire is a tool that enables background processing on an ASP.NET website. But before I go down that route, just checking if there are other solutions people use.

Also, .NET Core 2.1 has the concept of an IHostedService ... so I'm not sure if this is a legit solution and if so .. how?

2 Answers

Another option: Implement background tasks in microservices with IHostedService and the BackgroundService class.

  • This is for .NET Core 2.1
  • Doesn't require a website (e.g. Kestrel on ASP.NET Core)
  • Is basically a console app with some smarts to handle the lifecycle of your-tasks/your-services.
  • Possible clean replacement for webjobs/functions.
  • Generic Host Code samples

e.g a really basic code example:

using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace GenericHostSample
{
    public class ProgramHelloWorld
    {
        public static async Task Main(string[] args)
        {
            var builder = new HostBuilder()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<MyServiceA>();
                    services.AddHostedService<MyServiceB>();
                });

            await builder.RunConsoleAsync();
        }
    }
}
Related