mirror of https://github.com/abpframework/abp.git
23 changed files with 339 additions and 190 deletions
@ -1,12 +1,15 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.RabbitMQ |
|||
{ |
|||
public interface IJobQueue<TArgs> : IDisposable |
|||
public interface IJobQueue<TArgs> : IRunnable, IDisposable |
|||
{ |
|||
Task<string> Enqueue(TArgs args); |
|||
|
|||
Task StartAsync(); |
|||
Task<string> EnqueueAsync( |
|||
TArgs args, |
|||
BackgroundJobPriority priority = BackgroundJobPriority.Normal, |
|||
TimeSpan? delay = null |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.RabbitMQ |
|||
{ |
|||
public interface IJobQueueManager : IRunnable |
|||
{ |
|||
IJobQueue<TArgs> Get<TArgs>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.RabbitMQ |
|||
{ |
|||
public class JobQueueManager : IJobQueueManager, ISingletonDependency |
|||
{ |
|||
protected ConcurrentDictionary<string, IRunnable> JobQueues { get; } |
|||
|
|||
protected IServiceProvider ServiceProvider { get; } |
|||
protected BackgroundJobOptions Options { get; } |
|||
|
|||
public JobQueueManager( |
|||
IOptions<BackgroundJobOptions> options, |
|||
IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
Options = options.Value; |
|||
JobQueues = new ConcurrentDictionary<string, IRunnable>(); |
|||
} |
|||
|
|||
public async Task StartAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
if (!Options.IsJobExecutionEnabled) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var item in Options.JobTypes) |
|||
{ |
|||
var jobName = item.Key; |
|||
var jobType = item.Value; |
|||
var argsType = BackgroundJobArgsHelper.GetJobArgsType(jobType); |
|||
|
|||
var jobQueue = (IRunnable)ServiceProvider.GetRequiredService(typeof(IJobQueue<>).MakeGenericType(argsType)); |
|||
await jobQueue.StartAsync(cancellationToken); |
|||
JobQueues[jobName] = jobQueue; |
|||
} |
|||
} |
|||
|
|||
public async Task StopAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
foreach (var jobQueue in JobQueues.Values) |
|||
{ |
|||
await jobQueue.StopAsync(cancellationToken); |
|||
} |
|||
|
|||
JobQueues.Clear(); |
|||
} |
|||
|
|||
public IJobQueue<TArgs> Get<TArgs>() |
|||
{ |
|||
var jobName = BackgroundJobNameAttribute.GetName(typeof(TArgs)); |
|||
|
|||
if (!Options.JobTypes.ContainsKey(jobName)) |
|||
{ |
|||
throw new AbpException("No job registered"); |
|||
} |
|||
|
|||
return (IJobQueue<TArgs>)JobQueues.GetOrAdd(jobName, _ => |
|||
{ |
|||
var jobQueue = (IRunnable)ServiceProvider.GetRequiredService(typeof(IJobQueue<>).MakeGenericType(typeof(TArgs))); |
|||
jobQueue.Start(); |
|||
return jobQueue; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
namespace Volo.Abp.Threading |
|||
{ |
|||
/// <summary>
|
|||
/// Base implementation of <see cref="IRunnable"/>.
|
|||
/// </summary>
|
|||
public abstract class RunnableBase : IRunnable |
|||
{ |
|||
/// <summary>
|
|||
/// A boolean value to check if this is running.
|
|||
/// </summary>
|
|||
public bool IsRunning => _isRunning; |
|||
private volatile bool _isRunning; |
|||
|
|||
public virtual void Start() |
|||
{ |
|||
_isRunning = true; |
|||
} |
|||
|
|||
public virtual void Stop() |
|||
{ |
|||
_isRunning = false; |
|||
} |
|||
|
|||
public virtual void WaitToStop() |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +1,21 @@ |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Threading |
|||
{ |
|||
/// <summary>
|
|||
/// Some extension methods for <see cref="IRunnable"/>.
|
|||
/// </summary>
|
|||
public static class RunnableExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Calls <see cref="IRunnable.Stop"/> and then <see cref="IRunnable.WaitToStop"/>.
|
|||
/// </summary>
|
|||
public static void StopAndWaitToStop(this IRunnable runnable) |
|||
public static void Start([NotNull] this IRunnable runnable) |
|||
{ |
|||
runnable.Stop(); |
|||
runnable.WaitToStop(); |
|||
Check.NotNull(runnable, nameof(runnable)); |
|||
|
|||
AsyncHelper.RunSync(() => runnable.StartAsync()); |
|||
} |
|||
|
|||
public static void Stop([NotNull] this IRunnable runnable) |
|||
{ |
|||
Check.NotNull(runnable, nameof(runnable)); |
|||
|
|||
AsyncHelper.RunSync(() => runnable.StopAsync()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.BackgroundJobs.DemoApp.Shared; |
|||
using Volo.Abp.BackgroundJobs.RabbitMQ; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq |
|||
{ |
|||
[DependsOn( |
|||
typeof(DemoAppSharedModule), |
|||
typeof(AbpAutofacModule), |
|||
typeof(AbpBackgroundJobsRabbitMqModule) |
|||
)] |
|||
public class DemoAppHangfireModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddAssemblyOf<DemoAppHangfireModule>(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
context |
|||
.ServiceProvider |
|||
.GetRequiredService<ILoggerFactory>() |
|||
.AddConsole(LogLevel.Debug); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppHangfireModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>netcoreapp2.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.BackgroundJobs.RabbitMQ\Volo.Abp.BackgroundJobs.RabbitMQ.csproj" /> |
|||
<ProjectReference Include="..\Volo.Abp.BackgroundJobs.DemoApp.Shared\Volo.Abp.BackgroundJobs.DemoApp.Shared.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -1,8 +1,17 @@ |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
using System; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
[BackgroundJobName("GreenJob")] |
|||
public class WriteToConsoleGreenJobArgs |
|||
{ |
|||
public string Value { get; set; } |
|||
|
|||
public DateTime Time { get; set; } |
|||
|
|||
public WriteToConsoleGreenJobArgs() |
|||
{ |
|||
Time = DateTime.Now; |
|||
} |
|||
} |
|||
} |
|||
@ -1,8 +1,17 @@ |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
using System; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
[BackgroundJobName("YellowJob")] |
|||
public class WriteToConsoleYellowJobArgs |
|||
{ |
|||
public string Value { get; set; } |
|||
|
|||
public DateTime Time { get; set; } |
|||
|
|||
public WriteToConsoleYellowJobArgs() |
|||
{ |
|||
Time = DateTime.Now; |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue