Search code examples
azureazure-devopsazure-webjobs

Deploying continuous Azure WebJob and WebApp blocks App


I'm currently deploying one Azure WebJob with a corresponding Web App to Azure via Azure DevOps. My Web Job startup is mostly taken from various examples like https://github.com/Azure/azure-webjobs-sdk/tree/dev/sample/SampleHost

public class Program
{
    public static async Task Main()
    {
        IConfigurationRoot configRoot = null;

        var reg = new ServiceRegistry();
        reg.Scan(
            scanner =>
            {
                scanner.AssembliesFromApplicationBaseDirectory();
                scanner.LookForRegistries();
            });

        var builder = new HostBuilder()
            .ConfigureWebJobs(
                f =>
                {
                    f.AddAzureStorageCoreServices();
                    f.AddTimers();
                })
            .ConfigureAppConfiguration(
                (context, configurationBuilder) =>
                {
                    configurationBuilder.Sources.Clear();
                    configRoot = configurationBuilder
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", false, true)
                        .Build();
                })
            .ConfigureLogging(
                (context, b) =>
                {
                    b.AddApplicationInsightsWebJobs(
                        cfg =>
                        {
                            cfg.InstrumentationKey = "xxxx";
                        });

                    b.AddConsole();
                })
            .UseConsoleLifetime();

        var host = builder
            .UseLamar(reg)
            .Build();

        using (host)
        {
            var container = host.Services.GetService<IContainer>();
            var section = configRoot.GetSection(AppSettings.SectionKey);
            var settings = section.Get<AppSettings>();

            container.Configure(
                cfg =>
                {
                    cfg.AddSingleton(settings);
                });

            await host.RunAsync();
        }
    }
}

From what I understood, as I have a second file with functions like crontriggers, the webjob has to run in a continuous mode to stay alive. As I would like to deploy the webjob as part of the web app, I've added a YAML taks on the release-build, looking like this:

  - task: DotNetCoreCLI@2
    displayName: Publish Web Jobs
    inputs:
      command: publish

      publishWebProjects: false

      projects: 'Backend/Sources/Application/WebJobs/xxx.WebJobs.csproj'

      arguments: '--configuration $(BuildConfiguration) --output $(PublishPath)/App_Data/jobs/continuous/WebJobs /p:DeleteExistingFiles=True'

      zipAfterPublish: false

      modifyOutputPath: false

A variation of that task, is taken from https://www.rahulpnath.com/blog/azure-webjobs-dotnet-core-build-depoy/

After the build, I deploy the web app and the job in one go via a default configuration of the task "Azure App Service deploy".

What's interesting: If I start using this tasks and the code, the web app doesn't get properly deployed anymore. Checking the logs, I see that the web job is in fact deployed and immediately starts working. I would therefore think, the web job is now kindahow blocking the web app from being deployed completely, as it is already running (and probably locking stuff?)

The most examples I found are with former versions of the web jobs and also I don't find any examples, which mention this problem. Is it possible, this combination doesn't work and I would need to deploy web app and web job seperately, or is there another problem around?


Solution

  • Wow, that was tricky: so I ended up with two seperate deployment tasks: enter image description here

    For the WebJobs, I've defined a virtual path: enter image description here

    Most people write, that the webjobs have to be under wwwroot/App_Data, this is wrong. According to https://github.com/Azure/app-service-announcements/issues/84, site/jobs/ is working as well. It is also important to deploy the Web Jobs first, otherwise the web app data gets lost (I don't know why). Also, as the deployment and starting of the app become slower, I had to insert a wait-task before running some Postman tests.