-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Send IServiceProvider to DoWork method of the periodic background worker #2539 * Introduce AsyncPeriodicBackgroundWorkerBase #2540
- Loading branch information
Showing
8 changed files
with
137 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...rc/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJobExecuter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
namespace Volo.Abp.BackgroundJobs | ||
using System.Threading.Tasks; | ||
|
||
namespace Volo.Abp.BackgroundJobs | ||
{ | ||
public interface IBackgroundJobExecuter | ||
{ | ||
void Execute(JobExecutionContext context); | ||
Task ExecuteAsync(JobExecutionContext context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...olo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AsyncPeriodicBackgroundWorkerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Volo.Abp.Threading; | ||
|
||
namespace Volo.Abp.BackgroundWorkers | ||
{ | ||
public abstract class AsyncPeriodicBackgroundWorkerBase : BackgroundWorkerBase | ||
{ | ||
protected IServiceScopeFactory ServiceScopeFactory { get; } | ||
protected AbpTimer Timer { get; } | ||
|
||
protected AsyncPeriodicBackgroundWorkerBase( | ||
AbpTimer timer, | ||
IServiceScopeFactory serviceScopeFactory) | ||
{ | ||
ServiceScopeFactory = serviceScopeFactory; | ||
Timer = timer; | ||
Timer.Elapsed += Timer_Elapsed; | ||
} | ||
|
||
public override async Task StartAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await base.StartAsync(cancellationToken).ConfigureAwait(false); | ||
Timer.Start(cancellationToken); | ||
} | ||
|
||
public override async Task StopAsync(CancellationToken cancellationToken = default) | ||
{ | ||
Timer.Stop(cancellationToken); | ||
await base.StopAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
private void Timer_Elapsed(object sender, System.EventArgs e) | ||
{ | ||
try | ||
{ | ||
using (var scope = ServiceScopeFactory.CreateScope()) | ||
{ | ||
AsyncHelper.RunSync( | ||
() => DoWorkAsync(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)) | ||
); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogException(ex); | ||
} | ||
} | ||
|
||
protected abstract Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...work/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace Volo.Abp.BackgroundWorkers | ||
{ | ||
public class PeriodicBackgroundWorkerContext | ||
{ | ||
public IServiceProvider ServiceProvider { get; } | ||
|
||
public PeriodicBackgroundWorkerContext(IServiceProvider serviceProvider) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters