Permalink
Browse files

Bug: closing callback handle on completion.

git-svn-id: https://vmwaretasks.svn.codeplex.com/svn/trunk@33459 9b283d60-5439-405e-af05-b73fd8c4d996
  • Loading branch information...
1 parent 2a671c6 commit 60c9b99d67926e4e842d6f02e8009a27a2fd6e15 @dblock committed Oct 25, 2009
Showing with 78 additions and 48 deletions.
  1. +1 −0 Source/VMWareLib/VMWareJobCallback.cs
  2. +65 −45 Source/VixCOM/061209/Program.cs
  3. +12 −3 Source/VixCOM/061209/VMJobCallback.cs
@@ -28,6 +28,7 @@ public void OnVixEvent(IJob job, int eventType, IVixHandle moreEventInfo)
{
case Constants.VIX_EVENTTYPE_JOB_COMPLETED:
_jobCompleted.Set();
+ new VMWareVixHandle<IJob>(job).Close();
break;
}
}
@@ -1,78 +1,98 @@
using System;
using System.Collections.Generic;
using System.Text;
+using System.Threading;
using VixCOM;
namespace VMWareCrash
{
+
class Program
{
+ private static VixCOM.VixLib vix = new VixLib();
+
/// <summary>
- /// Connect to both VMWare VI and ESX demo, see http://communities.vmware.com/thread/186655.
+ /// AV demo
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
try
{
- VixCOM.VixLib vix = new VixLib();
+ string uri = "https://tubbs.nycapt35k.com/sdk";
+ string vmx = "[adpro-1] snowtest-w2k8/snowtest-w2k8.vmx";
+ string username = "vmuser";
+ string password = "admin123";
+ // connect to a VI host
+ Console.WriteLine("Connecting to {0}", uri);
+ IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER,
+ uri, 0, username, password, 0, null, null);
+ object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE };
+ object hosts = null;
+ ulong rc = connectJob.Wait(connectProperties, ref hosts);
+ if (vix.ErrorIndicatesFailure(rc))
{
- // connect to a VI host
- Console.WriteLine("Connecting to VI host");
- IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER,
- "https://linc.nycapt35k.com/sdk/", 0, "vmuser", "admin123", 0, null, null);
- object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE };
- object hosts = null;
- ulong rc = connectJob.Wait(connectProperties, ref hosts);
- if (vix.ErrorIndicatesFailure(rc)) throw new Exception(vix.GetErrorText(rc, "en-US"));
- IHost host = (IHost)((object[])hosts)[0];
+ ((IVixHandle2)connectJob).Close();
+ throw new Exception(vix.GetErrorText(rc, "en-US"));
+ }
+
+ IHost host = (IHost)((object[])hosts)[0];
+ {
// open a vm
- Console.WriteLine("Opening VM");
- IJob openJob = host.OpenVM("[dbprotect-1] ddoub-red/ddoub-red.vmx", null);
+ Console.WriteLine("Opening {0}", vmx);
+ IJob openJob = host.OpenVM(vmx, null);
object[] openProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE };
object openResults = null;
rc = openJob.Wait(openProperties, ref openResults);
- if (vix.ErrorIndicatesFailure(rc)) throw new Exception(vix.GetErrorText(rc, "en-US"));
- IVM2 vm = (IVM2) ((object[]) openResults)[0];
-
- // number of snapshots
- int nSnapshots = 0;
- vm.GetNumRootSnapshots(out nSnapshots);
- Console.WriteLine("Root snapshots: {0}", nSnapshots);
- List<ISnapshot> snapshots = new List<ISnapshot>();
- for (int i = 0; i < nSnapshots; i++)
+ if (vix.ErrorIndicatesFailure(rc))
{
- Console.WriteLine("Fetching snapshot: {0}", i);
- ISnapshot snapshot = null;
- rc = vm.GetRootSnapshot(i, out snapshot);
- snapshots.Add(snapshot);
- if (vix.ErrorIndicatesFailure(rc)) throw new Exception(vix.GetErrorText(rc, "en-US"));
+ ((IVixHandle2)openJob).Close();
+ throw new Exception(vix.GetErrorText(rc, "en-US"));
}
-
+ Console.WriteLine("Opened {0}", vmx);
+ IVM2 vm = (IVM2)((object[])openResults)[0];
+ ((IVixHandle2)openJob).Close();
// create a snapshot
- Console.WriteLine("Creating snapshot");
- VMWareJobCallback jobDoneCallback = new VMWareJobCallback();
- IJob createSnapshotJob = vm.CreateSnapshot(
- Guid.NewGuid().ToString(),
- Guid.NewGuid().ToString(),
- 0, null, jobDoneCallback);
- jobDoneCallback.WaitForCompletion(10000);
- rc = createSnapshotJob.WaitWithoutResults();
- if (vix.ErrorIndicatesFailure(rc)) throw new Exception(vix.GetErrorText(rc, "en-US"));
- snapshots = null;
- GC.Collect();
- GC.WaitForPendingFinalizers();
-
- // disconnect
- Console.WriteLine("Disconnecting");
- host.Disconnect();
+ string snapshotName = Guid.NewGuid().ToString();
+ Console.WriteLine("Creating snapshot {0}", snapshotName);
+ VMWareJobCallback callback = new VMWareJobCallback();
+ IJob createSnapshotJob = vm.CreateSnapshot(snapshotName, snapshotName, 0, null, callback);
+ object[] createSnapshotProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE };
+ object createSnapshotResults = null;
+ rc = createSnapshotJob.Wait(createSnapshotProperties, ref createSnapshotResults);
+ if (vix.ErrorIndicatesFailure(rc))
+ {
+ ((IVixHandle2)createSnapshotJob).Close();
+ throw new Exception(vix.GetErrorText(rc, "en-US"));
+ }
+ ISnapshot createdSnapshot = (ISnapshot)((object[])createSnapshotResults)[0];
+ ((IVixHandle2)createSnapshotJob).Close();
+ // delete snapshot
+ IJob deleteSnapshotJob = vm.RemoveSnapshot(createdSnapshot, 0, callback);
+ Console.WriteLine("Deleting snapshot {0}", snapshotName);
+ rc = deleteSnapshotJob.WaitWithoutResults();
+ if (vix.ErrorIndicatesFailure(rc))
+ {
+ ((IVixHandle2)deleteSnapshotJob).Close();
+ throw new Exception(vix.GetErrorText(rc, "en-US"));
+ }
+ ((IVixHandle2)deleteSnapshotJob).Close();
+ ((IVixHandle2)createdSnapshot).Close();
+ ((IVixHandle2)vm).Close();
}
+
+ // disconnect
+ Console.WriteLine("Done.");
+ host.Disconnect();
+
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
- Console.WriteLine(ex.Message);
+ Console.WriteLine("ERROR: {0}", ex.Message);
}
}
}
@@ -7,7 +7,7 @@
namespace VMWareCrash
{
/// <summary>
- /// A job completion callback, used with <see ref="Vestris.VMWareLib.VMWareJob" />.
+ /// A job completion callback, used with <see cref="Vestris.VMWareLib.VMWareJob" />.
/// </summary>
public class VMWareJobCallback : ICallback
{
@@ -26,10 +26,19 @@ public void OnVixEvent(IJob job, int eventType, IVixHandle moreEventInfo)
{
switch (eventType)
{
- case VixCOM.Constants.VIX_EVENTTYPE_JOB_COMPLETED:
+ case Constants.VIX_EVENTTYPE_JOB_COMPLETED:
_jobCompleted.Set();
break;
}
+
+ /*
+ * fix: close job object since we are done with this object
+ *
+ * note: this does not invalidate the original job object that specified this as its callback function
+ * i.e. IJob createSnapshotJob = vm.CreateSnapshot(...); createSnapshotJob is still valid and can be used
+ *
+ */
+ ((IVixHandle2)job).Close();
}
/// <summary>
@@ -44,7 +53,7 @@ public bool TryWaitForCompletion(int timeoutInMilliseconds)
/// <summary>
/// Wait for completion of the job with a timeout.
- /// A <see ref="System.TimeoutException" /> occurs if the job hasn't completed within the timeout specified.
+ /// A <see cref="System.TimeoutException" /> occurs if the job hasn't completed within the timeout specified.
/// </summary>
/// <param name="timeoutInMilliseconds">Timeout in milliseconds.</param>
public void WaitForCompletion(int timeoutInMilliseconds)

0 comments on commit 60c9b99

Please sign in to comment.