Home > PowerShell, SharePoint, SharePoint 2010 > SharePoint 2010: PowerShell to wait for Web.Config Modifications One-Time Timer Job to Complete

SharePoint 2010: PowerShell to wait for Web.Config Modifications One-Time Timer Job to Complete


When setting up a script to automate installation of numerous WSP solutions and features which (maybe unbeknown to you) use SPWebConfigModification to configure the web.config on your web applications, you may occasionally come across the following error, especially on a multi-server farm environment:

A web configuration modification operation is already running.

Although it is a feature that may do this, don’t forget that some WSP solutions automatically activate a feature during install, so could occur as a result of solution installs too.

The reason for this is that when submitting a batch of web.config changes applied by the SPWebConfigModification class, it schedules a one-time timer job to run asynchronously. When your script continues, the web.config modification may not have been applied yet as the timer job is still scheduled, but if your script tries to activate another feature which also uses SPWebConfigModification, it may fail because SPWebConfigModification will not schedule multiple batches of updates. The first batch must complete, and therefore the one-time timer job will no longer exist, before the next batch can be submitted.

A safe way to avoid this problem is to check for the existence of the web.config modifications timer job “job-webconfig-modifications” and waiting for them to complete before installing or uninstalling WSP each solution or feature.

This problem is not often seen on a single-server environment, especially a stand-alone environment, as the timer job usually runs quickly there, but on multi-server environments the timer job has to be executed on each server and so usually takes a lot longer to complete.

I had originally written some code in C# a few years back when I first encountered this on a MOSS 2007 environment, and last year converted it to PowerShell for use with SharePoint Server 2010. I thought it might come in useful so here it is:

function WaitForWebConfigTimerJobToComplete()
{
WaitForOneTimeTimerJobToComplete "job-webconfig-modifications"
}

function WaitForOneTimeTimerJobToComplete([string]$timerJobName)
{
$timerJob = $null
	do
	{
		# See if the one-time timer job exists (i.e. has been scheduled)
$timerJob = Get-SPTimerJob $timerJobName

		# If it exists then wait and repeat until it no longer exists
if ($timerJob)
		{
			Write-Host "Waiting a while for the '$timerJobName' to complete."
			Sleep 2
		}
	}
while ($timerJob)
}

Use the WaitForWebConfigTimerJobToComplete function to wait until the Web.Config modifications are complete (if any).

You can also use WaitForOneTimeTimerJobToComplete to wait for any other one-time timer job to complete, giving it the timer job’s internal name. This will not work for timer jobs which run on a repeatable schedule as they always exist and the function would just keep waiting. That would require code to check the timer job status and not just check the existence of the timer job.

Advertisements
  1. Peter Halverson
    August 14, 2012 at 17:25

    Thanks for the tip. I had to use “job-webconfig-modification” (no trailing ‘s’) as the job name, however, not sure if this was a change in SharePoint.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: