WebAdministrationDsc icon indicating copy to clipboard operation
WebAdministrationDsc copied to clipboard

xWebAppPool doesn't support Periodic Restart

Open mkeller25 opened this issue 7 years ago • 8 comments

I have a use case where I need to clear the periodicRestart property for an application pool. This is separate from the periodicRestart/schedule property (I've tested it.)

image

See image above. I'm trying to set the property "Regular Time Interval" not the "Specific Times" array. Which is what is supported as far as I can tell.

restartSchedule : Indicates a set of specific local times, in 24 hour format, when the application pool is recycled. The value must be an array of string representations of TimeSpan values. TimeSpan values must be between 00:00:00 and 23:59:59 seconds inclusive, with a granularity of 60 seconds. Setting the value of this property to "" disables the schedule.

I would be happy to contribute this myself except I've:

  1. never contributed anything before and
  2. don't think I'm skilled enough to look at the greater xWebAdministration code and put it properly and
  3. have never even written my own custom dsc resources etc.

What I have done, is write Script resources to handle getting and setting the properties in IIS that I need, so that's what I'm going to include here. My hope is that this will help whoever does actually integrate this into xWebAdministration a little bit easier.

#script to clear the periodic restart value since its not supported via xWebAdministration
        Script ClearAppPoolPeriodicRestart {
              #get the periodic restart value. store the value in a hastable with the key "result"
              GetScript = {
                $periodicrestartvalue = Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='MYAPPPOOL']/recycling/periodicRestart" -name "time"
                $periodicrestartvaluetotalminutes = $periodicrestartvalue.Value.TotalMinutes
                $int = [int]$periodicrestartvaluetotalminutes
                @{ "Result" = $int }
              }

              #use GetScript and check the "Result" key/value pair. If the value = 0, return true. Else return false which
              #will trigger the running of SetScript
              TestScript = {
                $state = $GetScript
                if( $state["Result"] -eq 0 ) 
                {
                    Write-Verbose -Message "The target resource is already in the desired state. No action is required."
                    return $true
                }
                Else
                {
                    Write-Verbose -Message "Periodic restart value does not = 0, running SetScript"
                    return $false
                }
              }

              #if TestScript returns $false, set the web configuration property "time" to a value of 0
              #remove the periodicRestart/schedule property if it exists
              SetScript = {
                Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='MYAPPPOOL']/recycling/periodicRestart" -name "time" -value "00:00:00"
                Remove-WebConfigurationProperty  -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='MYAPPPOOL']/recycling/periodicRestart/schedule" -name "." -AtElement @{value='00:00:00'}
                }
           }

mkeller25 avatar Jun 18 '18 13:06 mkeller25

If you want to give it a try, adding this and creating tests that validates the change, then I would start by looking how a present property is set, for example logEventOnRecycle,

In the Get-TargetResource https://github.com/PowerShell/xWebAdministration/blob/5497e7dc79b08f68852cfaee22b61a3ba17b793f/DSCResources/MSFT_xWebAppPool/MSFT_xWebAppPool.psm1#L85-L93

And it's a parameter in Set- and Test-TargetResource https://github.com/PowerShell/xWebAdministration/blob/5497e7dc79b08f68852cfaee22b61a3ba17b793f/DSCResources/MSFT_xWebAppPool/MSFT_xWebAppPool.psm1#L313

https://github.com/PowerShell/xWebAdministration/blob/5497e7dc79b08f68852cfaee22b61a3ba17b793f/DSCResources/MSFT_xWebAppPool/MSFT_xWebAppPool.psm1#L313

In the mean time I label this as enhancement and help wanted. 🙂

johlju avatar Jun 18 '18 15:06 johlju

Same issue here, re-creating app pools that have multiple apps tied to it (3 on avg). Unfortunately I've had to use another script post-DSC to make this happen. Being able to set the specific times of a recycle is huge for hosted applications. +1 from me!

Chadliel avatar Nov 27 '19 22:11 Chadliel

The property you are looking for is restartTimeLimit.

Indicates the period of time (in minutes) after which the application pool will recycle. The value must be a string representation of a TimeSpan value. The valid range (in minutes) is 0 to 432000. A value of 0 means the application pool does not recycle on a regular interval.

When I use your script the configuration of MYAPPPOOL would be like:

<add name="MYAPPPOOL">
  <recycling>
    <periodicRestart time="00:00:00" />
  </recycling>
</add>

In DSC I could configure it using restartTimeLimit:

xWebAppPool MYAPPPOOL
{
    Name = 'MYAPPPOOL'
    Ensure = 'Present'
    restartTimeLimit = '00:00:00'
}

This would result in:

<add name="MYAPPPOOL">
  <recycling>
    <periodicRestart time="00:00:00" />
  </recycling>
</add>

Hope that helps.

remcoeissing avatar Dec 11 '19 18:12 remcoeissing

Hi Remcoeissing, that works for specific intervals if you want to avoid the MS default 29 hours, but the specific issue here is a specific time (like 4am). This is where it errors and doesn't allow you to set specific times to recycle the app pools.

Specific to my case anyway, I have a for each loop that creates the app pools and Application based on a csv - up to 3 applications per app pool (total of 80-90 applications per server and 30 app pools). I inherited this mess and trying to fix parts of it - used to be 1 to 1. Unfortunately the application doesn't play well with recycles in the middle of the day which causes errors for the users, so we 100% have to do this during off hours. Timers are nice, but if they get rebooted the timers are off unless it's done at our 4am expected recycle time.

Sorry for the wall of text, but figured some context here might help.

Chadliel avatar Dec 12 '19 18:12 Chadliel

@Chadliel Thanks for the context. In that case you are looking for restartSchedule. That seems different from the original issue, which seems the exact opposite to me:

See image above. I'm trying to set the property "Regular Time Interval" not the "Specific Times" array. Which is what is supported as far as I can tell.

But both are supported, the configuration you are looking for should look something like:

xWebAppPool MYAPPPOOL
{
    Name = 'MYAPPPOOL'
    Ensure = 'Present'
    restartTimeLimit = '00:00:00'
    restartSchedule = @('04:00:00')
}

This would result in a configuration that disables the 29 hours, by setting time to 0. The schedule is set at 4 AM. With your 30 application pools you might want to spread them around so that the applications have time to start.

<add name="MYAPPPOOL">
  <recycling>
    <periodicRestart time="00:00:00">
      <schedule>
        <add value="04:00:00" />
      </schedule>
    </periodicRestart>
  </recycling>
</add>

remcoeissing avatar Dec 13 '19 08:12 remcoeissing

I believe I tried it previously with the same error. I will test and share the error details if I'm able to replicate it. Appreciate the response!

Get Outlook for Androidhttps://aka.ms/ghei36


From: Remco Eissing [email protected] Sent: Friday, December 13, 2019 12:43:30 AM To: dsccommunity/xWebAdministration [email protected] Cc: James Paad [email protected]; Mention [email protected] Subject: Re: [dsccommunity/xWebAdministration] xWebAppPool doesn't support Periodic Restart (#371)

@Chadlielhttps://github.com/Chadliel Thanks for the context. In that case you are looking for restartSchedule. That seems different from the original issue, which seems the exact opposite to me:

See image above. I'm trying to set the property "Regular Time Interval" not the "Specific Times" array. Which is what is supported as far as I can tell.

But both are supported, the configuration you are looking for should look something like:

xWebAppPool MYAPPPOOL { Name = 'MYAPPPOOL' Ensure = 'Present' restartTimeLimit = '00:00:00' restartSchedule = @('04:00:00') }

This would result in a configuration that disables the 29 hours, by setting time to 0. The schedule is set at 4 AM. With your 30 application pools you might want to spread them around so that the applications have time to start.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/dsccommunity/xWebAdministration/issues/371?email_source=notifications&email_token=AN4TNYK3SQYS4NSVMBPXZJ3QYNDLFA5CNFSM4FFPCTFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGZJZJI#issuecomment-565353637, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AN4TNYLIFDU2DKOLA4KJ2TTQYNDLFANCNFSM4FFPCTFA.

Chadliel avatar Dec 13 '19 13:12 Chadliel

Sorry for the overly late response, but this did resolve the issue.

Chadliel avatar Apr 14 '22 04:04 Chadliel