locked
Azure Web Job Timer trigger not firing when adding custom schedule to it RRS feed

  • Question

  • User1649464185 posted

    I have created a function which is triggered by a timer. It runs fine when triggered from the Azure portal by clicking on the Run option on the exact time provided through the database. But does not fire automatically for the scheduled date taken from a database. The timer schedule here is not static but the value is taken from the database and converted to CRON expression. Used INameResolver to resolve the name and overwrite it with the database date and time by converting it to CRON expression.

    It runs fine when a constant CRON expression is passed to TimerTrigger attribute.

    Any help on this matter would be appreciated.

    Thanks in Advance

    Regards,

    Raksha Solanki

    Thursday, May 2, 2019 10:55 AM

All replies

  • User61956409 posted

    Hi Raksha Solanki,

    Welcome to ASP.NET forums.

    The timer schedule here is not static but the value is taken from the database and converted to CRON expression. Used INameResolver to resolve the name and overwrite it with the database date and time by converting it to CRON expression.

    It seems that you'd like to dynamically specify ScheduleExpression in code for your timer trigger Web Job based on the data that you stored in database. As you mentioned, we can do that by passing a NameResolver object in to the JobHostConfiguration object, for detailed information, please check the following link:

    [Custom binding expressions](https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#custom-binding-expressions). 

    And I did a test to achieve the requirement with following code snippet, which work well for me.

    public class ScheduleExpressionResolver : INameResolver
    {
        public string Resolve(string name)
        {
            //in your code, you can retrieve the data from your database
            //then dynamically generate CRON expression 
    
            return "*/30 * * * * *";
        }
    }

    Pass NameResolver class in to the JobHost object, like below.

    static void Main()
    {
        var config = new JobHostConfiguration();
    
        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
        config.NameResolver = new ScheduleExpressionResolver();
    
        config.UseTimers();
    
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

    Code of schedule job.

    public static void StartJob([TimerTrigger("%custom_corn_expression_name_here%", RunOnStartup = false)]TimerInfo timerInfo)
    {
        Console.WriteLine("hello");
    }

    Test Result:

    With Regards,

    Fei Han

    Friday, May 3, 2019 2:42 AM
  • User1649464185 posted

    Hi Fei,

    Thanks for the above piece of code. 

    Can you please guide me with a solution for deploying this web job on Azure portal so that it gets triggered automatically on the schedule taken from the database as the user can change the settings for date and time from the front end at any time.

    I have tried by deploying the web job on Azure portal from the Visual Studio 2017. But it needs to be externally triggered by clicking the run option provided on the portal and does not trigger automatically on the provided schedule taken from the database. 

    Thanks and Regards,

    Raksha Solanki

    Friday, May 3, 2019 4:28 AM
  • User61956409 posted

    Hi Raksha Solanki,

    I tried to deploy my testing WebJobs to Azure, which do work well as expected. The following is execution logs:

    You can refer to this documentation to deploy the WebJobs to Azure App Service.

    https://docs.microsoft.com/en-us/azure/app-service/webjobs-dotnet-deploy-vs#webjobs-as-net-framework-console-apps

    Besides, if you have further questions about Azure WebJobs, you can also get help on StackOverflow for WebJobs.

    With Regards,

    Fei Han

    Monday, May 6, 2019 2:06 AM