Skip to content

Commit 30e2b52

Browse files
authored
Merge pull request #66 from XiaoHeitu/优化_多线程构建
添加并行构建功能,添加依赖任务配置
2 parents 4e80a8d + f0cd498 commit 30e2b52

File tree

7 files changed

+130
-17
lines changed

7 files changed

+130
-17
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

+1
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

src/SmartCode.Generator/GeneratorProjectBuilder.cs

+99-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
15
using System.Threading.Tasks;
26
using Microsoft.Extensions.Logging;
37
using SmartCode.Configuration;
@@ -20,28 +24,118 @@ Project project
2024
_logger = logger;
2125
}
2226

27+
CountdownEvent countdown = new CountdownEvent(1);
28+
2329

2430
public async Task Build()
2531
{
26-
BuildContext buildContext = null;
2732
var dataSource = _pluginManager.Resolve<IDataSource>(_project.DataSource.Name);
2833
await dataSource.InitData();
34+
35+
if (_project.AllowParallel)
36+
{
37+
await this.ParallelBuild(dataSource);
38+
}
39+
else
40+
{
41+
await this.SerialBuild(dataSource);
42+
}
43+
}
44+
45+
public async Task SerialBuild(IDataSource dataSource)
46+
{
2947
foreach (var buildKV in _project.BuildTasks)
3048
{
3149
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} Start! ---------");
32-
var output = buildKV.Value.Output;
33-
buildContext = new BuildContext
50+
var context = new BuildContext
3451
{
3552
PluginManager = _pluginManager,
3653
Project = _project,
3754
DataSource = dataSource,
3855
BuildKey = buildKV.Key,
3956
Build = buildKV.Value,
40-
Output = output?.Copy()
57+
Output = buildKV.Value.Output?.Copy(),
4158
};
42-
await _pluginManager.Resolve<IBuildTask>(buildKV.Value.Type).Build(buildContext);
59+
60+
//执行自身任务
61+
await _pluginManager.Resolve<IBuildTask>(context.Build.Type).Build(context);
62+
4363
_logger.LogInformation($"-------- BuildTask:{buildKV.Key} End! ---------");
4464
}
4565
}
66+
67+
private Task ParallelBuild(IDataSource dataSource)
68+
{
69+
70+
IList<BuildContext> allContexts = _project.BuildTasks.Select(d => new BuildContext
71+
{
72+
PluginManager = _pluginManager,
73+
Project = _project,
74+
DataSource = dataSource,
75+
BuildKey = d.Key,
76+
Build = d.Value,
77+
Output = d.Value.Output?.Copy(),
78+
}).ToArray();
79+
foreach (var context in allContexts)
80+
{
81+
if (context.Build.DependOn != null && context.Build.DependOn.Count() > 0)
82+
{
83+
context.DependOn = allContexts.Where(d => context.Build.DependOn.Contains(d.BuildKey)).ToArray();
84+
}
85+
}
86+
countdown.Reset();
87+
countdown.AddCount(allContexts.Count);
88+
foreach (var context in allContexts)
89+
{
90+
context.CountDown.Reset();
91+
if (context.DependOn != null && context.DependOn.Count > 0)
92+
{
93+
context.CountDown.AddCount(context.DependOn.Count);
94+
}
95+
96+
ThreadPool.QueueUserWorkItem((obj) => _ = this.BuildTask(obj), (context, allContexts));
97+
}
98+
99+
foreach (var context in allContexts)
100+
{
101+
context.CountDown.Signal();
102+
}
103+
104+
countdown.Signal();
105+
countdown.Wait();
106+
107+
return Task.CompletedTask;
108+
}
109+
110+
private async Task BuildTask(object obj)
111+
{
112+
var p = ((BuildContext context, IList<BuildContext> allContexts))obj;
113+
114+
if (p.context.DependOn != null && p.context.DependOn.Count > 0)
115+
{
116+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Wait [{string.Join(",", p.context.DependOn?.Select(d => d.BuildKey)?.ToArray())}]---------");
117+
}
118+
//等待依赖任务
119+
p.context.CountDown.Wait();
120+
121+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} Start! ---------");
122+
//执行自身任务
123+
await _pluginManager.Resolve<IBuildTask>(p.context.Build.Type).Build(p.context);
124+
125+
foreach (var c in p.allContexts)
126+
{
127+
if (c.DependOn == null || c.DependOn.Count == 0)
128+
{
129+
continue;
130+
}
131+
if (c.DependOn.Contains(p.context))
132+
{
133+
c.CountDown.Signal();
134+
}
135+
}
136+
137+
countdown.Signal();
138+
_logger.LogInformation($"-------- BuildTask:{p.context.BuildKey} End! ---------");
139+
}
46140
}
47141
}

src/SmartCode/BuildContext.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading;
5+
using System.Threading.Tasks;
46
using SmartCode.Configuration;
57

68
namespace SmartCode
79
{
810
public class BuildContext
911
{
12+
1013
public Project Project { get; set; }
1114
public IDataSource DataSource { get; set; }
1215
public IPluginManager PluginManager { get; set; }
@@ -26,5 +29,9 @@ public void SetItem(string key, object item)
2629
{
2730
Items[key] = item;
2831
}
32+
33+
public IList<BuildContext> DependOn { get; set; }
34+
//public Task BuildTask { get; set; }
35+
public CountdownEvent CountDown { get; } = new CountdownEvent(1);
2936
}
3037
}

src/SmartCode/Configuration/Build.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ public class Build
1313
/// </summary>
1414
public String Type { get; set; }
1515
public String Module { get; set; }
16-
public TemplateEngine TemplateEngine { get; set; }
16+
public TemplateEngine TemplateEngine { get; set; }
1717
public Output Output { get; set; }
1818
public IEnumerable<String> IncludeTables { get; set; }
1919
public IEnumerable<String> IgnoreTables { get; set; }
2020
public bool? IgnoreNoPKTable { get; set; }
2121
public bool? IgnoreView { get; set; }
2222
public NamingConverter NamingConverter { get; set; }
2323
/// <summary>
24+
/// 依赖于
25+
/// </summary>
26+
public IEnumerable<String> DependOn { get; set; }
27+
/// <summary>
2428
/// 自定义构建参数
2529
/// </summary>
2630
public IDictionary<String, object> Parameters { get; set; }

src/SmartCode/Configuration/Project.cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Project
1111
public String ConfigPath { get; set; }
1212
public String Module { get; set; }
1313
public String Author { get; set; }
14+
public bool AllowParallel { get; set; } = false;
1415
public ProjectMode? Mode { get; set; }
1516
public DataSource DataSource { get; set; }
1617
public TemplateEngine TemplateEngine { get; set; } = TemplateEngine.Default;

0 commit comments

Comments
 (0)