Skip to content

Commit

Permalink
added serilog and logging (Liminiens#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Ayrat Hudaygulov <ahudaygulov@microsoft.com>
  • Loading branch information
Szer and Ayrat Hudaygulov authored Dec 10, 2020
1 parent 0ddfd3d commit 21fb856
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 61 deletions.
10 changes: 10 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<PropertyGroup>
<TargetFramework>net5</TargetFramework>
<ServerGarbageCollection>false</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ COPY src/Grinder.Common/Grinder.Common.fsproj ./src/Grinder.Common/Grinder.Commo
COPY src/Grinder.DataAccess/Grinder.DataAccess.csproj ./src/Grinder.DataAccess/Grinder.DataAccess.csproj
COPY tests/Grinder.Tests/Grinder.Tests.fsproj ./tests/Grinder.Tests/Grinder.Tests.fsproj
COPY src/Grinder.ExportTool/Grinder.ExportTool.fsproj ./src/Grinder.ExportTool/Grinder.ExportTool.fsproj
COPY Directory.Build.props ./Directory.Build.props
RUN dotnet tool restore
RUN dotnet restore -r linux-x64
# Then build app
Expand Down
8 changes: 1 addition & 7 deletions src/Grinder.Common/Grinder.Common.fsproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Include="Library.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="5.0.0" />
<PackageReference Include="FSharp.Core" Version="5.0.0" />
<PackageReference Include="TaskBuilder.fs" Version="2.1.0" />
</ItemGroup>

Expand Down
5 changes: 1 addition & 4 deletions src/Grinder.ExportTool/Grinder.ExportTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,7 +10,7 @@

<ItemGroup>
<PackageReference Include="FSharp.Control.AsyncSeq" Version="2.0.21" />
<PackageReference Update="FSharp.Core" Version="5.0.0" />
<PackageReference Include="FSharp.Core" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="TDLib" Version="1.3.0" />
<PackageReference Include="tdlib.native" Version="1.3.0" />
Expand Down
8 changes: 4 additions & 4 deletions src/Grinder.ExportTool/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ let updateAuthorizationState (dialer: Dialer) (authLock: AutoResetEvent) (state:
|> Task.Ignore

| :? TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber ->
printfn "Enter phone"
Console.WriteLine "Enter phone"
let phone = Console.ReadLine()
do! dialer.ExecuteAsync(new TdApi.SetAuthenticationPhoneNumber(PhoneNumber = phone))
|> Task.Ignore

| :? TdApi.AuthorizationState.AuthorizationStateWaitCode ->
printfn "Enter code"
Console.WriteLine "Enter code"
let code = Console.ReadLine()
do! dialer.ExecuteAsync(new TdApi.CheckAuthenticationCode(Code = code))
|> Task.Ignore
Expand Down Expand Up @@ -143,7 +143,7 @@ let main argv =
dialer.ExecuteAsync(new TdApi.GetChats(Limit = 200, OffsetOrder = Int64.MaxValue))
|> Async.AwaitTask

printfn "Starting export of usernames"
Console.WriteLine "Starting export of usernames"
let! users =
chats.ChatIds
|> AsyncSeq.ofSeq
Expand Down Expand Up @@ -175,6 +175,6 @@ let main argv =
|> JsonConvert.SerializeObject
|> stream.Write

printfn "Finished export of usernames"
Console.WriteLine "Finished export of usernames"
} |> Async.RunSynchronously
0 // return an integer exit code
62 changes: 51 additions & 11 deletions src/Grinder/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ module Processing =
botSettings.AllowedUsers.Set
|> Set.contains username
|> not

let logErrors =
Seq.iter (function
| ApiError str
| AdminBanNotAllowedError str -> logErr str
)

match command with
| BanCommand context ->
Expand All @@ -438,8 +444,12 @@ module Processing =
if userCanBeBanned user then
for chat in botSettings.ChatsToMonitor.Set do
yield botApi.BanUserByUsername chat user context.Until.Value
|> Async.Map ^ fun result ->
Result.mapError ApiError result
|> Async.Map ^ Result.mapError
(sprintf "Error on baning user %A in chat %A until %A. %s"
user
chat
context.Until.Value
>> ApiError)
else
yield sprintf "Cannot ban admin @%s" %user
|> createCommandError AdminBanNotAllowedError
Expand All @@ -449,6 +459,8 @@ module Processing =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Expand Down Expand Up @@ -476,9 +488,14 @@ module Processing =
let requests =
if userCanBeBanned username then
[for chat in botSettings.ChatsToMonitor.Set do
yield botApi.BanUserByUserId chat context.UserId (DateTime.UtcNow.AddMonths(13))
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
let until = DateTime.UtcNow.AddMonths(13)
yield botApi.BanUserByUserId chat context.UserId until
|> Async.Map ^ Result.mapError
(sprintf "Error on baning userId %A in chat %A until %A. %s"
context.UserId
chat
until
>> ApiError)]
else
[sprintf "Cannot ban admin %s" %username
|> createCommandError AdminBanNotAllowedError
Expand All @@ -488,6 +505,8 @@ module Processing =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Expand All @@ -503,16 +522,24 @@ module Processing =
[for user in context.Usernames do
for chat in botSettings.ChatsToMonitor.Set do
yield botApi.UnbanUser chat user
|> Async.Map ^ fun result ->
Result.mapError ApiError result
|> Async.Map ^ Result.mapError
(sprintf "Error on unbaning user %A in chat %A. %s"
user
chat
>> ApiError)
yield botApi.UnrestrictUser chat user
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
|> Async.Map ^ Result.mapError
(sprintf "Error on unrestricting user %A in chat %A. %s"
user
chat
>> ApiError)]
let! errors =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Usernames = context.Usernames
Expand All @@ -537,24 +564,32 @@ module Processing =
let requests =
[for chat in botSettings.ChatsToMonitor.Set do
yield botApi.UnbanUser chat username
|> Async.Map ^ fun result ->
Result.mapError ApiError result]
|> Async.Map ^ Result.mapError
(sprintf "Error on unban user %A in chat %A. %s"
username
chat
>> ApiError)]

let! errors =
requests
|> Async.Parallel
|> Async.Map getErrors

logErrors errors

let message = {
Chats = botSettings.ChatsToMonitor.Set
Usernames = Set.empty.Add username
}

return Some <| UnbanOnReplyMessage(context.From, message, errors)
| PingCommand context ->
sprintf "Sending PONG to %A" context.ChatId
|> logInfo
do! botApi.SendTextMessage context.ChatId "pong"
return None
| DoNothingCommand ->
logDbg "Do Nothing Command has been successfully processed and we haven't done anything remotely useful"
return None
}

Expand All @@ -573,7 +608,12 @@ module Processing =
do! Datastore.upsertUsers users
do! "Updated user database"
|> ApiExt.sendMessage botSettings.ChannelId config
sprintf "Successfully processed admin command for fileId %s" fileId
|> logInfo

| Error e ->
sprintf "Error on processing admin command with fileId: %s. Error: %s" fileId e
|> logErr
do! ApiExt.sendMessage botSettings.ChannelId config e
}

Expand Down
14 changes: 9 additions & 5 deletions src/Grinder/Datastore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ type FindUsernameByUserIdResult =
module Datastore =
let upsertUsers (users: User seq) =
task {
for chunk in Seq.chunkBySize 500 users do
use context = new GrinderContext()
do! context.Users.AddOrUpdateUsers(chunk)
do! context.SaveChangesAsync() |> Task.Ignore
try
for chunk in Seq.chunkBySize 500 users do
use context = new GrinderContext()
do! context.Users.AddOrUpdateUsers(chunk)
do! context.SaveChangesAsync() |> Task.Ignore
with e ->
sprintf "Error on upserting new users %A" users
|> logExn e
}
|> Async.AwaitTask

Expand Down Expand Up @@ -52,4 +56,4 @@ open Grinder.Types

type IDataAccessApi =
abstract member GetUsernameByUserId: TelegramUserId -> Async<UserUsername option>
abstract member UpsertUsers: User seq -> Async<unit>
abstract member UpsertUsers: User seq -> Async<unit>
10 changes: 4 additions & 6 deletions src/Grinder/Grinder.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
<ServerGarbageCollection>false</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FParsec" Version="1.0.3" />
<PackageReference Update="FSharp.Core" Version="5.0.0" />
<PackageReference Include="FSharp.Core" Version="5.0.0" />
<PackageReference Include="FSharp.UMX" Version="1.0.0-preview-001" />
<PackageReference Include="Funogram" Version="1.3.1" />
<PackageReference Include="HttpToSocks5Proxy" Version="1.1.3" />
Expand All @@ -22,9 +17,12 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<Compile Include="Log.fs" />
<Compile Include="Types.fs" />
<Compile Include="Common.fs" />
<Compile Include="Datastore.fs" />
Expand Down
12 changes: 12 additions & 0 deletions src/Grinder/Log.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Grinder

open Serilog

[<AutoOpen>]
module Log =
let mutable logger = Unchecked.defaultof<ILogger>
let logInfo (str: string) = logger.Information str
let logExn (exn: exn) (msg: string) = logger.Error(exn, msg)
let logErr (msg: string) = logger.Error msg
let logDbg (str: string) = logger.Debug str
let logFatal (msg: string) (exn: exn) = logger.Fatal(exn, msg)
Loading

0 comments on commit 21fb856

Please sign in to comment.