How to Detect if uSync is running
Sometimes, you will want to check if uSync is running, or if the notification you are handling has been fired by uSync.
We generally recommend that you process events even when they have been fired by uSync, as they will need processing like a save or publish would from a user of your site.
Detecting via the uSyncEventService
The uSyncEventService is used internally to detect if a uSync import is in progeress.
You can inject the uSyncEventService into your own code, and then check to see if uSync event processing is currently paused.
public class MyContentPublishedHandler : INotificationHandler<ContentPublishedNotification>
{
private readonly uSyncEventService _uSyncEventService;
public MyContentPublishedHandler(uSyncEventService uSyncEventService)
{
_uSyncEventService = uSyncEventService;
}
public void Handle(ContentPublishedNotification notification)
{
// if uSync is paused don't process the event.
if (_uSyncEventService.IsPaused) return;
}
}
Detecting via Notification State (uSync v13.1+)
From v13.1 uSync sets some notification state values. You can use these to get details of where a notification came from.
uSync sets two notification states:
/// <summary>
/// a key we set on notifications, so you can tell if uSync processed them,
/// </summary>
public const string EventStateKey = "uSync.ProcessState";
/// <summary>
/// a key set on a notification to say uSync was paused while processing the item.
/// </summary>
public const string EventPausedKey = "uSync.PausedKey";
These keys can be checked in your notification handler.
if (notification.State.TryGetValue(uSync.EventStateKey, out var uSyncOrigin)
&& uSyncOrigin is true)
{
// notification was triggered by uSync
}
if (notification.State.TryGetValue(uSync.EventPausedKey, out var uSyncPaused)
&& uSyncPaused is true)
{
// notification was triggered by uSync, and uSync's own handlers where paused
}
uSync sets two events, because there are circumstances where uSync will import an item, but also want to process that import as a normal save. For example when content is pushed between sites using uSync.Complete, items that are imported are also exported to the default uSync folder so it can remain in sync.
from v13.1 using the notification states is recommended, because in v13.1 you can choose for notifications from uSync processes to be processed on background threads which will occur after uSync has completed any import and the uSyncEventService may no longer be paused.