aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-03-01 00:04:54 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2016-03-01 00:04:54 +0100
commit36434c74792c50be759172d158be0e9e56e28849 (patch)
tree1d9ccf224764a6de034fbb26191e4981e89b2b98
parent8dc71c2a4219f2e820e4c55ee7754e184574e8e5 (diff)
downloadthermoprint-36434c74792c50be759172d158be0e9e56e28849.tar
thermoprint-36434c74792c50be759172d158be0e9e56e28849.tar.gz
thermoprint-36434c74792c50be759172d158be0e9e56e28849.tar.bz2
thermoprint-36434c74792c50be759172d158be0e9e56e28849.tar.xz
thermoprint-36434c74792c50be759172d158be0e9e56e28849.zip
Thermoprint.Server.Queue.Utils
Reformulated from their flawed QMConfig-version
-rw-r--r--server/src/Thermoprint/Server.hs2
-rw-r--r--server/src/Thermoprint/Server/Queue/Utils.hs52
-rw-r--r--server/thermoprint-server.cabal1
3 files changed, 55 insertions, 0 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs
index 446c63e..f73a418 100644
--- a/server/src/Thermoprint/Server.hs
+++ b/server/src/Thermoprint/Server.hs
@@ -16,6 +16,7 @@ module Thermoprint.Server
16 , module Servant.Server.Internal.Enter 16 , module Servant.Server.Internal.Enter
17 , module Thermoprint.Server.Printer 17 , module Thermoprint.Server.Printer
18 , module Thermoprint.Server.Queue 18 , module Thermoprint.Server.Queue
19 , module Thermoprint.Server.Queue.Utils
19 ) where 20 ) where
20 21
21import Data.Default.Class 22import Data.Default.Class
@@ -79,6 +80,7 @@ import Thermoprint.Server.Push
79import Thermoprint.Server.Database 80import Thermoprint.Server.Database
80import Thermoprint.Server.Printer 81import Thermoprint.Server.Printer
81import Thermoprint.Server.Queue 82import Thermoprint.Server.Queue
83import Thermoprint.Server.Queue.Utils
82import qualified Thermoprint.Server.API as API (thermoprintServer) 84import qualified Thermoprint.Server.API as API (thermoprintServer)
83import Thermoprint.Server.API hiding (thermoprintServer) 85import Thermoprint.Server.API hiding (thermoprintServer)
84 86
diff --git a/server/src/Thermoprint/Server/Queue/Utils.hs b/server/src/Thermoprint/Server/Queue/Utils.hs
new file mode 100644
index 0000000..86b0162
--- /dev/null
+++ b/server/src/Thermoprint/Server/Queue/Utils.hs
@@ -0,0 +1,52 @@
1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE TypeOperators #-}
4
5module Thermoprint.Server.Queue.Utils
6 ( standardCollapse
7 , standardSleep
8 , limitHistorySize
9 , limitHistoryAge
10 ) where
11
12import Data.Sequence (Seq)
13import qualified Data.Sequence as Seq
14
15import Data.Time
16
17import Control.Monad.State
18import Control.Monad.IO.Class
19import Control.Monad.Trans.Identity
20import Servant.Server.Internal.Enter
21
22import Thermoprint.Server.Queue
23
24standardCollapse :: MonadIO m => IdentityT IO :~> m
25standardCollapse = Nat $ liftIO . runIdentityT
26
27standardSleep :: Monad (QueueManagerM t) => QueueManager t
28-- ^ Instruct 'runQM' to sleep some standard amount of time
29--
30-- /TODO/: Investigate implementing a smarter algorithm (PID-controller)
31standardSleep = return $ 20
32
33limitHistorySize :: MonadState Queue (QueueManagerM t) => Int -> QueueManager t
34-- ^ Limit a 'Queue's 'history's size to some number of 'QueueEntry's
35limitHistorySize max = modify' limitSize >> standardSleep
36 where
37 limitSize :: Queue -> Queue
38 limitSize q@Queue{..} = q { history = Seq.take max history }
39
40limitHistoryAge :: ( MonadState Queue (QueueManagerM t)
41 ) => NominalDiffTime -- ^ Maximum age relative to the youngest 'QueueEntry'
42 -> QueueManager t
43-- ^ Limit a 'Queue's 'history's contents to 'QueueEntry's below a certain age
44limitHistoryAge maxAge = modify' limitAge >> standardSleep
45 where
46 limitAge :: Queue -> Queue
47 limitAge q@Queue{..} = q { history = Seq.filter (filterAge . created . fst) history}
48 where
49 youngest :: UTCTime
50 youngest = maximum $ created . fst <$> history
51 filterAge :: UTCTime -> Bool
52 filterAge time = not $ (youngest `diffUTCTime` time) > maxAge
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal
index 3f0f832..62eb0ca 100644
--- a/server/thermoprint-server.cabal
+++ b/server/thermoprint-server.cabal
@@ -23,6 +23,7 @@ library
23 , Thermoprint.Server.API 23 , Thermoprint.Server.API
24 , Thermoprint.Server.Push 24 , Thermoprint.Server.Push
25 , Thermoprint.Server.Queue 25 , Thermoprint.Server.Queue
26 , Thermoprint.Server.Queue.Utils
26 , Thermoprint.Server.Printer 27 , Thermoprint.Server.Printer
27 , Thermoprint.Server.Printer.Debug 28 , Thermoprint.Server.Printer.Debug
28 , Thermoprint.Server.Printer.Debug.Delayed 29 , Thermoprint.Server.Printer.Debug.Delayed