Merge pull request #21 from kc284/master

CA-127510: Reworked the logic of differentiating between tasks started b...
This commit is contained in:
Gabor Apati-Nagy 2014-03-12 17:02:12 +01:00
commit 05feef5633
6 changed files with 73 additions and 82 deletions

View File

@ -32,6 +32,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using XenAdmin.Actions.GUIActions;
using XenAdmin.Network;
using System.Collections.ObjectModel;
using XenAdmin.Actions;
@ -93,7 +95,7 @@ namespace XenAdmin.Commands
return PromptAndDisconnectServer(connection);
}
// no prompt. All tasks are cancelled and the server d/c
// no prompt. All tasks are cancelled and the server disconnected
ConnectionsManager.CancelAllActions(connection);
DoDisconnect(connection);
return true;
@ -106,35 +108,37 @@ namespace XenAdmin.Commands
/// <returns>True if the user agreed to d/c and cancel their tasks, false if we are going to remain connected</returns>
private bool PromptAndDisconnectServer(IXenConnection connection)
{
if (!AsyncAction.AllActionsFinishedOrCanceling(connection,ConnectionsManager.History))
if (!AllActionsFinished(connection, true))
{
if (MainWindowCommandInterface.RunInAutomatedTestMode || new CloseXenCenterWarningDialog(connection).ShowDialog(Parent) == DialogResult.OK)
if (MainWindowCommandInterface.RunInAutomatedTestMode ||
new CloseXenCenterWarningDialog(connection).ShowDialog(Parent) == DialogResult.OK)
{
ConnectionsManager.CancelAllActions(connection);
DelegatedAsyncAction WaitForCancelAction = new DelegatedAsyncAction(connection, Messages.CANCELING_TASKS, Messages.CANCELING, Messages.COMPLETED,
delegate(Session s)
DelegatedAsyncAction waitForCancelAction = new DelegatedAsyncAction(connection,
Messages.CANCELING_TASKS, Messages.CANCELING, Messages.COMPLETED,
delegate
{
DateTime startTime = DateTime.Now;
while (((TimeSpan)(DateTime.Now - startTime)).TotalSeconds < 6.0)
while ((DateTime.Now - startTime).TotalSeconds < 6.0)
{
if (ConnectionsManager.AllActionsFinished(connection))
if (AllActionsFinished(connection, false))
break;
else
Thread.Sleep(2000);
Thread.Sleep(2000);
}
});
ActionProgressDialog pd = new ActionProgressDialog(WaitForCancelAction, ProgressBarStyle.Marquee);
pd.ShowDialog(Parent);
ActionProgressDialog pd = new ActionProgressDialog(waitForCancelAction, ProgressBarStyle.Marquee);
pd.ShowDialog(Parent);
}
else
{
return false;
}
}
DoDisconnect(connection);
DoDisconnect(connection);
return true;
}
@ -152,5 +156,28 @@ namespace XenAdmin.Commands
MainWindowCommandInterface.SaveServerList();
MainWindowCommandInterface.RequestRefreshTreeView();
}
private bool AllActionsFinished(IXenConnection connection, bool treatCancelingAsFinished)
{
foreach (ActionBase action in ConnectionsManager.History)
{
if (action.IsCompleted || action is MeddlingAction)
continue;
if (treatCancelingAsFinished)
{
AsyncAction a = action as AsyncAction;
if (a != null && a.Cancelling)
continue;
}
IXenObject xo = action.Pool ?? action.Host ?? action.VM ?? action.SR as IXenObject;
if (xo == null || xo.Connection != connection)
continue;
return false;
}
return true;
}
}
}

View File

@ -34,6 +34,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using XenAdmin.Actions;
using XenAdmin.Actions.GUIActions;
using XenAdmin.Network;
using XenAdmin.Core;
using XenAPI;
@ -100,17 +101,21 @@ namespace XenAdmin.Dialogs.WarningDialogs
private bool CanAddRowAction(ActionBase action)
{
if (connection == null)
return !action.IsCompleted;
if (action.IsCompleted || action is MeddlingAction)
return false;
AsyncAction a = action as AsyncAction;
if (!action.IsCompleted && (a == null || !a.Cancelling))
{
var xo = action.Pool ?? action.Host ?? action.VM ?? action.SR as IXenObject;
if (xo != null && xo.Connection == connection)
return true;
}
return false;
if (a != null && a.Cancelling)
return false;
if (connection == null)
return true;
var xo = action.Pool ?? action.Host ?? action.VM ?? action.SR as IXenObject;
if (xo == null || xo.Connection != connection)
return false;
return true;
}
private void RemoveActionRow(ActionBase action)

View File

@ -1909,19 +1909,19 @@ namespace XenAdmin
bool currentTasks = false;
foreach (ActionBase a in ConnectionsManager.History)
{
if (!(a is MeddlingAction) && !a.IsCompleted)
{
currentTasks = true;
break;
}
if (a is MeddlingAction || a.IsCompleted)
continue;
currentTasks = true;
break;
}
if (currentTasks)
{
e.Cancel = true;
DialogResult result = Program.RunInAutomatedTestMode ? DialogResult.OK :
new Dialogs.WarningDialogs.CloseXenCenterWarningDialog().ShowDialog(this);
if (result == DialogResult.OK)
if (Program.RunInAutomatedTestMode ||
new Dialogs.WarningDialogs.CloseXenCenterWarningDialog().ShowDialog(this) == DialogResult.OK)
{
this.Hide();
@ -1960,7 +1960,7 @@ namespace XenAdmin
a.Cancel();
}
}
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(CloseWhenActionsCanceled));
ThreadPool.QueueUserWorkItem(CloseWhenActionsCanceled);
}
return;
}

View File

@ -33,11 +33,11 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using NUnit.Framework;
using XenAdmin;
using XenAdmin.Actions;
using XenAdmin.Actions.GUIActions;
using XenAdmin.Model;
using XenAdmin.Network;
using XenAdmin.XenSearch;
@ -66,7 +66,7 @@ namespace XenAdminTests.FolderTests
{
Search search = EverythingInFolders();
XmlNode expectedResults = GetExpectedResults(after);
MW(delegate() { ComparerAdapter.CompareResults(search, expectedResults); });
MW(() => ComparerAdapter.CompareResults(search, expectedResults));
}
protected void WaitForActions()
@ -91,10 +91,12 @@ namespace XenAdminTests.FolderTests
private bool HasOutstandingActions()
{
foreach (XenAdmin.Actions.ActionBase a in ConnectionsManager.History)
foreach (ActionBase a in ConnectionsManager.History)
{
if (!a.IsCompleted)
return true;
if (a is MeddlingAction || a.IsCompleted)
continue;
return true;
}
return false;
}

View File

@ -361,23 +361,6 @@ namespace XenAdmin.Actions
}
}
public static bool AllActionsFinishedOrCanceling(IXenConnection connection, IEnumerable<ActionBase> actionBases)
{
foreach (ActionBase action in actionBases)
{
IXenObject xo = (action.Pool as IXenObject) ?? (action.Host as IXenObject) ?? (action.VM as IXenObject) ?? (action.SR as IXenObject);
if (xo == null || xo.Connection != connection)
continue;
AsyncAction a = action as AsyncAction;
if (!action.IsCompleted && (a == null || !a.Cancelling))
{
return false;
}
}
return true;
}
protected static void BestEffort(ref Exception caught, bool expectDisruption, Action func)
{
try

View File

@ -106,44 +106,18 @@ namespace XenAdmin
/// </summary>
public static readonly ChangeableList<Actions.ActionBase> History = new ChangeableList<Actions.ActionBase>();
/// <summary>
/// Use to find out if all actions are completed on the provided connection.
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
public static bool AllActionsFinished(IXenConnection connection)
{
foreach (ActionBase action in History)
{
IXenObject xo = (action.Pool as IXenObject) ??
(action.Host as IXenObject) ??
(action.VM as IXenObject) ??
(action.SR as IXenObject);
if (xo == null || xo.Connection != connection)
continue;
if (!action.IsCompleted)
{
return false;
}
}
return true;
}
public static void CancelAllActions(IXenConnection connection)
{
foreach (ActionBase action in History)
{
IXenObject xo = (action.Pool as IXenObject) ?? (action.Host as IXenObject) ?? (action.VM as IXenObject) ?? (action.SR as IXenObject);
IXenObject xo = action.Pool ?? action.Host ?? action.VM ?? action.SR as IXenObject;
if (xo == null || xo.Connection != connection)
continue;
if (!action.IsCompleted)
{
AsyncAction a = action as AsyncAction;
if (a != null && !a.IsCompleted)
action.Cancel();
}
}
}
}
}
}