Version v1.0 · dotnet
Example Tasks
Practical DurableStack job examples for common application workloads.
Example Tasks
These examples show common task shapes you can adapt quickly.
Email notification job (typed payload)
using DurableStack.Core;
using DurableStack.Core.Abstractions;
using DurableStack.Hosting.DependencyInjection;
[DurableJob(Name = "send-welcome-email", MaxAttempts = 5)]
public sealed class SendWelcomeEmailJob : IDurableJob<SendWelcomeEmailArgs>
{
private readonly ILogger<SendWelcomeEmailJob> _logger;
public SendWelcomeEmailJob(ILogger<SendWelcomeEmailJob> logger)
{
_logger = logger;
}
public Task ExecuteAsync(SendWelcomeEmailArgs args, JobContext context, CancellationToken cancellationToken)
{
_logger.LogInformation("Sending welcome email to {Email}. RunId={RunId}", args.Email, context.RunId);
return Task.CompletedTask;
}
}
public sealed class SendWelcomeEmailArgs
{
public string Email { get; set; } = string.Empty;
}
Enqueue:
var runId = await durableClient.EnqueueAsync<SendWelcomeEmailJob>(new SendWelcomeEmailArgs
{
Email = "[email protected]"
}, cancellationToken);
Recurring cleanup job
[DurableJob(Name = "cleanup-expired-sessions", MaxAttempts = 3)]
[RecurringJob("0 */1 * * *", TimeZone = "UTC", AllowConcurrentRuns = false)]
public sealed class CleanupExpiredSessionsJob : IDurableJob
{
public Task ExecuteAsync(JobContext context, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}
Third-party sync job with backoff
[DurableJob(
Name = "sync-third-party-orders",
MaxAttempts = 6,
RetryBehavior = RetryBehavior.Backoff,
RetryInitialDelaySeconds = 15)]
[RecurringJob("*/10 * * * *", TimeZone = "UTC", AllowConcurrentRuns = false)]
public sealed class ThirdPartyOrderSyncJob : IDurableJob
{
public async Task ExecuteAsync(JobContext context, CancellationToken cancellationToken = default)
{
await Task.Delay(TimeSpan.FromMilliseconds(50), cancellationToken);
}
}
Guidance
- Use typed jobs when payload shape is part of the contract.
- Prefer non-overlapping recurring jobs for mutable resources.
- Use retry backoff for external dependency instability.