/// <summary> /// 写文件 /// </summary> /// <param name="state"></param> private void WriteFile(object state) { ArrayList arr = state as ArrayList; string content = arr[0].ToString(); //如果页面内容中包含指定的验证字符串则生成 if (string.IsNullOrEmpty(validateString) || content.Contains(validateString)) { //内容进行编码处理 string dynamicurl = arr[1].ToString(); string staticurl = filePath; string extension = Path.GetExtension(staticurl); if (extension != null && extension.ToLower() == ".js") { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n//<!-- 生成方式:被动生成 -->\r\n//<!-- 更新时间:{0} -->\r\n//<!-- 动态URL:{1} -->\r\n//<!-- 静态URL:{2} -->", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), dynamicurl, RemoveRootPath(staticurl), content.Trim()); } else { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n<!-- 生成方式:被动生成 -->\r\n<!-- 更新时间:{0} -->\r\n<!-- 动态URL:{1} -->\r\n<!-- 静态URL:{2} -->", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), dynamicurl, RemoveRootPath(staticurl), content.Trim()); } //将内容写入文件 StaticPageManager.SaveFile(content, filePath, encoding); } }
/// <summary> /// 对页面进行更新 /// </summary> void IUpdateItem.Update(DateTime updateTime) { if (!(this as IUpdateItem).NeedUpdate(updateTime)) { return; } updateComplete = false; //如果未能全部生成成功,则生成失败列表 if (updateTime != DateTime.MaxValue && !staticPageDependency.UpdateSuccess) { var errors = Update(updateTime, updateErrorList); //添加到异常列表 if (errors.Count > 0) { updateErrorList.AddRange(errors); } } else { var dictPosition = new Dictionary <string, int>(); var dictValues = new Dictionary <string, IList <object> >(); foreach (var paramInfo in paramInfos) { try { if (!dictValues.ContainsKey(paramInfo.ParamName)) { var objlist = new List <object>(paramInfo.GetResult()); dictValues[paramInfo.ParamName] = objlist; dictPosition[paramInfo.ParamName] = 0; } } catch (Exception ex) { StaticPageManager.SaveError(new StaticPageException(string.Format("获取参数{0}的值出错,URL:{1}!", paramInfo.ParamName, templatePath), ex)); return; } } try { int count = GetPageCount(dictValues); var items = new List <UpdateItem>(); string dynamicurl = templatePath; string staticurl = savePath; if (!string.IsNullOrEmpty(query)) { dynamicurl = string.Format("{0}?{1}", dynamicurl, query); } for (int index = 0; index < count; index++) { var tmpDynamicUrl = dynamicurl; var tmpStaticUrl = staticurl; //生成对应的url foreach (string key in dictValues.Keys) { var value = dictValues[key][dictPosition[key]].ToString(); //动态地址 tmpDynamicUrl = tmpDynamicUrl.Replace(key, value); //静态地址 tmpStaticUrl = tmpStaticUrl.Replace(key, value); } //添加到队列中 items.Add(new UpdateItem { DynamicUrl = tmpDynamicUrl, StaticPath = tmpStaticUrl }); //定位处理 SetPosition(dictPosition, dictValues, dictPosition.Keys.Count - 1); } if (items.Count > 0) { //更新页面, 只有一个线程时 if (threadCount == 1) { var errors = Update(updateTime, items); //添加到异常列表 if (errors.Count > 0) { updateErrorList.AddRange(errors); } } else { int pageSize = (int)Math.Ceiling(items.Count / (threadCount * 1.0)); //分页生成静态页 var events = new AutoResetEvent[threadCount]; for (int index = 0; index < threadCount; index++) { events[index] = new AutoResetEvent(false); var updateItems = new List <UpdateItem>(); if (items.Count >= (index + 1) * pageSize) { updateItems = items.GetRange(index * pageSize, pageSize); } else { updateItems = items.GetRange(index * pageSize, items.Count - (index * pageSize)); } var thread = new Thread(state => { if (state == null) { return; } var arr = state as ArrayList; var list = arr[0] as List <UpdateItem>; var reset = arr[1] as AutoResetEvent; var errors = Update(updateTime, list); //添加到异常列表 if (errors.Count > 0) { lock (updateErrorList) { updateErrorList.AddRange(errors); } } reset.Set(); }); //启动线程 thread.Start(new ArrayList { updateItems, events[index] }); } //等待所有响应 WaitHandle.WaitAll(events); } } } catch (Exception ex) { StaticPageManager.SaveError(new StaticPageException("创建URL列表出错! Error => " + ex.Message, ex)); } } //全部生成成功才设置最后更新时间 if (updateTime == DateTime.MaxValue) { staticPageDependency.LastUpdateTime = DateTime.Now; } else { staticPageDependency.LastUpdateTime = updateTime; } if (!staticPageDependency.UpdateSuccess) { //全部生成成功才设置最后更新时间,否则往后推5分钟重新生成 staticPageDependency.LastUpdateTime = staticPageDependency.LastUpdateTime.AddMinutes(retryInterval); } //更新完成 updateComplete = true; }
/// <summary> /// 对页面进行更新 /// </summary> private IList <UpdateItem> Update(DateTime updateTime, IList <UpdateItem> items) { var errorList = new List <UpdateItem>(); try { foreach (var item in items) { try { string content = null; if (isRemote) { content = StaticPageManager.GetRemotePageString(item.DynamicUrl, inEncoding, validateString); } else { content = StaticPageManager.GetLocalPageString(item.Path, item.Query, inEncoding, validateString); } DateTime createTime = DateTime.Now; //开始生成 if (OnStart != null) { try { OnStart(createTime, item.DynamicUrl, item.StaticUrl); } catch { }; } //生成时回调 if (Callback != null) { try { content = Callback(item.DynamicUrl, item.StaticUrl, content); } catch { }; } string extension = Path.GetExtension(item.StaticPath); if (extension != null && extension.ToLower() == ".js") { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n//<!-- 生成方式:主动生成 -->\r\n//<!-- 更新时间:{0} -->\r\n//<!-- 动态URL:{1} -->\r\n//<!-- 静态URL:{2} -->", createTime.ToString("yyyy-MM-dd HH:mm:ss"), item.DynamicUrl, item.StaticUrl, content.Trim()); } else { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n<!-- 生成方式:主动生成 -->\r\n<!-- 更新时间:{0} -->\r\n<!-- 动态URL:{1} -->\r\n<!-- 静态URL:{2} -->", createTime.ToString("yyyy-MM-dd HH:mm:ss"), item.DynamicUrl, item.StaticUrl, content.Trim()); } StaticPageManager.SaveFile(content, item.StaticPath, outEncoding); //结束生成 if (OnComplete != null) { try { OnComplete(createTime, item.DynamicUrl, item.StaticUrl); } catch { }; } //置状态为生成成功 item.UpdateSuccess = true; } catch (Exception ex) { //置状态为生成失败 item.UpdateSuccess = false; StaticPageManager.SaveError(new StaticPageException(string.Format("单个生成静态文件失败,等待重新生成!\r\n{0} => {1}", item.DynamicUrl, item.StaticUrl), ex)); } } //未全部更新成功 if (items.Any(p => !p.UpdateSuccess)) { errorList = items.Where(p => !p.UpdateSuccess).ToList(); string html = string.Join("\r\n", errorList.Select(p => string.Format("{0} => {1}", p.DynamicUrl, p.StaticUrl)).ToArray()); throw new StaticPageException(string.Format("批量生成【{0}】个静态页失败,【{1}】分钟后重新生成!\r\n{2}", errorList.Count, retryInterval, html)); } staticPageDependency.UpdateSuccess = true; } catch (Exception ex) { //如果出错,则继续往下执行 if (ex is StaticPageException) { StaticPageManager.SaveError(ex as StaticPageException); } staticPageDependency.UpdateSuccess = false; } finally { //设置最后更新时间 //staticPageDependency.LastUpdateTime = updateTime; } return(errorList); }
/// <summary> /// 对页面进行更新 /// </summary> void IUpdateItem.Update(DateTime updateTime) { if (!(this as IUpdateItem).NeedUpdate(updateTime)) { return; } updateComplete = false; string dynamicurl = templatePath; string staticurl = savePath; if (!string.IsNullOrEmpty(query)) { dynamicurl = string.Format("{0}?{1}", dynamicurl, query); } var item = new UpdateItem { DynamicUrl = dynamicurl, StaticPath = staticurl }; try { string content = null; if (isRemote) { content = StaticPageManager.GetRemotePageString(item.DynamicUrl, inEncoding, validateString); } else { content = StaticPageManager.GetLocalPageString(item.Path, item.Query, inEncoding, validateString); } DateTime createTime = DateTime.Now; //开始生成 if (OnStart != null) { try { OnStart(createTime, item.DynamicUrl, item.StaticUrl); } catch (Exception ex) { }; } //生成时回调 if (Callback != null) { try { content = Callback(item.DynamicUrl, item.StaticUrl, content); } catch (Exception ex) { }; } string extension = Path.GetExtension(item.StaticPath); if (extension != null && extension.ToLower() == ".js") { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n//<!-- 生成方式:主动生成 -->\r\n//<!-- 更新时间:{0} -->\r\n//<!-- 动态URL:{1} -->\r\n//<!-- 静态URL:{2} -->", createTime.ToString("yyyy-MM-dd HH:mm:ss"), item.DynamicUrl, item.StaticUrl, content.Trim()); } else { //加入静态页生成元素 content = string.Format("{3}\r\n\r\n<!-- 生成方式:主动生成 -->\r\n<!-- 更新时间:{0} -->\r\n<!-- 动态URL:{1} -->\r\n<!-- 静态URL:{2} -->", createTime.ToString("yyyy-MM-dd HH:mm:ss"), item.DynamicUrl, item.StaticUrl, content.Trim()); } StaticPageManager.SaveFile(content, item.StaticPath, outEncoding); //结束生成 if (OnComplete != null) { try { OnComplete(createTime, item.DynamicUrl, item.StaticUrl); } catch (Exception ex) { }; } staticPageDependency.UpdateSuccess = true; } catch (Exception ex) { StaticPageManager.SaveError(new StaticPageException(string.Format("单个生成静态文件失败,【{2}】分钟后重新生成!\r\n{0} => {1}", item.DynamicUrl, item.StaticUrl, retryInterval), ex)); //如果出错,则继续往下执行 staticPageDependency.UpdateSuccess = false; } finally { //设置最后更新时间 //staticPageDependency.LastUpdateTime = updateTime; } //全部生成成功才设置最后更新时间 if (updateTime == DateTime.MaxValue) { staticPageDependency.LastUpdateTime = DateTime.Now; } else { staticPageDependency.LastUpdateTime = updateTime; } if (!staticPageDependency.UpdateSuccess) { //全部生成成功才设置最后更新时间,否则往后推5分钟重新生成 staticPageDependency.LastUpdateTime = staticPageDependency.LastUpdateTime.AddMinutes(retryInterval); } updateComplete = true; }