Permalink
Browse files

Fix: Clearing commands become a blocking action.

We should add a status to the queue: "Waiting for the current task to finish..." to prevent the user from double requesting.
Currently I have disabled the control + renabled in a finally block.
  • Loading branch information...
Cazzar committed Feb 28, 2018
1 parent 1d00241 commit 31e20468c21e040330d3f1e823fd396c99338663
@@ -157,6 +157,11 @@ public void Init()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

while (workerCommands.IsBusy)
{
Thread.Sleep(200);
}

processingCommands = true;
QueueState = new QueueStateStruct
{
@@ -193,10 +198,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
while (true)
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

// if paused we will sleep for 5 seconds, and the try again
// we will remove the pause if it was set more than 12 hours ago
@@ -206,10 +208,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
try
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

TimeSpan ts = DateTime.Now - pauseTime.Value;
if (ts.TotalHours >= 12)
@@ -232,10 +231,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
}

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

ICommandRequest icr = CommandHelper.GetCommand(crdb);
if (icr == null)
@@ -247,10 +243,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
QueueState = icr.PrettyDescription;

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

logger.Trace("Processing command request: {0}", crdb.CommandID);
try
@@ -179,10 +179,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
while (true)
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

// if paused we will sleep for 5 seconds, and the try again
// we will remove the pause if it was set more than 6 hours ago
@@ -192,10 +189,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
try
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}
TimeSpan ts = DateTime.Now - pauseTime.Value;
if (ts.TotalHours >= 6)
Paused = false;
@@ -217,10 +211,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
}

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

ICommandRequest icr = CommandHelper.GetCommand(crdb);
if (icr == null)
@@ -232,16 +223,18 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
QueueState = icr.PrettyDescription;

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

icr.ProcessCommand();

RepoFactory.CommandRequest.Delete(crdb.CommandRequestID);
QueueCount = RepoFactory.CommandRequest.GetQueuedCommandCountHasher();
}
}

public void UpdateQueue()
{
QueueCount = RepoFactory.CommandRequest.GetQueuedCommandCountHasher();
}
}
}
@@ -141,7 +141,6 @@ void WorkerCommands_RunWorkerCompleted(object sender, RunWorkerCompletedEventArg
public void Init()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

processingCommands = true;

QueueState = new QueueStateStruct
@@ -180,10 +179,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
while (true)
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

// if paused we will sleep for 5 seconds, and the try again
// we will remove the pause if it was set more than 6 hours ago
@@ -193,10 +189,7 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
try
{
if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

TimeSpan ts = DateTime.Now - pauseTime.Value;
if (ts.TotalHours >= 6)
@@ -214,20 +207,14 @@ void WorkerCommands_DoWork(object sender, DoWorkEventArgs e)
if (crdb == null) return;

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

ICommandRequest icr = CommandHelper.GetCommand(crdb);
if (icr == null)
return;

if (workerCommands.CancellationPending)
{
e.Cancel = true;
return;
}

QueueState = icr.PrettyDescription;

@@ -327,17 +327,16 @@ void btnUploadAzureCache_Click(object sender, RoutedEventArgs e)
}
}

void btnImagesClear_Click(object sender, RoutedEventArgs e)
async void btnImagesClear_Click(object sender, RoutedEventArgs e)
{
btnImagesClear.IsEnabled = true;
Task task = new Task(() =>
{
ShokoService.CmdProcessorImages.Stop();

// wait until the queue stops
while (ShokoService.CmdProcessorImages.ProcessingCommands)
{
while (ShokoService.CmdProcessorImages.ProcessingCommands || ShokoService.CmdProcessorImages.IsWorkerBusy)
Thread.Sleep(200);
}
Thread.Sleep(200);

RepoFactory.CommandRequest.Delete(RepoFactory.CommandRequest.GetAllCommandRequestImages());
@@ -347,77 +346,75 @@ void btnImagesClear_Click(object sender, RoutedEventArgs e)

try
{
Cursor = Cursors.Wait;
task.Start();
task.Wait();
Cursor = Cursors.Arrow;
await task;
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex.Message);
}
finally
{
btnImagesClear.IsEnabled = true;
}
}

void btnGeneralClear_Click(object sender, RoutedEventArgs e)
async void btnGeneralClear_Click(object sender, RoutedEventArgs e)
{
btnGeneralClear.IsEnabled = false;
Task task = new Task(() =>
{
ShokoService.CmdProcessorGeneral.Stop();

// wait until the queue stops
while (ShokoService.CmdProcessorGeneral.ProcessingCommands)
{
Thread.Sleep(200);
}
ShokoService.CmdProcessorGeneral.Stop();
while (ShokoService.CmdProcessorGeneral.ProcessingCommands || ShokoService.CmdProcessorGeneral.IsWorkerBusy)
Thread.Sleep(200);
Thread.Sleep(200);

RepoFactory.CommandRequest.Delete(RepoFactory.CommandRequest.GetAllCommandRequestGeneral());

ShokoService.CmdProcessorGeneral.Init();
RepoFactory.CommandRequest.Delete(RepoFactory.CommandRequest.GetAllCommandRequestGeneral());
ShokoService.CmdProcessorHasher.Init();
});

try
{
Cursor = Cursors.Wait;
task.Start();
task.Wait();
Cursor = Cursors.Arrow;
await task;
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex.Message);
}
finally
{
btnGeneralClear.IsEnabled = true;
}
}

void btnHasherClear_Click(object sender, RoutedEventArgs e)
async void btnHasherClear_Click(object sender, RoutedEventArgs e)
{
btnHasherClear.IsEnabled = false;
Task task = new Task(() =>
{
ShokoService.CmdProcessorHasher.Stop();

// wait until the queue stops
while (ShokoService.CmdProcessorHasher.ProcessingCommands)
{
while (ShokoService.CmdProcessorHasher.ProcessingCommands || ShokoService.CmdProcessorHasher.IsWorkerBusy)
Thread.Sleep(200);
}
Thread.Sleep(200);

RepoFactory.CommandRequest.Delete(RepoFactory.CommandRequest.GetAllCommandRequestHasher());

ShokoService.CmdProcessorHasher.Init();
});

try
{
Cursor = Cursors.Wait;
task.Start();
task.Wait();
Cursor = Cursors.Arrow;
await task;
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex.Message);
}
finally
{
btnHasherClear.IsEnabled = true;
}
}


0 comments on commit 31e2046

Please sign in to comment.