Data subscriptions: Maintenance operations
-
After creating a subscription, your client can manage it using the document store's
Subscriptionsproperty, of typeDocumentSubscriptions. -
You can enable or disable a subscription, read its state, delete it, or drop worker connections.
-
In this article:
Deleting a subscription
Removing subscriptions from the database can be helpful when, for example, many single-use subscriptions have accumulated and managing them becomes hard.
void Delete(string name, string database = null);
Task DeleteAsync(string name, string database = null, CancellationToken token = default);
Usage:
store.Subscriptions.Delete(subscriptionName);
Enabling and disabling a subscription
Disable a subscription to stop it from accepting worker connections, or enable it to resume.
A disabled subscription keeps its definition and progress; only its ability to connect is shut down.
void Disable(string name, string database = null);
Task DisableAsync(string name, string database = null, CancellationToken token = default);
void Enable(string name, string database = null);
Task EnableAsync(string name, string database = null, CancellationToken token = default);
Usage:
store.Subscriptions.Disable(subscriptionName);
store.Subscriptions.Enable(subscriptionName);
Dropping connections
Dropping all connections
Drop a subscription's active connections using DropConnection, passing the subscription name.
-
Dropping a connection ends the worker's
Runtask, but leaves the subscription enabled.
Once dropped, the worker will not reconnect on its own. -
To resume, create a new worker and run it.
The new worker continues processing after the last batch the previous worker acknowledged.If the connection was dropped before the previous worker acknowledged a batch, the server sends the batch again to the next worker that connects to the subscription, so the batch's documents may be processed more than once (see At-least-once delivery).
-
To stop the subscription from accepting workers, disable it.
void DropConnection(string name, string database = null);
Task DropConnectionAsync(string name, string database = null, CancellationToken token = default);
Usage:
store.Subscriptions.DropConnection(subscriptionName);
Dropping a specific worker's connection
To drop the connection of a specific worker, rather than all the subscription's connections, pass the worker object to DropSubscriptionWorker.
This is useful with concurrent subscriptions, where several workers are connected at the same time.
When the subscription runs in single-worker mode, only one worker is connected at a time, so dropping this worker's connection has the same effect as dropping all connections.
Listing subscriptions
Retrieve the database's subscriptions using GetSubscriptions.
The method returns a List<SubscriptionState>, one entry per subscription, each holding the state
described in Getting a subscription state below.
Use start to set how many subscriptions to skip and take to set the maximum number to return.
List<SubscriptionState> GetSubscriptions(int start, int take, string database = null);
Task<List<SubscriptionState>> GetSubscriptionsAsync(int start, int take,
string database = null, CancellationToken token = default);
Usage:
// List the first 10 subscriptions
List<SubscriptionState> firstSubscriptions = store.Subscriptions.GetSubscriptions(0, 10);
// List all subscriptions
List<SubscriptionState> allSubscriptions = store.Subscriptions.GetSubscriptions(0, int.MaxValue);
Getting a subscription state
To retrieve the state of a single subscription from the server use GetSubscriptionState, passing it the subscription's name.
The returned SubscriptionState lets you inspect the subscription. For example, read
LastBatchAckTime for the time the server last acknowledged the worker's progress,
ChangeVectorForNextBatchStartingPoint for the point the next batch will start from, or Disabled to check
whether the subscription currently accepts workers.
SubscriptionState GetSubscriptionState(string subscriptionName, string database = null);
Task<SubscriptionState> GetSubscriptionStateAsync(string subscriptionName,
string database = null, CancellationToken token = default);
Usage:
SubscriptionState subscriptionState = store.Subscriptions.GetSubscriptionState(subscriptionName);
The returned SubscriptionState object includes, among others, the following properties:
| Property | Type | Description |
|---|---|---|
| Query | string | The subscription's RQL query. |
| SubscriptionName | string | The subscription's name, which is also its unique identifier. |
| SubscriptionId | long | The subscription's internal identifier, assigned by the cluster at creation. |
| MentorNode | string | The mentor node, if one was set manually. |
| PinToMentorNode | bool | true when the subscription is pinned to the mentor node, so another node does not take it over. |
| NodeTag | string | The tag of the server node processing the subscription. |
| ChangeVectorForNextBatchStartingPoint | string | The change vector from which the subscription will begin sending documents to the worker. Updated on batch acknowledgment, and can also be set manually. |
| LastBatchAckTime | DateTime? | The last time the server acknowledged progress on the subscription's documents. |
| LastClientConnectionTime | DateTime? | The last time a client connected (retained after the connection ends). |
| Disabled | bool | When true, the subscription does not allow workers to connect. |
| ArchivedDataProcessingBehavior | ArchivedDataProcessingBehavior? | The subscription's handling of archived documents. See Archived documents. |
| ShardingState | SubscriptionShardingState | The subscription's state on each shard of a sharded database (e.g., the change vector from which each shard will start its next batch, and the node that processes the subscription on each shard).null when the database is not sharded. |
If no subscription with the given name exists, GetSubscriptionState throws a SubscriptionDoesNotExistException.
To check whether a subscription exists (e.g., before your application connects a worker to the subscription), call
GetSubscriptionState and catch this exception.
try
{
store.Subscriptions.GetSubscriptionState("OrdersShippedToUK");
// The subscription exists
}
catch (SubscriptionDoesNotExistException)
{
// No subscription named "OrdersShippedToUK" exists
}
Updating a subscription
Change the definition of an existing subscription using Update / UpdateAsync.
See Updating a subscription
on the Creating a data subscription page.