diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-27 02:13:06 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-27 02:13:06 +0100 |
commit | 844f5e96097f8fcced94220487a7c5ad40c1e97f (patch) | |
tree | e7a9dc0715893a692ef56107cce399bacd17b9a2 /server | |
parent | d7787ee41ec261db9152d548a9ebbffb4c44c52d (diff) | |
download | thermoprint-844f5e96097f8fcced94220487a7c5ad40c1e97f.tar thermoprint-844f5e96097f8fcced94220487a7c5ad40c1e97f.tar.gz thermoprint-844f5e96097f8fcced94220487a7c5ad40c1e97f.tar.bz2 thermoprint-844f5e96097f8fcced94220487a7c5ad40c1e97f.tar.xz thermoprint-844f5e96097f8fcced94220487a7c5ad40c1e97f.zip |
QMConfig tools stuck at combineQM
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Thermoprint/Server.hs | 7 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/QMConfig.hs | 75 | ||||
-rw-r--r-- | server/test/Thermoprint/ServerSpec.hs | 3 |
3 files changed, 77 insertions, 8 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs index cd3a4ed..49f3e82 100644 --- a/server/src/Thermoprint/Server.hs +++ b/server/src/Thermoprint/Server.hs | |||
@@ -14,8 +14,8 @@ module Thermoprint.Server | |||
14 | , module Data.Default.Class | 14 | , module Data.Default.Class |
15 | , module Servant.Server.Internal.Enter | 15 | , module Servant.Server.Internal.Enter |
16 | , module Thermoprint.Server.Printer | 16 | , module Thermoprint.Server.Printer |
17 | , module Thermoprint.Server.Queue | ||
18 | , module Thermoprint.Server.QMConfig | 17 | , module Thermoprint.Server.QMConfig |
18 | , Queue(..), QueueEntry(..) | ||
19 | ) where | 19 | ) where |
20 | 20 | ||
21 | import Data.Default.Class | 21 | import Data.Default.Class |
@@ -78,7 +78,7 @@ import Thermoprint.Server.Push | |||
78 | 78 | ||
79 | import Thermoprint.Server.Database | 79 | import Thermoprint.Server.Database |
80 | import Thermoprint.Server.Printer | 80 | import Thermoprint.Server.Printer |
81 | import Thermoprint.Server.Queue | 81 | import Thermoprint.Server.Queue hiding (intersection, idQM, union, nullQM, runQM) |
82 | import Thermoprint.Server.QMConfig | 82 | import Thermoprint.Server.QMConfig |
83 | import qualified Thermoprint.Server.API as API (thermoprintServer) | 83 | import qualified Thermoprint.Server.API as API (thermoprintServer) |
84 | import Thermoprint.Server.API hiding (thermoprintServer) | 84 | import Thermoprint.Server.API hiding (thermoprintServer) |
@@ -140,8 +140,7 @@ thermoprintServer dyre io = Dyre.wrapMain $ Dyre.defaultParams | |||
140 | gcChan <- liftIO newTChanIO | 140 | gcChan <- liftIO newTChanIO |
141 | fork tMgr $ jobGC gcChan | 141 | fork tMgr $ jobGC gcChan |
142 | let | 142 | let |
143 | runQM' (queueManagers -> QMConfig qm nat) printer = unNat nat $ runQM gcChan qm printer | 143 | runQM' = runQM gcChan . queueManagers |
144 | runQM' (queueManagers -> QMConfig' qm) printer = hoist liftIO $ runQM gcChan qm printer | ||
145 | mapM_ (fork tMgr . uncurry runQM') $ Map.toList printers | 144 | mapM_ (fork tMgr . uncurry runQM') $ Map.toList printers |
146 | nChan <- liftIO $ newBroadcastTChanIO | 145 | nChan <- liftIO $ newBroadcastTChanIO |
147 | let | 146 | let |
diff --git a/server/src/Thermoprint/Server/QMConfig.hs b/server/src/Thermoprint/Server/QMConfig.hs index 0cf7beb..8f08faf 100644 --- a/server/src/Thermoprint/Server/QMConfig.hs +++ b/server/src/Thermoprint/Server/QMConfig.hs | |||
@@ -2,9 +2,16 @@ | |||
2 | {-# LANGUAGE FlexibleContexts #-} | 2 | {-# LANGUAGE FlexibleContexts #-} |
3 | {-# LANGUAGE ImpredicativeTypes #-} | 3 | {-# LANGUAGE ImpredicativeTypes #-} |
4 | {-# LANGUAGE TypeOperators #-} | 4 | {-# LANGUAGE TypeOperators #-} |
5 | {-# LANGUAGE RecordWildCards #-} | ||
6 | {-# LANGUAGE ViewPatterns #-} | ||
5 | 7 | ||
6 | module Thermoprint.Server.QMConfig | 8 | module Thermoprint.Server.QMConfig |
7 | ( QMConfig(..) | 9 | ( QMConfig(..) |
10 | , nullQM | ||
11 | , idQM | ||
12 | , standardSleep | ||
13 | , limitHistorySize | ||
14 | , limitHistoryAge | ||
8 | ) where | 15 | ) where |
9 | 16 | ||
10 | import Control.Monad.Trans.Resource | 17 | import Control.Monad.Trans.Resource |
@@ -14,15 +21,23 @@ import Control.Monad.Logger | |||
14 | import Control.Monad.Reader | 21 | import Control.Monad.Reader |
15 | import Control.Monad.IO.Class | 22 | import Control.Monad.IO.Class |
16 | import Control.Monad.Morph | 23 | import Control.Monad.Morph |
24 | import Control.Monad.State | ||
17 | import Control.Category | 25 | import Control.Category |
18 | import Prelude hiding (id, (.)) | ||
19 | import Servant.Server.Internal.Enter (enter, (:~>)(..)) | 26 | import Servant.Server.Internal.Enter (enter, (:~>)(..)) |
20 | 27 | ||
21 | import Data.Default.Class | 28 | import Data.Default.Class |
22 | 29 | ||
30 | import Data.Sequence (Seq) | ||
31 | import qualified Data.Sequence as Seq | ||
32 | import Data.Time | ||
33 | import Data.Foldable | ||
34 | |||
23 | import Control.Concurrent.STM | 35 | import Control.Concurrent.STM |
24 | 36 | ||
25 | import Thermoprint.Server.Queue | 37 | import Thermoprint.Server.Queue hiding (idQM, intersection, nullQM, union, runQM) |
38 | import qualified Thermoprint.Server.Queue as Q | ||
39 | |||
40 | import Prelude hiding (length, maximum, id, (.)) | ||
26 | 41 | ||
27 | data QMConfig m where | 42 | data QMConfig m where |
28 | QMConfig :: ( MonadTrans t | 43 | QMConfig :: ( MonadTrans t |
@@ -35,8 +50,62 @@ data QMConfig m where | |||
35 | , MFunctor t | 50 | , MFunctor t |
36 | , Monad (t STM) | 51 | , Monad (t STM) |
37 | , MonadIO (t IO) | 52 | , MonadIO (t IO) |
53 | , Monad (QueueManagerM t) | ||
38 | ) => QueueManager t | 54 | ) => QueueManager t |
39 | ) -> QMConfig m | 55 | ) -> QMConfig m |
40 | 56 | ||
41 | instance MonadIO m => Default (QMConfig m) where | 57 | instance MonadIO m => Default (QMConfig m) where |
42 | def = QMConfig idQM $ Nat (liftIO . runIdentityT) | 58 | def = idQM |
59 | |||
60 | intersection :: (Functor f, Foldable f, MonadIO m) => f (QMConfig m) -> QMConfig m | ||
61 | intersection = foldr' (qmCombine Q.intersection) idQM | ||
62 | |||
63 | idQM :: MonadIO m => QMConfig m | ||
64 | idQM = QMConfig' Q.idQM | ||
65 | |||
66 | union :: (Foldable f, MonadIO m) => f (QMConfig m) -> QMConfig m | ||
67 | union = foldr' (qmCombine Q.union) idQM | ||
68 | |||
69 | nullQM :: MonadIO m => QMConfig m | ||
70 | nullQM = QMConfig' Q.nullQM | ||
71 | |||
72 | qmCombine :: ([QueueManager t] -> QueueManager t) -> QMConfig m -> QMConfig m -> QMConfig m | ||
73 | qmCombine qmCombine' (QMConfig a natA) (QMConfig b natB) = undefined | ||
74 | where | ||
75 | natA :: t IO :~> m | ||
76 | natB :: t' IO :~> m | ||
77 | --- | ||
78 | hoistNat natB :: t (t' IO) :~> t m | ||
79 | -- | ||
80 | ? :: t m :~> m | ||
81 | natAB :: ComposeT t t' IO :~> m | ||
82 | |||
83 | runQM :: ( HasQueue q ) => TChan JobId -> QMConfig m -> q -> t IO () | ||
84 | runQM gcChan (QMConfig qm nat) q = unNat nat $ runQM gcChan qm q | ||
85 | runQM gcChan (QMConfig' qm) q = hoist liftIO $ runQM gcChan qm q | ||
86 | |||
87 | standardSleep :: Monad (QueueManagerM t) => QueueManager t | ||
88 | -- ^ Instruct 'runQM' to sleep some standard amount of time | ||
89 | -- | ||
90 | -- /TODO/: Investigate implementing a smarter algorithm (PID-controller) | ||
91 | standardSleep = return $ 20 | ||
92 | |||
93 | limitHistorySize :: MonadIO m => Int -> QMConfig m | ||
94 | -- ^ Limit a 'Queue's 'history's size to some number of 'QueueEntry's | ||
95 | limitHistorySize max = QMConfig' $ modify' limitSize >> standardSleep | ||
96 | where | ||
97 | limitSize :: Queue -> Queue | ||
98 | limitSize q@Queue{..} = q { history = Seq.take max history } | ||
99 | |||
100 | limitHistoryAge :: MonadIO m => NominalDiffTime -- ^ Maximum age relative to the youngest 'QueueEntry' | ||
101 | -> QMConfig m | ||
102 | -- ^ Limit a 'Queue's 'history's contents to 'QueueEntry's below a certain age | ||
103 | limitHistoryAge maxAge = QMConfig' $ modify' limitAge >> standardSleep | ||
104 | where | ||
105 | limitAge :: Queue -> Queue | ||
106 | limitAge q@Queue{..} = q { history = Seq.filter (filterAge . created . fst) history} | ||
107 | where | ||
108 | youngest :: UTCTime | ||
109 | youngest = maximum $ created . fst <$> history | ||
110 | filterAge :: UTCTime -> Bool | ||
111 | filterAge time = not $ (youngest `diffUTCTime` time) > maxAge | ||
diff --git a/server/test/Thermoprint/ServerSpec.hs b/server/test/Thermoprint/ServerSpec.hs index 028ba2d..6407c3c 100644 --- a/server/test/Thermoprint/ServerSpec.hs +++ b/server/test/Thermoprint/ServerSpec.hs | |||
@@ -13,7 +13,8 @@ import qualified Test.Hspec as Hspec | |||
13 | import Test.QuickCheck | 13 | import Test.QuickCheck |
14 | 14 | ||
15 | import Thermoprint.API | 15 | import Thermoprint.API |
16 | import qualified Thermoprint.Server as S | 16 | import qualified Thermoprint.Server as S hiding (intersection, idQM, union, nullQM) |
17 | import qualified Thermoprint.Server.Queue as S | ||
17 | import Thermoprint.Client | 18 | import Thermoprint.Client |
18 | 19 | ||
19 | import Data.Monoid | 20 | import Data.Monoid |