aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
diff options
context:
space:
mode:
authorLia Lenckowski <lialenck@protonmail.com>2023-08-29 01:21:51 +0200
committerLia Lenckowski <lialenck@protonmail.com>2023-08-29 01:21:51 +0200
commita07a29ef8f22a96b12f9777fc34c2554e6c201ab (patch)
tree1ccf5bc23764d07d7988baedcb4f377ccabd7a60 /src/Main.hs
parentef4c38adb1cbf5479ab5cb67a6ebeaf207168d7f (diff)
downloadfastbangs-a07a29ef8f22a96b12f9777fc34c2554e6c201ab.tar
fastbangs-a07a29ef8f22a96b12f9777fc34c2554e6c201ab.tar.bz2
fastbangs-a07a29ef8f22a96b12f9777fc34c2554e6c201ab.tar.zst
batched/commulative notifications about pending bangs
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 91da385..d69ced6 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -1,16 +1,18 @@
{-# LANGUAGE OverloadedStrings, QuasiQuotes, TemplateHaskell,
- TypeFamilies, LambdaCase, EmptyDataDecls #-}
+ TypeFamilies, LambdaCase, EmptyDataDecls, ScopedTypeVariables #-}
{-# OPTIONS -Wno-unused-top-binds #-}
module Main (main) where
+import Control.Monad.IO.Unlift (toIO)
import Control.Monad.Logger (runStdoutLoggingT)
import Control.Monad.Trans.Resource (runResourceT)
import Control.Monad (when, unless)
import Database.Persist.Sqlite
import Data.Bool (bool)
import Data.Function ((&))
+import Data.Pool (Pool)
import Network.Wai.Handler.Warp hiding (getPort, getHost)
import Yesod
@@ -26,7 +28,8 @@ import BatchedRunner
data Search = Search {
bangState :: BangState,
sqlPool :: ConnectionPool,
- config :: Config
+ config :: Config,
+ notifyPbRunner :: Runner
}
mkYesod "Search" [parseRoutes|
@@ -89,6 +92,8 @@ postSubmitR = do
unless (verifyPendingBang pb) $ invalidArgs []
runDB $ insert400_ pb
+ liftIO . notifyRunner . notifyPbRunner =<< getYesod
+
getOpenSearchR :: Handler TypedContent
getOpenSearchR = do
neverExpires
@@ -136,6 +141,14 @@ getPendingR = do
pendingBangs <- runDB $ selectList [] []
return $ toJSON $ map entityVal (pendingBangs :: [Entity PendingBang])
+sendPendingBangs :: forall m. (MonadLogger m, MonadUnliftIO m)
+ => Config -> Pool SqlBackend -> m ()
+sendPendingBangs cfg pool = do
+ n <- length <$> (runSqlPool (selectList [] []) pool :: m [Entity PendingBang])
+ when (n > 0) $ sendEmail cfg (confAdminEmail cfg) "Pending Bang notification" $
+ "There are currently " <> show n <> " pending bangs.\n" <>
+ "Please do something about it.\n"
+
main :: IO ()
main = runStdoutLoggingT $ withSqlitePool "banger.db" 2 $ \pool -> do
runResourceT $ flip runSqlPool pool $ runMigration migrateAll
@@ -144,7 +157,10 @@ main = runStdoutLoggingT $ withSqlitePool "banger.db" 2 $ \pool -> do
cfg <- liftIO getConfig
$(logInfo) $ "Using config: " <> T.pack (show cfg)
- sApp <- liftIO $ toWaiApp $ Search bs pool cfg -- includes middlewares
+ sendPbIO <- toIO $ sendPendingBangs cfg pool
+ notifyPb <- liftIO $ makeRunner sendPbIO 600000000 {- 10 min -}
+
+ sApp <- liftIO $ toWaiApp $ Search bs pool cfg notifyPb -- includes middlewares
let settings = defaultSettings
& setHost (confHost cfg)