Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackgroundJobs - Enhance AsyncBackgroundJob with extra services #20257

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 1 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Authorization' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
using Microsoft.AspNetCore.Razor.Language.Intermediate;

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

Check failure on line 2 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;

Check failure on line 12 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Guids' does not exist in the namespace 'Volo.Abp' (are you missing an assembly reference?)

Check failure on line 12 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Guids' does not exist in the namespace 'Volo.Abp' (are you missing an assembly reference?)

Check failure on line 12 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Guids' does not exist in the namespace 'Volo.Abp' (are you missing an assembly reference?)

Check failure on line 12 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'Guids' does not exist in the namespace 'Volo.Abp' (are you missing an assembly reference?)
using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
using Volo.Abp.Timing;
using Volo.Abp.Uow;

namespace Volo.Abp.BackgroundJobs;

public abstract class AsyncBackgroundJob<TArgs> : IAsyncBackgroundJob<TArgs>
{
//TODO: Add UOW, Localization, CancellationTokenProvider and other useful properties..?
public IAbpLazyServiceProvider LazyServiceProvider { get; set; } = default!;

protected IGuidGenerator GuidGenerator => LazyServiceProvider.GetRequiredService<IGuidGenerator>();

Check failure on line 25 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'IGuidGenerator' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 25 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'IGuidGenerator' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 25 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'IGuidGenerator' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 25 in framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

View workflow job for this annotation

GitHub Actions / build-test

The type or namespace name 'IGuidGenerator' could not be found (are you missing a using directive or an assembly reference?)
protected ICurrentTenant CurrentTenant => LazyServiceProvider.GetRequiredService<ICurrentTenant>();
protected IClock Clock => LazyServiceProvider.GetRequiredService<IClock>();
protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.GetRequiredService<IUnitOfWorkManager>();
protected IUnitOfWork? CurrentUnitOfWork => UnitOfWorkManager?.Current;
protected ILoggerFactory LoggerFactory => LazyServiceProvider.GetRequiredService<ILoggerFactory>();
protected ILogger Logger => LazyServiceProvider.GetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName!) ?? NullLogger.Instance);
protected ICancellationTokenProvider CancellationTokenProvider => LazyServiceProvider.GetRequiredService<ICancellationTokenProvider>();

protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();

protected IStringLocalizer L
{
get
{
if (_localizer == null)
{
_localizer = CreateLocalizer();
}

public ILogger<AsyncBackgroundJob<TArgs>> Logger { get; set; }
return _localizer;
}
}
private IStringLocalizer? _localizer;

protected Type? LocalizationResource
{
get => _localizationResource;
set
{
_localizationResource = value;
_localizer = null;
}
}
private Type? _localizationResource = typeof(DefaultResource);

protected AsyncBackgroundJob()
protected virtual IStringLocalizer CreateLocalizer()
{
Logger = NullLogger<AsyncBackgroundJob<TArgs>>.Instance;
if (LocalizationResource != null)
{
return StringLocalizerFactory.Create(LocalizationResource);
}

var localizer = StringLocalizerFactory.CreateDefaultOrNull();
if (localizer == null)
{
throw new AbpException($"Set {nameof(LocalizationResource)} or define the default localization resource type (by configuring the {nameof(AbpLocalizationOptions)}.{nameof(AbpLocalizationOptions.DefaultResourceType)}) to be able to use the {nameof(L)} object!");
}

return localizer;
}

public abstract Task ExecuteAsync(TArgs args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,17 @@ public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependen

public Guid? TenantId { get; set; }

private readonly ICurrentTenant _currentTenant;
private readonly ICancellationTokenProvider _cancellationTokenProvider;

public bool Canceled { get; set; }

public MyAsyncJob(
ICurrentTenant currentTenant,
ICancellationTokenProvider cancellationTokenProvider)
{
_currentTenant = currentTenant;
_cancellationTokenProvider = cancellationTokenProvider;
}

public override Task ExecuteAsync(MyAsyncJobArgs args)
{
if (_cancellationTokenProvider.Token.IsCancellationRequested)
if (CancellationTokenProvider.Token.IsCancellationRequested)
{
Canceled = true;
}

ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id;
TenantId = CurrentTenant.Id;
return Task.CompletedTask;
}
}
Loading