1- module Share.BackgroundJobs.Diffs.ContributionDiffs (worker ) where
1+ module Share.BackgroundJobs.Diffs.CausalDiffs (worker ) where
22
33import Control.Lens
4- import Control.Monad.Except
54import Ki.Unlifted qualified as Ki
65import Share.BackgroundJobs.Diffs.Queries qualified as DQ
6+ import Share.BackgroundJobs.Diffs.Types (CausalDiffInfo (.. ))
77import Share.BackgroundJobs.Errors (reportError )
8- import Share.BackgroundJobs.Monad (Background , withTag )
8+ import Share.BackgroundJobs.Monad (Background , withTags )
99import Share.BackgroundJobs.Workers (newWorker )
10- import Share.Branch (branchCausals_ )
1110import Share.Codebase qualified as Codebase
12- import Share.Contribution (Contribution (.. ))
1311import Share.Env qualified as Env
14- import Share.IDs
1512import Share.IDs qualified as IDs
1613import Share.Metrics qualified as Metrics
1714import Share.Postgres qualified as PG
15+ import Share.Postgres.Causal.Queries qualified as CausalQ
1816import Share.Postgres.Contributions.Queries qualified as ContributionsQ
1917import Share.Postgres.Notifications qualified as Notif
20- import Share.Postgres.Queries qualified as Q
2118import Share.Prelude
2219import Share.Utils.Logging qualified as Logging
2320import Share.Web.Authorization qualified as AuthZ
24- import Share.Web.Errors (EntityMissing (.. ), ErrorID ( .. ) )
21+ import Share.Web.Errors (EntityMissing (.. ))
2522import Share.Web.Share.Diffs.Impl qualified as Diffs
2623import System.Clock qualified as Clock
2724
@@ -39,8 +36,8 @@ worker scope = do
3936 makeRuntime codebase = do
4037 runtime <- Codebase. codebaseRuntimeTransaction unisonRuntime codebase
4138 pure (badUnliftCodebaseRuntime runtime)
42- newWorker scope " diffs:contributions " $ forever do
43- Notif. waitOnChannel Notif. ContributionDiffChannel (maxPollingIntervalSeconds * 1000000 )
39+ newWorker scope " causal- diffs" $ forever do
40+ Notif. waitOnChannel Notif. CausalDiffChannel (maxPollingIntervalSeconds * 1000000 )
4441 processDiffs authZReceipt makeRuntime
4542
4643-- Process diffs until we run out of them. We claim a diff in a transaction and compute the diff in the same
@@ -52,20 +49,26 @@ processDiffs authZReceipt makeRuntime = do
5249 loop = do
5350 result <-
5451 PG. runTransactionMode PG. RepeatableRead PG. ReadWrite do
55- DQ. claimContributionToDiff >>= \ case
52+ DQ. claimCausalDiff >>= \ case
5653 Nothing -> pure Nothing
57- Just contributionId -> do
54+ Just causalDiffInfo -> do
5855 startTime <- PG. transactionUnsafeIO (Clock. getTime Clock. Monotonic )
59- result <- PG. catchTransaction (maybeComputeAndStoreCausalDiff authZReceipt makeRuntime contributionId)
60- pure (Just (contributionId, startTime, result))
61- whenJust result \ (contributionId, startTime, result) -> do
62- withTag " contribution-id" (IDs. toText contributionId) do
56+ result <- PG. catchTransaction (maybeComputeAndStoreCausalDiff authZReceipt makeRuntime causalDiffInfo)
57+ pure (Just (causalDiffInfo, startTime, result))
58+ whenJust result \ (CausalDiffInfo {fromCausalId, toCausalId, fromCodebaseOwner, toCodebaseOwner}, startTime, result) -> do
59+ let tags =
60+ [ (" from-causal-id" , IDs. toText fromCausalId),
61+ (" to-causal-id" , IDs. toText toCausalId),
62+ (" from-codebase-owner" , IDs. toText fromCodebaseOwner),
63+ (" to-codebase-owner" , IDs. toText toCodebaseOwner)
64+ ]
65+ withTags tags do
6366 case result of
6467 Left err -> reportError err
6568 Right didWork -> do
6669 when didWork do
67- liftIO (Metrics. recordContributionDiffDuration startTime)
68- Logging. textLog " Computed contribution diff"
70+ liftIO (Metrics. recordCausalDiffDuration startTime)
71+ Logging. textLog " Computed causal diff"
6972 & Logging. withSeverity Logging. Info
7073 & Logging. logMsg
7174 loop
@@ -76,27 +79,21 @@ processDiffs authZReceipt makeRuntime = do
7679maybeComputeAndStoreCausalDiff ::
7780 AuthZ. AuthZReceipt ->
7881 (Codebase. CodebaseEnv -> IO (Codebase. CodebaseRuntime IO )) ->
79- ContributionId ->
82+ CausalDiffInfo ->
8083 PG. Transaction EntityMissing Bool
81- maybeComputeAndStoreCausalDiff authZReceipt makeRuntime contributionId = do
82- Contribution {bestCommonAncestorCausalId, sourceBranchId = newBranchId, targetBranchId = oldBranchId, projectId} <-
83- ContributionsQ. contributionById contributionId
84- project <- Q. projectById projectId `whenNothingM` throwError (EntityMissing (ErrorID " project:missing" ) " Project not found" )
85- newBranch <- Q. branchById newBranchId `whenNothingM` throwError (EntityMissing (ErrorID " branch:missing" ) " Source branch not found" )
86- oldBranch <- Q. branchById oldBranchId `whenNothingM` throwError (EntityMissing (ErrorID " branch:missing" ) " Target branch not found" )
87- let oldCodebase = Codebase. codebaseForProjectBranch authZReceipt project oldBranch
88- let newCodebase = Codebase. codebaseForProjectBranch authZReceipt project newBranch
89- let oldCausal = oldBranch ^. branchCausals_
90- let newCausal = newBranch ^. branchCausals_
91- ContributionsQ. existsPrecomputedNamespaceDiff (oldCodebase, oldCausal) (newCodebase, newCausal) >>= \ case
84+ maybeComputeAndStoreCausalDiff authZReceipt makeRuntime (CausalDiffInfo {fromCausalId, toCausalId, fromCodebaseOwner, toCodebaseOwner}) = do
85+ bestCommonAncestorCausalId <- CausalQ. bestCommonAncestor fromCausalId toCausalId
86+ let fromCodebase = Codebase. codebaseEnv authZReceipt $ Codebase. codebaseLocationForUserCodebase fromCodebaseOwner
87+ let toCodebase = Codebase. codebaseEnv authZReceipt $ Codebase. codebaseLocationForUserCodebase toCodebaseOwner
88+ ContributionsQ. existsPrecomputedNamespaceDiff (fromCodebase, fromCausalId) (toCodebase, toCausalId) >>= \ case
9289 True -> pure False
9390 False -> do
94- oldRuntime <- PG. transactionUnsafeIO (makeRuntime oldCodebase )
95- newRuntime <- PG. transactionUnsafeIO (makeRuntime newCodebase )
91+ fromRuntime <- PG. transactionUnsafeIO (makeRuntime fromCodebase )
92+ toRuntime <- PG. transactionUnsafeIO (makeRuntime toCodebase )
9693 _ <-
9794 Diffs. computeAndStoreCausalDiff
9895 authZReceipt
99- (oldCodebase, oldRuntime, oldCausal )
100- (newCodebase, newRuntime, newCausal )
96+ (fromCodebase, fromRuntime, fromCausalId )
97+ (toCodebase, toRuntime, toCausalId )
10198 bestCommonAncestorCausalId
10299 pure True
0 commit comments