Skip to content

Commit 43eb0ad

Browse files
author
yuanwj
committed
完善任务依赖,移除单任务并行代码
1 parent 96f10dc commit 43eb0ad

File tree

5 files changed

+77
-63
lines changed

5 files changed

+77
-63
lines changed

src/SmartCode.App/PluginManager.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ private IEnumerable<TPlugin> ResolvePlugins<TPlugin>() where TPlugin : IPlugin
5656
var plugins = _serviceProvider.GetServices<TPlugin>();
5757
foreach (var plugin in plugins)
5858
{
59-
if (!plugin.Initialized)
59+
lock (this)
6060
{
61-
var pluginType = plugin.GetType();
62-
var names = pluginType.AssemblyQualifiedName.Split(',');
63-
var typeName = names[0].Trim();
64-
var assName = names[1].Trim();
65-
var pluginConfig = _smartCodeOptions
66-
.Plugins
67-
.FirstOrDefault(m => m.ImplAssemblyName == assName && m.ImplTypeName == typeName);
68-
plugin.Initialize(pluginConfig.Parameters);
61+
if (!plugin.Initialized)
62+
{
63+
var pluginType = plugin.GetType();
64+
var names = pluginType.AssemblyQualifiedName.Split(',');
65+
var typeName = names[0].Trim();
66+
var assName = names[1].Trim();
67+
var pluginConfig = _smartCodeOptions
68+
.Plugins
69+
.FirstOrDefault(m => m.ImplAssemblyName == assName && m.ImplTypeName == typeName);
70+
plugin.Initialize(pluginConfig.Parameters);
71+
}
6972
}
7073
}
7174
return plugins;
75+
7276
}
7377
}
7478
}

src/SmartCode.CLI/Program.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ namespace SmartCode.CLI
1212
{
1313
class Program
1414
{
15-
static async Task Main(string[] args)
15+
static async Task Main(string[] args)
1616
{
17-
await CommandLineApplication.ExecuteAsync<SmartCodeCommand>(args);
17+
ThreadPool.SetMinThreads(1000, 1000);
18+
ThreadPool.SetMaxThreads(1000, 1000);
19+
await CommandLineApplication.ExecuteAsync<SmartCodeCommand>(args);
1820
}
1921
}
2022
}

src/SmartCode.Generator/BuildTasks/TableBuildTask.cs

+10-31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Threading;
89
using System.Threading.Tasks;
910

1011
namespace SmartCode.Generator.BuildTasks
@@ -28,38 +29,16 @@ public override async Task Build(BuildContext context)
2829
var filterTables = FilterTable(context.GetDataSource<ITableSource>().Tables, context.BuildKey,
2930
context.Build);
3031
context.SetCurrentAllTable(filterTables);
31-
32-
var nameConverter = _pluginManager.Resolve<INamingConverter>();
33-
var templateEngine = _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name);
34-
var output = _pluginManager.Resolve<IOutput>(context.Build.Output.Type);
35-
36-
var tasks = filterTables.Select(table =>
32+
foreach (var table in filterTables)
3733
{
38-
BuildContext newContext = new BuildContext();
39-
newContext.Project = context.Project;
40-
newContext.DataSource = context.DataSource;
41-
newContext.PluginManager = context.PluginManager;
42-
newContext.BuildKey = context.BuildKey;
43-
newContext.Build = context.Build;
44-
newContext.Result = context.Result;
45-
newContext.Output = context.Output;
46-
newContext.SetCurrentTable(table);
47-
48-
return Task.Factory.StartNew(BuildTable,(newContext, nameConverter, templateEngine, output), TaskCreationOptions.LongRunning);
49-
}).ToArray();
50-
51-
await Task.WhenAll(tasks);
52-
}
53-
54-
private async void BuildTable(object obj)
55-
{
56-
var p=((BuildContext context, INamingConverter nameConverter, ITemplateEngine templateEngine, IOutput output)) obj;
57-
var table = p.context.GetCurrentTable();
58-
_logger.LogInformation($"BuildTable:{table.Name} Start!");
59-
p.nameConverter.Convert(p.context);
60-
p.context.Result = await p.templateEngine.Render(p.context);
61-
await p.output.Output(p.context);
62-
_logger.LogInformation($"BuildTable:{table.Name} End!");
34+
_logger.LogInformation($"BuildTable:{table.Name} Start!");
35+
context.SetCurrentTable(table);
36+
_pluginManager.Resolve<INamingConverter>().Convert(context);
37+
context.Result = await _pluginManager.Resolve<ITemplateEngine>(context.Build.TemplateEngine.Name)
38+
.Render(context);
39+
await _pluginManager.Resolve<IOutput>(context.Build.Output.Type).Output(context);
40+
_logger.LogInformation($"BuildTable:{table.Name} End!");
41+
}
6342
}
6443

6544
public override void Initialize(IDictionary<string, object> parameters)

src/SmartCode.Generator/GeneratorProjectBuilder.cs

+45-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
@@ -25,12 +26,13 @@ Project project
2526

2627
CountdownEvent countdown = new CountdownEvent(1);
2728

29+
2830
public async Task Build()
2931
{
3032
var dataSource = _pluginManager.Resolve<IDataSource>(_project.DataSource.Name);
3133
await dataSource.InitData();
3234

33-
BuildContext[] contexts = _project.BuildTasks.Select(d => new BuildContext
35+
IList<BuildContext> allContexts = _project.BuildTasks.Select(d => new BuildContext
3436
{
3537
PluginManager = _pluginManager,
3638
Project = _project,
@@ -39,40 +41,64 @@ public async Task Build()
3941
Build = d.Value,
4042
Output = d.Value.Output?.Copy(),
4143
}).ToArray();
42-
foreach (var context in contexts)
44+
foreach (var context in allContexts)
4345
{
44-
context.DependOn = contexts.Where(d => context.Build.DependOn != null && context.Build.DependOn.Contains(d.BuildKey)).ToArray();
46+
if (context.Build.DependOn != null && context.Build.DependOn.Count() > 0)
47+
{
48+
context.DependOn = allContexts.Where(d => context.Build.DependOn.Contains(d.BuildKey)).ToArray();
49+
}
4550
}
46-
4751
countdown.Reset();
48-
foreach (var context in contexts)
52+
countdown.AddCount(allContexts.Count);
53+
foreach (var context in allContexts)
54+
{
55+
context.CountDown.Reset();
56+
if (context.DependOn != null && context.DependOn.Count > 0)
57+
{
58+
context.CountDown.AddCount(context.DependOn.Count);
59+
}
60+
61+
ThreadPool.QueueUserWorkItem(this.BuildTask, (context, allContexts));
62+
}
63+
64+
foreach (var context in allContexts)
4965
{
50-
context.BuildTask = Task.Factory.StartNew(this.BuildTask, context, TaskCreationOptions.LongRunning);
66+
context.CountDown.Signal();
5167
}
5268

5369
countdown.Signal();
54-
55-
await Task.WhenAll(contexts.Select(d => d.BuildTask).ToArray());
70+
countdown.Wait();
5671
}
72+
5773
private async void BuildTask(object obj)
5874
{
59-
var context = (BuildContext)obj;
60-
_logger.LogInformation($"-------- BuildTask:{context.BuildKey} Wait! ---------");
61-
countdown.Wait();
75+
var p = ((BuildContext context, IList<BuildContext> allContexts))obj;
76+
77+
if (p.context.DependOn != null && p.context.DependOn.Count > 0)
78+
{
79+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Wait [{string.Join(",", p.context.DependOn?.Select(d => d.BuildKey)?.ToArray())}]---------");
80+
}
6281
//等待依赖任务
63-
if (context.DependOn != null)
82+
p.context.CountDown.Wait();
83+
84+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Start! ---------");
85+
//执行自身任务
86+
await _pluginManager.Resolve<IBuildTask>(p.context.Build.Type).Build(p.context);
87+
88+
foreach (var c in p.allContexts)
6489
{
65-
foreach (var dcontext in context.DependOn)
90+
if(c.DependOn==null || c.DependOn.Count == 0)
91+
{
92+
continue;
93+
}
94+
if (c.DependOn.Contains(p.context))
6695
{
67-
await dcontext.BuildTask;
96+
c.CountDown.Signal();
6897
}
6998
}
7099

71-
_logger.LogInformation($"-------- BuildTask:{context.BuildKey} Start! ---------");
72-
//执行自身任务
73-
await _pluginManager.Resolve<IBuildTask>(context.Build.Type).Build(context);
74-
75-
_logger.LogInformation($"-------- BuildTask:{context.BuildKey} End! ---------");
100+
countdown.Signal();
101+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} End! ---------");
76102
}
77103
}
78104
}

src/SmartCode/BuildContext.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using SmartCode.Configuration;
67

78
namespace SmartCode
89
{
910
public class BuildContext
1011
{
12+
1113
public Project Project { get; set; }
1214
public IDataSource DataSource { get; set; }
1315
public IPluginManager PluginManager { get; set; }
@@ -28,7 +30,8 @@ public void SetItem(string key, object item)
2830
Items[key] = item;
2931
}
3032

31-
public IEnumerable<BuildContext> DependOn { get; set; }
32-
public Task BuildTask { get; set; }
33+
public IList<BuildContext> DependOn { get; set; }
34+
//public Task BuildTask { get; set; }
35+
public CountdownEvent CountDown { get; } = new CountdownEvent(1);
3336
}
3437
}

0 commit comments

Comments
 (0)