Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 2ce31cf

Browse files
author
Ben Ford
committed
[DEVOPS-834] Add a process that checks the wallet balance periodically
1 parent 88b6bd9 commit 2ce31cf

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

faucet/src/Cardano/Faucet/Init.hs

+32-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DeriveGeneric #-}
44
{-# LANGUAGE DuplicateRecordFields #-}
5+
{-# LANGUAGE FlexibleContexts #-}
56
{-# LANGUAGE FlexibleInstances #-}
67
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
78
{-# LANGUAGE MultiParamTypeClasses #-}
@@ -202,10 +203,32 @@ readWalletBalance
202203
-> PaymentSource
203204
-> ExceptT InitFaucetError m Int64
204205
readWalletBalance client (psWalletId -> wId) = do
205-
lift $ logInfo "Reading initial wallet balance"
206+
-- lift $ logInfo "Reading initial wallet balance"
206207
(fromIntegral . getCoin . unV1 . walBalance)
207208
<$> runClient CouldntReadBalance (getWallet client wId)
208209

210+
--------------------------------------------------------------------------------
211+
-- | Monitor a wallet's balance
212+
--
213+
-- Sets the wallet's balance in the 'Gauge.Gauge' every 5 seconds
214+
monitorWalletBalance
215+
:: (HasLoggerName m, CanLog m, MonadIO m)
216+
=> FaucetEnv -> m ()
217+
monitorWalletBalance fEnv = do
218+
let wc = fEnv ^. feWalletClient . to liftClient
219+
paymentSource = fEnv ^. (feSourceWallet . to cfgToPaymentSource)
220+
balGauge = fEnv ^. feWalletBalance
221+
forever $ do
222+
liftIO $ threadDelay 5000000
223+
eBal <- runExceptT $ readWalletBalance wc paymentSource
224+
case eBal of
225+
Left err -> do
226+
logError ("Error reading balance: " <> (Text.pack $ show err))
227+
Right bal -> do
228+
logInfo ("Read wallet balance: " <> (Text.pack $ show bal))
229+
liftIO $ Gauge.set balGauge bal
230+
231+
--------------------------------------------------------------------------------
209232
-- | Gets an address out of an existing wallet
210233
--
211234
-- Fails with 'BadAddress'
@@ -311,8 +334,14 @@ initEnv fc store = do
311334
withSublogger "initEnv" $ logInfo "Initializing environment"
312335
env <- createEnv
313336
withSublogger "initEnv" $ logInfo "Created environment"
314-
tID <- liftLogIO forkIO $ processWithdrawls env
315-
withSublogger "initEnv" $ logInfo ("Forked thread for processing withdrawls:" <> show tID ^. packed)
337+
wdTId <- liftLogIO forkIO $ processWithdrawls env
338+
withSublogger "initEnv"
339+
$ logInfo ( "Forked thread for processing withdrawls:"
340+
<> show wdTId ^. packed)
341+
monTId <- liftLogIO forkIO $ monitorWalletBalance env
342+
withSublogger "initEnv"
343+
$ logInfo ( "Forked thread for monitoring the wallet balance:"
344+
<> show monTId ^. packed)
316345
return env
317346
where
318347
createEnv = withSublogger "init" $ do

0 commit comments

Comments
 (0)