Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Task Scheduler incorrectly spawns multiple instances per trigger (but need to be able to run multiple instances in parallel)

I have a task set to run an executable every 30 minutes via Windows Task Scheduler (OS: 64bit Windows Server Standard with SP2). This task needs to be able to run multiple instances of itself simultaneously, so this setting is selected: "If the task is already running, then the following rule applies: Run a new instance in parallel". (Reason: the task processes records in a queue table which may be empty or contain hundreds of thousands of records. Each task instance reserves a chunk of records to work on, so the instances won't collide)

The problem is, the task spawns multiple NEW instances at each trigger interval. It should only fire ONE NEW instance every 30 minutes. Often it spawns 2, 3, 4 or more new instances. At this point, the executable can handle the duplicate new instances without significant errors, but the server is doing more work than it needs to, and it just bugs me to no end that the task scheduler is misbehaving in this way. Here is what I have tried so far to fix:

  • Deleted and recreated the task (many times)
  • Rebooted the server
  • Installed this hotfix: http://support.microsoft.com/en-us/kb/2461249
  • Set to run every 30 minutes indefinitely
  • Set to run every 30 minutes daily, for duration of one day
  • Set "Synchronize across time zones" = true
  • Set "Run with highest privileges" = true
  • Set "Delay task for up to random delay of [X] seconds" = false (multiple new - instances are spawned all within the same second)
  • Set "Delay task for up to random delay of [30] seconds" = true (instead of firing during the same second, multiple new instances fire within a 30 second span)
  • Set "If the task fails, restart every 1 minute" = true
  • Set "If the task fails, restart every 1 minute" = false
  • Set "Run task as soon as possible after a scheduled start is missed" = false (if set to true, the problem is worse)

Even more puzzling: some other tasks on this server have the same or similar settings and do not have this problem. They had the problem before the hotfix, but after the hotfix it has been rare. Except for with this one task. What on earth could be the problem?

The exported task settings are below (with XXXX replacing sensitive info). I compared this point for point with another similar task that is not having the issue. The only differences: the working task has a different author, a different exe file, and runs every 5 minutes instead of every 30 minutes.

I'm about to chalk this up as a bug that Microsoft needs to fix some day, but thought I'd offer it up for review here before giving up.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2014-08-27T10:09:33.7980839</Date>
    <Author>XXXX\XXXX</Author>
    <Description>Links newly downloaded images to products. Resizes and uploads different sizes to XXXX. Updates relevant tables. Logs errors.</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT30M</Interval>
        <Duration>P1D</Duration>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2015-02-11T19:06:00Z</StartBoundary>
      <Enabled>true</Enabled>
      <RandomDelay>PT30S</RandomDelay>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>XXXX\XXXX</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings>
    <MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>true</WakeToRun>
    <ExecutionTimeLimit>P1D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\XXXX\XXXX\XXXX.exe</Command>
    </Exec>
  </Actions>
</Task>
like image 468
jay-danger Avatar asked Mar 31 '15 18:03

jay-danger


1 Answers

I was in the same boat, had one task firing off 3 times, and another firing off 9 times. but also a bunch that only fired once as expected. Problem persisted after installing the hotfix as well.

After all my research turned up no good leads, my next step was going to be opening a support case with Microsoft. Before doing so I figured I would try deleting and recreating the task now that I've installed the patch. I started by only deleting and recreating the trigger (which was set to run once a day) and setting it for a different time. Bingo, my problem was fixed!

So I don't know what the key was, whether it was deleting and recreating the trigger after the patch was installed or if it was changing the time, but doing both worked.

Hope this helps!

like image 80
Mike A Avatar answered Sep 29 '22 13:09

Mike A