allow flushDbQueue to be run repeatedly
This commit is contained in:
parent
d43ac8056b
commit
959b060e26
1 changed files with 16 additions and 12 deletions
|
@ -13,6 +13,7 @@ module Database.Queue (
|
||||||
openDbQueue,
|
openDbQueue,
|
||||||
queryDbQueue,
|
queryDbQueue,
|
||||||
closeDbQueue,
|
closeDbQueue,
|
||||||
|
flushDbQueue,
|
||||||
QueueSize,
|
QueueSize,
|
||||||
queueDb,
|
queueDb,
|
||||||
) where
|
) where
|
||||||
|
@ -39,14 +40,25 @@ openDbQueue db tablename = DQ
|
||||||
<$> openDb db tablename
|
<$> openDb db tablename
|
||||||
<*> (newMVar =<< emptyQueue)
|
<*> (newMVar =<< emptyQueue)
|
||||||
|
|
||||||
{- Must be called to ensure queued changes get written to the database. -}
|
{- This or flushDbQueue must be called, eg at program exit to ensure
|
||||||
|
- queued changes get written to the database. -}
|
||||||
closeDbQueue :: DbQueue -> IO ()
|
closeDbQueue :: DbQueue -> IO ()
|
||||||
closeDbQueue h@(DQ hdl _) = do
|
closeDbQueue h@(DQ hdl _) = do
|
||||||
flushDbQueue h
|
flushDbQueue h
|
||||||
closeDb hdl
|
closeDb hdl
|
||||||
|
|
||||||
{- Makes a queury using the DbQueue. This should not be used to make
|
{- Blocks until all queued changes have been written to the database. -}
|
||||||
- changes to the database!
|
flushDbQueue :: DbQueue -> IO ()
|
||||||
|
flushDbQueue (DQ hdl qvar) = do
|
||||||
|
q@(Queue sz _ qa) <- takeMVar qvar
|
||||||
|
if sz > 0
|
||||||
|
then do
|
||||||
|
commitDb hdl qa
|
||||||
|
putMVar qvar =<< emptyQueue
|
||||||
|
else putMVar qvar q
|
||||||
|
|
||||||
|
{- Makes a query using the DbQueue's database connection.
|
||||||
|
- This should not be used to make changes to the database!
|
||||||
-
|
-
|
||||||
- Queries will not return changes that have been recently queued,
|
- Queries will not return changes that have been recently queued,
|
||||||
- so use with care.
|
- so use with care.
|
||||||
|
@ -67,12 +79,6 @@ emptyQueue = do
|
||||||
now <- getCurrentTime
|
now <- getCurrentTime
|
||||||
return $ Queue 0 now (return ())
|
return $ Queue 0 now (return ())
|
||||||
|
|
||||||
flushDbQueue :: DbQueue -> IO ()
|
|
||||||
flushDbQueue (DQ hdl qvar) = do
|
|
||||||
Queue sz _ qa <- takeMVar qvar
|
|
||||||
when (sz > 0) $
|
|
||||||
commitDb hdl qa
|
|
||||||
|
|
||||||
{- Queues a change to be made to the database. It will be queued
|
{- Queues a change to be made to the database. It will be queued
|
||||||
- to be committed later, unless the commitchecker action returns true,
|
- to be committed later, unless the commitchecker action returns true,
|
||||||
- in which case any previously queued changes are also committed.
|
- in which case any previously queued changes are also committed.
|
||||||
|
@ -97,8 +103,6 @@ queueDb (DQ hdl qvar) commitchecker a = do
|
||||||
r <- commitDb' hdl qa'
|
r <- commitDb' hdl qa'
|
||||||
case r of
|
case r of
|
||||||
Left _ -> enqueue $ Queue sz' lastcommittime qa'
|
Left _ -> enqueue $ Queue sz' lastcommittime qa'
|
||||||
Right _ -> do
|
Right _ -> enqueue =<< emptyQueue
|
||||||
now <- getCurrentTime
|
|
||||||
enqueue $ Queue 0 now (return ())
|
|
||||||
, enqueue $ Queue sz' lastcommittime qa'
|
, enqueue $ Queue sz' lastcommittime qa'
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue