diff --git a/docs/en/solution-templates/single-layer-web-application/background-workers.md b/docs/en/solution-templates/single-layer-web-application/background-workers.md index a41f795c88..6b9a3b3dc1 100644 --- a/docs/en/solution-templates/single-layer-web-application/background-workers.md +++ b/docs/en/solution-templates/single-layer-web-application/background-workers.md @@ -16,4 +16,48 @@ Background workers are long-running processes that run in the background of your application. They are useful for tasks that are not time-sensitive, such as processing data, sending notifications, or monitoring system health. Background workers are typically started when the application starts and run continuously until the application stops. You can learn more about background workers in the [Background Workers](../../framework/infrastructure/background-workers/index.md) document. +Basically, you can create scheduled workers for a specific time interval based on your requirements, such as checking the status of inactive users and changing their status to passive if they have not logged in to the application in the last 30 days. + +```csharp +public class PassiveUserCheckerWorker : AsyncPeriodicBackgroundWorkerBase +{ + public PassiveUserCheckerWorker( + AbpAsyncTimer timer, + IServiceScopeFactory serviceScopeFactory) : base( + timer, + serviceScopeFactory) + { + Timer.Period = 600000; //10 minutes + } + + protected async override Task DoWorkAsync( + PeriodicBackgroundWorkerContext workerContext) + { + Logger.LogInformation("Starting: Setting status of inactive users..."); + + // Resolve dependencies + var userRepository = workerContext + .ServiceProvider + .GetRequiredService(); + + // Do the work + await userRepository.UpdateInactiveUserStatusesAsync(); + + Logger.LogInformation("Completed: Setting status of inactive users..."); + } +} +``` + +After creating a worker, we should also register it in the related microservice or project. You can register your worker in the `OnApplicationInitializationAsync` method of your module class. + +```csharp +public class BookstoreModule : AbpModule +{ + public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + await context.AddBackgroundWorkerAsync(); + } +} +``` + > When scaling out your application in a distributed system, it's crucial to consider that the same background workers might run on multiple instances of the same service. This requires careful management of potential side effects. For example, if you're processing messages from a queue, you need to ensure that each message is processed only once. To prevent multiple instances from handling the same message, you can use [distributed locking](../../framework/infrastructure/distributed-locking.md). diff --git a/docs/en/solution-templates/single-layer-web-application/multi-tenancy.md b/docs/en/solution-templates/single-layer-web-application/multi-tenancy.md index 452cfa9bcd..8a9a9be41f 100644 --- a/docs/en/solution-templates/single-layer-web-application/multi-tenancy.md +++ b/docs/en/solution-templates/single-layer-web-application/multi-tenancy.md @@ -4,8 +4,8 @@ //[doc-nav] { "Previous": { - "Name": "Swagger integration", - "Path": "solution-templates/single-layer-web-application/swagger-integration" + "Name": "Background Workers", + "Path": "solution-templates/single-layer-web-application/background-workers" }, "Next": { "Name": "BLOB storing",