How to run scheduled scripts using Azure WebJobs - azure

I want to run a scheduled script using WebJob.
Due to source control enabled, I could not create WebJob using portal & had to copy files using Kudu (Diagnostic) Console. These are the files under triggered jobs in the folder:
The run.js file simply calls an API. The settings.job contains the schedule I want it to run at:
{
"schedule": "0 /1 * * * *"
}
But the job does not start on its own or shows up in list of WebJobs:
Is there something else that needs to be done to publish or trigger a WebJob or something more that I need to add to settings.job file?
I have been referring to this

Follow my steps, you can solve the problems you can't add webjob on portal.
Due to source control enabled, I could not create WebJob using portal & had to copy files using Kudu (Diagnostic) Console.
Step 1. Add a webjob arbitrarily on the portal.(upload .zip file or create by kudu)
Step 2. Deploy by VS2019(source control)
Step 3. After success, you will find you can add webjob by upload .zip file.
Now, I will upload nodejs webjob.
Please note that you need to create a continuous or Triggered webjob. The two are different. In the portal, the schedule of the continuous webjob is displayed as n/a, while the Triggered display format is such as: 0 */15 * * * *.
run program in my local pc and it works fine.
Compress it and upload in portal.
check running logs and it works fine, and result is same as local.
Finally ,let us check the content in .zip file.

Related

Azure web app - syncing files from external process

This question feels so simple that I'm confused why I can't find an answer!
We have an Azure web app, typically running on 2 to 5 instances.
At the moment we manually run a quite intensive PHP script a few times a day on a local computer to generate a folder of files. (The resulting folder isn't huge - typically about 10MB in size and a few hundred files in total.) We then sync them via Github and they deploy to the website. Easy.
That process is fine, but we want to move the PHP script to Azure so we can remove the dependency on running it locally and instead run it as a chron job.
How can we reliably sync the outputted folder from our script into our web app?
One option is to use a triggered WebJob with a cron schedule. Your WebJob can contain just your PHP script. Or if it needs a special command line to run, include a run.cmd batch file with the full PHP command line.
In your PHP script, do whatever you need to gather the right set of files, and then just copy them to %home%\site\wwwroot\json-data.
For this to work, everything you need to do within your PHP script needs to be runnable within the App Service sandbox. You should first try this directly from Kudu Console before moving it to a WebJob, to make sure everything can run.
Set your web app deployment source to Local Git. After that push your code to WebApp by Git Push . I am not sure about PHP build process but when you push your code to AzureWeb app via LocalGit method it builds and restores all dependencies and deploy it . For custom build script you can refer https://github.com/projectkudu/kudu/wiki/Custom-Deployment-Script.
https://medium.com/#trstringer/custom-build-logic-post-git-push-with-azure-app-service-and-kudu-for-a-node-js-web-app-1b2719598916

Azure webjobs deployment and working directory

I'm confused about deployment webjobs to azure.
I'm using .net core, so I manaully publish my webjobs in my deploy.cmd file, for example like this:
call :ExecuteCmd dotnet publish "%DEPLOYMENT_SOURCE%\My.WebJobs\Mt.WebJobs.csproj" --output "%DEPLOYMENT_TEMP%\App_Data\Jobs\Continuous\MyWebJobs" --configuration Release
That then deploys the webjob to the deployment_temp folder. After that, KuduSync kicks in and syncs to DEPLOYMENT_TARGET which is d:\home\site\wwwroot I can see in there if I look, that there is a App_Data\Jobs\Continuous\MyWebJobs folder, and that ALL my files have been correctly deployed and sync-ed up to that folder.
However, when I run the webjob, it reports it's running from a totally different location (D:\local\Temp\jobs\continuous\MyCustomerIO\bp003r3f.h2g). And when I look in that folder, I see most of my deployed webjob files, but some JSON configuration files that are in App_Data... are missing from here.
So - why is my webjob running from here? And why are some of my deployed files missing?
why is my webjob running from here?
The WebJob is copied to a temporary directory under %TEMP%\jobs\{job type}\{job name}\{random name} and will run from there. This option prevents the original WebJob binaries from being locked which might cause issues redeploying the WebJob.
The WebJob is run directly from the WebJob binaries directory. We call this option in place. This option can have the locking issue and should only be used when there is no risk of locking the file.
By default the first option is used (in place = false).You can explicitly configure this setting in your settings.job. e.g.
{ "is_in_place": true/false }
And why are some of my deployed files missing?
I tested and reproduced this situation too. But I did not find solution to solve this. I suggest that you could use Environment.GetEnvironmentVariable("WEBJOBS_ROOT_PATH"); to get the root path
direcroty of webjob.
WEBJOBS_ROOT_PATH which is location of webjob files, you can specify an absolute path, or otherwise the value will be combined with the default root path:
D:/home/site/wwwroot/ + WEBJOBS_ROOT_PATH(relative) to get webjob_name, webjob_run_id and so on.
For more detail, you could read this article.

Azure Web Job Missing from the list

I am currently facing one issue with my Azure Webjobs. I have created custom build tasks in TFS that build my solution and copy the files of 5 different console projects to app_data/jobs/triggered/MyProjectName then zip them into 1 file and deploy into my staging slot of my Azure Web App. When I navigate to the slot I can see 4 out of 5 jobs. When I log in to the FTP location I can see all 5 jobs. What is the possible issue, is there a limit on the number of jobs ? I do not think it is the webjob name as I have one with longer name and it is showing fine. The name format is following xxxx.webjobs.projectname no numbers, no special characters. I have tried to rename the webjob did not help.
It was a silly mistake that I made, when I publish to azure I was publishing both Release and Debug folder. The Release folder was my leftover from my previous test with no files. I have removed the Release folder and it all started working. Perhaps, if Azure by default will use Release folder and does not find anything in that folder and if there is Debug folder it should go to debug and leave a note in logs that Debug is currently being used? I hope that helps!

Azure Webjobs ignores CRON expression

I have a program that I want to run once per day. I put my program.exe and my settings.job in one zip file and uploaded it. I sat the running mode to continuous. My settings.job looks like:
{
"schedule": "0 0 8 * * *"
}
My plan was that it runs every day at 8 but instead it runs repeated all the time over and over again.
What did I do wrong?
You webjob running mode should be On Demand:
Create a scheduled WebJob using a CRON expression
From the documentation :
You still need Always On setting to be enabled on the app.
Note: when deploying a WebJob from Visual Studio, make sure to mark your settings.job file properties as 'Copy if newer'.
After days of trying unsuccessfully to make the elusive Azure WebJob run on a CRON expression following the top online tutorials:
https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateScheduledCRON
http://blog.amitapple.com/post/2015/06/scheduling-azure-webjobs/#.V5irdlcdpw8
http://www.samulihaverinen.com/web-development/dotnet/2016/02/24/guide-to-azure-webjobs/
I finally managed to make my jobs run on cron schedules.
For me the settings.json in the root folder never worked. What did work and was actually extremelly straightforward to implement was to use the Azure Webjobs SDK Extensions.
This approach gives you the most flexibility in implementing the scheduling, it is very well documented and there are complete sample projects for it: https://github.com/Azure/azure-webjobs-sdk-extensions/blob/master/src/ExtensionsSample/Samples/TimerSamples.cs
With a function definition as simple as this you can be up and running with cron scheduling:
public static void CronJob([TimerTrigger("0 */5 * * * *")] TimerInfo timer)
{
Console.WriteLine("Cron job fired!");
}
The Webjobs extensions also open up a whole world of other possibilities so they are 100% worth checking out if you are using Azure Webjobs: https://github.com/Azure/azure-webjobs-sdk-extensions

Change webjob from on demand to schedule

We have an azure webjob that was deployed as on demand. Is there a way to change this to run on a schedule without redeploying?
Not a lot of info out there on this.
I tried creating a new schedule collection like this and adding a job to run the existing webjob, but that didn't seem to work either.
I prefer to do this in the GUI portal, but if its not possible, I'll do it in powershell (if it is possible like that).
(Also, if it can only be changed by redeploying, I need to know that and it effectively answers the question)
To easily add a schedule to your triggered (on demand) webjob add a file called settings.job at the root of your webjob with this content:
{"schedule": "the schedule as a cron expression"}
Find out more about this here
Note: it'll only work properly for Standard or Premium sites and requires you to set the site as always on.
In the end the link in the original post I referenced worked. The thing that was missing for me was the understanding that creating a trigger job in scheduler will not affect the run status (on-demand or scheduled) of the web job itself. In my case it stayed "on-demand", but the schedule was in fact running it.
This should point you in the direction on how to do this via PowerShell. It looks possible to add already existing WebJobs to a scheduler.
Create a Scheduled Azure WebJob with PowerShell

Resources