user2439903
user2439903

Reputation: 1305

Azure webjobs: send notification emails at scheduled time?

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.

Upvotes: 0

Views: 1623

Answers (1)

George Chen
George Chen

Reputation: 14334

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.

Upvotes: 1

Related Questions