Skip to content

Instantly share code, notes, and snippets.

@artur-s
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save artur-s/aad83214c148fcd2efc0 to your computer and use it in GitHub Desktop.

Select an option

Save artur-s/aad83214c148fcd2efc0 to your computer and use it in GitHub Desktop.
Allows to handle an event that fires when a WebJob process is about to close
module WebJobShutdownWatcher
open System
open System.IO
/// Allows to handle an event that fires when a WebJob process is about to close.
/// Based on a blog-post: http://blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown
let subscribe handler =
let shutdownFile = Environment.GetEnvironmentVariable "WEBJOBS_SHUTDOWN_FILE"
if shutdownFile <> null then
let watcher = new FileSystemWatcher(path = (shutdownFile |> Path.GetDirectoryName),
NotifyFilter = (NotifyFilters.CreationTime ||| NotifyFilters.FileName ||| NotifyFilters.LastWrite),
IncludeSubdirectories = false,
EnableRaisingEvents = true)
Event.merge watcher.Created watcher.Changed
|> Event.filter (fun e -> e.FullPath.IndexOf(shutdownFile, StringComparison.InvariantCultureIgnoreCase) >= 0)
|> Event.add (handler >> watcher.Dispose)
()
// usage
// WebJobShutdownWatcher.subscribe (fun e -> printfn "%s" e.FullPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment