@@ -25,6 +25,7 @@ import Share.Web.Authorization qualified as AuthZ
2525import Share.Web.Errors (EntityMissing (.. ))
2626import Share.Web.Share.Diffs.Impl qualified as Diffs
2727import System.Clock qualified as Clock
28+ import UnliftIO qualified
2829
2930-- | Check every 10 minutes if we haven't heard on the notifications channel.
3031-- Just in case we missed a notification.
@@ -50,19 +51,32 @@ processDiffs authZReceipt unisonRuntime = do
5051
5152-- | Process a diff, then return whether or not we did any work.
5253processDiff :: AuthZ. AuthZReceipt -> CR. UnisonRuntime -> Background Bool
53- processDiff authZReceipt unisonRuntime = do
54- result <- Trace. withSpan " background:causal-diffs:process-diff" mempty $
55- PG. runTransactionMode PG. RepeatableRead PG. ReadWrite do
56- DQ. claimCausalDiff >>= \ case
57- Nothing -> pure Nothing
58- Just causalDiffInfo -> withTags (causalDiffTags causalDiffInfo) do
59- startTime <- PG. transactionUnsafeIO (Clock. getTime Clock. Monotonic )
60- result <- PG. catchTransaction (maybeComputeAndStoreCausalDiff authZReceipt unisonRuntime causalDiffInfo)
61- DQ. deleteClaimedCausalDiff causalDiffInfo
62- pure (Just (causalDiffInfo, startTime, result))
54+ processDiff authZReceipt unisonRuntime = Trace. withSpan " background:causal-diffs:process-diff" mempty $ do
55+ pendingCausalDiffVar <- liftIO $ UnliftIO. newEmptyMVar
56+ result <- UnliftIO. tryAny $ PG. runTransactionMode PG. RepeatableRead PG. ReadWrite do
57+ DQ. claimCausalDiff >>= \ case
58+ Nothing -> pure Nothing
59+ Just causalDiffInfo -> withTags (causalDiffTags causalDiffInfo) do
60+ PG. transactionUnsafeIO $ UnliftIO. tryPutMVar pendingCausalDiffVar causalDiffInfo
61+ startTime <- PG. transactionUnsafeIO (Clock. getTime Clock. Monotonic )
62+ result <- PG. catchTransaction (maybeComputeAndStoreCausalDiff authZReceipt unisonRuntime causalDiffInfo)
63+ DQ. deleteClaimedCausalDiff causalDiffInfo
64+ pure (Just (causalDiffInfo, startTime, result))
6365 case result of
64- Nothing -> pure False
65- Just (cdi, startTime, result) -> do
66+ -- The transaction failed with an exception.
67+ -- One possible cause is an unknown builtin.
68+ -- We should report it and mark it as invalid so we don't keep retrying it.
69+ Left err -> do
70+ mCausalDiffInfo <- liftIO $ UnliftIO. tryTakeMVar pendingCausalDiffVar
71+ case mCausalDiffInfo of
72+ Nothing -> pure ()
73+ Just cdi -> withTags (causalDiffTags cdi) do
74+ reportError err
75+ PG. runTransaction $ DQ. markCausalDiffInvalid (tShow err) cdi
76+ -- Continue processing other diffs.
77+ pure True
78+ Right Nothing -> pure False
79+ Right (Just (cdi, startTime, result)) -> do
6680 let tags = causalDiffTags cdi
6781 withTags tags do
6882 case result of
0 commit comments