implementation-patterns

Implements lease-based background processing, constraint-based duplicate detection, and streaming file exports in .NET.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill implementation-patterns-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: implementation-patterns
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/implementation-patterns
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill implementation-patterns-d1ssolve

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Background workers that claim tasks from a database can leave tasks stuck forever when pods are killed or the DB goes down, pre-check duplicate queries race under concurrency, and large file exports buffered in MemoryStream cause OOM kills. This Skill provides three concrete .NET patterns that solve each of these failure modes. ## Core Features & Use Cases - Lease-Based Background Processing: Claim DB tasks with FOR UPDATE SKIP LOCKED, renew a time-limited lease via heartbeat, and finalize with guarded LockToken checks so crashed workers' tasks are safely reclaimed. - Race-Free Duplicate Detection: Replace racy HasActive...() pre-checks with a unique filtered index plus a CommitResult contract that converts constraint violations into domain exceptions (HTTP 409). - Streaming File Export: Stream IAsyncEnumerable records to a temp file and upload to S3 as a Stream, keeping memory near zero for unbounded exports. - Use Case: A user triggers a large Excel export processed by a background worker; the worker claims the session with a lease, heartbeats during the S3 upload, and a duplicate export request is rejected atomically by the database constraint. ## Quick Start Ask the agent to implement a crash-safe background export worker using the lease-based processing pattern with heartbeat and guarded finalize.

Frequently Asked Questions about implementation-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make a background worker survive pod kills when processing DB tasks?

Use the lease-based processing pattern: claim tasks with FOR UPDATE SKIP LOCKED, set a LockToken and LockedUntil lease, renew it with a heartbeat every 30 seconds, and finalize only with a guarded update that verifies the LockToken. If the worker dies, the lease expires and another worker reclaims the task.

How to prevent duplicate active records in PostgreSQL without a pre-check query?

Create a unique filtered index on the columns defining an active duplicate, then insert directly and let the database reject conflicts. Handle the violation through a CommitResult contract that catches DbUpdateException and throws a domain exception mapped to HTTP 409.

How do I export large datasets to Excel without MemoryStream OOM in C#?

Stream records as IAsyncEnumerable directly to a temp file via an IReportWriter, then open the temp file as a FileStream and upload it to S3. Peak memory stays near zero regardless of export size, and the temp file is deleted after upload.

When should I not use the lease and heartbeat pattern for background jobs?

Skip it for sub-second tasks where a plain transaction suffices, for idempotent cheap tasks where simple retries work, and when you already have a message broker like RabbitMQ or Kafka with DLQ and redelivery, which handles this natively.

Why does my background worker create duplicate S3 files after a retry?

Random object keys like Guid.NewGuid() create a new orphan file on every retry. Use a deterministic key based on the session ID, such as exports/{sessionId}.xlsx, so retries overwrite the same object.