Browse Source

server jobs fro process startup

master
yangxiaodong 7 years ago
parent
commit
391adbf1cb
1 changed files with 29 additions and 9 deletions
  1. +29
    -9
      src/Cap.Consistency/Job/IProcessingServer.Job.cs

+ 29
- 9
src/Cap.Consistency/Job/IProcessingServer.Job.cs View File

@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Job;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Cap.Consistency.Infrastructure;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Cap.Consistency
@@ -16,8 +16,8 @@ namespace Cap.Consistency
private ILogger _logger;
private ILoggerFactory _loggerFactory;
private IServiceProvider _provider;
private IJobProcessor[] _processors;
private CancellationTokenSource _cts;
private IJobProcessor _processor;
private ConsistencyOptions _options;
private ProcessingContext _context;
private DefaultCronJobRegistry _defaultJobRegistry;
@@ -42,7 +42,8 @@ namespace Cap.Consistency
public void Start() {

var processorCount = Environment.ProcessorCount;
_processor = _provider.GetService<IJobProcessor>();
processorCount = 1;
_processors = GetProcessors(processorCount);
_logger.ServerStarting(processorCount, 1);

_context = new ProcessingContext(
@@ -50,9 +51,10 @@ namespace Cap.Consistency
_defaultJobRegistry,
_cts.Token);

_compositeTask = Task.Run(() => {
InfiniteRetry(_processor).ProcessAsync(_context);
});
var processorTasks = _processors
.Select(p => InfiniteRetry(p))
.Select(p => p.ProcessAsync(_context));
_compositeTask = Task.WhenAll(processorTasks);
}

public void Dispose() {
@@ -76,7 +78,25 @@ namespace Cap.Consistency

private IJobProcessor InfiniteRetry(IJobProcessor inner) {
return new InfiniteRetryProcessor(inner, _loggerFactory);
}
}

private IJobProcessor[] GetProcessors(int processorCount) {

var returnedProcessors = new List<IJobProcessor>();
for (int i = 0; i < processorCount; i++) {
var processors = _provider.GetServices<IJobProcessor>();
foreach (var processor in processors) {
if (processor is CronJobProcessor) {
if (i == 0) // only add first cronJob
returnedProcessors.Add(processor);
}
else {
returnedProcessors.Add(processor);
}
}
}

return returnedProcessors.ToArray();
}
}
}

Loading…
Cancel
Save