Skip to content

Commit 965d3a7

Browse files
committed
Cleaner and events sender background workers
1 parent f8b7eab commit 965d3a7

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src/Web/Gathering/Database.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ getNewUser name email =
4444
(contramap fst (SqlE.value SqlE.text) <> contramap snd (SqlE.value SqlE.text))
4545
(SqlD.maybeRow decodeUser)
4646
True
47-
4847

4948
-- | Get a user from the users table with a specific id
5049
getUserById :: UserId -> Sql.Session (Maybe User)
@@ -213,7 +212,7 @@ verifyNewUser key email = do
213212
case insertResult of
214213
Just r ->
215214
pure (pure r)
216-
Nothing ->
215+
Nothing ->
217216
pure (Left "Could not find associated email. Maybe verification expired?")
218217

219218

src/Web/Gathering/Run.hs

+15-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Web.Gathering.Types
1515
import Web.Gathering.Config
1616
import Web.Gathering.Database
1717
import Web.Gathering.Router
18+
import Web.Gathering.Workers.SendEmails
19+
import Web.Gathering.Workers.Cleaner
1820

1921
import Control.Monad (void)
2022
import Control.Concurrent (forkIO)
@@ -35,11 +37,18 @@ import Web.Spock.Config
3537
--
3638
run :: IO ()
3739
run = do
38-
(config, cmd) <- parseArgs
39-
print (config, cmd)
40-
let connstr = cfgDbConnStr config
41-
spockCfg <- defaultSpockCfg EmptySession (PCConn $ hasqlPool connstr) (AppState config cmd)
42-
case cmd of
40+
(uncurry AppState -> state) <- parseArgs
41+
print state
42+
let connstr = cfgDbConnStr (appConfig state)
43+
spockCfg <- defaultSpockCfg EmptySession (PCConn $ hasqlPool connstr) state
44+
45+
-- Background workers
46+
void $ forkIO $ newEventsWorker state
47+
void $ forkIO $ cleanerWorker state
48+
49+
-- app
50+
51+
case appCommand state of
4352
HTTP port ->
4453
runSpock port (spock spockCfg appRouter)
4554
HTTPS tls -> do
@@ -49,6 +58,7 @@ run = do
4958
runHttps spockCfg tls
5059

5160

61+
5262
-- | Run the spock app with HTTPS
5363
runHttps :: SpockCfg Connection MySession AppState -> TLSConfig -> IO ()
5464
runHttps spockCfg tls = do

src/Web/Gathering/Workers/Cleaner.hs

+16-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@ import Hasql.Session
1111
import Hasql.Connection
1212

1313
import Web.Gathering.Database
14+
import Web.Gathering.Types
15+
import Web.Gathering.Config
1416

15-
cleanerWorker :: Connection -> IO ()
16-
cleanerWorker conn = forever $ do
17-
cleaner conn
17+
cleanerWorker :: AppState -> IO ()
18+
cleanerWorker state = forever $ do
19+
mConn <- acquire (cfgDbConnStr $ appConfig state)
20+
21+
case mConn of
22+
Right conn -> do
23+
putStrLn "Cleaner starting..."
24+
cleaner conn
25+
release conn
26+
27+
Left ex ->
28+
err ("Cleaner: " <> pack (show ex))
29+
30+
putStrLn "Cleaner sleeping..."
1831
sleep (60 * 60) -- sleep for 1 hour
1932

2033
cleaner :: Connection -> IO ()

src/Web/Gathering/Workers/SendEmails.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ import Web.Gathering.Database
3434

3535
newEventsWorker :: AppState -> IO ()
3636
newEventsWorker config = forever $ do
37+
putStrLn "Events worker starting..."
3738
newEventsWorker' config
39+
putStrLn "Events Worker sleeping..."
3840
sleep (60 * 20) -- sleep for 20 minutes
3941

4042
newEventsWorker' :: AppState -> IO ()
4143
newEventsWorker' config = do
4244
mConn <- acquire (cfgDbConnStr $ appConfig config)
4345
case mConn of
4446
Right conn -> do
45-
sendNewEvents config conn `catch` \ex -> err ("New Events Worker: " <> (pack $ show (ex :: IOException)))
47+
sendNewEvents config conn `catch` \ex -> err ("New Events Worker: " <> (pack $ show (ex :: SomeException)))
4648
release conn
4749
Left ex ->
4850
err ("New Events Worker: " <> pack (show ex))

0 commit comments

Comments
 (0)