Skip to main content

Changes API: How to Subscribe to Operation Changes

Subscribe to operation changes using ForOperationId or ForAllOperations.

The Subscribe method on the observable these methods return accepts only an IObserver object by default.
To instead subscribe with a delegate, like the change => ... lambda used in the examples below, add the System.Reactive package to your project.
This adds Subscribe overloads that take delegates, including ones with error and completion handlers.

ForOperationId​

Operation changes for one operation can be observed using the ForOperationId method.

Please note that from RavenDB 6.2 on, operation changes can be tracked only on a specific node.
The purpose of this change is to improve results consistency, as an operation may behave very differently on different nodes and cross-cluster tracking of an operation may become confusing and ineffective if the operation fails over from one node to another.
Tracking operations will therefore be possible only if the Changes API was opened using a method that limits tracking to a single node: store.Changes(dbName, nodeTag)

ForOperationId syntax​

IChangesObservable<OperationStatusChange> ForOperationId(long operationId);

Parameters
operationIdlongID of an operation for which notifications will be processed.
Return Value
IChangesObservable<OperationStatusChange>Observable that allows you to add subscriptions to notifications for an operation with a given ID.

Example​

IDisposable subscription = store
.Changes(dbName, nodeTag)
.ForOperationId(operationId)
.Subscribe(
change =>
{
switch (change.State.Status)
{
case OperationStatus.InProgress:
//Do Something
break;
case OperationStatus.Completed:
//Do Something
break;
case OperationStatus.Faulted:
//Do Something
break;
case OperationStatus.Canceled:
//Do Something
break;
default:
throw new ArgumentOutOfRangeException();
}
});

ForAllOperations​

Operations changes for all Operations can be observed using the ForAllOperations method.

Please note that from RavenDB 6.2 on, operation changes can be tracked only on a specific node.
The purpose of this change is to improve results consistency, as an operation may behave very differently on different nodes and cross-cluster tracking of an operation may become confusing and ineffective if the operation fails over from one node to another.
Tracking operations will therefore be possible only if the Changes API was opened using a method that limits tracking to a single node: store.Changes(dbName, nodeTag)

ForAllOperations syntax​

IChangesObservable<OperationStatusChange> ForAllOperations();

Return Value
IChangesObservable<OperationStatusChange>Observable that allows to add subscriptions to notifications for all operations.

Example​

IDisposable subscription = store
.Changes(dbName, nodeTag)
.ForAllOperations()
.Subscribe(change => Console.WriteLine("Operation #{1} reports progress: {0}", change.State.Progress.ToJson(), change.OperationId));

Syntax​

The notification object delivered to subscribers when an operation changes.

class OperationStatusChange
{
long OperationId
OperationState State
}

PropertyTypeDescription
OperationIdlongOperation ID
StateOperationStateOperation state
interface IOperationResult
{
string Message
bool ShouldPersist
bool CanMerge
}

PropertyTypeDescription
MessagestringOperation message
ShouldPersistbooldetermine whether or not the result should be saved in the storage
CanMergeboolWhether this result can be merged with another operation result

In this article