ApptaskJob.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Hotline.BatchTask;
  2. using Hotline.BatchTask.Notifications;
  3. using Quartz;
  4. using Hotline.Share.Enums.BatchTask;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Hotline.EventBus;
  7. using Hotline.Application.OrderApp.OrderDelayApp;
  8. using Hotline.Application.OrderApp.OrderVisitApp;
  9. using Microsoft.AspNetCore.Http;
  10. namespace Hotline.Application.Jobs
  11. {
  12. public class ApptaskJob : IJob, IDisposable
  13. {
  14. private readonly IApptaskDomainService _apptaskDomainService;
  15. private readonly IServiceProvider _serviceProvider;
  16. private readonly Publisher _publisher;
  17. public ApptaskJob(
  18. IApptaskDomainService apptaskDomainService,
  19. IServiceProvider serviceProvider,
  20. Publisher publisher)
  21. {
  22. _apptaskDomainService = apptaskDomainService;
  23. _serviceProvider = serviceProvider;
  24. _publisher = publisher;
  25. }
  26. public async Task Execute(IJobExecutionContext context)
  27. {
  28. //Console.WriteLine($"执行ApptaskJob: {DateTime.Now}");
  29. var task = await _apptaskDomainService.GetWaitingTaskAsync(context.CancellationToken);
  30. if (task is null) return;
  31. var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
  32. httpContextAccessor.HttpContext ??= new DefaultHttpContext();
  33. ApptaskExecuteResult? result = null;
  34. switch (task.TaskType)
  35. {
  36. case ETaskType.OrderDelay:
  37. var delayExecutor = _serviceProvider.GetService<IApptaskExecutor<OrderDelayReviewWithSessionRequest>>();
  38. result = await _apptaskDomainService.ExecuteAsync(delayExecutor, task, context.CancellationToken);
  39. break;
  40. case ETaskType.OrderScreen:
  41. break;
  42. case ETaskType.VoiceVisit:
  43. var vvExecutor = _serviceProvider.GetService<IApptaskExecutor<VoiceVisitWithSessionRequest>>();
  44. result = await _apptaskDomainService.ExecuteAsync(vvExecutor, task, context.CancellationToken);
  45. break;
  46. default:
  47. throw new ArgumentOutOfRangeException();
  48. }
  49. if (result is not null)
  50. {
  51. if (result.IsSuccess)
  52. {
  53. //todo pub single complete suc
  54. await _publisher.PublishAsync(new ApptaskSuccessNotify(task), PublishStrategy.Async, context.CancellationToken);
  55. //todo check if task is all complete
  56. //pub all complete
  57. var isCompleted = await _apptaskDomainService.IsCompletedAsync(task.ApptaskId, context.CancellationToken);
  58. if (isCompleted)
  59. await _publisher.PublishAsync(new ApptaskCompletedNotify(task), PublishStrategy.Async, context.CancellationToken);
  60. }
  61. else
  62. {
  63. //todo pub single complete fail
  64. await _publisher.PublishAsync(new ApptaskFailNotify(task), PublishStrategy.Async, context.CancellationToken);
  65. }
  66. }
  67. }
  68. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  69. public void Dispose()
  70. {
  71. GC.SuppressFinalize(this);
  72. }
  73. }
  74. }