Skip to content

Commit

Permalink
moved azure hack under env variable for a better dev UX (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer authored Aug 15, 2023
1 parent 0216ee1 commit 9fb290a
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/Grinder/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,32 @@ module Program =
sprintf "Error on upserting new users %A" users
|> logExn e
)
}
}

let azureLoop(botAsync: Async<Unit>) =
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<Unit>) =
logInfo "Bot started"
botAsync
|> Async.RunSynchronously

let onUpdate (settings: BotSettings) (botApi: IBotApi) (dataApi: IDataAccessApi) (context: UpdateContext) =
async {
Expand Down Expand Up @@ -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
0 // return an integer exit code

0 comments on commit 9fb290a

Please sign in to comment.