D
D
Dmitry2017-11-09 15:48:29
ASP.NET
Dmitry, 2017-11-09 15:48:29

How to execute a method on the controller once a day at a certain time?

Actually, the controller has a method:

public ActionResult HideAllFood()
        {
            var foods = _canteenService.GetCanteenFoods();
            foreach (var food in foods)
            {
                if (!food.Block)
                {
                    var menu = _canteenService.GetCanteenMenubyId(food.MenuID);
                    if (menu.StatusID != 2)
                    {
                        food.StatusID = 3;
                        _canteenService.UpdateCanteenFood(food);
                    }
                }
            }
            return Json(new {success = true});
        }

How can I make it run once a day at a certain time?
I kind of found a solution, but I can't seem to do it. I'm trying to execute it in Global.asax.cs , where my studio swears at my repository.
public class TimerModule : IHttpModule
        {
            static System.Threading.Timer timer;
            long interval = 30000; //30 секунд
            static object synclock = new object();
            static bool sent = false;

            public void Init(HttpApplication app)
            {
                timer = new System.Threading.Timer(new TimerCallback(HideallFood), null, 0, interval);
            }

            private void HideallFood(object obj)
            {
                lock (synclock)
                {
                    DateTime dd = DateTime.Now;
                    if (dd.Hour == 14 && dd.Minute == 1 && sent == false)
                    {
                        var foods = _canteenService.GetCanteenFoods();
                        foreach (var food in foods)
                        {
                            if (!food.Block)
                            {
                                var menu = _canteenService.GetCanteenMenubyId(food.MenuID);
                                if (menu.StatusID != 2)
                                {
                                    food.StatusID = 3;
                                    _canteenService.UpdateCanteenFood(food);
                                }
                            }
                        }

                        sent = true;
                    }
                    else if (dd.Hour != 14 && dd.Minute != 1)
                    {
                        sent = false;
                    }
                }
            }
            public void Dispose()
            { }
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2017-11-13
@Nardil

In general, colleagues at work suggested how best to do it. I paint, maybe it will be useful to someone.
I created a table in the database with three columns (Name, Type, Value, all values ​​of type nvarchar)
Then we create an entity:

public class SystemVariable : BaseEntity
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public string Value { get; set; }
    }

Then we register in the Service.
On the controller, it ended up looking like this:
public ActionResult Menu()
        {
            var var = _systemService.GetSystemVariableByName("DateMenuUpdate");
            if (var != null)
            {
                var type = Type.GetType(var.Type);
                var menuUpdate = (DateTime)Convert.ChangeType(var.Value, type);
                var dayCount = DateTime.Now.DayOfWeek == DayOfWeek.Monday ? -3 : -1;
                if (DateTime.Now.DayOfWeek != DayOfWeek.Saturday && DateTime.Now.DayOfWeek != DayOfWeek.Sunday && 
                    (menuUpdate.Day != DateTime.Now.Day && DateTime.Now.Hour >= 14) || (menuUpdate.Day < DateTime.Now.AddDays(dayCount).Day) && DateTime.Now.Hour < 14) 
                {
                    var.Value = DateTime.Now.ToString();
                    var foods = _canteenService.GetCanteenFoods();
                    foreach (var food in foods)
                    {
                        if (!food.Block)
                        {
                            var menu = _canteenService.GetCanteenMenubyId(food.MenuID);
                            if (menu.StatusID != 2)
                            {
                                food.StatusID = 3;
                                _canteenService.UpdateCanteenFood(food);
                            }
                        }
                    }
                     _systemService.UpdateSystemVariable(var);
                }
            }
            return View();
        }

C
cicatrix, 2017-11-09
@cicatrix

It is better to let cron run a stored procedure in the database.
The web server only launches your application on demand (when a client accesses it). Periodically (by default once every 20 minutes) your application will be restarted.
Where is the guarantee that your code will not be executed twice? Maybe not scary, but who knows what will be added to this code later?
Any service operations must be performed outside the context of the web application. A web application should serve client requests, nothing more.

D
Dmitry Filandor, 2017-11-09
@LifeAct

public class TimerModule : IHttpModule
    {
        static Timer timer;
        long interval = 30000; //30 сек
        static object synclock = new object();
        static bool sent = false;

        public void Init(HttpApplication app)
        {
            timer = new Timer(new TimerCallback(SendEmail), null, 0, interval);
        }

        private void SendEmail(object obj)
            {
            lock (synclock)
            {
                DateTime dd = DateTime.Now;


                
                if (dd.Hour == 12 && dd.Minute == 13 && sent == false)
                {

                   //что то делаем


                    sent = true;

                }
                
                else if (dd.Hour != 12 && dd.Minute != 13)
                {
                    sent = false;
                }
            }
        }
        public void Dispose()
        { }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question