How to run a scheduled job on Azure? - c#

I am planning to create a service that checks something on a website with an interval. Compares the results with the DB and sends a email out if changes has been made.
No user interface is needed.
What type of service should I use for this type of application?

There's no single correct answer to your question. If you want to keep things really lightweight, you could use an Azure Function App with a timer trigger.
If the Function App model isn't right for you, you can deploy a scheduled Web Job to an App Service deployment.
For some scenarios, the best alternative is a virtual machine with cron / Windows Task Scheduler. For others, it's to write an app that comes with its own scheduler.

You can try to look at Azure Functions. That is probably the simplest thing you can do and it can run on timer. That seems quite relevant to your use case.
Azure Functions supports an event based on a
timer using Cron job syntax. For example, execute code that runs every
15 minutes and clean up a database table based on customised business
logic.

Related

How to schedule a method in .net core?

I have to give a call to Web API from that Helper method which will dump required Data to database.
I wanted to create a method which will be called automatically every
hour.
Do i need to call it from Startup CS file if yes then how ?
The intended method will suppose to call API to get the data which
will be dumped in SQL database in hourly manner.
I would highly recommend Hangfire for this. https://www.hangfire.io/
It depends on how you want to do it, and which OS you use.
If you use Linux, you can use the OS scheduler - cron. Or AnaCron.
If you you Windows, you could use Windows task scheduler.
Cron will probably work on MacOS too, though I suppose they have their own task-scheduler.
If you're using your application on Azure/AWS/DigitalOcean/AppEngine, then they all probably have facilities to do this.
There is a library to do job scheduling in .NET, it's called Quartz.
I don't know if they've ported to .NET Core yet, but I suppose they have.
Also, if you're using your application inside an ASP.NET (Core) application, I don't know how this is with IIS recycling the app-pool after 20 minutes of inactivity. If it does, then you can't do it in ASP.NET or you need to change the application's or IIS's configuration.
Note: According to this, the ASP.NET Core application definitely gets stopped when IIS recycles the app-pool, even though IIS would only have to recycle the reverse-proxy.
One of the ways we have done it in the past, is having an ashx handler in the application, which does the job once (using JWT header authentication so no DOS/DDOS is possible).
Then we just have a program running somewhere that issues a get request to the ashx handler every hour. This ensures the task is run every hour, and it lets IIS recycle the app-pool. Because that means IIS potentially needs to start then entire application, you should increase the get request's timeout accordingly. That system is simple, and it works the same everywhere, dev-machine, on-premise, self-hosted, shared-hosting, azure, aws, appengine, digitalocean, etc. and it doesn't even require any library.
Best of all, in its simplest form, you can just use wget and cron, which is really simple, and you can do rather complicated stuff with that, e.g run a program at the last day of the month (28,29,30,31) at 01:05, or like every 1st friday of the month, or on every monday-saturday from 4 to 23 o'clock, every 13 minutes starting 04:23, or whatever the hell you want.
ZERO .NET programming required at all for the job scheduling.
You could set up a Web Hook in your application that call that method and create a Logic App in Azure that fires every hour to call that web hook.
It depends on a few things, notably where your application(s) are hosted ( azure, aws, on prem, locally) and how you want to couple your scheduler to the web api service.
For example, say you are hosted in azure. You can use a timer in azure scheduled every hour to trigger and invoke your endpoint.
AWS has a similar feature, and you can invoke a lambda function.
If you are on prem, you could create a service such as hangfire that does the scheduling.
And if your really looking to run the schedule manually, you could even create a WCF service with a Timer and TimerCallback that is invoked every hour.

Is it possible to have an ASP.NET application perform a timed action? [duplicate]

Once a day, I want my ASP.NET MVC4 website, which may be running on multiple servers, to email a report to me. This seems like a pretty common thing to want to do, but I'm having a tough time coming up with a good way to do it.
Trying to run a timer on a server to do this work is problematic for a couple of reasons. If I have multiple servers then I'd have the timer running on all of them (in case a server goes down); I'd need to coordinate between them, which gets complicated. Also, trying to access the database via Entity Framework from a background thread adds the complication that I must employ a locking strategy to serialize construction/disposal of the DbContext object between the periodic background thread and the "foreground" Controller thread.
Another option would be to run a timer on the servers, but to have the timer thread perform a GET to a magic page that generates and emails the report. This solves the DbContext problem because the database accesses happen in a normal Controller action, serialized with all of the other Controller accesses to the database. But I'm still stuck with the problem of having potentially more than one timer running, so I'd need some smarts in the Controller action to ignore redundant report requests.
Any suggestions? How is this sort of thing normally done?
You should not be doing this task from your web application as Phil Haack nicely explains it in his blog post.
How is this sort of thing normally done?
You could perform this task from a Windows Service or even a console application that is scheduled to run at regular intervals using the Windows Scheduler.
The proper solution is to create a background service that runs independently of your website. However, if that is not an option there is a hack where you can use the cache as explained in Easy Background Tasks in ASP.NET by Jeff Atwood.
A few options:
If you are hosting on Azure as a Website, check out WebJobs which was released recently in preview (http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/)
If you don't want the pain of extracting out your email logic outside of the website, expose that functionality at a url (with a handler, mvc action, etc.) and then run a Windows Scheduled task that hits that url on a schedule.
Write a simple console app that is executed similarly via a Windows Scheduled task.
Or write a simple Windows Service that internally is looping and checking the time and when reached, hits that url, runs that exe, or has it's own code to send you the email.
I would recommend running Quartz.NET as a Windows Service:
Quartz.NET - Enterprise Job Scheduler for .NET Platform
There's boilerplate code for a Windows Service in the download.

Scheduling in Asp.net.What is best solution?

I want to Scheduling in Asp.net
I have following options to implement this
To write SQLServer JOB(I dont want to do this.Dont want to go outside of .Net environment)
Second option is I will write windows service and this window service will call asp.net
webservice then this webservice calls asp.net method
(I also dont need to do this because my hosting provider might not be allow me to install
window service)
Third option is I call my scheduling method in Application_Start event in global class
(Drawback is, webserver will kill thread any time )
To call Scheduling Code in Page_Load event of Home Page(Might be nobody visits my website for hours
,Also page execution might be slow due to scheduling code)
I also found some online services that calls your page at given interval,some are given below
http://www.cronservice.co.uk/new/
http://scheduler.codeeffects.com
Anybody give me bettor solution of this and also explain why it is bettor?
Thanks in Advance
The ASP.NET application isn't the right place to implement scheduling. I would suggest creating a service or a scheduled task that runs in short intervals.
You don't have many options in a shared hosting environment. My host (WinHost) allows remote access to their database, so I was able to create an executable that ran on a local server with Task Scheduler.
The performance isn't great since the database is accessed over the internet, but it's still better than attempting to run pseudo scheduled tasks with ASP.NET.
Some hosts also offer a service that will request a url within your site on a scheduled basis. However, this didn't work for me because the task I had to run took longer than the request timeout.
There is no one solution that fits all. SQL jobs and windows jobs (scheduled thru windows task scheduler) are very widely used. In one of my previous work places they had jobs that ran on multiple platforms (mainframe,windows,sql server). Failure in some of these jobs, would cost in thousands by the day. So they employed something called ESP. This software monitored jobs on all platforms and sent a message to the control room in case of a failure.
If you throw some more light on the requirement, we might be able to help you better.
ASP.NET is not the right place to house your Scheduled Tasks. I'm using Quartz.net when I have to create Scheduled Tasks.
Create a page that launches your task and place it at the URL http://www.mydomain.com/runtask.
Create a scheduled task on your home PC that sends a request to http://www.mydomain.com/runtask.
You'll need to keep your home PC on all the time.
Ideally I would go with number 1 as you get full control/history/error reporting etc. You can write an SSIS job in .NET and have SQL server schedule it.
However, I have had a similar problem with shared hosting that is very restrictive. What I did was create a page which runs the process on page load (using validation in the querystring for security). I then used a machine I have which is always on to schedule a Windows Task Scheduler (this is part of Windows as standard) to call a bit of VB script that opens the browser and then shuts it.

Building and controlling a scheduled task or service from a web application

I'm building a web application that will need to import data from other database servers when it starts.
I would like to have this import done automatically at regular intervals. I would also like to be able to start and stop the import process from my web application.
What would be the best implementation for the import agent - a Windows Service? Something else?
If your web application needs to have this data in memory, you can use the Cache class.
Set it to expire every X hours, as you need and when it expires, re-fetch the data..
You could create a Windows Service that uses Quartz.Net to run the scheduled tasks.
You should not run scheduled task from your web app, since you don't have any guarantee that your web app is running. You're at IIS app pool management's mercy.
You might want to look at Best way to run scheduled tasks.
Of what I heard this looks like a description for Microsoft Sync Framework. I have just few information about it for myself but will be pleased to see you pointed into that direction.
I'm not sure about your question because you are talking about hourly syncing. When talking web applications, there can't be a nice way to do such a task. You have to create a console app or best task would be a Windows Service Process (which are easier then it sounds)?
Sync Framework Intro
http://msdn.microsoft.com/en-us/sync/bb821992
Sync Framework Tutorial
http://alexduggleby.com/2007/12/16/sync-framework-tutorial-part-1-introduction/
Sync Framework Samples
http://archive.msdn.microsoft.com/sync
And, when I'm editing the answer with links
Nice guide to create a Windows Service (and setup)
http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx
(if first time, try it on a test project before the production project)
This might be an oversimplification, but can you create a class that does all of this work using a Timer, and then in the application_start of the global.asax, create a BackgroundWorker that kicks off this process?
Your web application could then control the BackgroundWorker object, starting/stopping as necessary.

sending email notifications for future dates in asp.net 3.5

I want to develop an Online Reminder service in ASP.NET 2.0 (C#) and SQL2005. But I am not getting the concept of reminder service. What I know is using an online reminder service I can schedule a reminder for future dates, which is sent to me (who schedule reminder) via email or SMS on that date. But in asp.net how to do this, caz anyone can schedule a reminder for any date, how we'll know that when to send that mail to the person. We have to put some loop or what.
So please guide me, what is the concept of an online reminder service and how I can easily develop this application using ASP.NET and SQL
Edited
I am on Shared hosting server, so that solution must be able to work on shared hosting.
Or
Please tell me if anyone knows about any FREE and open-source reminder service CMS which I can download and study it.
Microsoft SQL Server 2005 have scheduling (sql jobs) and email features. You may even donot need to use ASP.NET.
Ideally, you would have a windows service that would periodically (every few minutes) check if any new reminders need to be sent out. Since you are on shared hosting, you probably can't install a service though.
I'm not very familiar with windows shared hosting, but if you have the option of creating scheduled/cron job type tasks you could probably do it that way.
If you can't create a scheduled task on your server, another option would be to create a scheduled task on your home PC with a program/script that runs every few minutes and simply hits a special web page on your site. That page could then have the code that checks for reminders and sends them out. It's a bit of a hack, but it should work.
Have a look at Quartz.Net (http://quartznet.sourceforge.net/). You can create an instance of the quartz scheduler in your Application_Start event and as long as the ASP.Net application is running, it will poll the database and trigger any functions you have registered with it. Since you are on a shared host environment, this is probably your best bet unless your hosting provider has a scheduler that can trigger a WebForm (or ASP.Net MVC Controller) periodically.
First you will obviously need to create a user interface and database to store the reminders. That part you got. The next step is to create a service which periodically queries the database for reminders that are due for notification.
The best way to do this is to write a lightweight Windows Service which, as you suggest, uses a loop and a reasonable sleep time (so as not to monopolize the CPU) to continually check the database for reminders and dispatches notifications. It then processes each reminder based on your requirements.
But since you are on shared hosting, you can't deploy a Windows Service, so the next best thing is to run a background thread on Application_Start of your global.asax. There are many examples of how to do this, e.g.:
http://www.west-wind.com/WebLog/posts/67557.aspx
What are some best practices for managing background threads in IIS?
Shared hosting will not work well with what you are trying to do. You could create a background polling thread on Application start, but it will get shut down at some point and may actually be prohibited by your hosting company. An infinite loop will most likely be detected by your hoster and result in your account being automatically shut down, especially if it is using a fair bit of CPU. As John suggests, there may be a scheduled tasks or hosted cron option with your ISP, but generally, those are just for doing things like nightly backups, not really having the level of granularity you need.
Simple answer is, you most likely need something other than a hosted account. You may need to look into a VPS shared hosting service or you may wish to consider looking into MS Azure or Amazon EC2. To do this right, you need to create an application, or better, a service that runs constantly, something a shared hosting account will not provide.
There also a few services out there who can call a specific web page on your service periodically. You could use that to make the page check if there are any reminders that need to be sent.
However since you're then relying on an external site you can't control this might not be the ideal solution if it is very important that the reminders are always being sent.
1) Create a database for storing messages, with a datestamp
2) Create an SQL job, that selects all messages in a time period
3) From the SQL job, you can initialize an .net based SQL Function, that would send out the emails with the System.Net.Mail namespace.
You might consider a 'hack' using the Cache expiration in for triggering events. Create new cache keys that expire at specific Date-Times to run the reminder or make it recur at defined intervals, checking a queue to see if anything new should be sent.
See:
Easy Background Tasks in ASPNET

Categories

Resources