Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ resulting log entries (which is up to you to implement), and then for each one,
calling the `ISyncService.SyncEntityIfEligible` method to process just that one
entity. This allows you to break down large synchronization operations into
smaller, manageable pieces, while still keeping track of the overall synchronization
state of the entity. Note that this currently will not consider eligibility
when determining whether to enqueue the entity, because that will be done when
the `SyncEntityIfEligible` method is called. This means that you can enqueue
entities that are not currently eligible for synchronization, and in case they
become eligible before the next sync run, they will be processed as part of that run.
state of the entity.

## Installation

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

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions Syncerbell/QueueBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Syncerbell;

/// <summary>
/// Specifies the behavior for determining which entities should be queued for sync processing.
/// </summary>
public enum QueueBehavior
{
/// <summary>
/// Queue all entities regardless of their eligibility status.
/// This will create sync entries for every registered entity without checking sync eligibility strategies.
/// </summary>
QueueAll,

/// <summary>
/// Queue only entities that are eligible for sync according to their configured eligibility strategies.
/// This respects sync eligibility rules such as interval-based strategies or custom eligibility logic.
/// </summary>
QueueEligibleOnly

}
18 changes: 18 additions & 0 deletions Syncerbell/SyncQueueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SyncQueueService(
{
/// <inheritdoc />
public async Task<IReadOnlyList<ISyncLogEntry>> CreateAllQueuedSyncEntries(SyncTriggerType syncTrigger,
QueueBehavior behavior = QueueBehavior.QueueEligibleOnly,
CancellationToken cancellationToken = default)
{
var entities = await entityResolver.ResolveEntities(cancellationToken);
Expand All @@ -38,6 +39,23 @@ public async Task<IReadOnlyList<ISyncLogEntry>> CreateAllQueuedSyncEntries(SyncT
continue;
}

// Check eligibility if behavior requires it
if (behavior == QueueBehavior.QueueEligibleOnly)
{
var trigger = new SyncTrigger
{
TriggerType = syncTrigger,
PriorSyncInfo = acquireResult.PriorSyncInfo
};

var isEligible = await entity.Eligibility.IsEligibleToSync(trigger, entity, cancellationToken);
if (!isEligible)
{
logger.LogDebug("Entity {EntityName} is not eligible for sync. Skipping queue creation.", entity.Entity);
continue;
}
}

// Mark the entry as queued
logEntry.SyncStatus = SyncStatus.Pending;
logEntry.QueuedAt = currentTime;
Expand Down