Skip to content

Commit 25eebc2

Browse files
authored
Merge pull request #93 from Erol444/hot-fix-weekly-26-03-2022
Hot fix weekly 26 03 2022
2 parents 62608e2 + afacbd9 commit 25eebc2

File tree

15 files changed

+185
-165
lines changed

15 files changed

+185
-165
lines changed

.github/workflows/pre-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: 'Create changelog'
3939
id: Changelog
4040
run: |
41-
changelog="$({github.event.pull_request.body})"
41+
changelog="${{github.event.pull_request.body}}"
4242
changelog="${changelog//'%'/'%25'}"
4343
changelog="${changelog//$'\n'/'%0A'}"
4444
changelog="${changelog//$'\r'/'%0D'}"

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: 'Create changelog'
4141
id: Changelog
4242
run: |
43-
changelog="$({github.event.pull_request.body})"
43+
changelog="${{github.event.pull_request.body}}"
4444
changelog="${changelog//'%'/'%25'}"
4545
changelog="${changelog//$'\n'/'%0A'}"
4646
changelog="${changelog//$'\r'/'%0D'}"

TbsCore/Core/TaskTimer.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace TbsCore.Models.AccModels
1010
{
1111
public sealed class TaskTimer : IDisposable
1212
{
13+
private readonly Random _random;
1314
private readonly Account _acc;
1415
private readonly Timer _mainTimer;
1516

@@ -49,6 +50,7 @@ public TaskTimer(Account Account)
4950
_acc = Account;
5051
_mainTimer = new Timer(500);
5152
_subTimer = new Timer(500);
53+
_random = new Random();
5254
_mainTimer.Elapsed += MainTimerElapsed;
5355
_subTimer.Elapsed += SubTimerElapsed;
5456
}
@@ -132,25 +134,32 @@ private async void NewTick()
132134

133135
private void NoTasks(Account _acc)
134136
{
135-
BotTask task = null;
136-
137-
if (_acc.Settings.AutoCloseDriver &&
138-
TimeSpan.FromMinutes(5) < TimeHelper.NextPrioTask(_acc, TaskPriority.Medium))
139-
{
140-
// Auto close chrome and reopen when there is a high/normal prio BotTask
141-
task = new ReopenDriver();
142-
((ReopenDriver)task).LowestPrio = TaskPriority.Medium;
143-
}
144-
else if (_acc.Settings.AutoRandomTasks)
137+
if (_acc.Settings.AutoCloseDriver)
145138
{
146-
task = new RandomTask();
139+
var nextTask = _acc.Tasks.ToList().FirstOrDefault();
140+
var delay = TimeSpan.FromMinutes(5);
141+
if (nextTask == null || nextTask.ExecuteAt - DateTime.Now > delay)
142+
{
143+
_acc.Tasks.Add(new ReopenDriver()
144+
{
145+
ExecuteAt = DateTime.Now,
146+
});
147+
return;
148+
}
147149
}
148150

149-
if (task != null)
151+
if (_acc.Settings.AutoRandomTasks)
150152
{
151-
task.ExecuteAt = DateTime.Now;
152-
task.Priority = TaskPriority.Low;
153-
_acc.Tasks.Add(task);
153+
var nextTask = _acc.Tasks.ToList().FirstOrDefault();
154+
var delay = TimeSpan.FromMinutes(20);
155+
if (nextTask == null || nextTask.ExecuteAt - DateTime.Now > delay)
156+
{
157+
_acc.Tasks.Add(new RandomTask()
158+
{
159+
ExecuteAt = DateTime.Now.AddSeconds(_random.Next(60, 600)),
160+
});
161+
return;
162+
}
154163
}
155164
}
156165

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public void Close()
288288

289289
public void Dispose()
290290
{
291+
Close();
291292
chromeService.Dispose();
292293
}
293294
}

TbsCore/Helpers/AccountHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public static void StartAccountTasks(Account acc)
7676
acc.Tasks.Add(new Sleep()
7777
{
7878
ExecuteAt = DateTime.Now + TimeHelper.GetWorkTime(acc),
79-
AutoSleep = true
8079
}, true);
8180

8281
// Access change

TbsCore/Helpers/IoHelperCore.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ public static void SaveAccounts(List<Account> accounts, bool logout)
161161
{
162162
foreach (var acc in accounts)
163163
{
164-
if (logout) Logout(acc);
164+
if (logout)
165+
{
166+
Logout(acc);
167+
acc.Wb?.Dispose();
168+
acc.TaskTimer?.Dispose();
169+
acc.WebhookClient?.Dispose();
170+
}
165171
acc.Tasks.Save();
166172
DbRepository.SaveAccount(acc);
167173
}
@@ -175,12 +181,6 @@ public static async Task<bool> LoginAccount(Account acc)
175181
{
176182
if (acc.Wb == null)
177183
{
178-
SerilogSingleton.LogOutput.AddUsername(acc.AccInfo.Nickname);
179-
180-
acc.Logger = new Logger(acc.AccInfo.Nickname);
181-
182-
acc.Villages.ForEach(vill => vill.UnfinishedTasks = new List<VillUnfinishedTask>());
183-
184184
acc.Wb = new WebBrowserInfo();
185185
var opened = await acc.Wb.Init(acc);
186186
if (!opened)
@@ -209,16 +209,8 @@ public static async Task<bool> LoginAccount(Account acc)
209209
/// <param name="acc"></param>
210210
public static void Logout(Account acc)
211211
{
212-
if (acc.TaskTimer != null)
213-
{
214-
acc.TaskTimer.Dispose();
215-
acc.TaskTimer = default;
216-
}
217-
if (acc.Wb != null)
218-
{
219-
acc.Wb.Dispose();
220-
acc.Wb = default;
221-
}
212+
acc.TaskTimer.Stop();
213+
acc.Wb?.Close();
222214
}
223215
}
224216
}

TbsCore/Helpers/TimeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static async Task SleepUntilPrioTask(Account acc, TaskPriority lowestPrio
102102
do
103103
{
104104
await Task.Delay(1000);
105-
nextTask = TimeHelper.NextPrioTask(acc, lowestPrio);
105+
nextTask = NextPrioTask(acc, lowestPrio);
106106

107107
var log = $"Chrome will reopen in {(int)nextTask.TotalMinutes} min";
108108
if (log != previousLog)

TbsCore/Models/Logging/LogOutput.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
3-
using System.ComponentModel;
44

55
namespace TbsCore.Models.Logging
66
{
77
public class LogOutput
88
{
9+
private LogOutput()
10+
{
11+
}
12+
13+
private static LogOutput instance = null;
14+
15+
public static LogOutput Instance
16+
{
17+
get
18+
{
19+
if (instance == null)
20+
{
21+
instance = new LogOutput();
22+
}
23+
return instance;
24+
}
25+
}
26+
927
public event EventHandler<UpdateLogEventArgs> LogUpdated;
1028

11-
private IDictionary<string, LinkedList<string>> _logs = new Dictionary<string, LinkedList<string>>();
12-
private readonly object _syncRoot = new object();
29+
private readonly ConcurrentDictionary<string, LinkedList<string>> _logs = new ConcurrentDictionary<string, LinkedList<string>>();
1330

1431
public string GetLog(string username)
1532
{
1633
if (_logs.ContainsKey(username))
1734
{
18-
lock (_syncRoot)
19-
{
20-
var log = string.Join("", _logs[username]);
21-
return log;
22-
}
35+
var log = string.Join("", _logs[username]);
36+
return log;
2337
}
2438
return "";
2539
}
@@ -29,31 +43,21 @@ public string GetLastLog(string username)
2943
return _logs.ContainsKey(username) ? _logs[username].First.Value : "";
3044
}
3145

32-
/// <summary>
33-
/// lock before use because as i read Dictionary is not theard safe
34-
/// </summary>
35-
/// <param name="username"></param>
36-
/// <param name="message"></param>
3746
public void Add(string username, string message)
3847
{
39-
lock (_syncRoot)
48+
_logs[username].AddFirst(message);
49+
// keeps 200 message
50+
while (_logs[username].Count > 200)
4051
{
41-
_logs[username].AddFirst(message);
42-
// keeps 200 message
43-
while (_logs[username].Count > 200)
44-
{
45-
_logs[username].RemoveLast();
46-
}
52+
_logs[username].RemoveLast();
4753
}
54+
4855
OnUpdateLog(username);
4956
}
5057

5158
public void AddUsername(string username)
5259
{
53-
if (!_logs.ContainsKey(username))
54-
{
55-
_logs.Add(username, new LinkedList<string>());
56-
}
60+
_logs.TryAdd(username, new LinkedList<string>());
5761
}
5862

5963
private void OnUpdateLog(string username)

TbsCore/Models/Logging/SerilogSingleton.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33

44
namespace TbsCore.Models.Logging
55
{
6-
public static class SerilogSingleton
6+
public class SerilogSingleton
77
{
8-
public static LogOutput LogOutput = new LogOutput();
9-
108
public static void Init()
119
{
1210
Log.Logger = new LoggerConfiguration()
1311
.WriteTo.Map("Username", "Other", (name, wt) => wt.File($"./logs/log-{name}-.txt",
1412
rollingInterval: RollingInterval.Day,
1513
encoding: Encoding.Unicode,
1614
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}"))
17-
.WriteTo.TbsSink(LogOutput)
15+
.WriteTo.TbsSink()
1816
.CreateLogger();
1917
}
18+
19+
public static void Close()
20+
{
21+
Log.CloseAndFlush();
22+
}
2023
}
2124
}

TbsCore/Models/Logging/Sink.cs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,49 @@
1-
using System;
2-
using System.IO;
3-
using System.Collections.Generic;
4-
5-
using Serilog;
1+
using Serilog;
2+
using Serilog.Configuration;
63
using Serilog.Core;
74
using Serilog.Events;
85
using Serilog.Formatting;
9-
using Serilog.Configuration;
106
using Serilog.Formatting.Display;
7+
using System;
8+
using System.IO;
119

1210
namespace TbsCore.Models.Logging
1311
{
1412
public class TbsSink : ILogEventSink
1513
{
1614
private readonly ITextFormatter _textFormatter;
17-
private readonly LogOutput _logs;
18-
private readonly object _syncRoot = new object();
1915

20-
public TbsSink(LogOutput logs, ITextFormatter textFormatter)
16+
public TbsSink(ITextFormatter textFormatter)
2117
{
22-
if (textFormatter == null) throw new ArgumentNullException(nameof(textFormatter));
23-
24-
_logs = logs;
2518
_textFormatter = textFormatter;
2619
}
2720

2821
public void Emit(LogEvent logEvent)
2922
{
30-
lock (_syncRoot)
31-
{
32-
var stringWriter = new StringWriter();
23+
var stringWriter = new StringWriter();
3324

34-
_textFormatter.Format(logEvent, stringWriter);
35-
LogEventPropertyValue username;
36-
logEvent.Properties.TryGetValue("Username", out username);
37-
// toString problem, it turn {"username"} to \"username\"
38-
// replace here to solve that
39-
_logs.Add(username.ToString().Replace("\"", ""), stringWriter.ToString());
40-
}
25+
_textFormatter.Format(logEvent, stringWriter);
26+
logEvent.Properties.TryGetValue("Username", out LogEventPropertyValue username);
27+
// toString problem, it turn {"username"} to \"username\"
28+
// replace here to solve that
29+
LogOutput.Instance.Add(username.ToString().Replace("\"", ""), stringWriter.ToString());
4130
}
4231
}
43-
}
4432

45-
namespace Serilog
46-
{
47-
public static class TBSLogExtensions
33+
public static class TbsSinkExtensions
4834
{
4935
private const string DefaultOutputTemplate = "{Timestamp:HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}";
5036

5137
public static LoggerConfiguration TbsSink(
5238
this LoggerSinkConfiguration loggerConfiguration,
53-
TbsCore.Models.Logging.LogOutput logs,
5439
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
5540
string outputTemplate = DefaultOutputTemplate,
5641
IFormatProvider formatProvider = null,
5742
LoggingLevelSwitch levelSwitch = null)
5843
{
59-
if (logs == null) throw new ArgumentNullException(nameof(logs));
60-
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
6144
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
62-
var sink = new TbsCore.Models.Logging.TbsSink(logs, formatter);
63-
64-
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
65-
}
66-
67-
public static LoggerConfiguration TbsSink(
68-
this LoggerSinkConfiguration loggerConfiguration,
69-
ITextFormatter formatter,
70-
TbsCore.Models.Logging.LogOutput logs,
71-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
72-
LoggingLevelSwitch levelSwitch = null)
73-
{
74-
if (logs == null) throw new ArgumentNullException(nameof(logs));
75-
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
45+
var sink = new TbsSink(formatter);
7646

77-
var sink = new TbsCore.Models.Logging.TbsSink(logs, formatter);
7847
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
7948
}
8049
}

0 commit comments

Comments
 (0)