我正在编写一个将csv文件从"queue"文件夹移动到"processing"文件夹的程序,然后以csv文件路径作为参数启动名为import.exe的第三方进程.Import.exe是一个长期运行的任务.
我需要程序继续运行并检查队列中的新文件.出于这个原因,我选择了一个Windows服务应用程序,因为它将长期运行.
我的问题是我对选项不知所措,无法理解我是否应该使用后台线程或并行编程来解决这个问题,或者很可能是两者的结合.
到目前为止,我有这个代码只是同步运行.您很快就会看到,我只是疯狂地解决流程而无需管理或检查完成情况.我已经注释掉了process.WaitForExit(),显然这是一个阻塞调用.
public int maxConcurrentProcesses = 10;
protected override void OnStart(string[] args)
{
// Set up a timer to trigger every minute.
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
private void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
// How many instances of import.exe are running?
Process[] importProcesses = Process.GetProcessesByName("import");
int countRunning = importProcesses.Count();
// If there are less than maxConcurrentProcesses, create as many as needed to reach maxConcurrentProcesses
if (countRunning < …
Run Code Online (Sandbox Code Playgroud) 我在 Startup.ConfigureServices 方法中在 HttpClient 上创建了重试策略。另请注意,默认情况下,asp.net core 2.1 为 HttpClient 进行的每个调用记录 4 行 [信息] 行,这些行显示在我的问题末尾的日志中。
\nservices.AddHttpClient("ResilientClient")\n .AddPolicyHandler(\n Policy.WrapAsync(\n PollyRetryPolicies.TransientErrorRetryPolicy(),\n Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(60))));\n
Run Code Online (Sandbox Code Playgroud)\n该策略定义如下。请注意,我将重试尝试写入日志,因此我会知道是否调用了重试策略。
\npublic static IAsyncPolicy < HttpResponseMessage > TransientErrorRetryPolicy() {\n return HttpPolicyExtensions\n .HandleTransientHttpError()\n .Or < TimeoutRejectedException > ()\n .WaitAndRetryAsync(sleepDurations: ExponentialBackoffPolicy.DecorrelatedJitter(3, SEED_DELAY, MAX_DELAY),\n onRetry: (message, timespan, attempt, context) => {\n context.GetLogger() ? .LogInformation($ "Retrying request to {message?.Result?.RequestMessage?.RequestUri} in {timespan.TotalSeconds} seconds. Retry attempt {attempt}.");\n });\n}\n
Run Code Online (Sandbox Code Playgroud)\nHandleTransientHttpError() 是一个 Polly 扩展,它在注释中指出:
\n\n\n配置要处理的条件是:\n\xe2\x80\xa2 网络故障(如 System.Net.Http.HttpRequestException)
\n
我的httpclient用法是这样的:
\nusing (HttpResponseMessage …
Run Code Online (Sandbox Code Playgroud) 我有一个链接到基于谓词的动作块的转换块。
// Blocks
public TransformBlock<Document, Document> DocumentCreationTransformBlock =
new TransformBlock<Document, Document>(async document =>
{
return await CreateAsync(document); // REST API call that sets document.NewId
},
new ExecutionDataflowBlockOptions {
BoundedCapacity = 100,
MaxDegreeOfParallelism = 20
});
public ActionBlock<Document> SplitPipelineActionBlock =
new ActionBlock<Document>(async document =>
{ // implementation obfuscated
},
new ExecutionDataflowBlockOptions {
BoundedCapacity = 100
};
// Shared block elements
public DataflowLinkOptions CommonLinkOptions = new DataflowLinkOptions {
PropagateCompletion = true };
// Link mesh
DocumentCreationTransformBlock.LinkTo(SplitPipelineActionBlock,
CommonLinkOptions,
document => !string.IsNullOrEmpty(document.NewId));
DocumentCreationTransformBlock.LinkTo(DataflowBlock.NullTarget<Document>(),
CommonLinkOptions); …
Run Code Online (Sandbox Code Playgroud)