Answer the question
In order to leave comments, you need to log in
How to teach the IIS server to perform timer checks?
Task: to make a site that will collect news from news resources (several specific ones) and show only the latest news (only for the last hour, for example). If there was no news for an hour, then the site will display that "there was no news for an hour."
I am using ASP.NET MVC framework and free hosting on IIS. In order to "take" news from news sites, I want to use Selenium Web Driver. And the question is: how to implement such a task so that a separate service would access sites every few minutes (for example, 60) and watch the news (how to implement news viewing and selection of new ones, I roughly imagine) and record relevant news in the Model? That is, the emphasis is on how to teach the server to do something on a timer?
So far, I have done this: in the index method in the controller, every time the page is accessed, I execute the logic using selenium, that is, every time the site is accessed, the server accesses the news sites, processes the information and returns the result. And I need the server to check the news sites for updates once every hour and write the results of the check to the model, and each time the site is accessed, the latest news would be taken from the model. How?
Answer the question
In order to leave comments, you need to log in
more or less like this:
public class JobHost : IRegisteredObject
{
private readonly object _lock = new object();
private bool _shuttingDown;
public JobHost()
{
HostingEnvironment.RegisterObject(this);
}
public void Stop(bool immediate)
{
lock (_lock)
{
_shuttingDown = true;
}
HostingEnvironment.UnregisterObject(this);
}
public void DoWork(Action work)
{
lock (_lock)
{
if (_shuttingDown)
{
return;
}
work();
}
}
}
public static class MyHelper
{
private static readonly Timer _timer = new Timer(OnTimerElapsed);
private static readonly JobHost _jobHost = new JobHost();
public static void Start()
{
_timer.Change(TimeSpan.Zero, TimeSpan.FromHours(1));
}
public static void OnTimerElapsed(object sender)
{
_jobHost.DoWork(() =>
{
// action ...
});
}
}
public class Application : HttpApplication
{
protected void Application_Start()
{
// ....
MyHelper.Start();
}
}
I would not use a web server for such tasks at all (it has enough of its own tasks), but would write a desktop bot (or service) that would collect and save data to your source.
Indeed, the easiest option is a console that goes to sites, collects all the news and writes to the right storage. Well, the scheduled launch of the
Service is a slightly more complicated option, but more correct.
And also, look at once, do the information portals, on which you set the bot, have any APIs?
This option would be even better.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question