Example #1
0
        /// <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);
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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;
        }