Last active
August 29, 2015 14:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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