示例#1
0
        public static void StartViewsCountUpdateTask(WikiPassageService wikiPassageService)
        {
            try
            {
                Ensure.NotNull(wikiPassageService, nameof(wikiPassageService));

                if (WikiPassageService.viewsCountTask != null)
                {
                    return;
                }

                WikiPassageService.viewsCountTask = Task.Run(() =>
                {
                    while (true)
                    {
                        LockViewsDictionary(dic =>
                        {
                            lock (WikiPassageService._viewsCountTaskLock)
                            {
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > item in dic)
                                {
                                    int passageId     = item.Key;
                                    int newViewsCount = item.Value.Select(viewsCountDto => viewsCountDto.Value.NewViews).ToList().Sum();

                                    if (newViewsCount > 0)
                                    {
                                        WikiPassage wikiPassage      = wikiPassageService.GetById(passageId);
                                        wikiPassage.TotalViewsCount += newViewsCount;
                                        _ = wikiPassageService.UpdateAsync(wikiPassage, false);
                                    }
                                }
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > passageViewsDictionary in dic)
                                {
                                    foreach (KeyValuePair <string, ViewsCountDto> viewsCountItem in passageViewsDictionary.Value)
                                    {
                                        viewsCountItem.Value.NewViews = 0;
                                    }
                                }
                            }
                        });
                        if (RunAs.Debug)
                        {
                            Thread.Sleep(5 * 1000);     //5秒
                        }
                        if (RunAs.Release)
                        {
                            Thread.Sleep(20 * 1000);   //20秒
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                new LogService().Log(message: "访问量统计:新增访问量出现异常。",
                                     parameters: new { ex = ex.ToString(), wikiPassageService = wikiPassageService.ToString() });
            }
        }
示例#2
0
        public static void StartViewsCountUpdateTask(WikiPassageService wikiPassageService)
        {
            LogService logService = new LogService();

            try
            {
                Ensure.NotNull(wikiPassageService, nameof(wikiPassageService));

                logService.Log(message: "访问量统计:浏览量统计任务初始化。",
                               parameters: new { wikiPassageService = wikiPassageService.ToString() });

                if (WikiPassageService.viewsCountTask != null)
                {
                    logService.Log(message: "访问量统计:已启动过统计任务,重复启动无效,已中止。",
                                   parameters: new { wikiPassageService = wikiPassageService.ToString() });
                    return;
                }

                WikiPassageService.viewsCountTask = Task.Run(() =>
                {
                    logService.Log(message: "访问量统计:启动了定时任务,即将进入 while(true) 循环。",
                                   parameters: new { wikiPassageService = wikiPassageService.ToString() });
                    while (true)
                    {
                        logService.Log(message: "访问量统计:执行了 while(true) 循环。",
                                       parameters: new { wikiPassageService = wikiPassageService.ToString() });
                        LockViewsDictionary(dic =>
                        {
                            logService.Log(message: "访问量统计:锁定了访问缓存列表。",
                                           parameters: new { ViewsDictionary = dic });
                            lock (WikiPassageService._viewsCountTaskLock)
                            {
                                logService.Log("访问量统计:准备遍历访问数据缓存。");
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > item in dic)
                                {
                                    int passageId     = item.Key;
                                    int newViewsCount = item.Value.Select(viewsCountDto => viewsCountDto.Value.NewViews).ToList().Sum();

                                    logService.Log(message: "访问量统计:正在遍历访问缓存,读取到数据。",
                                                   parameters: new { passageId, newViewsCount });

                                    if (newViewsCount > 0)
                                    {
                                        WikiPassage wikiPassage      = wikiPassageService.GetById(passageId);
                                        wikiPassage.TotalViewsCount += newViewsCount;
                                        _ = wikiPassageService.UpdateAsync(wikiPassage);
                                        logService.Log(message: "访问量统计:读取缓存成功,持久化了新的文章访问统计数量。",
                                                       parameters: new { passageId, newViewsCount });
                                    }
                                }
                                logService.Log("访问量统计:准备遍历文章的访问量缓存,将所有访问量重置为 0 。");
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > passageViewsDictionary in dic)
                                {
                                    foreach (KeyValuePair <string, ViewsCountDto> viewsCountItem in passageViewsDictionary.Value)
                                    {
                                        viewsCountItem.Value.NewViews = 0;
                                        logService.Log(message: "访问量统计:重置了文章的访问量。",
                                                       parameters: new { passageId = viewsCountItem.Key, newViewsCount = viewsCountItem.Value.NewViews });
                                    }
                                }
                            }
                        });
                        if (RunAs.Debug)
                        {
                            logService.Log("访问量统计:访问量统计进入休眠状态。Debug:5秒");
                            Thread.Sleep(5 * 1000);     //5秒
                        }
                        if (RunAs.Release)
                        {
                            logService.Log("访问量统计:访问量统计进入休眠状态。Debug:3分钟");
                            Thread.Sleep(180 * 1000);   //180秒=3分钟
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                logService.Log(message: "访问量统计:新增访问量出现异常。",
                               parameters: new { ex = ex.ToString(), wikiPassageService = wikiPassageService.ToString() });
            }
        }
示例#3
0
        public async static Task StartViewsCountUpdateTask()
        {
            try
            {
                if (WikiPassageService.viewsCountTask != null)
                {
                    return;
                }

                WikiPassageService.viewsCountTask = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            LockViewsDictionary(async dic =>
                            {
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > item in dic)
                                {
                                    int passageId     = item.Key;
                                    int newViewsCount = item.Value.Select(viewsCountDto => viewsCountDto.Value.NewViews).ToList().Sum();

                                    if (newViewsCount > 0)
                                    {
                                        WikiPassageService wikiPassageService = GlobalService.ServiceProvider.GetService <WikiPassageService>();
                                        WikiPassage wikiPassage      = await wikiPassageService.GetById(passageId);
                                        wikiPassage.TotalViewsCount += newViewsCount;
                                        await wikiPassageService.UpdateAsync(wikiPassage, false);
                                    }
                                }
                                foreach (KeyValuePair <int, Dictionary <string, ViewsCountDto> > passageViewsDictionary in dic)
                                {
                                    foreach (KeyValuePair <string, ViewsCountDto> viewsCountItem in passageViewsDictionary.Value)
                                    {
                                        viewsCountItem.Value.NewViews = 0;
                                    }
                                }
                                _ = GlobalService.ServiceProvider.GetService <RedisService>().ObjectSetAsync(StringConsts.ViewsCountDictionary, dic);
                            });
                            if (RunAs.Debug)
                            {
                                Thread.Sleep(5 * 1000);     //5秒
                            }
                            if (RunAs.Release)
                            {
                                Thread.Sleep(20 * 1000);   //20秒
                            }
                        }
                        catch (Exception ex)
                        {
                            File.AppendAllText("counter-log.txt", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "counter catch ex inside task: " + Environment.NewLine + ex.ToString());
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                File.AppendAllText("counter-log.txt", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "ex:" + ex.ToString() + Environment.NewLine);
                await new LogService().Log(message: "访问量统计:启动访问量统计系统前出错",
                                           parameters: new { ex = ex.ToString() });
            }
        }