0

I have an ASP.NET app which sends emails whenever the user signs up in the web site. I'm using hangfire in order to manage the jobs and postal in order to send emails.

It all works great, but here's the thing:

I want the superuser to change how many times the APP can send the email before deleting the job.

Here's my code

    public static void WelcomeUser(DBContexts.Notifications not)
    {
        try{
            var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
            var engines = new ViewEngineCollection();
            engines.Add(new FileSystemRazorViewEngine(viewsPath));

            Postal.EmailService service = new Postal.EmailService(engines);

            WelcomeUserMail welcomeUserMail = new WelcomeUserMail();
            welcomeUserMail.To = not.ReceiverEmail;
            welcomeUserMail.UserEmail = not.ReceiverEmail;
            welcomeUserMail.From = BaseNotification.GetEmailFrom();

            service.Send(welcomeUserMail);
        }
        catch(Exception e)
        {
            DBContexts.DBModel dbModel = new DBModel();
            DBContexts.Notifications notificacionBD =  dbModel.Notifications.Find(not.NotificationID);

            notificacionBD.Status = false;
            notificacionBD.Timestamp = DateTime.Now;
            notificacionBD.Error = e.Message;

            int numberOfRetriesAllowed = ParameterHelper.getNumberOfRetriesAllowed();

            if (notificacionBD.Retries > numberOfRetriesAllowed)
            {
                //In this case Hangfire won't put this job in the failed section but rather in the processed section.
                dbModel.SaveChanges();
            }
            else
            {
                notificacionBD.Retries++;
                dbModel.SaveChanges();

                throw new Exception(e.Message);
            }
        }
    }
Luis Deras
  • 1,239
  • 2
  • 19
  • 48

1 Answers1

0

Why not just add attributes to handle it automatically?

[AutomaticRetry(Attempts = 10, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void MyTask(){
    //doing stuff
}

Or you could just make your own attribute that mimics the AutommaticRetryAttribute class but you handle it how you want?

https://github.com/HangfireIO/Hangfire/blob/a5761072f18ff4caa80910cda4652970cf52e693/src/Hangfire.Core/AutomaticRetryAttribute.cs

Erick Smith
  • 937
  • 7
  • 27
  • Well, point is I want the super user to be allowed to change the number of attempts. That's why I want to handle it my way. – Luis Deras Apr 14 '16 at 14:06
  • Right. You can create your own AutomaticRetry attribute by reviewing how the AutomaticRetryAttribute was done but replacing with your own conditions, maybe? – Erick Smith Apr 14 '16 at 14:34