vSphere

 View Only
  • 1.  wait for a task

    Posted Mar 02, 2010 12:31 AM

    I am cloning a VM and need to wait for the task to complete in my perl script.

    I looked at the SDK documentation ,( on the task and taskinfo object ) i dont see anything for this.

    If any one has sample perl code which does this, it would be really appreciated.

    suresh.



  • 2.  RE: wait for a task
    Best Answer

    Broadcom Employee
    Posted Mar 02, 2010 03:28 AM

    Here is a small function that you can use to wait for a task to complete, it basically loops until the state is success OR if it errors out. Remember, when you get a task back, you only have access to it for ~10min, once it's passed, even if you have the taskRef handle, you can't query it. You'll need to use the taskManager and CreateCollectorForTasks to collect the specific task and query it's status and basically block until it's complete.

    
    my $task = ...
    my $success = "suceess!";
    
    &getStatus($task,$sucess);
    
    
    sub getStatus {
            my ($taskRef,$message) = @_;
    
            my $task_view = Vim::get_view(mo_ref => $taskRef);
            my $taskinfo = $task_view->info->state->val;
            my $continue = 1;
            while ($continue) {
                    my $info = $task_view->info;
                    if ($info->state->val eq 'success') {
                            print $message,"\n";
                            return $info->result;
                            $continue = 0;
                    } elsif ($info->state->val eq 'error') {
                            my $soap_fault = SoapFault->new;
                            $soap_fault->name($info->error->fault);
                            $soap_fault->detail($info->error->fault);
                            $soap_fault->fault_string($info->error->localizedMessage);
                            die "$soap_fault\n";
                    }
                    sleep 5;
                    $task_view->ViewBase::update_view_data();
            }
    }
    

    =========================================================================

    William Lam

    VMware vExpert 2009

    VMware ESX/ESXi scripts and resources at:

    Twitter: @lamw

    VMware Code Central - Scripts/Sample code for Developers and Administrators

    VMware Developer Community

    If you find this information useful, please award points for "correct" or "helpful".



  • 3.  RE: wait for a task

    Posted Mar 02, 2010 07:37 PM

    Thanks William again !!

    i also found that with the vi perl toolkit there are blocking functions correpsonding to the async functions.

    There exist CloneVM which is blocking corresponding to async functions.

    the sdk documentation says taskinfo->state is a string , but it looks like a hash with state->val has the string value.

    thanks again !!