improve with continuation

no behavior change
This commit is contained in:
Joey Hess 2021-10-20 12:13:49 -04:00
parent 47e30f78be
commit c47794991c
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -132,33 +132,29 @@ data Job
| CloseJob | CloseJob
workerThread :: T.Text -> TableName -> MVar Job -> IO () workerThread :: T.Text -> TableName -> MVar Job -> IO ()
workerThread db tablename jobs = go workerThread db tablename jobs = newconn
where where
go = do newconn = do
v <- tryNonAsync (runSqliteRobustly tablename db loop) v <- tryNonAsync (runSqliteRobustly tablename db loop)
case v of case v of
Left e -> hPutStrLn stderr $ Left e -> hPutStrLn stderr $
"sqlite worker thread crashed: " ++ show e "sqlite worker thread crashed: " ++ show e
Right True -> go Right cont -> cont
Right False -> return ()
getjob :: IO (Either BlockedIndefinitelyOnMVar Job)
getjob = try $ takeMVar jobs
loop = do loop = do
job <- liftIO getjob job <- liftIO getjob
case job of case job of
-- Exception is thrown when the MVar is garbage -- Exception is thrown when the MVar is garbage
-- collected, which means the whole DbHandle -- collected, which means the whole DbHandle
-- is not used any longer. Shutdown cleanly. -- is not used any longer. Shutdown cleanly.
Left BlockedIndefinitelyOnMVar -> return False Left BlockedIndefinitelyOnMVar -> return (return ())
Right CloseJob -> return False Right CloseJob -> return (return ())
Right (QueryJob a) -> a >> loop Right (QueryJob a) -> a >> loop
Right (ChangeJob a) -> do Right (ChangeJob a) -> do
a a
-- Exit this sqlite connection so the -- Exit this sqlite connection so the
-- database gets updated on disk. -- database gets updated on disk.
return True return newconn
-- Change is run in a separate database connection -- Change is run in a separate database connection
-- since sqlite only supports a single writer at a -- since sqlite only supports a single writer at a
-- time, and it may crash the database connection -- time, and it may crash the database connection
@ -169,7 +165,10 @@ workerThread db tablename jobs = go
-- change that was just written, using -- change that was just written, using
-- a different db handle, is immediately -- a different db handle, is immediately
-- visible to queries. -- visible to queries.
return True return newconn
getjob :: IO (Either BlockedIndefinitelyOnMVar Job)
getjob = try $ takeMVar jobs
-- Like runSqlite, but more robust. -- Like runSqlite, but more robust.
-- --