aboutsummaryrefslogtreecommitdiff
path: root/src/executable/haskell/Service/AdListener.hs
diff options
context:
space:
mode:
authorJoris2019-09-05 20:46:36 +0200
committerJoris2019-09-05 20:46:36 +0200
commit317e7b1e7319182e5caa5169119aea9fc8d660b6 (patch)
treedc9f9c458c42d1e2c60a12ff55267e042c88f6ba /src/executable/haskell/Service/AdListener.hs
parent223ae6aa0b14c071d5719ada0cc6b43e9199a81b (diff)
downloadad-listener-317e7b1e7319182e5caa5169119aea9fc8d660b6.tar.gz
ad-listener-317e7b1e7319182e5caa5169119aea9fc8d660b6.tar.bz2
ad-listener-317e7b1e7319182e5caa5169119aea9fc8d660b6.zip
Enable the listener only during some hours
Diffstat (limited to 'src/executable/haskell/Service/AdListener.hs')
-rw-r--r--src/executable/haskell/Service/AdListener.hs26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/executable/haskell/Service/AdListener.hs b/src/executable/haskell/Service/AdListener.hs
index c393f38..9af92f4 100644
--- a/src/executable/haskell/Service/AdListener.hs
+++ b/src/executable/haskell/Service/AdListener.hs
@@ -5,8 +5,8 @@ module Service.AdListener
import Control.Concurrent (threadDelay)
import qualified Data.Text as T
import qualified Data.Text.IO as T
+import qualified Data.Time.LocalTime as LocalTime
import Network.Wreq.Session (Session)
-import Prelude hiding (error)
import Conf (Conf)
import qualified Conf
@@ -21,10 +21,11 @@ import qualified View.Ad as Ad
start :: Conf -> Session -> IO ()
start conf session = do
- ads <- fetchAds conf session
+ -- ads <- fetchAds conf session
+ let ads = []
let newURLs = map Ad.url ads
T.putStrLn "Listening to new ads…"
- waitListenInterval conf
+ sleepUntilReady conf
listenToNewAdsWithViewedURLs conf session newURLs
listenToNewAdsWithViewedURLs :: Conf -> Session -> [URL] -> IO ()
@@ -39,7 +40,7 @@ listenToNewAdsWithViewedURLs conf session viewedURLs = do
sendMail conf newAds
else
return ()
- waitListenInterval conf
+ sleepUntilReady conf
listenToNewAdsWithViewedURLs conf session (viewedURLs ++ newURLs)
fetchAds :: Conf -> Session -> IO [Ad]
@@ -61,5 +62,18 @@ sendMail conf ads =
mail = Mail (Conf.mailFrom conf) (Conf.mailTo conf) title plainBody
in MailService.send (Conf.mailMock conf) mail >> return ()
-waitListenInterval :: Conf -> IO ()
-waitListenInterval = threadDelay . (*) 1000000 . round . Conf.listenInterval
+sleepUntilReady :: Conf -> IO ()
+sleepUntilReady conf = do
+ timeSinceMidnight <-
+ (LocalTime.timeOfDayToTime . LocalTime.localTimeOfDay . LocalTime.zonedTimeToLocalTime)
+ <$> LocalTime.getZonedTime
+ case TimeUtils.asleepDuration (Conf.listenFrom conf) (Conf.listenTo conf) timeSinceMidnight of
+ Just d -> do
+ sleepSeconds d
+
+ Nothing ->
+ -- TODO 04/09/2019: Add noise
+ sleepSeconds . Conf.listenInterval $ conf
+ where
+ sleepSeconds =
+ threadDelay . (*) 1000000 . round