Slide 1

Slide 1 text

Working with WebJobs TIM VANFOSSON @TVANFOSSON

Slide 2

Slide 2 text

About Me Principal Software Engineer @The_Nerdery “Full Stack” – C#/.NET, MVC, WebApi, JavaScript, MSSQL Twitter: @tvanfosson Web: http://farm-fresh-code.blogspot.com

Slide 3

Slide 3 text

What are WebJobs Programs/scripts Execute within a Web App in 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

Slide 4

Slide 4 text

Advantages Reduced infrastructure to maintain Can deploy in/with your web site Easy integration with message queues, topics Managed through Azure portal Scalability Logging is baked in

Slide 5

Slide 5 text

Disadvantages Logging is baked in ◦ Duplication if you want 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

Slide 6

Slide 6 text

Creating and Deploying WebJobs Azure Portal ◦ Caveat: Jobs deployed 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

Slide 7

Slide 7 text

Azure Blob / Table Blob ◦ Use appropriate trigger [BlobTrigger(“path”)] ◦ 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 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/

Slide 8

Slide 8 text

Azure Queue Built on Azure storage Simple REST-based interface Can 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

Slide 9

Slide 9 text

Service Bus Queues/Topics Messaging Infrastructure ◦ Namespace ◦ Hosts both 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

Slide 10

Slide 10 text

WebJobs and Service Bus Generally receiving messages Associated with a 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

Slide 11

Slide 11 text

Create A Service Bus Web Job Add -> New Azure 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();

Slide 12

Slide 12 text

Applications Sending transactional emails Updating dashboards Image processing Long-running tasks

Slide 13

Slide 13 text

Triggered Tasks Scheduled ◦ Azure Scheduler ◦ Schedule jobs and 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”

Slide 14

Slide 14 text

Create A Triggered Web Job Add -> New Azure WebJob 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); }

Slide 15

Slide 15 text

Integrate WebHooks (Send) Set up your WebApp to allow WebHooks 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

Slide 16

Slide 16 text

Applications Dashboard roll ups Billing Scheduled notifications ◦ Periodic emails ◦ 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

Slide 17

Slide 17 text

Azure WebJobs Dashboard Azure Portal ◦ Web App -> Settings -> WebJobs -> -> Logs (click functions) Direct ◦ https://.scm.azurewebsites.net/azurejobs Dashboard shows statistics Drill down to see specific invocations, errors, log messages

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

A Word About Kudu Access through the Azure Portal ◦ WebApp -> Tools -> Kudu -> Go Access directly ◦ https://.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

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

What I Didn’t Talk About WebJobs RESTful API TimerTrigger Channel 9 Video WebJob Video series Examples on GitHub F# - or other platforms Testing/Replay Tracing

Slide 22

Slide 22 text

What’s Next Azure Functions ◦ https://azure.microsoft.com/en-us/documentation/articles/functions-overview/ ◦ Triggered Only ◦ Blob, EventHub, WebHooks, Http Request, Queue, Service Bus Queue/Topic, Timer ◦ C# Scripts/NodeJS ◦ Small, pay only for what you use

Slide 23

Slide 23 text

Resources (not exhaustive  ) https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/ https://azure.microsoft.com/en-us/documentation/articles/websites-webjobs-resources/ https://github.com/azure/azure-webjobs-sdk-samples https://www.troyhunt.com/azure-webjobs-are-awesome-and-you/ https://azure.microsoft.com/en-us/documentation/articles/scheduler-intro/ https://blogs.msdn.microsoft.com/webdev/2015/09/15/sending-webhooks-with-asp-net-webhooks- preview/ https://blogs.msdn.microsoft.com/webdev/2016/01/30/sending-asp-net-webhooks-from-azure- webjobs/ https://blogs.msdn.microsoft.com/webdev/2014/11/12/new-developer-and-debugging-features-for- azure-webjobs-in-visual-studio/ https://github.com/projectkudu/kudu/wiki