Have Azure Webjob Function run once on startup - azure

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

Related

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.

Understanding how WebJobs run

I am struggling to answer this question for myself so can anyone answer it for me. How, exactly, do webjobs get run? By this I mean does the Azure framework manage access to the WebJob and are multiple instances run in separate processes?
I am under the impression that I get 12 or 16 parrallel instances by default. Is this correct? So if three messages are placed on a queue, that my webjob is triggered by, they will all run in parrallel.
AFAIK, when you scale out multiple instances, the webjobs will run on the instances in parallel in separate processes. But there are prerequisites of it,
The webjob should be continous and not Manual/Scheduled.
For this to happen correctly, you need to be running in Standard mode, and have the Always On setting enabled.
Note: If you use TimerTrigger in your webjob, it will not scale out. Refer to this article.
Behind the scenes, TimerTrigger uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time. When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken. This distrubuted lock ensures that only a single instance of your scheduled function is running at any time.
For more details about the issue, here are two similar posts for you to refer, 1 and 2.

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:

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

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."

SharePoint timer jobs not getting invoked

I have a timer job which has been deployed to a server with multiple Web front ends.
This timer job reads it's configuration from a Hierarchical Object Store.
This timer job is scheduled to run daily on the server.
But the problem is that this timer job is not getting invoked daily. I have implemented event logging in the timer job's Execute() method, but I dont see any logs being generated.
Any ideas as to what could cause a timer job to be not picked up for execution by the SharePoint Timer Service? How can I troubleshoot this problem?
Are there any "gotcha"s for running timer jobs in servers from multiple front ends? Will the timer job get execute in all the web front ends, or any one of them arbitarily? How to know which machine will have my event logs?
This might be a stupid question, but does having multiple front ends for load balancing affect the way Hierarchical Object Stores behave?
EDIT:
One of the commenters, Sean McDounough, (Thanks Sean!! ) made a very good point that:
"whether or not the timer job runs on all WFEs will be a function of the SPJobLockType enum value you specified in the constructor. Using a value of "None" means that the job will run on all WFEs."
Now, my timer job is responsible for sending periodic mails to a list of users. Currently it is marked as SPJobLockType.Job"
If I change this to SPJobLockType.None, does this mean that my timer job will be executed in all the WFEs separately? (THis is not desired, it will spam all the users with multiple emails)
Or does it mean that the timer job will execute in any one of the WFEs, arbitarily?
Try restarting the SharePoint timer service from the command-line using NET STOP SPTIMERV3 followed by a NET START SPTIMERV3. My guess is that the timer service is running with an older version of your .NET assembly. The timer service does not automatically reload assemblies when you upgrade the WSP solution.
To do this, follow these steps:
Stop the Timer service.
Click Start, point to Administrative Tools, and then click Services.
Right-click Windows SharePoint Services Timer, and then click Stop, or Restart service.
This URL helped me.

Resources