Skip to content

Commit 73d887b

Browse files
authored
Merge pull request siteserver#54 from siteserverekun/master
后台可以配置是否启用多线程生成页面
2 parents 3993d34 + 1ce795c commit 73d887b

File tree

9 files changed

+187
-17
lines changed

9 files changed

+187
-17
lines changed

source/SiteServer.BackgroundPages/Cms/PageConfigurationCreate.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class PageConfigurationCreate : BasePageCms
2929
public PlaceHolder phIsCreateStaticContentByAddDate;
3030
public DateTimeTextBox tbCreateStaticContentAddDate;
3131

32+
public RadioButtonList IsCreateMultiThread; // 是否启用多线程生成页面
33+
3234
public void Page_Load(object sender, EventArgs e)
3335
{
3436
if (IsForbidden) return;
@@ -78,8 +80,12 @@ public void Page_Load(object sender, EventArgs e)
7880
{
7981
tbCreateStaticContentAddDate.DateTime = PublishmentSystemInfo.Additional.CreateStaticContentAddDate;
8082
}
83+
84+
EBooleanUtils.AddListItems(IsCreateMultiThread, "启用", "不启用");
85+
ControlUtils.SelectListItemsIgnoreCase(IsCreateMultiThread, PublishmentSystemInfo.Additional.IsCreateMultiThread.ToString());
86+
8187
}
82-
}
88+
}
8389

8490
public void IsCreateStaticContentByAddDate_SelectedIndexChanged(object sender, EventArgs e)
8591
{
@@ -108,7 +114,9 @@ public override void Submit_OnClick(object sender, EventArgs e)
108114
if (PublishmentSystemInfo.Additional.IsCreateStaticContentByAddDate)
109115
{
110116
PublishmentSystemInfo.Additional.CreateStaticContentAddDate = tbCreateStaticContentAddDate.DateTime;
111-
}
117+
}
118+
119+
PublishmentSystemInfo.Additional.IsCreateMultiThread = TranslateUtils.ToBool(IsCreateMultiThread.SelectedValue);
112120

113121
try
114122
{

source/SiteServer.CMS/Core/Create/CreateTaskManager.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface ICreateTaskManager
1313

1414
CreateTaskInfo GetLastPendingTask();
1515

16+
List<CreateTaskInfo> GetLastPendingTasks(int topNum);
17+
1618
void RemovePendingAndAddSuccessLog(CreateTaskInfo taskInfo, string timeSpan);
1719

1820
void RemovePendingAndAddFailureLog(CreateTaskInfo taskInfo, Exception ex);
@@ -121,6 +123,38 @@ public CreateTaskInfo GetLastPendingTask()
121123
return null;
122124
}
123125

126+
public List<CreateTaskInfo> GetLastPendingTasks(int topNum)
127+
{
128+
List<CreateTaskInfo> list = null;
129+
130+
foreach (var entry in PendingTaskDict)
131+
{
132+
var pendingTasks = entry.Value;
133+
if (pendingTasks.Count > 0)
134+
{
135+
list = new List<CreateTaskInfo>();
136+
if (pendingTasks.Count >= topNum)
137+
{
138+
while (topNum > 0)
139+
{
140+
list.Add(pendingTasks[pendingTasks.Count - topNum]);
141+
topNum--;
142+
}
143+
}
144+
else
145+
{
146+
foreach (var taskInfo in pendingTasks)
147+
{
148+
list.Add(taskInfo);
149+
}
150+
}
151+
152+
return list;
153+
}
154+
}
155+
return list;
156+
}
157+
124158
public void RemovePendingAndAddSuccessLog(CreateTaskInfo taskInfo, string timeSpan)
125159
{
126160
var pendingTasks = GetPendingTasks(taskInfo.PublishmentSystemID);
@@ -240,6 +274,11 @@ public CreateTaskInfo GetLastPendingTask()
240274
return DataProvider.CreateTaskDao.GetLastPendingTask();
241275
}
242276

277+
public List<CreateTaskInfo> GetLastPendingTasks(int topNum)
278+
{
279+
return DataProvider.CreateTaskDao.GetLastPendingTasks(topNum);
280+
}
281+
243282
public void RemovePendingAndAddSuccessLog(CreateTaskInfo taskInfo, string timeSpan)
244283
{
245284
DataProvider.CreateTaskDao.Delete(taskInfo.ID);

source/SiteServer.CMS/Model/PublishmentSystemInfoExtend.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ public DateTime CreateStaticContentAddDate
348348
set { SetExtendedAttribute("CreateStaticContentAddDate", DateUtils.GetDateString(value)); }
349349
}
350350

351+
public bool IsCreateMultiThread
352+
{
353+
get { return GetBool("IsCreateMultiThread", false); }
354+
set { SetExtendedAttribute("IsCreateMultiThread", value.ToString()); }
355+
}
356+
351357
/****************站点地图设置********************/
352358

353359
public string SiteMapGooglePath

source/SiteServer.CMS/Provider/CreateTaskDao.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ public CreateTaskInfo GetLastPendingTask()
163163
return info;
164164
}
165165

166+
/// <summary>
167+
/// Ò»´Î»ñÈ¡¶à¸öÈÎÎñ
168+
/// </summary>
169+
/// <param name="topNum"></param>
170+
/// <returns></returns>
171+
public List<CreateTaskInfo> GetLastPendingTasks(int topNum)
172+
{
173+
List<CreateTaskInfo> list = null;
174+
175+
var sqlString = SqlUtils.GetTopSqlString("siteserver_CreateTask", "ID, CreateType, PublishmentSystemID, ChannelID, ContentID, TemplateID", "ORDER BY ID", topNum);
176+
177+
using (var rdr = ExecuteReader(sqlString))
178+
{
179+
while (rdr.Read())
180+
{
181+
var i = 0;
182+
var info = new CreateTaskInfo(GetInt(rdr, i++), ECreateTypeUtils.GetEnumType(GetString(rdr, i++)), GetInt(rdr, i++), GetInt(rdr, i++), GetInt(rdr, i++), GetInt(rdr, i));
183+
list.Add(info);
184+
}
185+
rdr.Close();
186+
}
187+
return list;
188+
}
189+
166190
public bool IsPendingTask()
167191
{
168192
var retval = false;

source/SiteServer.CMS/StlParser/FileSystemObject.cs

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
24
using System.Text;
35
using BaiRong.Core;
46
using BaiRong.Core.Model.Attributes;
@@ -171,22 +173,46 @@ public void CreateChannel(int nodeId)
171173

172174
StlUtility.ParseStl(PublishmentSystemInfo, pageInfo, contextInfo, contentBuilder, filePath, false);
173175

174-
for (var currentPageIndex = 0; currentPageIndex < pageCount; currentPageIndex++)
175-
{
176-
var thePageInfo = new PageInfo(pageInfo.PageNodeId, pageInfo.PageContentId, pageInfo.PublishmentSystemInfo, pageInfo.TemplateInfo, null);
177-
var pageHtml = pageContentsElementParser.Parse(totalNum, currentPageIndex, pageCount, true);
178-
var pagedBuilder = new StringBuilder(contentBuilder.ToString().Replace(stlElementTranslated, pageHtml));
176+
//for (var currentPageIndex = 0; currentPageIndex < pageCount; currentPageIndex++)
177+
//{
178+
// PageContentsDetail(filePath, pageInfo, stlLabelList, pageContentsElementParser, contentBuilder, stlElementTranslated, totalNum, pageCount, currentPageIndex);
179179

180-
StlParserManager.ReplacePageElementsInChannelPage(pagedBuilder, thePageInfo, stlLabelList, thePageInfo.PageNodeId, currentPageIndex, pageCount, totalNum);
180+
// var thePageInfo = new PageInfo(pageInfo.PageNodeId, pageInfo.PageContentId, pageInfo.PublishmentSystemInfo, pageInfo.TemplateInfo, null);
181+
// var pageHtml = pageContentsElementParser.Parse(totalNum, currentPageIndex, pageCount, true);
182+
// var pagedBuilder = new StringBuilder(contentBuilder.ToString().Replace(stlElementTranslated, pageHtml));
181183

182-
filePath = PathUtility.GetChannelPageFilePath(PublishmentSystemInfo, thePageInfo.PageNodeId, currentPageIndex);
183-
thePageInfo.AddLastPageScript(pageInfo);
184+
// StlParserManager.ReplacePageElementsInChannelPage(pagedBuilder, thePageInfo, stlLabelList, thePageInfo.PageNodeId, currentPageIndex, pageCount, totalNum);
184185

185-
GenerateFile(filePath, pageInfo.TemplateInfo.Charset, pagedBuilder);
186+
// filePath = PathUtility.GetChannelPageFilePath(PublishmentSystemInfo, thePageInfo.PageNodeId, currentPageIndex);
187+
// thePageInfo.AddLastPageScript(pageInfo);
186188

187-
thePageInfo.ClearLastPageScript(pageInfo);
188-
pageInfo.ClearLastPageScript();
189+
// GenerateFile(filePath, pageInfo.TemplateInfo.Charset, pagedBuilder);
190+
191+
// thePageInfo.ClearLastPageScript(pageInfo);
192+
// pageInfo.ClearLastPageScript();
193+
//}
194+
195+
if (PublishmentSystemInfo.Additional.IsCreateMultiThread) // 多线程并发生成页面
196+
{
197+
for (int i = 1; i <= pageCount; i = i + 3)
198+
{
199+
var list = new List<int>();
200+
list.Add(i);
201+
if (i <= pageCount - 1)
202+
list.Add(i + 1);
203+
if (i <= pageCount - 2)
204+
list.Add(i + 2);
205+
Parallel.ForEach(list, currentPageIndex => PageContentsDetail(filePath, pageInfo, stlLabelList, pageContentsElementParser, contentBuilder, stlElementTranslated, totalNum, pageCount, currentPageIndex));
206+
}
189207
}
208+
else // 单线程生成页面
209+
{
210+
for (var currentPageIndex = 0; currentPageIndex < pageCount; currentPageIndex++)
211+
{
212+
PageContentsDetail(filePath, pageInfo, stlLabelList, pageContentsElementParser, contentBuilder, stlElementTranslated, totalNum, pageCount, currentPageIndex);
213+
}
214+
}
215+
190216
}
191217
//如果标签中存在<stl:pageChannels>
192218
else if (StlParserUtility.IsStlElementExists(StlPageChannels.ElementName, stlLabelList))
@@ -267,16 +293,67 @@ public void CreateChannel(int nodeId)
267293
}
268294
}
269295

296+
/// <summary>
297+
/// 处理栏目列表页分页标签PageContents
298+
/// </summary>
299+
/// <param name="filePath"></param>
300+
/// <param name="pageInfo"></param>
301+
/// <param name="stlLabelList"></param>
302+
/// <param name="pageContentsElementParser"></param>
303+
/// <param name="contentBuilder"></param>
304+
/// <param name="stlElementTranslated"></param>
305+
/// <param name="totalNum"></param>
306+
/// <param name="pageCount"></param>
307+
private void PageContentsDetail(string filePath, PageInfo pageInfo,List<string> stlLabelList, StlPageContents pageContentsElementParser, StringBuilder contentBuilder, string stlElementTranslated,int totalNum,int pageCount, int currentPageIndex)
308+
{
309+
var thePageInfo = new PageInfo(pageInfo.PageNodeId, pageInfo.PageContentId, pageInfo.PublishmentSystemInfo, pageInfo.TemplateInfo, null);
310+
var pageHtml = pageContentsElementParser.Parse(totalNum, currentPageIndex, pageCount, true);
311+
var pagedBuilder = new StringBuilder(contentBuilder.ToString().Replace(stlElementTranslated, pageHtml));
312+
313+
StlParserManager.ReplacePageElementsInChannelPage(pagedBuilder, thePageInfo, stlLabelList, thePageInfo.PageNodeId, currentPageIndex, pageCount, totalNum);
314+
315+
filePath = PathUtility.GetChannelPageFilePath(PublishmentSystemInfo, thePageInfo.PageNodeId, currentPageIndex);
316+
thePageInfo.AddLastPageScript(pageInfo);
317+
318+
GenerateFile(filePath, pageInfo.TemplateInfo.Charset, pagedBuilder);
319+
320+
thePageInfo.ClearLastPageScript(pageInfo);
321+
pageInfo.ClearLastPageScript();
322+
}
323+
270324
public void CreateContents(int nodeId)
271325
{
272326
var nodeInfo = NodeManager.GetNodeInfo(PublishmentSystemId, nodeId);
273327
var tableStyle = NodeManager.GetTableStyle(PublishmentSystemInfo, nodeInfo);
274328
var tableName = NodeManager.GetTableName(PublishmentSystemInfo, nodeInfo);
275329
var orderByString = ETaxisTypeUtils.GetContentOrderByString(ETaxisType.OrderByTaxisDesc);
276330
var contentIdList = DataProvider.ContentDao.GetContentIdListChecked(tableName, nodeId, orderByString);
277-
foreach (var contentId in contentIdList)
331+
332+
//foreach (var contentId in contentIdList)
333+
//{
334+
// CreateContent(tableStyle, tableName, nodeId, contentId);
335+
//}
336+
337+
if (PublishmentSystemInfo.Additional.IsCreateMultiThread) // 多线程并发生成页面
278338
{
279-
CreateContent(tableStyle, tableName, nodeId, contentId);
339+
for (int i = 0; i < contentIdList.Count; i = i + 3)
340+
{
341+
var list = new List<int>();
342+
list.Add(contentIdList[i]);
343+
if (i < contentIdList.Count - 1)
344+
list.Add(contentIdList[i + 1]);
345+
if (i < contentIdList.Count - 2)
346+
list.Add(contentIdList[i + 2]);
347+
Parallel.ForEach(list, contentId => CreateContent(tableStyle, tableName, nodeId, contentId));
348+
}
349+
}
350+
else // 单线程生成页面
351+
{
352+
foreach (var contentId in contentIdList)
353+
{
354+
CreateContent(tableStyle, tableName, nodeId, contentId);
355+
356+
}
280357
}
281358
}
282359

source/SiteServer.Web/CreateHub.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
24
using BaiRong.Core;
5+
using SiteServer.CMS.Model;
36
using Microsoft.AspNet.SignalR;
47
using SiteServer.CMS.Core;
58
using SiteServer.CMS.Core.Create;
@@ -42,7 +45,7 @@ public void Execute()
4245
Clients.Client(Context.ConnectionId).next(true);
4346
}
4447
}
45-
}
48+
}
4649

4750
public void GetTasks(int publishmentSystemId)
4851
{

source/SiteServer.Web/SiteServer/Cms/pageConfigurationCreate.aspx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@
9191
</td>
9292
</tr>
9393
</asp:PlaceHolder>
94+
<tr>
95+
<td>是否启用多线程生成页面:</td>
96+
<td>
97+
<asp:RadioButtonList ID="IsCreateMultiThread" RepeatDirection="Horizontal" class="noborder" runat="server"></asp:RadioButtonList>
98+
<span>此功能通常用于CMS服务器配置较高而且现在生成页面时CPU和内存利用率不太高(不超过60%)时建议启用</span>
99+
</td>
100+
</tr>
94101
</table>
95102

96103
<hr />

source/SiteServer.Web/SiteServer/assets/sql/sqlserver/upgrade.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ GO
113113
CREATE INDEX IX_siteserver_Comment_ContentID ON siteserver_Comment(ContentID)
114114
GO
115115

116+
ALTER TABLE siteserver_PublishmentSystem ADD
117+
PublishmentSystemType varchar(50) NULL
118+
GO
119+
116120
CREATE TABLE wx_Account(
117121
Id int IDENTITY(1,1),
118122
PublishmentSystemId int NULL,

source/siteserver/ExecutionManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
24
using BaiRong.Core;
35
using SiteServer.CMS.Core;
46
using SiteServer.CMS.Core.Create;
@@ -52,7 +54,7 @@ public static bool ExecutePendingCreate()
5254
}
5355

5456
return false;
55-
}
57+
}
5658

5759
public static bool ExecuteTask()
5860
{

0 commit comments

Comments
 (0)