# JobShard Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/)

## ConsumeDurableJobsAsync {#consumedurablejobsasync-e2995956}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/consumedurablejobsasync-e2995956/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L195)

```csharp
public IAsyncEnumerable<IJobRunContext> ConsumeDurableJobsAsync()
```

Consumes durable jobs from this shard in order of their due time.

### Returns

An asynchronous enumerable of durable job contexts.

## DisposeAsync {#disposeasync-841c77d2}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/disposeasync-841c77d2/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L391-L392)

```csharp
public virtual ValueTask DisposeAsync()
```

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

### Returns

A task that represents the asynchronous dispose operation.

## EnqueueJob(DurableJob, int) {#enqueuejob-orleans-durable-jobs-durablejob-int-0628c877}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/enqueuejob-orleans-durable-jobs-durablejob-int-0628c877/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L357-L358)

```csharp
protected void EnqueueJob(DurableJob job, int dequeueCount)
```

Enqueues a job into the in-memory queue with the specified dequeue count.

### Parameters

- `job` (`DurableJob`): The job to enqueue.
- `dequeueCount` (`int`): The number of times this job has been dequeued.

## GetJobCountAsync {#getjobcountasync-acdd9055}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/getjobcountasync-acdd9055/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L190)

```csharp
public ValueTask<int> GetJobCountAsync()
```

Gets the number of jobs currently scheduled in this shard.

### Returns

A task that represents the asynchronous operation. The task result contains the job count.

## MarkAsCompleteAsync(CancellationToken) {#markascompleteasync-system-threading-cancellationtoken-b4716e2d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/markascompleteasync-system-threading-cancellationtoken-b4716e2d/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L282-L292)

```csharp
public Task MarkAsCompleteAsync(CancellationToken cancellationToken)
```

Marks this shard as complete, preventing new jobs from being scheduled.

### Parameters

- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task that represents the asynchronous operation.

## PersistAddJobAsync(DurableJob, CancellationToken) {#persistaddjobasync-orleans-durable-jobs-durablejob-system-threading-cancellation-8abccd5d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/persistaddjobasync-orleans-durable-jobs-durablejob-system-threading-cancellation-8abccd5d/)

```csharp
protected abstract Task PersistAddJobAsync(DurableJob job, CancellationToken cancellationToken)
```

Persists the addition of a new job to the underlying storage.

### Parameters

- `job` (`DurableJob`): The job to persist.
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task that represents the asynchronous operation.

## PersistRemoveJobAsync(string, CancellationToken) {#persistremovejobasync-string-system-threading-cancellationtoken-de511990}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/persistremovejobasync-string-system-threading-cancellationtoken-de511990/)

```csharp
protected abstract Task PersistRemoveJobAsync(string jobId, CancellationToken cancellationToken)
```

Persists the removal of a job from the underlying storage.

### Parameters

- `jobId` (`string`): The unique identifier of the job to remove.
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task that represents the asynchronous operation.

## PersistRetryJobAsync(IJobRunContext, DateTimeOffset, CancellationToken) {#persistretryjobasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-s-9eec6874}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/persistretryjobasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-s-9eec6874/)

```csharp
protected abstract Task PersistRetryJobAsync(IJobRunContext jobContext, DateTimeOffset newDueTime, CancellationToken cancellationToken)
```

Persists the rescheduling of a job to the underlying storage.

### Parameters

- `jobContext` (`IJobRunContext`): The execution context of the job to retry.
- `newDueTime` (`DateTimeOffset`): The new due time for the job. This value supersedes [DurableJob](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejob/) on [IJobRunContext](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.ijobruncontext/).
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task that represents the asynchronous operation.

## RemoveJobAsync(string, CancellationToken) {#removejobasync-string-system-threading-cancellationtoken-d35672b5}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/removejobasync-string-system-threading-cancellationtoken-d35672b5/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L260-L277)

```csharp
public Task<DurableJobMutationResult> RemoveJobAsync(string jobId, CancellationToken cancellationToken)
```

Removes a durable job from this shard.

### Parameters

- `jobId` (`string`): The unique identifier of the job to remove.
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task containing the durable mutation outcome.

## RescheduleJobAsync(IJobRunContext, DateTimeOffset, CancellationToken) {#reschedulejobasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-sys-d7e94701}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/reschedulejobasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-sys-d7e94701/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L325-L348)

```csharp
public Task<DurableJobMutationResult> RescheduleJobAsync(IJobRunContext jobContext, DateTimeOffset newDueTime, CancellationToken cancellationToken)
```

Reschedules a successfully completed execution with its dequeue count reset.

### Parameters

- `jobContext` (`IJobRunContext`): The context of the completed execution.
- `newDueTime` (`DateTimeOffset`): The new due time for the job. This value supersedes [DurableJob](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejob/) on [IJobRunContext](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.ijobruncontext/).
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task containing the durable mutation outcome.

## RetryJobLaterAsync(IJobRunContext, DateTimeOffset, CancellationToken) {#retryjoblaterasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-sys-4e8710cb}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/retryjoblaterasync-orleans-durable-jobs-ijobruncontext-system-datetimeoffset-sys-4e8710cb/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L300-L317)

```csharp
public Task<DurableJobMutationResult> RetryJobLaterAsync(IJobRunContext jobContext, DateTimeOffset newDueTime, CancellationToken cancellationToken)
```

Reschedules a job to be retried at a later time.

### Parameters

- `jobContext` (`IJobRunContext`): The context of the job to retry.
- `newDueTime` (`DateTimeOffset`): The new due time for the job. This value supersedes [DurableJob](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejob/) on [IJobRunContext](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.ijobruncontext/).
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task containing the durable mutation outcome.

## TryScheduleJobAsync(ScheduleJobRequest, CancellationToken) {#tryschedulejobasync-orleans-durable-jobs-schedulejobrequest-system-threading-can-3a8cb2a1}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/tryschedulejobasync-orleans-durable-jobs-schedulejobrequest-system-threading-can-3a8cb2a1/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L220-L255)

```csharp
public Task<DurableJob?> TryScheduleJobAsync(ScheduleJobRequest request, CancellationToken cancellationToken)
```

Attempts to schedule a new job on this shard.

### Parameters

- `request` (`ScheduleJobRequest`): The request containing the job scheduling parameters.
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task that represents the asynchronous operation. The task result contains the durable job if successful, or null if the job could not be scheduled (e.g., the shard was marked as complete).

### Exceptions

- `System.ArgumentOutOfRangeException`: Thrown when the due time is outside the shard's time range.

## TryStartAttemptAsync(IJobRunContext, CancellationToken) {#trystartattemptasync-orleans-durable-jobs-ijobruncontext-system-threading-cancel-8e800fc1}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.jobshard/methods/trystartattemptasync-orleans-durable-jobs-ijobruncontext-system-threading-cancel-8e800fc1/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/DurableJobs/JobShard.cs#L203-L215)

```csharp
public Task<DurableJobMutationResult> TryStartAttemptAsync(IJobRunContext jobContext, CancellationToken cancellationToken)
```

Attempts to reserve a dequeued durable job for execution.

### Parameters

- `jobContext` (`IJobRunContext`): The dequeued job context.
- `cancellationToken` (`CancellationToken`): A token to cancel the operation.

### Returns

A task containing [DurableJobMutationResult](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejobmutationresult/) when the attempt may start, [DurableJobMutationResult](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejobmutationresult/) when cancellation or completion removed the job first, or [DurableJobMutationResult](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.jobs.durablejobmutationresult/) when this shard is no longer locally owned.
