Skip to content

Commit

Permalink
Query improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Jul 31, 2023
1 parent 2eb39c7 commit 5936399
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions NeuroSpeech.Eternity.SqlStorage/EternitySqlStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,28 @@ public async Task<List<EternityEntity>> DequeueAsync(int max, DateTimeOffset utc
var now = utcNow.UtcDateTime;
var futureLock = now.AddMinutes(1);
var query = TemplateQuery.New(@$"
SELECT TOP ({max}) *
SELECT TOP ({max}) nID
FROM [{schemaName}].[{tableName}]
WHERE
UtcETA < {now}
AND IsWorkflow=1
AND (QueueTTL IS NULL OR QueueTTL<{now})
ORDER BY Priority DESC");
var list = await db.FromSqlAsync<SqlEternityEntity>(query, ignoreUnmatchedProperties: true);
var rlist = new List<EternityEntity>();
var ids = new List<long>();
foreach(var item in list)
{
rlist.Add(item);
ids.Add(item.NID);
}
var ids = list.Select((x) => x.NID);
if (ids.Any())
{
await db.ExecuteNonQueryAsync(TemplateQuery.New(@$"
var result = await db.FromSqlAsync<SqlEternityEntity>(TemplateQuery.New(@$"
UPDATE [{schemaName}].[{tableName}]
SET
QueueTTL={futureLock}
OUTPUT INSERTED.*
WHERE
nID IN ({ids})
"));
"), ignoreUnmatchedProperties: true);
return result.OfType<EternityEntity>().OrderBy(x => x.UtcETA).ToList();
}
return rlist;
return new List<EternityEntity>();
}

public async Task<EternityEntity?> GetAsync(string? id)
Expand Down

0 comments on commit 5936399

Please sign in to comment.