-2

I am planning to create an asp.net mvc web application which will perform a single sync job, to achieve this:-

  1. We have a 3rd party ERP system which will be generating a .csv file on timely basis, where it will generates a .csv file once per hour. The .csv file contain info about our company assets, such as type, price, name ,location, etc…

  2. Now i will develop an asp.net mvc web application which will read the .csv data and update a database with this data.

So I am planning to do the following :-

  1. I will create a new database, which will contain the data.

  2. I will create a new asp.net mvc-5 web application which have a SYNC action method, which will read the .csv data, and update the database.

now the problem I am facing is that I need the sync job to run per hour or on a specific schedule. Now from my previous experience I can list these 2 approaches to call an action method on timely basis:-

  1. Inside the asp.net mvc ‘s global.asax I can create a schedule which runs each hour.

  2. I can use third party tools such as Hangfire to schedule the tasks.

Now using any of these approaches will cause this limitation:-

  1. The global.asax or the third party tools such as Hangfire will run under the application pool, and if no action is performed on the application, then the schedule will never run, since the application pool will not be active. But on my previous applications this was not a real problem as the systems contained many views beside the schedule jobs , so the system stay active almost 100% over working hours.

But in my current project the web application will not be accessed by users, since it will only do a single sync job, and there is not any other functionalities for end users. so in this case the sync job will never run , since the application pool will not be active. So can anyone advice if these approaches sound valid to fix my problem:-

  1. To create a windows schedule task which will be calling the action method URL per hour:-

schtasks /create /tn "my scheduled task" /tr "powershell -ExecutionPolicy unrestricted -Command \"(New-Object Net.WebClient).DownloadString(\\http://url .to.be.executed/cron.php\\")\" /sc DAILY /st 07:00:00 /ru System

  1. To create 2 schedule tasks inside the global.asax . one schedule task will call the application url each 5 minutes to keep the application pool alive, and another schedule task which call the sync action method each hour. In this case the application will keep calling itself every 5 minutes which will force the application pool to say alive and the sync job will run each hour even if no users access the system …

So can anyone advice on this please?

Thanks

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    What kind of advice are you seeking? You need to be clear. By the way, you can configure Hangfire [to always run](http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html). – mason Dec 17 '15 at 17:36
  • @mason But can i add a Windows Schedule Task , which will call the application each 10 minutes for example , and use hangfire as is without having to configure it to always run ? – John John Dec 17 '15 at 17:38
  • 1
    Sure, I think you already knew that. – mason Dec 17 '15 at 17:39
  • @mason so can I follow this approach. 1) to add hangfire to my application, 2) schedule it to run each hour to call the sync job. 3)define a windows schedule task which will call the application every 10 minutes. in this case the application pool will stay alive and the hangfire will run each hour , even if no user access the application... ? – John John Dec 17 '15 at 17:42
  • I don't understand why you're asking these obvious questions. You seem to know that an app pool shuts down (and thus Hangfire) if no requests are made to the site. So clearly if you have something making requests to the site, it won't shut down, right? – mason Dec 17 '15 at 17:44
  • @mason my question is how to keep the application pool alive ,,so is calling the application from the windows scheduler an approach to achieve this ? second question. as i know that hangfire will be able to handle long running processes, because if the application pool are about to shut down during the long running execution , then hang fire will prevent the application pool from shutting down until the long running job ends .. is this correct ? if this is correct, then is this achieved by defualt , or i need to configure hangfire to do so ? – John John Dec 17 '15 at 17:58
  • 1
    An application pool shuts down if it receives no requests. If you have something making a request to it, then it won't shut down due to idleness. And no, Hangfire can't force it to stay open. At some point, if the application pool is going down, then it *will* kill your jobs. Which is why Hangfire documentation encourages you to make your jobs [re-entrant](http://docs.hangfire.io/en/latest/best-practices.html#make-your-background-methods-reentrant). – mason Dec 17 '15 at 18:02
  • @mason now let say my sync job will take 30 minutes to complete, now what can cause the application pool to shutdown , if we exclude a server fault or network issue ? i mean could the application pool goes down during the method execution , even if no problem happens ? – John John Dec 17 '15 at 18:12
  • 1
    Sure, you could manually take it down. Or a thread that's not associated with an HTTP request/response could have an unhandled exception causing everything to go down. But it doesn't matter. If I were you, I would stop asking endless questions and instead implement what I'm talking about and *see* how it works. *Test* it yourself. – mason Dec 17 '15 at 18:15
  • @mason let me re-construct my question. now let say i have a long running sync job which might take 30-40 minutes to complete.. so is using hangfire to run this long-running process on timely basis an approach to follow ? i mean is hangfire designed out-of the box to handle such a long running processes ? – John John Dec 17 '15 at 18:19
  • You need to *stop* asking here every time you have a question. *Think* and *research*. **Test**. – mason Dec 17 '15 at 18:21

1 Answers1

3

MVC is all about user interaction, and many users interacting with the same application at that. If all you need is one task to be run on a schedule, then you don't even need a UI for that, and there's no issues of needing to handle multiple simultaneous remote requests or such that a web application satisfies. You could literally just create a console app and use Windows Task Scheduler. MVC is complete overkill, and more than that, unsuited to the purpose.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • you are correct if i will never add more features to the application.. but in our case i want to have this inside an asp.net mvc web application, since we might add extra functions beyond the sync job ,, so is calling the action method using Windows Task Scheduler an approach to follow ? – John John Dec 17 '15 at 18:07
  • 1
    It's a mistake to go with some tool because you *may* use functionality in the future. If you need to add web-type stuff later, you can create an MVC project *then* and migrate this functionality over to that project. However, there's an argument to be made that this functionality is unsuitable entirely for a web project. Use MVC for the web stuff and have this as a console app. Nothing says you have to have just one thing. The two things can share code via a class library if there's crossover. – Chris Pratt Dec 17 '15 at 18:13
  • so you mean it is better to write a console application and call this console application from a windows scheduler ? is this what you mean ? second question, now i have never write a console application before , so not sure where it will be deployed ? or it will be running as a .exe file ? – John John Dec 17 '15 at 18:23
  • 1
    Yes, for this particular task, a console app is most suited. Console apps are pretty straight-forward, especially if they're self-sufficient and don't require user input (meaning you don't have to worry about handling console flags and such). You don't even need to publish them. Just build with the Release configuration and then take the files from the bin directory. You will get a folder with an exe and any supporting DLLs. Just deploy this wherever you like and create a task to run it. – Chris Pratt Dec 17 '15 at 18:35
  • ok thnaks.In my case i might also need users to run the sync job manually. let say a user want to sync the result, and he do not want to wait for the schedule, so i will add a sync button inside a web page.. so in this case i need the sync to be web enabled is this correct ?i am a bit confused if i need to have the sync as an action method inside my mvc or as a console app. as i need it to run manually and/or based on a schedule.second point i found many tools such as Hangfire which are designed to run background tasks under the application pools, so when i should use these tools ? – John John Dec 17 '15 at 22:40