Actually I have did some research to run CRON job on AWS but did not found any good document which explain how to run c# script in AWS periodically.
I have found that we could do it with CloudWatch + Lambda, but it will not help in my case because Lambda has maximum 5 min timeout.
Then I start more research on this and found that there is a 'AWS Elastic Beanstalk' which has two options to select environment:
Web server environment
Worker environment
But here we can't create a script for .NET C# for worker environment.
So what are another options to create cron jobs on AWS?
I do this by creating a self-hosted c#/.net webapi as a windows service running on my ec2 instance.
The main loop of this webapi/windows service polls a dedicated SQS queue for 'jobs' to complete.
I then use aws cloudwatch events to put those jobs into the SQS queue at the desired interval. i.e. every hour, every day, every 5 minutes etc.
Its not as easy as just pasting a c# script someplace and telling it when to run, but it does give you a nice flexible framework for hanging lots of custom jobs that take a long time to run, and need to run on a regular basis.
Related
I'm new to Azure and I need to DoSomeWork() in Azure. This DoSomeWork() should be running periodically, every N minutes or so. However, DoSomeWork() can't be executed twice at the same time. In other words, any DoSomeWork() execution can't start before a prior DoSomeWork() execution finished.
I've been taken a look at Azure Web Jobs, particularly Continuous Azure Web Jobs. This seems the way to go but it's not clear on how to start, especially with the starting code that you get in VS:
static void Main()
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
There is also Functions class that takes an input a QueueTrigger decorated parameter, but I don't want the code to be triggered by any queue message or so.
How can I get a simple Console.WriteLine("hello world") running e.g. every minute but without overlapping? If Azure Web Jobs is not the way to go, what should I use instead (should be Azure-based)?
As mentioned in the comment, azure webjobs supports TimerTrigger feature(scheduled WebJob) which can be ran every xxx minutes as per your need.
It's quite simple when using azure webjob. For example, in visual studio, just create a console project -> Add this line of code: Console.WriteLine("hello world") -> then build the project -> then zip all the necessary files including the .exe into a .zip file -> at last, upload the .zip file into your webjob, and set the schedule(like execute the code every 5 minutes).
Please refer to this doc for more details about creating a scheduled WebJob.
You can also consider using other azure services which supports timerTrigger feature, like azure function.
WebJobs can be complicated to set up and maintain. Especially when dealing with a CI/CD pipeline. I have a few running right now and am getting ready to move them into one of the following more dependable and maintainable solutions:
The way we set up scheduled work is to use an Azure Function that runs via CRON schedule. It's super dependable and durable since it's managed by Azure. You just set it up, throw your code up and the rest is up to Azure to make sure it fires off when you configured it to.
If you want to do this in your own application, take a look at running a background service in an ASP.NET Core application. You can run a timer in the background service that will fire off and do some work. Keep in mind that if your app scales horizontally, you will be running two timers, which probably isn't good in your situation.
You could do something fancy like setting up a Azure Function to hit an endpoint on your WebAPI at a scheduled time. Then you could send the work to a BackgroundService which is singleton, so you could block a second request if you are currently running your job.
We tend to go the last route. Azure fires off a timer, the function executes, sends a message to an endpoint, the endpoint places work in the background.
There are tons of options outside of what I mentioned, but these are the only ones I have had the privilege to architect.
I have a console application that scrapes from different sites and update data in SQL Server, currently, it works on a physical server and is scheduled to run once a day.
We want to start using existing functions in Azure without the need for VM setup.
We thought about using WebJobs but we wonder if this solution can be effective for us.
(We do it without using selenium, without using any web browser)
You have some options.
Web Job
The name web job does not imply it must be a process with a frontend. So yes, it can be a console app (.exe) that is scheduled once a day. You pay for the underlying provisioned App Service Plan.
Azure Function
You can create a timer triggered azure function that will run some code. If the maximum processing time is 10 minutes you can use the pay-per-use model in which you only pay for the resources while the function is running.
If you cannot refactor it or split up into multiple functions each taking max. 10 minutes you have to provision an App Service Plan just like using a Web Job.
Azure Container Instances
If you can put your console app into a container you can use Azure Container Instances to spin up a container once a day and tear it down once the job is done. You will only pay for the resources used when the container is up and running.
My opinion
Since you have a job running once a day for approximately one hour I'd say go for Azure Container Instances as you will only pay for the time the container (job) is running.
If the process would take less then 10 minutes I would personally go for Azure Functions hosted on a consumption plan (pay-per-use model) as it is the cheapest option I know.
I'm trying to implement a logic for my .NET MVC application where I would trigger scheduled tasks in my application on a following basis:
First scheduled task to run from 00 am to 1 am in the morning
Second scheduled task to run from 1:10 am to 08:00 am in the morning
Third scheduled task to run from 8:15 am to 11:15 pm every 1 hour
This would repeat every day 365 days a year...
Which mechanism in .NET or external library could I use to implement this the easiest way with little code?
I was trying to implement it with Quartz.NET library but I didn't like it at all, I had two scheduled tasks running where one would never run at all for the scheduled time...
You don't want to be performing long running and recurring background tasks in your web application for multiple reasons. You may go through the following blog post which outlines them very well.
I would recommend you moving this logic into a separate Windows Service or even a Console Application whose runs could be scheduled with the Windows Task Scheduler at the desired intervals.
The benefit of this is that you will be off-loading your web application from doing long running tasks which will be consuming precious resources from your webserver. Of course both the webserver and the worker service could share a common centralized datastore or messaging layer in order to communicate if necessary.
For scheduling tasks in ASP.NET MVC you have several options:
3rd party libraries like FluentScheduler, HangFire or Quartz.net.
Since .Net 4.5.2 there is QueueBackgroundWorkItem, a native way to schedule work in the background. Here is a guide on how to use it with ASP.NET MVC.
Azure services for background workloads and scheduled tasks. There are a couple of those aimed at different tasks, here is an overview of them.
Be careful if you try to implement a custom solution yourself, as any exception in a thread not associated with a request would halt the entire process.
Visual Cron is a decent task scheduler you can use. We use it, calling service methods on our asp.net / mvc / webapi servers.
http://www.visualcron.com/
There are some ways that you can create a scheduled task.
1- Create a WebApi and call it by local console or windows service application in period of time.
2- Using 3rd party libraries like FluentScheduler, HangFire or Quartz.net as Andreas said. it has its own side effects though.
3- If Sql server is used in your application, you can create a procedure for your task and put it on a job in Sql server. you can even use C# for your function if CLR is enabled in Sql Server,
If I want to choose, I will choose Sql Server jobs.
I had similar problem with my ASP.NET MVC App, I Wanted to do some works in special times and finally I could find a way.
My solution was Plesk scheduels task feature, as where the most windows servers use plesk control panel You can publish your app on a windows server with plesk CP and create a new actionresualt in your MVC App and write Your codes that want to execute them in special times.
By plesk scheduels tasks create a new task and call your Url with a cron that You made it, You can make many crons.
If I Could not explain It very well You can search about Plesk Schedueled Tasks on the Internet, You can find many good answers.
I am in need of some advice on the best approach to dynamically creating some form of scheduled task at web application level. The scheduled task will be pulling information from an external API endpoint to store within a database at regular intervals until a specific end time.
Normally, I would create a Windows Service that will carry out this job for me. Unfortunately, the site administrator could create multiple tasks to query different criteria from the external API. So there could be multiple tasks running.
I have been working on implementing a background worker thread that runs on a timer. Would this be the best approach? Or would using something like Hangfire or Quartz.net be a better solution to ensure my background process is constantly running?
I don't know if it's feasible inside a web application to be ensure a task can constantly be run when required for a specific duration of time.
I tried to use Hangfire and Quartz.Net and in my opinion both of them are very good for scheduling task. Hangfire has a good interface for managing scheduled tasks, but as I encountered some problems after publishing the application on IIS, I switched to use Quartz.Net which works perfectly with Keep Alive Service For IIS 6.0/7.5. So, for using Quartz.Net you might have a look at Scheduled Tasks In ASP.NET With Quartz.Net. On the other hand, in order to make your published application to be alive after application pool recycling, IIS/Application restarting, etc. just install Keep Alive Service For IIS 6.0/7.5 on the server to which you publish your application. For detailed information regarding to these problems have a look at my answers on the pages below:
Quartz.net scheduler doesn't fire jobs/triggers once deployed
execute a schedule with quartz in visual start now with interval 24 hours
Hope this helps...
I am developing an ASP.NET application, which will be uploaded on Azure. If I have multiple instances on Azure and I want to run a cron job that will be necessary for my application. Then, I just want to confirm if that cron job will be run only one time or each instance will run that cron by itself?
For example: If I have 4 instances of cloud service on Azure and my application runs a cron job every day at 11:00 PM. So, I just want to confirm if that cron will be run only one time or each instance will run that cron on its own (i.e. cron will be run 4 times or we can say one time by each instance)?
Please suggest.
So far I've found 3 ways to do cron jobs BUT they all require some level of managing the multiple instances possibly running the tasks.
The choices I've used so far:
Windows Task Scheduler - create a startup script that adds the user and task the schedules it. More information here: Running Azure startup tasks as a real user and here: Building a Task Scheduler in Windows Azure
Using Quartz.Net - this I started with, but then moved to the windows task scheduler, but it may work for you since you can customize stuff easier. More information here: Using Quartz.net to Schedule Jobs in Windows.Azure Worker Roles
Using the new job scheduler in Mobile Services. I have not used this one, but when I read this blog: Job Scheduling in Windows Azure late last year I put it on my mental list to look at next time I need a job scheduler. It's still a little new, but it also may help you.
In your example, all 4 instances will try and run the cron job. If you would want to have only one instance run the job, you would need to implement some kind of "locking" mechanism. What normally folks do is that each instance will try and acquire a 1 minute lease on the same blob. Only one instance will be successful in acquiring the lease. You can put the logic that only the instance which is able to acquire the lease is executing that cron job.
As other users said, there are many ways to do that. I add a couple of suggestions. Have you developed a Cloude service web role or a Web site. If the former, the easiest way is to create also 1 worker role (only one) and run tasks from there. It the latter, you need an external "trigger": you can use Scheduler (by Aditi). You can get it from the Azure store (there is a free flan).
If a role (Web/Worker/VM role) has multiple instances, it means that you got the same account of Windows Server VMs (the version depends which guest OS version you configured). Each VM will run exact the same code of your application (like a ASP.NET Web Application), all requests to this role will be load-balanced and one of the VMs will sever the request based on round-robin policy.
So if your cron job only needs to be run only once, as Guarav said, you need some lock mechanism to let one of your role instances run it.