60 lines
3.5 KiB
Transact-SQL
60 lines
3.5 KiB
Transact-SQL
SET ANSI_NULLS ON;
|
|
SET QUOTED_IDENTIFIER ON;
|
|
GO
|
|
IF COL_LENGTH('dbo.LocationGeography','RelaxedGeneration') IS NULL
|
|
ALTER TABLE dbo.LocationGeography ADD RelaxedGeneration bigint NULL;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.Geography_Claim @Enabled bit AS
|
|
BEGIN
|
|
SET NOCOUNT ON; SET XACT_ABORT ON;
|
|
DECLARE @Now datetime2=SYSUTCDATETIME(),@Lease uniqueidentifier=NEWID();
|
|
;WITH job AS(SELECT TOP(1) * FROM dbo.LocationGeography WITH(UPDLOCK,READPAST,ROWLOCK)
|
|
WHERE (JobState='Queued' OR (JobState='Waiting configuration' AND @Enabled=1) OR (JobState='Processing' AND LeaseUntil<@Now))
|
|
AND NextAttempt<=@Now ORDER BY NextAttempt,LocationID)
|
|
UPDATE job SET JobState='Processing',Lease=@Lease,LeaseUntil=DATEADD(minute,2,@Now)
|
|
OUTPUT inserted.LocationID,inserted.Generation,inserted.Attempts,inserted.Lease,
|
|
CONVERT(bit,CASE WHEN inserted.RelaxedGeneration=inserted.Generation THEN 1 ELSE 0 END) RelaxedContext;
|
|
END;
|
|
GO
|
|
CREATE OR ALTER PROCEDURE dbo.Geography_Complete
|
|
@LocationID int,@Generation bigint,@Lease uniqueidentifier,@Status varchar(24),@JobState varchar(24),
|
|
@RequestKey char(64)=NULL,@CandidatesJson nvarchar(max)=NULL,@MatchJson nvarchar(max)=NULL,
|
|
@Message nvarchar(200)=NULL,@Failure bit=0,@DelaySeconds int=0,@RelaxContext bit=0
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON; SET XACT_ABORT ON;
|
|
BEGIN TRANSACTION;
|
|
DELETE dbo.GeographicLookupLease WHERE Lease=@Lease;
|
|
UPDATE dbo.LocationGeography SET Status=@Status,JobState=@JobState,RequestKey=@RequestKey,CandidatesJson=@CandidatesJson,
|
|
MatchJson=@MatchJson,Message=@Message,Attempts=Attempts+CONVERT(int,@Failure),
|
|
NextAttempt=DATEADD(second,@DelaySeconds,SYSUTCDATETIME()),Lease=NULL,LeaseUntil=NULL,
|
|
RelaxedGeneration=CASE WHEN @RelaxContext=1 THEN Generation ELSE RelaxedGeneration END
|
|
WHERE LocationID=@LocationID AND Generation=@Generation AND Lease=@Lease AND Confirmed=0;
|
|
IF @@ROWCOUNT=1 AND @RequestKey IS NOT NULL AND @CandidatesJson IS NOT NULL
|
|
BEGIN
|
|
UPDATE dbo.GeographicCache WITH(UPDLOCK,SERIALIZABLE) SET CandidatesJson=@CandidatesJson,ResolvedAt=SYSUTCDATETIME() WHERE RequestKey=@RequestKey;
|
|
IF @@ROWCOUNT=0 INSERT dbo.GeographicCache(RequestKey,CandidatesJson)VALUES(@RequestKey,@CandidatesJson);
|
|
END;
|
|
COMMIT;
|
|
END;
|
|
GO
|
|
-- Recovery reuses cached responses without charging the location allowance again.
|
|
CREATE OR ALTER PROCEDURE dbo.Geography_Retry @ProjectID int,@UserID int,@LocationID int,@Generation bigint,@ReplaceConfirmed bit=0 AS
|
|
BEGIN
|
|
SET NOCOUNT ON; SET XACT_ABORT ON;
|
|
EXEC dbo.StoryMap_Authorize @ProjectID,@UserID;
|
|
BEGIN TRANSACTION;
|
|
DECLARE @Key char(64),@Valid bit=0;
|
|
SELECT @Key=g.RequestKey,@Valid=1 FROM dbo.LocationGeography g WITH(UPDLOCK,HOLDLOCK) JOIN dbo.Locations l ON l.LocationID=g.LocationID
|
|
WHERE l.ProjectID=@ProjectID AND l.IsArchived=0 AND g.LocationID=@LocationID AND g.Generation=@Generation AND g.IsExcluded=0
|
|
AND (g.Confirmed=0 OR @ReplaceConfirmed=1) AND g.Status IN('Unresolved','Needs confirmation','Resolved');
|
|
IF @Valid=0 THROW 51000,'Retry is unavailable. Refresh the geographical status.',1;
|
|
DECLARE @Cost int=CASE WHEN EXISTS(SELECT 1 FROM dbo.GeographicCache WHERE RequestKey=@Key) THEN 0 ELSE 1 END;
|
|
EXEC dbo.Geography_BatchPermit @UserID,@Cost;
|
|
UPDATE dbo.LocationGeography SET Status='Pending',JobState='Queued',Attempts=0,NextAttempt=SYSUTCDATETIME(),Lease=NULL,LeaseUntil=NULL,
|
|
Generation=Generation+1,Confirmed=0,CandidatesJson=NULL,MatchJson=NULL,Message='Recovery lookup queued; cached responses will be reused.',LastRetry=SYSUTCDATETIME()
|
|
WHERE LocationID=@LocationID AND Generation=@Generation;
|
|
COMMIT;
|
|
END;
|
|
GO
|