Michel Barneveld
To unpathed waters, undreamed shores
WaitForWspJob Tool

 

WaitForWspJob.exe is a tool for SharePoint 2007 that will wait for the Admin Service to finish the deployment or retraction of a SharePoint solution (wsp). Download page: http://blog.michelbarneveld.nl/media/p/5623.aspx

Usage

The tool accepts the solution file name or the solution id (guid) and you can give it a timeout value. By default it waits 5 minutes for the solutiion job to finish before it returns control.

Usage:
           WaitForWspJob.exe <wsp> [<timeout>]

Arguments:
           wsp          The name or guid of the SharePoint solution
           timeout      The maximum wait time in minutes. Default: 5

Examples:
WaitForWspJob.exe ExampleSharePointSolution.wsp
WaitForWspJob.exe ExampleSharePointSolution.wsp 10
WaitForWspJob.exe 3ce2bc5d-69b2-47bd-bd89-80d138f4faba

Why do you need it

Well, you don't have to use it ;-) There are other good (GUI) SharePoint solution installers available or you can even use the Solution Management screens in Central Admin or the STSADM commands. But when working with batchfiles and deploying/retracting solutions you might run into errors/weird behaviour. I wrote this tool in a few minutes to solve my issues with deployments with batchscripts.

After deploying or retracting a SharePoint solution, you have to wait till the Admin service has executed the jobs to deploy/retract the solution, before you can delete the solution (in case of retract scenario) or activate the features in the solution (deploy scenario).
Otherwise you will get an error like in below example of deleting the solution directly after retracting it.

D:\>stsadm -o retractsolution -name Sample_Solution_v1.0.wsp -allcontenturls -im
mediate

Timer job successfully created.


D:\>stsadm -o deletesolution -name Sample_Solution_v1.0.wsp

The solution "Sample_Solution_v1.0.wsp" has been deployed in the farm. Please re
tract the deployment before removing the solution.You can also use the -override
parameter to forcibly remove the solution, but you will not be able to retract
the solution deployment.



You can force running the jobs by executing the STSADM command ExecAdmSvcJobs, however that STSADM operation doesn't check if the Admin Service is also running that solution job. So most of the times the job gets executed twice at the same time! This can lead to all kinds of problems. In the deployment scripts I was working on a few weeks ago, this resulted in solutions that couldn't be deployed because they were already deployed according to STSADM, however the Solution Management in Central Admin showed them as not deployed!?! You can use the -force parameter in such a case, but I only want to do that if nothing else works.

Using the ExecAdmSvcJobs after a deployment can lead to messages like: "The job completed successfully, but could not be properly cleaned up.  This job may execute again on this server."
Like in this example:

D:\>stsadm -o deploysolution -name Sample_Solution_v1.0.wsp -url http://sp07dev
-immediate -allowgacdeployment

Timer job successfully created.


D:\>stsadm -o execadmsvcjobs

Executing .
Executing solution-deployment-sample_solution_v1.0.wsp-0.
The solution-deployment-sample_solution_v1.0.wsp-0 job completed successfully, b
ut could not be properly cleaned up. This job may execute again on this server.

Operation completed successfully.



When you get a message as above you can also see that the job was executed twice in the Solution Manager in Central Admin. The message that the solution was deployed is shown twice!

My solution was simple:

  • Make sure that the Admin Service is running
  • Don't use the STSADM command ExecAdmSvcJobs
  • Use WaitForWspJob.exe to wait for the jobs to finish before executing any other command

Example usage in a script:

stsadm -o retractsolution -name Sample_Solution_v1.0.wsp -allcontenturls -immediate
WaitForWspJob.exe Sample_Solution_v1.0.wsp
stsadm -o deletesolution -name Sample_Solution_v1.0.wsp

Example output:

D:\>stsadm -o retractsolution -name Sample_Solution_v1.0.wsp -allcontenturls -im
mediate

Timer job successfully created.


D:\>WaitForWspJob.exe Sample_Solution_v1.0.wsp
Solution (Sample_Solution_v1.0.wsp) found in the solution store.
Wating for solution job to finish
...................
Solution job finished!
Last Operation Result: RetractionSucceeded

D:\>stsadm -o deletesolution -name Sample_Solution_v1.0.wsp

Operation completed successfully.


 

Internals of the tool

So how does the tool determine if there is still a job running?

You can get a SPSolution object to your solution from the solution collection from SPFarm.Local.Solutions. The SPSolution has a property JobExists that returns true if there is still a job running or waiting.
There is also a property JobStatus, but be carefull when you use it, because it will throw an exception the moment the job is finished.

So the tool is nothing more than a loop checking for the JobExists property to go false. In short:

SPSolution wsp = SPFarm.Local.Solutions[wspNameOrGuid];
while (wsp.JobExists)
{
    System.Threading.
Thread.Sleep(1000);
}

The SPSolution class has also properties to determine if the job went successful or not: LastOperationResult, LastOperationDate and LastOperationDetails.

Full source is downloadable at: http://blog.michelbarneveld.nl/media/p/5624.aspx  

 


Posted 04-05-2010 3:08 PM by Michel Barneveld
Filed under: ,

Comments

Girish wrote re: WaitForWspJob Tool
on 04-26-2010 12:08 PM

Can't you just use powershell?

Uninstall-SPSolution -IdentitySample_Solution_v1.0.wsp -AllWebApplication

Michel Barneveld wrote re: WaitForWspJob Tool
on 04-26-2010 2:06 PM

Hi Girish,

I forgot to make it explicit that this is for SharePoint 2007 only, and not for SharePoint 2010. But thanks for the info, I'll take a look at it for SP2010.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

  Enter code:
Copyright © 2003 - 2009 Michel Barneveld
Powered by Community Server (Non-Commercial Edition), by Telligent Systems