mirror of
https://github.com/SlothDpal/Relaunch-Process.git
synced 2026-02-22 17:27:38 +03:00
queue fixes
This commit is contained in:
@@ -12,16 +12,24 @@ namespace Discord.Webhook
|
|||||||
public class DiscordWebhook
|
public class DiscordWebhook
|
||||||
{
|
{
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
public int queueRetryCount = 3;
|
||||||
|
public int sendTimeoutSeconds = 5;
|
||||||
|
|
||||||
private UInt64 totalMessages = 0;
|
private UInt64 totalMessages = 0;
|
||||||
private readonly ConcurrentQueue<(UInt64 num, DiscordMessage message, FileInfo[] files)> _queue = new ConcurrentQueue<(UInt64 num, DiscordMessage, FileInfo[])>();
|
private ConcurrentQueue<(UInt64 num, DiscordMessage message, FileInfo[] files)> _queue = new ConcurrentQueue<(UInt64 num, DiscordMessage, FileInfo[])>();
|
||||||
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
||||||
private bool _isProcessing;
|
private bool _isProcessing;
|
||||||
private CancellationTokenSource _cts = new CancellationTokenSource();
|
private CancellationTokenSource _cts = new CancellationTokenSource();
|
||||||
|
private int queueErrorCounter = 0;
|
||||||
|
private int queueSuppressedCounter = 0;
|
||||||
|
private HttpRequestException lastHpptEx = null;
|
||||||
|
|
||||||
public UInt64 GetTotalMessages() => totalMessages;
|
public UInt64 TotalMessages => totalMessages;
|
||||||
public int GetQueueSize() => _queue.Count;
|
public int QueueSize => _queue.Count;
|
||||||
public void CancelProcessing() => _cts.Cancel();
|
public void CancelProcessing() => _cts.Cancel();
|
||||||
|
public int ErrorCount => queueErrorCounter;
|
||||||
|
public bool IsProcessing => _isProcessing;
|
||||||
|
public HttpRequestException LastHpptEx => lastHpptEx;
|
||||||
|
|
||||||
public async Task<bool> SendAsync(DiscordMessage message, params FileInfo[] files)
|
public async Task<bool> SendAsync(DiscordMessage message, params FileInfo[] files)
|
||||||
{
|
{
|
||||||
@@ -30,7 +38,7 @@ namespace Discord.Webhook
|
|||||||
|
|
||||||
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
|
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
|
||||||
|
|
||||||
using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(30) })
|
using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(sendTimeoutSeconds) })
|
||||||
using (var content = new MultipartFormDataContent(boundary))
|
using (var content = new MultipartFormDataContent(boundary))
|
||||||
{
|
{
|
||||||
// Добавляем JSON payload
|
// Добавляем JSON payload
|
||||||
@@ -64,12 +72,13 @@ namespace Discord.Webhook
|
|||||||
}
|
}
|
||||||
catch (HttpRequestException ex)
|
catch (HttpRequestException ex)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Discord webhook request failed: {ex.Message}");
|
Debug.WriteLine($"SendAsync: Discord webhook request failed: {ex.Message}");
|
||||||
|
lastHpptEx = ex;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (TaskCanceledException ex)
|
catch (TaskCanceledException ex)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Discord webhook request cancelled: {ex.Message}");
|
Debug.WriteLine($"SendAsync: Discord webhook request cancelled: {ex.Message}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,20 +87,34 @@ namespace Discord.Webhook
|
|||||||
|
|
||||||
private async Task ProcessQueueAsync()
|
private async Task ProcessQueueAsync()
|
||||||
{
|
{
|
||||||
|
queueErrorCounter = 0;
|
||||||
|
|
||||||
while (_queue.TryPeek(out var item))
|
while (_queue.TryPeek(out var item))
|
||||||
{
|
{
|
||||||
if (_cts.Token.IsCancellationRequested)
|
if (_cts.Token.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Discord queue processing cancelled.");
|
Debug.WriteLine("ProcessQueueAsync: Discord queue processing cancelled.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await _semaphore.WaitAsync();
|
await _semaphore.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Processing message {item.num}. Queue size: {_queue.Count}");
|
Debug.WriteLine($"ProcessQueueAsync: Processing message {item.num}. Queue size: {_queue.Count}");
|
||||||
if (await SendAsync(item.message, item.files))
|
if (await SendAsync(item.message, item.files))
|
||||||
{
|
{
|
||||||
_queue.TryDequeue(out var deqItem);
|
_queue.TryDequeue(out var deqItem);
|
||||||
|
queueErrorCounter = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
queueErrorCounter++;
|
||||||
|
if (queueErrorCounter == queueRetryCount)
|
||||||
|
{
|
||||||
|
_queue.TryDequeue(out var deqItem);
|
||||||
|
queueErrorCounter = 0;
|
||||||
|
queueSuppressedCounter++;
|
||||||
|
Debug.WriteLine($"ProcessQueueAsync: Message dropped. Total messages dropped:{queueSuppressedCounter}. Queue size: {_queue.Count}.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -104,10 +127,18 @@ namespace Discord.Webhook
|
|||||||
}
|
}
|
||||||
catch (TaskCanceledException)
|
catch (TaskCanceledException)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Discord queue processing cancelled during delay. Was {_queue.Count} messages in queue. {totalMessages} messages in session. ");
|
Debug.WriteLine($"ProcessQueueAsync: Discord queue processing cancelled during delay. Was {_queue.Count} messages in queue. {totalMessages} messages in session. ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( _cts.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"ProcessQueueAsync: Discord queue processing cancelled. Was {_queue.Count} messages in queue.");
|
||||||
|
Debug.WriteLine("Clearing queue.");
|
||||||
|
var _newqueue = new ConcurrentQueue<(UInt64 num, DiscordMessage, FileInfo[])>();
|
||||||
|
Interlocked.Exchange(ref _queue, _newqueue);
|
||||||
|
}
|
||||||
|
Debug.WriteLine($"ProcessQueueAsync: Discord queue processing finished.");
|
||||||
_isProcessing = false;
|
_isProcessing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +156,7 @@ namespace Discord.Webhook
|
|||||||
_cts = new CancellationTokenSource();
|
_cts = new CancellationTokenSource();
|
||||||
_isProcessing = true;
|
_isProcessing = true;
|
||||||
Task.Run(ProcessQueueAsync);
|
Task.Run(ProcessQueueAsync);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,15 +9,19 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using Process_Auto_Relaunch;
|
||||||
using RelaunchProcess.Properties;
|
using RelaunchProcess.Properties;
|
||||||
|
|
||||||
namespace RelaunchProcess
|
namespace RelaunchProcess
|
||||||
{
|
{
|
||||||
public partial class WebhookSettings : Form
|
public partial class WebhookSettings : Form
|
||||||
{
|
{
|
||||||
public WebhookSettings()
|
private Form1 parent;
|
||||||
|
|
||||||
|
public WebhookSettings(Form1 _parent)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
parent = _parent;
|
||||||
RestoreSettings();
|
RestoreSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +68,10 @@ namespace RelaunchProcess
|
|||||||
{
|
{
|
||||||
chbxDiscordEnabled.Checked = false;
|
chbxDiscordEnabled.Checked = false;
|
||||||
}
|
}
|
||||||
|
if (!chbxDiscordEnabled.Checked)
|
||||||
|
{
|
||||||
|
parent.dwhHook.CancelProcessing();
|
||||||
|
}
|
||||||
SaveSettings();
|
SaveSettings();
|
||||||
DialogResult = DialogResult.OK;
|
DialogResult = DialogResult.OK;
|
||||||
Close();
|
Close();
|
||||||
|
|||||||
6
Form1.cs
6
Form1.cs
@@ -32,7 +32,7 @@ namespace Process_Auto_Relaunch
|
|||||||
}
|
}
|
||||||
private delegate void UpdateLogDelegate(string text, NotifyLevel level = NotifyLevel.logUpdateStatus);
|
private delegate void UpdateLogDelegate(string text, NotifyLevel level = NotifyLevel.logUpdateStatus);
|
||||||
private readonly UpdateLogDelegate updateLogDelegate;
|
private readonly UpdateLogDelegate updateLogDelegate;
|
||||||
private DiscordWebhook dwhHook;
|
public DiscordWebhook dwhHook;
|
||||||
private DiscordMessage dwhMessage;
|
private DiscordMessage dwhMessage;
|
||||||
private Process WatchedProcess;
|
private Process WatchedProcess;
|
||||||
private double cpuLastTime = 0;
|
private double cpuLastTime = 0;
|
||||||
@@ -268,8 +268,8 @@ namespace Process_Auto_Relaunch
|
|||||||
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
||||||
{
|
{
|
||||||
dwhHook.CancelProcessing();
|
dwhHook.CancelProcessing();
|
||||||
Settings.Default.Save();
|
|
||||||
Status("Наблюдение отменено - приложение закрыто.", NotifyLevel.logAlways);
|
Status("Наблюдение отменено - приложение закрыто.", NotifyLevel.logAlways);
|
||||||
|
Settings.Default.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ProcessByNameIsRuning(string name)
|
private bool ProcessByNameIsRuning(string name)
|
||||||
@@ -495,7 +495,7 @@ namespace Process_Auto_Relaunch
|
|||||||
private void webhookDiscordToolStripMenuItem_Click(object sender, EventArgs e)
|
private void webhookDiscordToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
WebhookSettings discordSettings;
|
WebhookSettings discordSettings;
|
||||||
discordSettings = new WebhookSettings();
|
discordSettings = new WebhookSettings(this);
|
||||||
discordSettings.ShowDialog(this);
|
discordSettings.ShowDialog(this);
|
||||||
discordSettings.Dispose();
|
discordSettings.Dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ using System.Runtime.InteropServices;
|
|||||||
// Номер сборки
|
// Номер сборки
|
||||||
// Редакция
|
// Редакция
|
||||||
//
|
//
|
||||||
[assembly: AssemblyVersion("1.6.0.32")]
|
[assembly: AssemblyVersion("1.6.1.2")]
|
||||||
[assembly: AssemblyFileVersion("1.6.0.32")]
|
[assembly: AssemblyFileVersion("1.6.1.2")]
|
||||||
|
|
||||||
[assembly: AssemblyInformationalVersion("1.6.0.32")]
|
[assembly: AssemblyInformationalVersion("1.6.1.2")]
|
||||||
Reference in New Issue
Block a user