Browse Source

Update counter

Justin 1 year ago
parent
commit
05a30df7e8
4 changed files with 193 additions and 24 deletions
  1. 71 0
      StationProbe/Counter.cs
  2. 117 7
      StationProbe/DelayConfig.cs
  3. 3 2
      StationProbe/Program.cs
  4. 2 15
      StationProbe/SuperImageTask.cs

+ 71 - 0
StationProbe/Counter.cs

@@ -0,0 +1,71 @@
+
+namespace StationProbe
+{
+    internal class Counter
+    {
+        private static Timer? _timer;
+
+        private static List<string> _messages = new List<string>();
+
+        private static int _hourSeconds;
+        private static int _hourExams;
+        private static int _fourHourExams;
+        private static int _totalExams;
+
+        private static DateTime _startTime;
+
+        public static void Initialize()
+        {
+            _timer = new Timer(TimerCallback);
+            _timer.Change(0, 1000);
+            _startTime = DateTime.Now;
+        }
+
+        private static void TimerCallback(object? stateInfo)
+        {
+            _hourSeconds++;
+            if(_hourSeconds >= 3600)
+            {
+                var sts = _startTime.ToString("yyyy-MM-dd HH:mm:ss");
+                var ets = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+
+                _hourSeconds = 0;
+                _hourExams = 0;
+                _startTime = DateTime.Now;
+
+                var hourStr = $"[{sts}-{ets}] {_hourExams} exams processed.";
+                Logger.WriteLine(hourStr);
+
+                //One hour
+                _messages.Add(hourStr);
+
+                if(_messages.Count >= 4)
+                {
+                    _messages.Add($"{_fourHourExams} exams processed in 4 hours.");
+                    _messages.Add($"{_totalExams} exams processed.");
+                    var sendMessage = string.Join(Environment.NewLine, _messages);
+                    Task.Run(async()=> 
+                    {
+                        try
+                        {
+                            await DingTalk.SendMessageAsync(sendMessage);
+                        }
+                        catch (Exception dx)
+                        {
+                            Logger.WriteLine($"Send dingtalk message error:{dx.Message}");
+                        }
+                    });
+                    _fourHourExams = 0;
+                    _messages.Clear();
+                }               
+            }
+        }
+
+        public static void IncreaseExamCount()
+        {
+            _hourExams++;
+            _fourHourExams++;
+            _totalExams++;
+        }
+    }
+}

+ 117 - 7
StationProbe/DelayConfig.cs

@@ -2,18 +2,128 @@
 {
     internal class DelayConfig
     {
-        public const int JumpDelay = 10000;
+        private static bool _isNight; 
 
-        public const int FilterDelay = 15000;
+        public static bool IsNight
+        {
+            get
+            {
+                var isNight = DateTime.Now.Hour > 19 && DateTime.Now.Hour < 7;
+                if(_isNight != isNight)
+                {
+                    _isNight = isNight;
+                    if(_isNight)
+                    {
+                        Task.Run(async () => 
+                        {
+                            try
+                            {
+                                await DingTalk.SendMessageAsync("Switch to night mode");
+                            }
+                            catch (Exception dx)
+                            {
+                                Logger.WriteLine($"Send dingtalk message error:{dx.Message}");
+                            }
+                        });
+                    }
+                    else
+                    {
+                        Task.Run(async () =>
+                        {
+                            try
+                            {
+                                await DingTalk.SendMessageAsync("Switch to normal mode");
+                            }
+                            catch (Exception dx)
+                            {
+                                Logger.WriteLine($"Send dingtalk message error:{dx.Message}");
+                            }
+                        });
+                    }
+                }
+                return _isNight;
+            }
+        }
 
-        public const int SaveImageDelay = 1000;
+        public static int JumpDelay
+        {
+            get
+            {
+                if(IsNight)
+                {
+                    return 3000;
+                }
+                return 9000;
+            }
+        }
 
-        public const int RetryDelay = 10000;
+        public static int FilterDelay
+        {
+            get
+            {
+                if (IsNight)
+                {
+                    return 9000;
+                }
+                return 15000;
+            }
+        }
 
-        public const int ExamContentDelay = 2000;
+        public static int SaveImageDelay
+        {
+            get
+            {
+                if (IsNight)
+                {
+                    return 250;
+                }
+                return 500;
+            }
+        }
 
-        public const int KillChromeDelay = 2000;
+        public static int RetryDelay
+        {
+            get
+            {
+                if (IsNight)
+                {
+                    return 3000;
+                }
+                return 5000;
+            }
+        }
 
-        public const int TaskDelay = 2000;
+
+        public static int ExamContentDelay
+        {
+            get
+            {
+                if (IsNight)
+                {
+                    return 500;
+                }
+                return 1000;
+            }
+        }
+
+        public static int KillChromeDelay
+        {
+            get
+            {
+                return 2000;
+            }
+        }
+
+        public static int TaskDelay
+        {
+            get
+            {
+                if (IsNight)
+                {
+                    return 1000;
+                }
+                return 1500;
+            }
+        }
     }
 }

+ 3 - 2
StationProbe/Program.cs

@@ -3,6 +3,7 @@ using PuppeteerSharp;
 using StationProbe;
 using System.Diagnostics;
 
+
 if (args.Length == 0)
 {
     Console.WriteLine("Use one of 'create','run','list' commands to start this application.");
@@ -32,7 +33,7 @@ else
         {
             Console.WriteLine("Downloading headless browser...");
             await new BrowserFetcher().DownloadAsync();
-
+            Counter.Initialize();
             bool error = true;
             while (error)
             {
@@ -68,7 +69,7 @@ else
                     error = true;
                     try
                     {
-                        await DingTalk.SendMessageAsync($"Run super image task error:{ex.Message}, will restart in 10s.");
+                        await DingTalk.SendMessageAsync($"Run super image task error:{ex.Message}, will restart in {DelayConfig.RetryDelay/1000}s.");
                     }
                     catch(Exception dx)
                     {

+ 2 - 15
StationProbe/SuperImageTask.cs

@@ -109,7 +109,6 @@ namespace StationProbe
                 }
             }
             Logger.WriteLine($"Start from page: {_pageIndex}, exam index: {_examIndexInPage}");
-            int processedExams = 0;
             while (_pageIndex < _batchTask.PageCount)
             {
                 Logger.WriteLine($"Start page task (page:{_pageIndex})");
@@ -120,7 +119,7 @@ namespace StationProbe
                     var currentExamCountinPage = _pageIndex < (_batchTask.PageCount - 1) ? _examCountInPage : (_batchTask.ExamCount - _pageIndex * _examCountInPage);
                     while (_examIndexInPage < currentExamCountinPage)
                     {
-                        Logger.WriteLine($"Start exam task (exam index: {_examIndexInPage})...");
+                        Logger.WriteLine($"Start exam task (exam index: {_examIndexInPage}, page index:{_pageIndex})...");
                         try
                         {
                             outputs = await new ExamTask(_examStore, _batchTask.Id, _pageIndex, _examIndex ,_examIndexInPage).ExecuteAsync(outputs);
@@ -132,19 +131,7 @@ namespace StationProbe
                         }
                         _examIndex++;
                         _examIndexInPage++;
-                        processedExams++;
-                        if (processedExams % 150 == 0)
-                        {
-                            //Notify per 10 pages.
-                            try
-                            {
-                                await DingTalk.SendMessageAsync($"{processedExams} exams processed.");
-                            }
-                            catch (Exception dx)
-                            {
-                                Logger.WriteLine($"Send dingtalk message error:{dx.Message}");
-                            }
-                        }
+                        Counter.IncreaseExamCount();
                         await Task.Delay(DelayConfig.TaskDelay);
                     }
                     _examIndexInPage = 0;