Is Azure Scheduled WebJob started if previous one is still running? - azure

I have a Scheduled Azure WebJob which runs every 5 mins. It's not clear what happens if the running times takes 10 mins. Is a new one started parallel to the one still running, or is it not started until the previous one has finished?

From this answer What happens when a scheduled WebJob runs for a long time :
As i understand it scheduled webjobs is just triggered webjobs that is run using Azure Scheduler, if you open Azure Scheduler in management portal you can see the webjobs and even configure them in more detail. (You can see the log too which would give you the simple answer to your question).
If you like to look at whats going on your scheduled webjob is run as a Triggered webjob by Kudu, if you look in the Kudu source you will see that a lockfile is created when a job is started, and if you try to start another job a ConflictException is thrown if there is already a lock file.
The Azure scheduler calls your job using a webhook that catches the ConflictException and gives you the "Error_WebJobAlreadyRunning" warning which will tell you: "Cannot start a new run since job is already running."

Related

Have Azure Webjob Function run once on startup

Is there a specific way to have Azure WebJobs trigger a function once on startup without using external messages/triggers? I know there is the RunOnStartup annotation that can be added to the TimerTrigger trigger but that still requires a time interval to run the function on. In my case, I am simply looking to run the function once on the startup.
WebJobs have two types: Continuous and Triggered.
Here is a table describes the differences between continuous and triggered WebJobs.
If you just want your task run once, you could set it triggered manually. Follow this turorial: Create a manually triggered WebJob
Thanks for asking question! Azure WebJob is known for its ‘Event-driven background processing’ method. Further, WebJobs is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app. To elaborate on this there are continuous and triggered types of Web Jobs.
The continuous type; Starts immediately when the WebJob is created. To keep the job from ending, the program or script typically does its work inside an endless loop. If the job does end, you can restart it. But triggered type Starts only when triggered manually or on a schedule.
Check this link on creating scheduled web job: https://learn.microsoft.com/en-us/azure/app-service/webjobs-create#CreateScheduledCRON

Azure Webjob Fails in Portal But Runs Fine

I have an Azure webjob created using the SDK that runs hourly. The job runs, and works fine, but when I look at the job in the portal it always shows Failed. I can run the job from the Debug Console and everything appears fine. When run from the console the job typically takes seconds to run, but when run on the schedule it usually shows 12-20 minutes, before it fails.
How can I get more details as to why this is failing? Do I need to be telling webjobs somehow the task is finished and it's waiting on me?
Thanks,
Russ
Webjob Failure
This error happens if the job uses TimerTrigger.
If the job is long-running use WEBJOBS_IDLE_TIMEOUT and SCM_COMMAND_IDLE_TIMEOUT in Azure app settings instead of web.config.
If the job is not long-running, it should have scheduled timers less than 2 minutes, which will probably work well for testing only.
Finally, the ultimate solution is to use Basic or Standard offering of AppPlan.
In that case you can ENABLE Always On to keep the container loaded all the time.
However, WEBJOBS_IDLE_TIMEOUT and SCM_COMMAND_IDLE_TIMEOUT must also be set as described above. Continuous WebJobs or of WebJobs triggered using a CRON (TimerTrigger) expression without Always On, will not run reliably.
For more details, you could refer to this article.

Does Azure start up another instance of a scheduled web job if it is already running?

I have a Azure web job that is scheduled to run every 5 minutes using the cron expression in the settings.job file. If the process doesn't finish within 5 minutes will Azure kick off another instance of the job or will it wait until the first one finishes?
I would like to make sure it waits until the first one finishes so it isn't running multiple instances.
When a scheduled webjob is started, Azure places a lock file. This lock file will remain until the scheduled webjob is completed. If there's an attempt to fire up another scheduled job, you'll get a ConflictException.
You can read about the lock file here. And the code that checks to see if another job is already running is here.

Azure Web Job - Scale out kills existing web job

I'm looking to use the API to change the number of web job instances I have running based on the size of a processing Q, I know I can setup rules in the portal but the minimum aggregation time is 60 minutes and I don't want to have the system waiting 60 minutes before scaling up if we suddenly get a burst of work.
The issue I have is that currently if I scale out in the portal manually from say 1 to 5 instances it kills the single running instance and then starts 5 new ones.
I assume if I did this through the API the same thing would happen, do you know if there is any way to avoid this?
Thanks
Si
UPDATE:
See below, I submitted 4 jobs and then as the first was processing I scaled out from 1 to 3 instances and this is what happened, the job that Never Finished then reran after the next 3 had finished as it's message would have popped back on the queue because it's processing failed initially.
if I scale out in the portal manually from say 1 to 5 instances it kills the single running instance and then starts 5 new ones.
As my test, if you scale your webjob, it will not kill the single running instance. I created a Webjob template and write a timer trigger in it.
Here is the time I scaled my web app:
Here is the trigger log in Azure storage ('azure-jobs-host-output'):
If you find your webjob is running in 'inactive instance' state in Azure webjob dashboard. Please do not worry about it. Your webjob is still in running. Please have a look at David's reply in this thread. Here is a snippet:
This is actually a bug in what the Portal displays. The Portal ends up asking an arbitrary instance about the WebJob status, and if it happens to hit any instance other than the one that's actually running it, it will be reported as inactive.

Execute time-consuming jobs on Azure

I have used Azure Schedulers before for quick jobs before. It targets a URL which is ASPX page or WebApi and it did the job.
Now I have a job that takes up to 15-20 minutes. Of course, I am getting timeout error after 30 seconds.
I'm trying to avoid creating a Windows Service or some console application that would run on Azure VM, rather have a non-UI application that runs in the background.
Do you have any suggestion what should I do?
You should use an Azure WebJob for this. WebJobs support simple scheduling via a cron expression (details here). Basically you upload a simple script file or exe that performs the work you want done, upload it to your WebApp along with a cron schedule expression, and Azure WebJobs will make sure it runs on schedule.
For your scenario, you'll want to create a "Continuous" WebJob and ensure you've enabled "Always On" which ensures the background job continues running (i.e. it isn't request triggered).
WebJobs sure is a good solutions, but it will share resources with its attached Web App.
You could consider using an Azure Cloud Service. I do that myself for longer running tasks, that are more CPU intensive.
Read more
For long running WebJobs, you have to tinker with the Timeout value (by default 2 minutes) or make sure your Webjob makes some Console.Writes.
To achieve that, go to the Web App Settings > Application Settings and add the following configurations:
WEBJOBS_IDLE_TIMEOUT - Time in seconds after which we'll abort a running triggered job's process if it's in idle, has no cpu time or output.
SCM_COMMAND_IDLE_TIMEOUT - Time in milisecods. By default, when your build process launches some command, it's allowed to run for up to 60 seconds without producing any output. If that is not long enough, you can make it longer, e.g. to make it 10 minutes:

Resources