PoshCode Archive  Artifact [b2346fcca4]

Artifact b2346fcca4819e6a301f0a0d741f747fbcc92922bff4da836278a3881e95d422:

  • File Stop-stuck-jobs.ps1 — part of check-in [ea4c1b2e23] at 2018-06-10 14:20:19 on branch trunk — Stops stuck jobs and gets info from them (user: Autom8 size: 1060)

# encoding: ascii
# api: powershell
# title: Stop stuck jobs
# description: Stops stuck jobs and gets info from them
# version: 0.1
# author: Autom8
# license: CC0
# x-poshcode-id: 6741
# x-archived: 2017-02-23T01:15:33
# x-published: 2017-02-19T05:36:00
#
#
$ThreadTimeout = 300

#Check for jobs we can timeout
$RunningJobs = get-job -State Running
foreach($RunningJob in $RunningJobs)
{
    $CurrentTime = get-date
    $TimeoutTime = $RunningJob.PSBeginTime
    $TimeoutTime = $TimeoutTime.AddSeconds($ThreadTimeout)

    #The equation here is:
    #if the current time is more than the time the job started + 5 minutes (300 seconds)
    #then its time we get the info about the job and then stop it
    if($CurrentTime -gt $TimeoutTime)
    {
        $Log = @()
        $Log += ("Killing stuck thread " + $RunningJob.Name)
        $Log += $RunningJob.Output
        $Log += $RunningJob.Error

        echo $Log
        $Log >> $LogFileName

        #Stop the job
        $RunningJob | Stop-Job
    }                
}