mirror of https://github.com/abpframework/abp.git
committed by
GitHub
24 changed files with 282 additions and 54 deletions
@ -1,23 +1,40 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Hangfire; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire; |
|||
|
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
async static Task Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppHangfireModule>(options => |
|||
using (var application = await AbpApplicationFactory.CreateAsync<DemoAppHangfireModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
await application.InitializeAsync(); |
|||
|
|||
await CancelableBackgroundJobAsync(application.ServiceProvider); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
await application.ShutdownAsync(); |
|||
} |
|||
} |
|||
|
|||
private async static Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider) |
|||
{ |
|||
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>(); |
|||
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" }); |
|||
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" }); |
|||
Thread.Sleep(1000); |
|||
BackgroundJob.Delete(jobId); |
|||
} |
|||
} |
|||
|
|||
@ -1,23 +1,41 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Quartz; |
|||
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz; |
|||
|
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
async static Task Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppQuartzModule>(options => |
|||
using (var application = await AbpApplicationFactory.CreateAsync<DemoAppQuartzModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
await application.InitializeAsync(); |
|||
|
|||
await CancelableBackgroundJobAsync(application.ServiceProvider); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
await application.ShutdownAsync(); |
|||
} |
|||
} |
|||
|
|||
private async static Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider) |
|||
{ |
|||
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>(); |
|||
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs {Value = "test-1"}); |
|||
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" }); |
|||
Thread.Sleep(1000); |
|||
var scheduler = serviceProvider.GetRequiredService<IScheduler>(); |
|||
await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0])); |
|||
} |
|||
} |
|||
|
|||
@ -1,23 +1,24 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq; |
|||
|
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
async static Task Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppRabbitMqModule>(options => |
|||
using (var application = await AbpApplicationFactory.CreateAsync<DemoAppRabbitMqModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
await application.InitializeAsync(); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
await application.ShutdownAsync(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
public class LongRunningJob : BackgroundJob<LongRunningJobArgs>, ITransientDependency |
|||
{ |
|||
private readonly ICancellationTokenProvider _cancellationTokenProvider; |
|||
|
|||
public LongRunningJob(ICancellationTokenProvider cancellationTokenProvider) |
|||
{ |
|||
_cancellationTokenProvider = cancellationTokenProvider; |
|||
} |
|||
|
|||
public override void Execute(LongRunningJobArgs args) |
|||
{ |
|||
lock (Console.Out) |
|||
{ |
|||
var oldColor = Console.ForegroundColor; |
|||
try |
|||
{ |
|||
Console.WriteLine($"Long running {args.Value} start: {DateTime.Now}"); |
|||
|
|||
for (var i = 1; i <= 10; i++) |
|||
{ |
|||
_cancellationTokenProvider.Token.ThrowIfCancellationRequested(); |
|||
|
|||
Thread.Sleep(1000); |
|||
Console.WriteLine($"{args.Value} step-{i} done: {DateTime.Now}"); |
|||
} |
|||
|
|||
Console.ForegroundColor = ConsoleColor.Green; |
|||
Console.WriteLine($"Long running {args.Value} completed: {DateTime.Now}"); |
|||
} |
|||
catch (OperationCanceledException) |
|||
{ |
|||
Console.ForegroundColor = ConsoleColor.Red; |
|||
Console.WriteLine($"Long running {args.Value} cancelled!!!"); |
|||
} |
|||
finally |
|||
{ |
|||
Console.ForegroundColor = oldColor; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
[BackgroundJobName("LongJob")] |
|||
public class LongRunningJobArgs |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
} |
|||
@ -1,23 +1,23 @@ |
|||
using System; |
|||
|
|||
using System.Threading.Tasks; |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp; |
|||
|
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
async static Task Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppModule>(options => |
|||
using (var application = await AbpApplicationFactory.CreateAsync<DemoAppModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
await application.InitializeAsync(); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
await application.ShutdownAsync(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue