Answer the question
In order to leave comments, you need to log in
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});
}
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
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; }
}
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();
}
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.
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 questionAsk a Question
731 491 924 answers to any question