0

I needed some feedback on how to use azure webjobs for this requirement.

I have a requirement where we need to send notification emails at scheduled time. The frequency of the notification are stored in database tables. example of table entries: enter image description here

The webjob should read the configuration from DB table and send notification emails at the configured time (configured time is the time mentioned in DB entries). There will be multiple entries in DB table and there can be new entries added or modified. The example above just has 2 entries, there can be more.

Questions:

  1. How can I make the webjob trigger emails at that particular time of day after reading the configuration from DB?
  2. Is there a better way to implement this notification scheduling using any other azure resources?
  3. How to make sure the webjob successfully sent the email?
  4. Is there a retry mechanism if the webjob fails to send email?

Any help would be appreciated.

Thanks in advance.

user2439903
  • 1,277
  • 2
  • 34
  • 68
  • You could use webjob, azure function, logic apps to do scheduler job, but you need convert your time to cron expression or data format. You could also choose to add a column to your table named with "cron". – George Chen Jan 08 '19 at 08:52
  • Another option would be to trigger the webjob/function every minute and check to see if there are any mails to be sent. – rickvdbosch Jan 08 '19 at 08:59
  • @GeorgeChen: in that case should the webjob be continuous or triggered? – user2439903 Jan 08 '19 at 18:56
  • @user2439903 cause you should monitor the table continually so I think you could set it as continuous, then add the timer triggers. – George Chen Jan 09 '19 at 01:01
  • @George Chen: if creating "cron" as a column in the table was a solution, how would we set the time for webjob? Do you have an example. Sorry, that I am asking on how it can be done programatically. I m new to using webjobs. – user2439903 Jan 09 '19 at 22:52

1 Answers1

1

If you want to select "cron" data from table then create timer trigger, you could use expression binding. From the official doc we could know we could get the binding from an app setting, you could do this or refer to the below code.

 public class Program
{
  public static void Main()
   {
    JobHostConfiguration config = new JobHostConfiguration();
    config.NameResolver = new TimeResolver();
    config.UseTimers();
    JobHost host = new JobHost(config);
    host.RunAndBlock();
}

private class TimeResolver : INameResolver
{
    public string Resolve(string name)
    {
        string value = string.Empty;
        switch (name)
        {
            case "TimerJob":
                Console.WriteLine("Name Is TimerJob : " + name);
                value = "00:00:10";
                break;
            case "TimerJobAlter":
                Console.WriteLine("Name Is TimerJobAlter : " + name);
                value = "00:00:20";
                break;
        }
        return value;
    }
}


//Runs once every 30 seconds
public static void TimerJob([TimerTrigger("%TimerJob%")] TimerInfo timer)
{
    Console.WriteLine("Timer1 job fired!");
}

// Runs once every 60 seconds
public static void TimerJobAlter([TimerTrigger("%TimerJobAlter%")] TimerInfo timer)
{
    Console.WriteLine("Timer2 job fired!");
}
}

And you could choose to set the value into configuration file then read from it. About how to read it , you could look in to this doc.

About detailed example code to create webjob with table data, sorry I don't have, hope these code could help you. If you still have other questions, please let me know.

George Chen
  • 13,703
  • 2
  • 11
  • 26
  • thanks a lot. This code sample helps. The example you shared shows how to set cron expression dynamically and how to read values from app config. What solution do you suggest when I have 100+ configuration or cron expressions in db table and I have to read the entries and start the same job with those 100+ cron configurations? I also have to restart the webjob if any of the cron expressions get updated in database. Also, how can I read the db in this webjob and update cron expressions? – user2439903 Jan 10 '19 at 06:25
  • You could run job with for loop. You can access Azure SQL Databases from the web job as demonstrated in this answer.https://stackoverflow.com/questions/24941001/sql-database-access-from-a-webjob-in-azure/24943522#24943522 And if you store your data in azure SQL I don't think you need to restart it, you could just update in database the it will read it. – George Chen Jan 10 '19 at 06:53
  • @user2439903 , any more questions? If it's helpful, you could accept it as the answer. – George Chen Jan 17 '19 at 06:10
  • @ George Chen: I was thinking if there is a way where i can read the sql DB table column which has cron, get all the cron expressions and execute the same TimerTrigger method with all those cron expression. I need to execute the same method at different timing(specified in cron expression). How can I do that? I still couldn't achieve it. Let me know if my question is not clear. Thanks for following up. – user2439903 Jan 17 '19 at 18:58
  • @user2439903, actually I have not written the example code like yours, so it's my thoughts. You get all cron data and wet them as a list then loop execute the method and replace the %TimerJob%. – George Chen Jan 18 '19 at 02:37
  • ok. how do I loop execute TimerTrigger? I don't know how to do it. My assumption was, once the cron expression is set for TimerTrigger method, it gets executed for 1 particular time configuration. Not sure how to call the same method for multiple cron expressions. Please do share if you have a working example. – user2439903 Jan 18 '19 at 17:54