1: CREATE PROCEDURE [dbo].[proc_GetRunnableWorkItems] (
2: @ProcessingId uniqueidentifier,
3: @SiteId uniqueidentifier,
4: @WorkItemType uniqueidentifier,
5: @BatchId uniqueidentifier,
6: @MaxFetchSize int = 1000,
7: @ThrottleThreshold int = 0
8: )
9: AS
10: SET NOCOUNT ON
11: IF (dbo.fn_IsOverQuotaOrWriteLocked(@SiteId) >= 1)
12: BEGIN
13: RETURN 0
14: END
15: DECLARE @iRet int
16: SET @iRet = 0
17: DECLARE @oldTranCount int
18: SET @oldTranCount = @@TRANCOUNT
19: DECLARE @Now datetime
20: SET @Now = dbo.fn_RoundDateToNearestSecond(GETUTCDATE())
21: DECLARE @InProgressCount int
22: DECLARE @ThrottledFetch int
23: DECLARE @ReturnWorkItems bit
24: SET @ReturnWorkItems = 0
25: BEGIN TRAN
26: SET @InProgressCount = 0
27: SET @ThrottledFetch = 0
28: SET @ThrottleThreshold = @ThrottleThreshold + 1
29: IF @ThrottleThreshold > 1
30: BEGIN
31: SET ROWCOUNT @ThrottleThreshold
32: SELECT
33: @InProgressCount = COUNT(DISTINCT BatchId)
34: FROM
35: dbo.ScheduledWorkItems WITH (NOLOCK)
36: WHERE
37: Type = @WorkItemType AND
38: DeliveryDate <= @Now AND
39: (InternalState & (1 | 16)) = (1 | 16)
40: END
41: IF @BatchId IS NOT NULL
42: BEGIN
43: SET @ThrottledFetch = 16
44: END
45: IF @InProgressCount < @ThrottleThreshold
46: BEGIN
47: SET ROWCOUNT @MaxFetchSize
48: UPDATE
49: dbo.ScheduledWorkItems
50: SET
51: InternalState = InternalState | 1 | @ThrottledFetch,
52: ProcessingId = @ProcessingId
53: WHERE
54: Type = @WorkItemType AND
55: DeliveryDate <= @Now AND
56: (@SiteId IS NULL OR
57: SiteId = @SiteId) AND
58: (@BatchId IS NULL OR
59: BatchId = @BatchId) AND
60: (InternalState & ((1 | 2))) = 0
61: SET @InProgressCount = @@ROWCOUNT
62: SET ROWCOUNT 0
63: IF @InProgressCount <> 0
64: BEGIN
65: EXEC @iRet = proc_AddFailOver @ProcessingId, NULL, NULL, 20, 0
66: END
67: SET @ReturnWorkItems = 1
68: END
69: CLEANUP:
70: SET ROWCOUNT 0
71: IF @iRet <> 0
72: BEGIN
73: IF @@TRANCOUNT = @oldTranCount + 1
74: BEGIN
75: ROLLBACK TRAN
76: END
77: END
78: ELSE
79: BEGIN
80: COMMIT TRAN
81: IF @InProgressCount <> 0
82: AND @InProgressCount <> @MaxFetchSize
83: AND @WorkItemType = 'BDEADF09-C265-11d0-BCED-00A0C90AB50F'
84: AND @BatchId IS NOT NULL AND @SiteId IS NOT NULL
85: BEGIN
86: UPDATE
87: dbo.Workflow
88: SET
89: InternalState = InternalState & ~(1024)
90: WHERE
91: SiteId = @SiteId AND
92: Id = @BatchId
93: END
94: IF @ReturnWorkItems = 1
95: BEGIN
96: SELECT ALL
97: DeliveryDate, Type, ProcessMachineId as SubType, Id,
98: SiteId, ParentId, ItemId, BatchId, ItemGuid, WebId,
99: UserId, Created, BinaryPayload, TextPayload, InternalState
100: FROM
101: dbo.ScheduledWorkItems
102: WHERE
103: Type = @WorkItemType AND
104: DeliveryDate <= @Now AND
105: ProcessingId = @ProcessingId
106: ORDER BY
107: Created
108: IF @@ROWCOUNT <> 0
109: BEGIN
110: EXEC @iRet = proc_UpdateFailOver @ProcessingId, NULL, 20
111: END
112: END
113: END
114: RETURN @iRet