Presentation at Iowa Code Camp 17, July 2016. This presentation provides an overview of Azure WebJobs with some examples. A full demonstration can also be found at https://github.com/tvanfosson/azure-web-jobs-demo
Azure ◦ Triggered ◦ Scheduled ◦ On Demand ◦ Continuous (Message Handlers) ◦ Access to Web App Files and Azure configuration Included in the price of the Web App Supports many technologies ◦ EXE, CMD, PowerShell, Bash, PHP, Python, NodeJS, Java Single- or Multi-Instance ◦ Continuous: Single instance { “is_singleton”: true } in settings.job ◦ Triggered – always single instance on a random instance if your site is scaled out
to use different logging ◦ Extensibility through TraceWriters ◦ Aggregation – write your own connector Deploying independently => complexity On-premise VPN connectivity ◦ Site – to –Site VPN ◦ Hybrid Connection (Preview) Specific Conventions (magic?) for some things
via the portal may not survive a deployment Visual Studio ◦ Create a WebApp ◦ Create WebJob(s) ◦ Add WebJobs to WebApp ◦ Adds publishing package to solution ◦ Adds publishing settings to each web job ◦ Can be created and added at the same time ◦ Publish to Azure ◦ Through the WebApp
◦ Path must be a constant ◦ https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-blobs-how- to/ Table ◦ [NoAutomaticTrigger] – run on a schedule or manual ◦ Can bind to Table as the parameter for a job ◦ [Table(“name”)]ICollector<CollectionType> tableBinding ◦ CollectionType usually drives from TableEntity or implements ITableType ◦ Can be “shape”-based (have PartitionKey and RowKey string properties along with other table properties) ◦ Can be combined with other triggers to run based on messages ◦ https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-tables- how-to/
be used with Web Jobs - https://azure.microsoft.com/en- us/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/ See https://azure.microsoft.com/en-us/documentation/articles/service-bus-azure-and-service- bus-queues-compared-contrasted/ for more comparisons Not a feature of today’s presentation
queues and topics ◦ Topics require a Standard namespace, Basic namespaces only support queues ◦ Queue ◦ FIFO – Guaranteed ◦ Supports both At Least Once and At Most Once delivery ◦ Topic/Subscriptions ◦ Publish/subscribe model Permissions ◦ Manage/Send/Listen ◦ Applied at each level
queue or topic/subscription via attribute ◦ Queue – usually one handler (or multiple instances of a single handler) ◦ Each message handled once ◦ Topic – one (or more) listeners per subscription. ◦ Each message handled once per subscription. May need to tolerate repeat/retry messages When using ServiceBus triggers the app is loaded only once ◦ Dependencies should create disposable objects, not be disposable objects ◦ EntityFramework ◦ Cannot inject DbContext directly ◦ A single error will prevent future updates unless they are cleared until the app is recycled
WebJob Project ◦ Can also just add a Console Application using the same template if you aren’t deploying with a WebApp ◦ Retarget to latest framework (4.6) if desired/needed Manage NuGet Packages ◦ Add Microsoft.Azure.Webjobs.ServiceBus ◦ Automatically adds additional WebJobs packages, et al ◦ Install updates to freshly installed packages Create and configure JobHost (right) Modify (or create) Functions.cs ◦ Create and decorate listener method var config = new JobHostConfiguration(); config.UseServiceBus(new ServiceBusConfiguration { ConnectionString = _serviceBusConnectionString }); var host = new JobHost(config); host.RunAndBlock();
job collections across Azure/non-Azure resources, including web jobs ◦ More features, more complexity ◦ settings.job file ◦ “cron-like” schedule file per WebJob ◦ Simple but still powerful “Console Program”
Project ◦ Or a new ConsoleApplication… ◦ Retarget to latest framework (4.6) if desired/needed Create and configure JobHost (right) Modifiy (or create) Functions.cs ◦ Create and decorate NoAutomaticTrigger method(s) [Optional] Add settings.job ◦ Content/Copy Always? ◦ Create schedule var config = new JobHostConfiguration(); var host = new JobHost(config); var tasks = typeof(Functions).GetMethods() .Where(m => … ); foreach (var method in tasks) { host.Call(method); }
to be registered ◦ See resources – note the packages have been updated ◦ Skip the part about sending WebHooks, we’re using the WebJob for that Select and create a WebJob with the appropriate trigger Add BETA WebHooks Packages ◦ Add Microsoft.AspNet.WebHooks.Custom ◦ Add Microsoft.AspNet.WebHooks.Custom.AzureStorage ◦ This adds a TON of packages Add Azure storage configuration to ConnectionStrings Configure (inject/static) WebHookManager Configure triggered method
◦ Send transactional emails with retry ◦ Ex. Service bus task formats and queues email to send, scheduled task attempts delivery with retry Maintenance ◦ Backups ◦ Log rolling ◦ Clean up ◦ Credit card updates
-> WebJobs -> <choose job> -> Logs (click functions) Direct ◦ https://<your-web-app>.scm.azurewebsites.net/azurejobs Dashboard shows statistics Drill down to see specific invocations, errors, log messages
WebApp -> Tools -> Kudu -> Go Access directly ◦ https://<yourwebapp>.scm.azurewebsites.net Functions ◦ Access the REST API to see data (as JSON) ◦ See system/environment ◦ Get a console – CMD or PowerShell ◦ Oooh look, there are more environment variables – in particular WEBROOT_PATH