Browse Source

Merge pull request #21781 from abpframework/doc/background-worker-and-job

Layered and Single-Layer documentations revised
pull/21783/head
Engincan VESKE 2 years ago
committed by GitHub
parent
commit
71e344676d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 19
      docs/en/solution-templates/layered-web-application/background-jobs.md
  2. 63
      docs/en/solution-templates/layered-web-application/background-workers.md
  3. 44
      docs/en/solution-templates/layered-web-application/distributed-locking.md
  4. 3
      docs/en/solution-templates/layered-web-application/index.md
  5. 4
      docs/en/solution-templates/layered-web-application/multi-tenancy.md
  6. 4
      docs/en/solution-templates/layered-web-application/swagger-integration.md
  7. 19
      docs/en/solution-templates/single-layer-web-application/background-jobs.md
  8. 63
      docs/en/solution-templates/single-layer-web-application/background-workers.md
  9. 44
      docs/en/solution-templates/single-layer-web-application/distributed-locking.md
  10. 3
      docs/en/solution-templates/single-layer-web-application/index.md
  11. 4
      docs/en/solution-templates/single-layer-web-application/multi-tenancy.md
  12. 4
      docs/en/solution-templates/single-layer-web-application/swagger-integration.md

19
docs/en/solution-templates/layered-web-application/background-jobs.md

@ -0,0 +1,19 @@
# Layered Solution: Background Jobs
```json
//[doc-nav]
{
"Previous": {
"Name": "Swagger integration",
"Path": "solution-templates/layered-web-application/swagger-integration"
},
"Next": {
"Name": "Background Workers",
"Path": "solution-templates/layered-web-application/background-workers"
}
}
```
Background jobs are long-running, asynchronous tasks that operate in the background of your application. They are ideal for non-time-sensitive tasks, such as sending emails, generating reports, or processing data. These jobs are usually triggered by a user action or a scheduled task. For more information, refer to the [Background Jobs](../../framework/infrastructure/background-jobs/index.md) document.
In the layered solution template, background jobs are implemented using the [Background Jobs](../../modules/background-jobs.md) module. This module offers a simple and efficient way to create and manage background jobs in your application. It provides features like job queues and job scheduling. Job information is stored in the database, enabling you to track job statuses and retry failed jobs.

63
docs/en/solution-templates/layered-web-application/background-workers.md

@ -0,0 +1,63 @@
# Layered Solution: Background Workers
```json
//[doc-nav]
{
"Previous": {
"Name": "Background Jobs",
"Path": "solution-templates/layered-web-application/background-jobs"
},
"Next": {
"Name": "Distribution Locking",
"Path": "solution-templates/layered-web-application/distributed-locking"
}
}
```
Background workers are long-running processes that operate in the background of your application. They are ideal for non-time-sensitive tasks, such as processing data, sending notifications, or monitoring system health. Typically, background workers start when the application launches and run continuously until the application stops. For more information, refer to the [Background Workers](../../framework/infrastructure/background-workers/index.md) document.
Basically, you can create scheduled workers to run at specific time intervals based on your requirements. For example, you might create a worker to check the status of inactive users and change their status to passive if they haven't 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<IUserRepository>();
// Do the work
await userRepository.UpdateInactiveUserStatusesAsync();
Logger.LogInformation("Completed: Setting status of inactive users...");
}
}
```
After creating a worker, you should also register it in the application. You might add it in the *Domain* or *Application* layer. You can register your worker in the `OnApplicationInitializationAsync` method of your module class:
```csharp
public class BookstoreApplicationModule : AbpModule
{
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
await context.AddBackgroundWorkerAsync<PassiveUserCheckerWorker>();
}
}
```
> 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).

44
docs/en/solution-templates/layered-web-application/distributed-locking.md

@ -0,0 +1,44 @@
# Layered Solution: Distributed Locking
```json
//[doc-nav]
{
"Previous": {
"Name": "Background Workers",
"Path": "solution-templates/layered-web-application/background-workers"
},
"Next": {
"Name": "Multi-Tenancy",
"Path": "solution-templates/layered-web-application/multi-tenancy"
}
}
```
Distributed locking is a mechanism that enables multiple instances of an application to coordinate and synchronize access to shared resources. It is particularly useful in scenarios where multiple instances need to ensure that only one instance can access a resource at a time. For more information, refer to the [Distributed Locking](../../framework/infrastructure/distributed-locking.md) document.
## Distributed Locking in Layered Solutions
The layered solution template does not include the distributed lock package by default unless it's a *Tiered* or *Public Website* application. To use distributed locking, you can add the [Volo.Abp.DistributedLock](https://www.nuget.org/packages/Volo.Abp.DistributedLocking) package to your project. This package provides a distributed lock mechanism that works with Redis. You can inject the `IAbpDistributedLock` service to acquire and release locks. Below is an example of using distributed locking in your application:
```csharp
public class MyService : ITransientDependency
{
private readonly IAbpDistributedLock _distributedLock;
public MyService(IAbpDistributedLock distributedLock)
{
_distributedLock = distributedLock;
}
public async Task MyMethodAsync()
{
await using (var handle = await _distributedLock.TryAcquireAsync("MyLockName"))
{
if (handle != null)
{
// your code that access the shared resource
}
}
}
}
```

3
docs/en/solution-templates/layered-web-application/index.md

@ -29,6 +29,9 @@ ABP Studio provides pre-architected, production-ready templates to jump-start a
* [Database configurations](database-configurations.md)
* [Logging (with Serilog)](logging.md)
* [Swagger integration](swagger-integration.md)
* [Background Jobs](background-jobs.md)
* [Background Workers](background-workers.md)
* [Distributed Locking](distributed-locking.md)
* [Multi-Tenancy](multi-tenancy.md)
* [BLOB storing](blob-storing.md)
* [CORS configuration](cors-configuration.md)

4
docs/en/solution-templates/layered-web-application/multi-tenancy.md

@ -4,8 +4,8 @@
//[doc-nav]
{
"Previous": {
"Name": "Swagger integration",
"Path": "solution-templates/layered-web-application/swagger-integration"
"Name": "Distribution Locking",
"Path": "solution-templates/layered-web-application/distributed-locking"
},
"Next": {
"Name": "BLOB storing",

4
docs/en/solution-templates/layered-web-application/swagger-integration.md

@ -8,8 +8,8 @@
"Path": "solution-templates/layered-web-application/logging"
},
"Next": {
"Name": "Multi-Tenancy",
"Path": "solution-templates/layered-web-application/multi-tenancy"
"Name": "Background Jobs",
"Path": "solution-templates/layered-web-application/background-jobs"
}
}
```

19
docs/en/solution-templates/single-layer-web-application/background-jobs.md

@ -0,0 +1,19 @@
# Single Layer Solution: Background Jobs
```json
//[doc-nav]
{
"Previous": {
"Name": "Swagger integration",
"Path": "solution-templates/single-layer-web-application/swagger-integration"
},
"Next": {
"Name": "Background Workers",
"Path": "solution-templates/single-layer-web-application/background-workers"
}
}
```
Background jobs are long-running, asynchronous tasks that run in the background of your application. They are useful for tasks that are not time-sensitive, such as sending emails, generating reports, or processing data. Background jobs are typically triggered by a user action or a scheduled task. You can learn more about background jobs in the [Background Jobs](../../framework/infrastructure/background-jobs/index.md) document.
In the Single Layer solution template, background jobs are implemented using the [Background Jobs](../../modules/background-jobs.md) module. This module provides a simple and efficient way to create and manage background jobs in your application. It includes features such as job queues, job scheduling. It stores job information in the database, allowing you to track the status of jobs and retry failed jobs.

63
docs/en/solution-templates/single-layer-web-application/background-workers.md

@ -0,0 +1,63 @@
# Single Layer Solution: Background Workers
```json
//[doc-nav]
{
"Previous": {
"Name": "Background Jobs",
"Path": "solution-templates/single-layer-web-application/background-jobs"
},
"Next": {
"Name": "Distributed Locking",
"Path": "solution-templates/single-layer-web-application/distributed-locking"
}
}
```
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<IUserRepository>();
// Do the work
await userRepository.UpdateInactiveUserStatusesAsync();
Logger.LogInformation("Completed: Setting status of inactive users...");
}
}
```
After creating a worker, you should also register it in the application. 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<PassiveUserCheckerWorker>();
}
}
```
> 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).

44
docs/en/solution-templates/single-layer-web-application/distributed-locking.md

@ -0,0 +1,44 @@
# Single Layer Solution: Distributed Locking
```json
//[doc-nav]
{
"Previous": {
"Name": "Background Workers",
"Path": "solution-templates/single-layer-web-application/background-workers"
},
"Next": {
"Name": "Multi-Tenancy",
"Path": "solution-templates/single-layer-web-application/multi-tenancy"
}
}
```
Distributed locking is a mechanism that allows multiple instances of an application to coordinate and synchronize access to shared resources. It is useful for scenarios where multiple instances of an application need to ensure that only one instance can access a resource at a time. You can learn more in the [Distributed Locking](../../framework/infrastructure/distributed-locking.md) document.
## Distributed Locking in Single Layer Solutions
The single-layer solution template does not include distributed lock package by default. You can add the [Volo.Abp.DistributedLock](https://www.nuget.org/packages/Volo.Abp.DistributedLocking) package to your project to use distributed locking. This package provides a distributed lock mechanism that works with Redis. You can inject the `IAbpDistributedLock` service to acquire and release. Here is an example of using distributed locking in your application:
```csharp
public class MyService : ITransientDependency
{
private readonly IAbpDistributedLock _distributedLock;
public MyService(IAbpDistributedLock distributedLock)
{
_distributedLock = distributedLock;
}
public async Task MyMethodAsync()
{
await using (var handle = await _distributedLock.TryAcquireAsync("MyLockName"))
{
if (handle != null)
{
// your code that access the shared resource
}
}
}
}
```

3
docs/en/solution-templates/single-layer-web-application/index.md

@ -28,6 +28,9 @@ ABP Studio offers pre-architected, production-ready templates to quickly start a
* [Database configurations](database-configurations.md)
* [Logging (with Serilog)](logging.md)
* [Swagger integration](swagger-integration.md)
* [Background Jobs](background-jobs.md)
* [Background Workers](background-workers.md)
* [Distributed Locking](distributed-locking.md)
* [Multi-Tenancy](multi-tenancy.md)
* [BLOB storing](blob-storing.md)
* [CORS configuration](cors-configuration.md)

4
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": "Distributed Locking",
"Path": "solution-templates/single-layer-web-application/distributed-locking"
},
"Next": {
"Name": "BLOB storing",

4
docs/en/solution-templates/single-layer-web-application/swagger-integration.md

@ -8,8 +8,8 @@
"Path": "solution-templates/single-layer-web-application/logging"
},
"Next": {
"Name": "Multi-Tenancy",
"Path": "solution-templates/single-layer-web-application/multi-tenancy"
"Name": "Background Jobs",
"Path": "solution-templates/single-layer-web-application/background-jobs"
}
}
```

Loading…
Cancel
Save