orchardcore-background-tasks
Scannednpx machina-cli add skill CrestApps/CrestApps.AgentSkills/orchardcore-background-tasks --openclawFiles (1)
SKILL.md
3.7 KB
Orchard Core Background Tasks - Prompt Templates
Create Background Tasks
You are an Orchard Core expert. Generate background task implementations for Orchard Core.
Guidelines
- Background tasks implement
IBackgroundTaskand run on a schedule. - Tasks are registered in
Startup.csusingAddBackgroundTask<T>(). - The schedule is configured using
SetSchedule()with cron expressions orTimeSpan. - Background tasks run in the context of the tenant's service scope.
- Use
ILoggerfor logging task execution and errors. - Tasks should be idempotent and handle concurrent execution gracefully.
- Always seal classes.
Basic Background Task
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundTasks;
[BackgroundTask(
Schedule = "*/15 * * * *",
Description = "{{TaskDescription}}")]
public sealed class {{TaskName}} : IBackgroundTask
{
private readonly ILogger<{{TaskName}}> _logger;
public {{TaskName}}(ILogger<{{TaskName}}> logger)
{
_logger = logger;
}
public Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
_logger.LogInformation("Running {{TaskName}}...");
// Task logic here
return Task.CompletedTask;
}
}
Background Task with Service Dependencies
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundTasks;
using OrchardCore.ContentManagement;
[BackgroundTask(
Schedule = "0 */6 * * *",
Description = "{{TaskDescription}}")]
public sealed class {{TaskName}} : IBackgroundTask
{
private readonly ILogger<{{TaskName}}> _logger;
public {{TaskName}}(ILogger<{{TaskName}}> logger)
{
_logger = logger;
}
public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
// Resolve services from the service provider
var contentManager = serviceProvider.GetRequiredService<IContentManager>();
var session = serviceProvider.GetRequiredService<YesSql.ISession>();
_logger.LogInformation("Running {{TaskName}}...");
// Example: query and process content items
var items = await session
.Query<ContentItem, ContentItemIndex>(x =>
x.ContentType == "{{ContentType}}" && x.Published)
.ListAsync();
foreach (var item in items)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
// Process item
}
_logger.LogInformation("{{TaskName}} completed. Processed {Count} items.", items.Count());
}
}
Registering a Background Task
using OrchardCore.BackgroundTasks;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddBackgroundTask<{{TaskName}}>();
}
}
Enabling Background Tasks Feature
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.BackgroundTasks"
],
"disable": []
}
]
}
Common Cron Schedule Expressions
* * * * *— Every minute.*/5 * * * *— Every 5 minutes.*/15 * * * *— Every 15 minutes.0 * * * *— Every hour.0 */6 * * *— Every 6 hours.0 0 * * *— Daily at midnight.0 0 * * 0— Weekly on Sunday at midnight.0 0 1 * *— Monthly on the 1st at midnight.
Source
git clone https://github.com/CrestApps/CrestApps.AgentSkills/blob/main/src/CrestApps.AgentSkills/orchardcore/orchardcore-background-tasks/SKILL.mdView on GitHub Overview
Provides a pattern for building background tasks in Orchard Core. Tasks implement IBackgroundTask, run on a schedule, and are registered via AddBackgroundTask. They execute within the tenant service scope and log progress with ILogger, while remaining idempotent and sealed.
How This Skill Works
Create a class that implements IBackgroundTask and annotate it with BackgroundTask and a Schedule. The task resolves services from the provided IServiceProvider in DoWorkAsync to perform work, then completes. The host executes it according to the configured cron or TimeSpan, and each run occurs inside the tenant scope.
When to Use It
- Regular maintenance tasks like cleanup or indexing on a cron schedule
- Processing or aggregating content items using services resolved from the tenant's service provider
- Long-running routines that must run in the tenant scope with proper cancellation handling
- Tasks that need to log progress and errors with ILogger
- Enabling and testing background tasks via the OrchardCore.BackgroundTasks feature
Quick Start
- Step 1: Create a sealed class that implements IBackgroundTask and decorate it with BackgroundTask, specifying a Schedule
- Step 2: Implement DoWorkAsync and resolve required services from serviceProvider inside the method
- Step 3: In Startup.ConfigureServices, register the task with services.AddBackgroundTask<YourTask>() and enable the BackgroundTasks feature
Best Practices
- Implement IBackgroundTask, seal the class, and decorate with the BackgroundTask attribute
- Register the task in Startup using services.AddBackgroundTask<YourTask>()
- Configure the schedule using the Schedule property or SetSchedule with cron expressions or TimeSpan
- Resolve dependencies from the provided IServiceProvider inside DoWorkAsync and use ILogger for logging
- Ensure idempotent execution and gracefully handle concurrent runs to avoid conflicts
Example Use Cases
- A basic task that logs when it runs on a 15-minute cron schedule
- A task that resolves IContentManager and YesSql.ISession to query and process published ContentItem entities
- Startup registration example: services.AddBackgroundTask<MyTask>()
- Feature enablement snippet showing how to enable OrchardCore.BackgroundTasks
- Common cron expressions reference such as running every 15 minutes or hourly
Frequently Asked Questions
Add this skill to your agents