Skip to content

Commit

Permalink
Cleanup in ThreadPooledJobStore
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 11, 2024
1 parent 95e343a commit 3667002
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions Shoko.Server/Scheduling/ThreadPooledJobStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext
TimeSpan timeWindow,
CancellationToken cancellationToken = default)
{
if (timeWindow < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(timeWindow));
}
if (timeWindow < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeWindow));

var acquiredTriggers = new List<IOperableTrigger>();
var context = new TriggerAcquisitionContext();
Expand All @@ -65,11 +62,7 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext
.SelectTriggerToAcquire(conn, noLaterThan + timeWindow, MisfireTime, maxCount, typesToExclude, cancellationToken).ConfigureAwait(false);

// No trigger is ready to fire yet.
if (results.Count == 0)
{
return acquiredTriggers;
}

if (results.Count == 0) return acquiredTriggers;
var batchEnd = noLaterThan;

foreach (var result in results)
Expand All @@ -78,10 +71,7 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext

// If our trigger is no longer available, try a new one.
var nextTrigger = await RetrieveTrigger(conn, triggerKey, cancellationToken).ConfigureAwait(false);
if (nextTrigger == null)
{
continue; // next trigger
}
if (nextTrigger == null) continue; // next trigger

// If trigger's job is set as @DisallowConcurrentExecution, and it has already been added to result, then
// put it back into the timeTriggers set and continue to search for next trigger.
Expand Down Expand Up @@ -118,18 +108,13 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext
continue;
}

if (nextFireTimeUtc > batchEnd)
{
break;
}
if (nextFireTimeUtc > batchEnd) break;

// We now have a acquired trigger, let's add to return list.
// If our trigger was no longer in the expected state, try a new one.
var rowsUpdated = await Delegate.UpdateTriggerStateFromOtherStateWithNextFireTime(conn, triggerKey, StateAcquired, StateWaiting, nextFireTimeUtc.Value, cancellationToken).ConfigureAwait(false);
if (rowsUpdated <= 0)
{
continue; // next trigger
}
if (rowsUpdated <= 0) continue; // next trigger

nextTrigger.FireInstanceId = GetFiredTriggerRecordId();
await Delegate.InsertFiredTrigger(conn, nextTrigger, StateAcquired, null, cancellationToken).ConfigureAwait(false);

Expand All @@ -147,10 +132,7 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext

// if we didn't end up with any trigger to fire from that first
// batch, try again for another batch. We allow with a max retry count.
if (acquiredTriggers.Count == 0 && context.CurrentLoopCount < context.MaxDoLoopRetry)
{
continue;
}
if (acquiredTriggers.Count == 0 && context.CurrentLoopCount < context.MaxDoLoopRetry) continue;

// We are done with the while loop.
break;
Expand All @@ -168,10 +150,7 @@ protected override async Task<IReadOnlyCollection<IOperableTrigger>> AcquireNext
private Type[] GetTypesToExclude()
{
var result = new List<Type>();
foreach (var filter in _acquisitionFilters)
{
result.AddRange(filter.GetTypesToExclude());
}
foreach (var filter in _acquisitionFilters) result.AddRange(filter.GetTypesToExclude());

return result.Distinct().ToArray();
}
Expand Down

0 comments on commit 3667002

Please sign in to comment.