Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Working with WebJobs ICC17 2016

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Working with WebJobs ICC17 2016

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

Avatar for Tim VanFosson

Tim VanFosson

July 23, 2016
Tweet

More Decks by Tim VanFosson

Other Decks in Technology

Transcript

  1. About Me Principal Software Engineer @The_Nerdery “Full Stack” – C#/.NET,

    MVC, WebApi, JavaScript, MSSQL Twitter: @tvanfosson Web: http://farm-fresh-code.blogspot.com
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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<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/
  7. 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
  8. 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
  9. 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
  10. 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();
  11. 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”
  12. 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); }
  13. 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
  14. 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
  15. Azure WebJobs Dashboard Azure Portal ◦ Web App -> Settings

    -> 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
  16. A Word About Kudu Access through the Azure Portal ◦

    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
  17. 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
  18. 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
  19. 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