diff --git a/src/Grinder/Program.fs b/src/Grinder/Program.fs index 6e1154e..37c2756 100644 --- a/src/Grinder/Program.fs +++ b/src/Grinder/Program.fs @@ -140,7 +140,32 @@ module Program = sprintf "Error on upserting new users %A" users |> logExn e ) - } + } + + let azureLoop(botAsync: Async) = + botAsync + |> Async.Start + + logInfo "Bot started" + + // Needed for azure web app deploy check. We have to response with anything on port 80 + use listener = new HttpListener() + listener.Prefixes.Add("http://*:80/") + listener.Start() + + let buffer = System.Text.Encoding.UTF8.GetBytes "OK" + + while true do + let ctx = listener.GetContext() + let output = ctx.Response.OutputStream + output.Write(buffer, 0, buffer.Length) + output.Close() + logInfo "Sending OK on HTTP request" + + let localLoop(botAsync: Async) = + logInfo "Bot started" + botAsync + |> Async.RunSynchronously let onUpdate (settings: BotSettings) (botApi: IBotApi) (dataApi: IDataAccessApi) (context: UpdateContext) = async { @@ -272,24 +297,14 @@ module Program = |> logInfo logInfo "Starting bot" - startBot botConfiguration (onUpdate settings (createBotApi botConfiguration settings) dataApi) None - |> Async.Start + let bot = startBot botConfiguration (onUpdate settings (createBotApi botConfiguration settings) dataApi) None - logInfo "Bot started" - - // Needed for azure web app deploy check. We have to response with anything on port 80 - use listener = new HttpListener() - listener.Prefixes.Add("http://*:80/") - listener.Start() - - let buffer = System.Text.Encoding.UTF8.GetBytes "OK" - - while true do - let ctx = listener.GetContext() - let output = ctx.Response.OutputStream - output.Write(buffer, 0, buffer.Length) - output.Close() - logInfo "Sending OK on HTTP request" + match Environment.GetEnvironmentVariable("DOTNETRU_IS_AZURE") with + | null -> localLoop bot + | isAzureStr -> + match bool.TryParse isAzureStr with + | true, true -> azureLoop bot + | _ -> localLoop bot logInfo "Bot exited" - 0 // return an integer exit code \ No newline at end of file + 0 // return an integer exit code