Skip to content

Commit 96eb51d

Browse files
authored
Add ability to specify enqueueing behavior (#13)
1 parent 0513b46 commit 96eb51d

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ resulting log entries (which is up to you to implement), and then for each one,
6868
calling the `ISyncService.SyncEntityIfEligible` method to process just that one
6969
entity. This allows you to break down large synchronization operations into
7070
smaller, manageable pieces, while still keeping track of the overall synchronization
71-
state of the entity. Note that this currently will not consider eligibility
72-
when determining whether to enqueue the entity, because that will be done when
73-
the `SyncEntityIfEligible` method is called. This means that you can enqueue
74-
entities that are not currently eligible for synchronization, and in case they
75-
become eligible before the next sync run, they will be processed as part of that run.
71+
state of the entity.
7672

7773
## Installation
7874

Syncerbell/ISyncQueueService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public interface ISyncQueueService
1717
/// details in the sync log.
1818
/// </remarks>
1919
/// <param name="syncTrigger">The type of trigger that initiated this sync operation (e.g., manual, timer).</param>
20+
/// <param name="behavior">Specifies whether to queue all entities or only those that are eligible for sync.</param>
2021
/// <param name="cancellationToken">A cancellation token to cancel the operation if needed.</param>
2122
/// <returns>
2223
/// A read-only list of sync log entries that were created and queued for processing.
2324
/// Each entry represents a sync operation that needs to be performed by a distributed worker.
2425
/// </returns>
2526
/// <exception cref="OperationCanceledException">Thrown when the operation is cancelled via the cancellation token.</exception>
2627
Task<IReadOnlyList<ISyncLogEntry>> CreateAllQueuedSyncEntries(SyncTriggerType syncTrigger,
28+
QueueBehavior behavior = QueueBehavior.QueueEligibleOnly,
2729
CancellationToken cancellationToken = default);
2830

2931
/// <summary>

Syncerbell/QueueBehavior.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Syncerbell;
2+
3+
/// <summary>
4+
/// Specifies the behavior for determining which entities should be queued for sync processing.
5+
/// </summary>
6+
public enum QueueBehavior
7+
{
8+
/// <summary>
9+
/// Queue all entities regardless of their eligibility status.
10+
/// This will create sync entries for every registered entity without checking sync eligibility strategies.
11+
/// </summary>
12+
QueueAll,
13+
14+
/// <summary>
15+
/// Queue only entities that are eligible for sync according to their configured eligibility strategies.
16+
/// This respects sync eligibility rules such as interval-based strategies or custom eligibility logic.
17+
/// </summary>
18+
QueueEligibleOnly
19+
20+
}

Syncerbell/SyncQueueService.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SyncQueueService(
1313
{
1414
/// <inheritdoc />
1515
public async Task<IReadOnlyList<ISyncLogEntry>> CreateAllQueuedSyncEntries(SyncTriggerType syncTrigger,
16+
QueueBehavior behavior = QueueBehavior.QueueEligibleOnly,
1617
CancellationToken cancellationToken = default)
1718
{
1819
var entities = await entityResolver.ResolveEntities(cancellationToken);
@@ -38,6 +39,23 @@ public async Task<IReadOnlyList<ISyncLogEntry>> CreateAllQueuedSyncEntries(SyncT
3839
continue;
3940
}
4041

42+
// Check eligibility if behavior requires it
43+
if (behavior == QueueBehavior.QueueEligibleOnly)
44+
{
45+
var trigger = new SyncTrigger
46+
{
47+
TriggerType = syncTrigger,
48+
PriorSyncInfo = acquireResult.PriorSyncInfo
49+
};
50+
51+
var isEligible = await entity.Eligibility.IsEligibleToSync(trigger, entity, cancellationToken);
52+
if (!isEligible)
53+
{
54+
logger.LogDebug("Entity {EntityName} is not eligible for sync. Skipping queue creation.", entity.Entity);
55+
continue;
56+
}
57+
}
58+
4159
// Mark the entry as queued
4260
logEntry.SyncStatus = SyncStatus.Pending;
4361
logEntry.QueuedAt = currentTime;

0 commit comments

Comments
 (0)