Skip to content

Commit

Permalink
Fix Some Math Errors and improve Efficiency in Getting Jobs from API
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Mar 13, 2024
1 parent a863cb1 commit a265719
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Shoko.Server/API/v3/Controllers/QueueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public async Task<ActionResult> Clear()

var offset = (page - 1) * pageSize;
// simplified from (page - 1) * pageSize + pageSize
if (page * pageSize <= _settingsProvider.GetSettings().Quartz.WaitingCacheSize)
if (page * pageSize <= _settingsProvider.GetSettings().Quartz.WaitingCacheSize && showAll)
{
var results = _queueHandler.GetExecutingJobs().Skip(offset).Take(pageSize).ToList();
if (pageSize - results.Count > 0)
Expand Down
27 changes: 15 additions & 12 deletions Shoko.Server/Scheduling/ThreadPooledJobStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,23 +408,21 @@ protected override async Task<TriggerFiredBundle> TriggerFired(ConnectionAndTran

public Task<int> GetWaitingTriggersCount()
{
return ExecuteInNonManagedTXLock(LockTriggerAccess, async conn => await GetWaitingTriggersCount(conn), new CancellationToken());
return ExecuteInNonManagedTXLock(LockTriggerAccess, async conn => await GetWaitingTriggersCount(conn, GetTypes()), new CancellationToken());
}

private Task<int> GetWaitingTriggersCount(ConnectionAndTransactionHolder conn, CancellationToken cancellationToken = new CancellationToken())
private Task<int> GetWaitingTriggersCount(ConnectionAndTransactionHolder conn, JobTypes types, CancellationToken cancellationToken = new CancellationToken())
{
var types = GetTypes();
return Delegate.SelectWaitingTriggerCount(conn, NoLaterThan, NoEarlierThan, types, cancellationToken);
}

public Task<int> GetBlockedTriggersCount()
{
return ExecuteInNonManagedTXLock(LockTriggerAccess, async conn => await GetBlockedTriggersCount(conn), new CancellationToken());
return ExecuteInNonManagedTXLock(LockTriggerAccess, async conn => await GetBlockedTriggersCount(conn, GetTypes()), new CancellationToken());
}

private Task<int> GetBlockedTriggersCount(ConnectionAndTransactionHolder conn, CancellationToken cancellationToken = new CancellationToken())
private Task<int> GetBlockedTriggersCount(ConnectionAndTransactionHolder conn, JobTypes types, CancellationToken cancellationToken = new CancellationToken())
{
var types = GetTypes();
return Delegate.SelectBlockedTriggerCount(conn, _typeLoadHelper, NoLaterThan, NoEarlierThan, types,
cancellationToken);
}
Expand Down Expand Up @@ -477,6 +475,7 @@ public Task<List<QueueItem>> GetJobs(int maxCount, int offset, bool excludeBlock
};
}).OrderBy(a => a.StartTime));
}
else offset -= _executingJobs.Count;
}

if (maxCount - result.Count <= 0) return result;
Expand Down Expand Up @@ -518,8 +517,9 @@ private async Task JobStoringQueueEvents(ConnectionAndTransactionHolder conn, IJ
{
try
{
var waitingTriggerCount = await GetWaitingTriggersCount(conn, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, cancellationToken);
var types = GetTypes();
var waitingTriggerCount = await GetWaitingTriggersCount(conn, types, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, types, cancellationToken);
if (_threadPoolSize == 0) _threadPoolSize = await GetThreadPoolSize(cancellationToken);
var executing = GetExecutingQueueItems();
var waiting = await GetJobs(conn, _settingsProvider.GetSettings().Quartz.WaitingCacheSize, _threadPoolSize, false, cancellationToken);
Expand All @@ -546,8 +546,9 @@ private async Task JobFiringQueueEvents(ConnectionAndTransactionHolder conn, IOp
try
{
lock(_executingJobs) _executingJobs[jobDetail.Key] = (jobDetail, trigger.StartTimeUtc.LocalDateTime);
var waitingTriggerCount = await GetWaitingTriggersCount(conn, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, cancellationToken);
var types = GetTypes();
var waitingTriggerCount = await GetWaitingTriggersCount(conn, types, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, types, cancellationToken);
if (_threadPoolSize == 0) _threadPoolSize = await GetThreadPoolSize(cancellationToken);
var executing = GetExecutingQueueItems();
var waiting = await GetJobs(conn, _settingsProvider.GetSettings().Quartz.WaitingCacheSize, _threadPoolSize, false, cancellationToken);
Expand Down Expand Up @@ -595,11 +596,13 @@ private async Task JobCompletedQueueEvents(ConnectionAndTransactionHolder conn,
{
// this runs before the states have been updated, so things that were blocked for concurrency are still blocked at this point
lock(_executingJobs) _executingJobs.Remove(jobDetail.Key);
var waitingTriggerCount = await GetWaitingTriggersCount(conn, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, cancellationToken);
var types = GetTypes();
var waitingTriggerCount = await GetWaitingTriggersCount(conn, types, cancellationToken);
var blockedTriggerCount = await GetBlockedTriggersCount(conn, types, cancellationToken);
if (_threadPoolSize == 0) _threadPoolSize = await GetThreadPoolSize(cancellationToken);
var executing = GetExecutingQueueItems();
var waiting = await GetJobs(conn, _settingsProvider.GetSettings().Quartz.WaitingCacheSize, _threadPoolSize, false, cancellationToken);

_queueStateEventHandler.OnJobCompleted(jobDetail, new QueueStateContext
{
ThreadCount = _threadPoolSize,
Expand Down

0 comments on commit a265719

Please sign in to comment.