Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions hasheous-lib/Classes/TaskManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ ORDER BY tq.create_time ASC
LIMIT 1
FOR UPDATE SKIP LOCKED;";

// Use a subquery-based UPDATE that will atomically update the row we just locked
// UPDATE using the same logic to identify the task
// This ensures we're updating the same row that the SELECT would return
string updateSql = @"UPDATE Task_Queue
SET client_id = @client_id,
status = @status,
Expand Down Expand Up @@ -322,22 +323,23 @@ FOR UPDATE SKIP LOCKED
) AS subquery
);";

// Execute both in transaction: SELECT first to lock the row, then UPDATE
var transactionCommands = new List<Database.SQLTransactionItem>
{
new Database.SQLTransactionItem(selectSql, new Dictionary<string, object>
{
{ "@client_id", client.Id }
}),
new Database.SQLTransactionItem(updateSql, new Dictionary<string, object>
{
{ "@client_id", client.Id },
{ "@status", (int)QueueItemStatus.Assigned },
{ "@start_time", now },
{ "@completion_time", now }
}),
new Database.SQLTransactionItem(selectSql, new Dictionary<string, object>
{
{ "@client_id", client.Id }
})
};

// Execute both commands in a single transaction
// Execute transaction with SELECT first, then UPDATE
DataTable dt = await db.ExecuteTransactionCMDAsync(transactionCommands);

if (dt.Rows.Count == 0)
Expand Down
Loading