From 0d97e405b432be580871bba23fba36bdcffc3b71 Mon Sep 17 00:00:00 2001 From: Keroosha Date: Thu, 13 Apr 2023 23:59:45 +0300 Subject: [PATCH] Notify user if files too large --- .infrastructure/silencer-bot.service | 8 ++++-- Keroosha.SilencerBot/Processing.fs | 39 ++++++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/.infrastructure/silencer-bot.service b/.infrastructure/silencer-bot.service index 76416bf..6cc26d7 100644 --- a/.infrastructure/silencer-bot.service +++ b/.infrastructure/silencer-bot.service @@ -1,6 +1,6 @@ [Unit] # A short human readable title of the unit -Description= Keroosha.SilencerBot Prod +Description=Keroosha.SilencerBot Prod # A list of units whose activations will occur before this unit starts. After=network.target @@ -13,9 +13,13 @@ After=network.target Type=simple # Command with arguments to invoke when the unit is activated. ExecStart=/opt/apps/Keroosha.SilncerBot/app/Keroosha.SilencerBot +WorkingDirectory=/opt/apps/Keroosha.SilncerBot/app # Configures under what conditions the unit will be restarted. -Restart=on-failure +User=apps +Restart=always CPUQuota=15% +KillSignal=SIGINT +Environment=SILENCER_BOT_CONFIG_PATH=/opt/apps/Keroosha.SilncerBot/config/config.json [Install] # A list of units who when activated will try and activate this unit diff --git a/Keroosha.SilencerBot/Processing.fs b/Keroosha.SilencerBot/Processing.fs index 9e4c41a..37c0098 100644 --- a/Keroosha.SilencerBot/Processing.fs +++ b/Keroosha.SilencerBot/Processing.fs @@ -121,19 +121,32 @@ let processUploading (job: UserJob, botConfig: Funogram.Types.BotConfig, config: async { let ctx = getContext job let instrumentalPath, vocalsPath = getArtifactsPath ctx - use fInstrumental = File.OpenRead instrumentalPath - use fVocals = File.OpenRead vocalsPath - - let media = [| - InputFile.File (Path.GetFileName instrumentalPath, fInstrumental) - InputFile.File (Path.GetFileName vocalsPath, fVocals) - |] - - Logging.logger.Information $"Uploading results for {job.Id} job" - // TODO Error handling! - let uploadMedia (x: InputFile) = TgClient.makeRequestAsync botConfig <| Api.sendAudio (ctx.chatId) (x) (0) - do! media |> Seq.map uploadMedia |> Async.Sequential |> Async.Ignore - return { job with State = JobState.CleanUp } + + let sizeCheck = [ + FileInfo(instrumentalPath) + FileInfo(vocalsPath) ] + |> List.map (fun x -> Math.Floor(double(x.Length) / double(Math.Pow(1024, 2)))) + |> List.fold (fun acc x -> acc && x < double(50)) true + + match sizeCheck with + | false -> + do! TgClient.makeRequestAsync botConfig <| Api.sendMessage ctx.chatId "Не могу загрузить результаты - они весят больше 50мб" + |> Async.Ignore + return { job with State = JobState.CleanUp } + | true -> + use fInstrumental = File.OpenRead instrumentalPath + use fVocals = File.OpenRead vocalsPath + + let media = [| + InputFile.File (Path.GetFileName instrumentalPath, fInstrumental) + InputFile.File (Path.GetFileName vocalsPath, fVocals) + |] + + Logging.logger.Information $"Uploading results for {job.Id} job" + // TODO Error handling! + let uploadMedia (x: InputFile) = TgClient.makeRequestAsync botConfig <| Api.sendAudio (ctx.chatId) (x) (0) + do! media |> Seq.map uploadMedia |> Async.Sequential |> Async.Ignore + return { job with State = JobState.CleanUp } } let processCleanUp (job: UserJob, botConfig: Funogram.Types.BotConfig, config: BotConfig) =