diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-04 21:59:52 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-04 21:59:52 +0000 |
commit | bf1bea05f992dd21f267d25034d2ffd5ef6f865d (patch) | |
tree | 393d55e5ebc2bf4e66194030cbc9fd7981c466a5 /server | |
parent | f1ceaffc159fe7a591bfc615762867db3f6aa199 (diff) | |
download | thermoprint-bf1bea05f992dd21f267d25034d2ffd5ef6f865d.tar thermoprint-bf1bea05f992dd21f267d25034d2ffd5ef6f865d.tar.gz thermoprint-bf1bea05f992dd21f267d25034d2ffd5ef6f865d.tar.bz2 thermoprint-bf1bea05f992dd21f267d25034d2ffd5ef6f865d.tar.xz thermoprint-bf1bea05f992dd21f267d25034d2ffd5ef6f865d.zip |
Queue managers
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Thermoprint/Server/Printer.hs | 3 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/Queue.hs | 33 | ||||
-rw-r--r-- | server/thermoprint-server.cabal | 1 | ||||
-rw-r--r-- | server/thermoprint-server.nix | 10 |
4 files changed, 41 insertions, 6 deletions
diff --git a/server/src/Thermoprint/Server/Printer.hs b/server/src/Thermoprint/Server/Printer.hs index 3700f45..d9cea9d 100644 --- a/server/src/Thermoprint/Server/Printer.hs +++ b/server/src/Thermoprint/Server/Printer.hs | |||
@@ -56,6 +56,9 @@ data Printer = Printer | |||
56 | , queue :: TVar Queue | 56 | , queue :: TVar Queue |
57 | } | 57 | } |
58 | 58 | ||
59 | instance HasQueue Printer where | ||
60 | extractQueue = queue | ||
61 | |||
59 | printer :: MonadResource m => m PrinterMethod -> m Printer | 62 | printer :: MonadResource m => m PrinterMethod -> m Printer |
60 | printer p = Printer <$> p <*> liftIO (newTVarIO def) | 63 | printer p = Printer <$> p <*> liftIO (newTVarIO def) |
61 | 64 | ||
diff --git a/server/src/Thermoprint/Server/Queue.hs b/server/src/Thermoprint/Server/Queue.hs index 672d540..bae9617 100644 --- a/server/src/Thermoprint/Server/Queue.hs +++ b/server/src/Thermoprint/Server/Queue.hs | |||
@@ -1,7 +1,12 @@ | |||
1 | {-# LANGUAGE FlexibleInstances #-} | ||
2 | {-# LANGUAGE ViewPatterns #-} | ||
1 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} | 3 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} |
4 | {-# LANGUAGE ExistentialQuantification #-} | ||
2 | 5 | ||
3 | module Thermoprint.Server.Queue | 6 | module Thermoprint.Server.Queue |
4 | ( Queue(..), QueueEntry(..) | 7 | ( Queue(..), QueueEntry(..) |
8 | , HasQueue(..) | ||
9 | , QueueManager(..), runQM | ||
5 | ) where | 10 | ) where |
6 | 11 | ||
7 | import Thermoprint.API (PrintingError(..), Printout) | 12 | import Thermoprint.API (PrintingError(..), Printout) |
@@ -12,14 +17,22 @@ import Thermoprint.Server.Database | |||
12 | import Data.Sequence (Seq) | 17 | import Data.Sequence (Seq) |
13 | import qualified Data.Sequence as Seq | 18 | import qualified Data.Sequence as Seq |
14 | 19 | ||
15 | import Data.Time | 20 | import Data.Time |
21 | import Data.Time.Clock | ||
16 | 22 | ||
17 | import Control.DeepSeq (NFData) | 23 | import Control.DeepSeq (NFData) |
18 | import Data.Typeable (Typeable) | 24 | import Data.Typeable (Typeable) |
19 | import GHC.Generics (Generic) | 25 | import GHC.Generics (Generic) |
20 | 26 | ||
27 | import Control.Concurrent | ||
28 | import Control.Concurrent.STM | ||
29 | import Control.Monad.State | ||
30 | |||
21 | import Data.Default.Class | 31 | import Data.Default.Class |
22 | 32 | ||
33 | import Control.Monad.Morph | ||
34 | import Control.Monad.Trans.Compose | ||
35 | |||
23 | -- | Zipper for 'Seq QueueEntry' with additional support for 'PrintingError' in the section after point | 36 | -- | Zipper for 'Seq QueueEntry' with additional support for 'PrintingError' in the section after point |
24 | data Queue = Queue | 37 | data Queue = Queue |
25 | { pending :: Seq QueueEntry -- ^ Pending jobs, closest last | 38 | { pending :: Seq QueueEntry -- ^ Pending jobs, closest last |
@@ -28,6 +41,12 @@ data Queue = Queue | |||
28 | } | 41 | } |
29 | deriving (Typeable, Generic, NFData) | 42 | deriving (Typeable, Generic, NFData) |
30 | 43 | ||
44 | class HasQueue a where | ||
45 | extractQueue :: a -> TVar Queue | ||
46 | |||
47 | instance HasQueue (TVar Queue) where | ||
48 | extractQueue = id | ||
49 | |||
31 | instance Default Queue where | 50 | instance Default Queue where |
32 | def = Queue | 51 | def = Queue |
33 | { pending = Seq.empty | 52 | { pending = Seq.empty |
@@ -41,3 +60,15 @@ data QueueEntry = QueueEntry | |||
41 | } | 60 | } |
42 | deriving (Typeable, Generic, NFData) | 61 | deriving (Typeable, Generic, NFData) |
43 | 62 | ||
63 | type QueueManager t = ComposeT (StateT Queue) t STM DiffTime | ||
64 | |||
65 | runQM :: ( HasQueue q | ||
66 | , MFunctor t | ||
67 | , MonadTrans t | ||
68 | , MonadIO (t IO) | ||
69 | , Monad (t STM) | ||
70 | ) => QueueManager t -> q -> t IO () | ||
71 | runQM qm (extractQueue -> q) = forever $ liftIO . threadDelay . toMicro =<< qm' | ||
72 | where | ||
73 | qm' = hoist atomically $ (\(a, s) -> lift (writeTVar q s) >> return a) =<< runStateT (getComposeT qm) =<< lift (readTVar q) | ||
74 | toMicro = (`div` 10^6) . fromEnum | ||
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal index b948ce3..2eda3b5 100644 --- a/server/thermoprint-server.cabal +++ b/server/thermoprint-server.cabal | |||
@@ -47,6 +47,7 @@ library | |||
47 | , transformers >=0.3.0 && <1 | 47 | , transformers >=0.3.0 && <1 |
48 | , wai >=3.0.4 && <4 | 48 | , wai >=3.0.4 && <4 |
49 | , warp >=3.1.9 && <4 | 49 | , warp >=3.1.9 && <4 |
50 | , mmorph >=1.0.5 && <2 | ||
50 | hs-source-dirs: src | 51 | hs-source-dirs: src |
51 | default-language: Haskell2010 | 52 | default-language: Haskell2010 |
52 | 53 | ||
diff --git a/server/thermoprint-server.nix b/server/thermoprint-server.nix index afcf2ba..1cbccd2 100644 --- a/server/thermoprint-server.nix +++ b/server/thermoprint-server.nix | |||
@@ -1,8 +1,8 @@ | |||
1 | { mkDerivation, base, conduit, containers, data-default-class | 1 | { mkDerivation, base, conduit, containers, data-default-class |
2 | , deepseq, dyre, either, exceptions, monad-control, monad-logger | 2 | , deepseq, dyre, either, exceptions, mmorph, monad-control |
3 | , mtl, persistent, persistent-sqlite, persistent-template | 3 | , monad-logger, mtl, persistent, persistent-sqlite |
4 | , resourcet, servant-server, stdenv, stm, text, thermoprint-spec | 4 | , persistent-template, resourcet, servant-server, stdenv, stm, text |
5 | , time, transformers, wai, warp | 5 | , thermoprint-spec, time, transformers, wai, warp |
6 | }: | 6 | }: |
7 | mkDerivation { | 7 | mkDerivation { |
8 | pname = "thermoprint-server"; | 8 | pname = "thermoprint-server"; |
@@ -12,7 +12,7 @@ mkDerivation { | |||
12 | isExecutable = true; | 12 | isExecutable = true; |
13 | libraryHaskellDepends = [ | 13 | libraryHaskellDepends = [ |
14 | base conduit containers data-default-class deepseq dyre either | 14 | base conduit containers data-default-class deepseq dyre either |
15 | exceptions monad-control monad-logger mtl persistent | 15 | exceptions mmorph monad-control monad-logger mtl persistent |
16 | persistent-template resourcet servant-server stm text | 16 | persistent-template resourcet servant-server stm text |
17 | thermoprint-spec time transformers wai warp | 17 | thermoprint-spec time transformers wai warp |
18 | ]; | 18 | ]; |