Question

I'm developing an ASP.NET web application. There is some functionality I need to trigger every 10 minutes. So currently I'm considering a 'scheduled task' or to create a Windows service to call the URL.

But I remember once I did the same thing in a PHP web hosting space, using a cron job.

So is there anything like cron jobs in IIS?

Note: I'm not expecting to use 3rd-party online scheduler services.

Was it helpful?

Solution

Apples and oranges. cron is a Unix service. IIS is a web server. Just as cron is not included in Apache or nginx, there is no sense in which IIS contains a scheduler. However, you can use schtasks.exe in a similar way you can use cron on Unix.

You might find more ideas in this question.

OTHER TIPS

I agree with @Amadan - apples and oranges.

However, I would tend toward writing a custom Windows service for this purpose rather than using Windows Task Scheduler. Another SO question speaks to pros & cons of each option.

While Windows services are straightforward(-enough) to develop, you might consider the open-source Topshelf framework to ease some of the development and deployment quirks that typically come with them.

And to be fair, remember that cron is not part of PHP of course, just a tool that may be available to you in *nix PHP hosting environments: ASP.NET is no different really; the question is what cron-like tool is available to you in your ASP.NET environment that meets your requirements and you prefer.

You may want to take a look at the Revalee open source project.

You can use it to schedule web callbacks at specific times. In your case, you could schedule a web callback (10 minutes in the future). When your application receives the callback, it can schedule the next 10 minute callback. When your ASP.NET application launches for the very first time, then you would schedule the first web callback. Since you web application is being called back you do not need to worry about IIS unloading your web application (which it, of course, will).

For example using Revalee, you might do the following:

  1. Register a future (10 minutes from now) callback when your application launches via the ScheduleTenMinuteCallback() method (see below).

    private DateTimeOffet? previousCallbackTime = null;
    
    private void ScheduleTenMinuteCallback()
    {
        // Schedule your callback 10 minutes from now
        DateTimeOffset callbackTime = DateTimeOffset.Now.AddMinutes(10.0);
    
        // Your web service's Uri
        Uri callbackUrl = new Uri("http://yourwebapp.com/ScheduledCallback.aspx");
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    
        previousCallbackTime = callbackTime;
    }
    
  2. When the web scheduled task activates and calls your application back, you perform whatever action you need to do every 10 minutes and you schedule the next callback too. You do this by adding the following method call (CallbackMonitor()) to your ScheduledCallback.aspx page handler.

    private void CallbackMonitor()
    {
        if (!previousCallbackTime.HasValue 
            || previousCallbackTime.Value <= DateTimeOffset.Now.AddMinutes(-10.0))
        {
            // Perform your "10 minutes have elapsed" action
    
            // ...do your work here...
    
            // Schedule subsequent 10 minute callback
            ScheduleTenMinuteCallback();
        }
    }
    

Your point about not using '3rd party online scheduler services' is understood. The Revalee Service is not an external 3rd party online scheduler service, but instead a service (a Windows Service, more specifically) that you install and control fully on your own network. It resides and runs on a server of your own choosing, most likely behind your firewall, where it can receive callback registration requests from your web application on IIS. (It can, of course, be installed on the IIS web server if necessary.)

I hope this helps.

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.

For the new readers, now you can easily create a background cron job from the .Net or .Net Core App itself using QuartZ Library. Instead of creating a task and windows service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top