Skip to content

Commit 4a0d92b

Browse files
Long Running Agent Jobs
1 parent a2ac79a commit 4a0d92b

File tree

2 files changed

+78
-58
lines changed

2 files changed

+78
-58
lines changed

source/checks/Agentv5.Tests.ps1

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,43 @@ Describe "Last Agent Job Run" -Tag LastJobRunTime, Agent -ForEach $InstancesToTe
149149
}
150150

151151

152+
Describe "Long Running Agent Jobs" -Tag LongRunningJob, Agent -ForEach $InstancesToTest {
153+
$skipAgentLongRunningJobs = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.longrunningjobs' }).Value
152154

155+
Context "Testing long running jobs on <_.Name>" {
156+
It "Running job <_.JobName> duration should not be more than <_.ExpectedLongRunningJobPercentage>% extra of the average run time (<_.Average> seconds) on <_.InstanceName>" -Skip:$skipAgentLongRunningJobs -ForEach ($PSItem.LongRunningJobs) {
157+
$PSItem.ActualLongRunningJobPercentage | Should -BeLessThan $PSItem.ExpectedLongRunningJobPercentage -Because "The current running job $($PSItem.JobName) has been running for $($PSItem.Diff) seconds longer than the average run time. This is more than the $($PSItem.ExpectedLongRunningJobPercentage)% specified as the maximum"
158+
}
159+
}
160+
}
153161

162+
# Describe "Long Running Agent Jobs" -Tags LongRunningJob, $filename {
154163

164+
# }
165+
# if ($NotContactable -contains $psitem) {
166+
# Context "Testing long running jobs on $psitem" {
167+
# It "Can't Connect to $Psitem" {
168+
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
169+
# }
170+
# }
171+
# }
172+
# else {
173+
# Context "Testing long running jobs on $psitem" {
174+
# if ($runningjobs) {
175+
# foreach ($runningjob in $runningjobs | Where-Object { $_.AvgSec -ne 0 }) {
176+
# It "Running job $($runningjob.JobName) duration should not be more than $runningjobpercentage % extra of the average run time on $psitem" -Skip:$skip {
177+
# Assert-LongRunningJobs -runningjob $runningjob -runningjobpercentage $runningjobpercentage
178+
# }
179+
# }
180+
# }
181+
# else {
182+
# It "There are no running jobs currently on $psitem" -Skip:$skip {
183+
# $True | SHould -BeTrue
184+
# }
185+
# }
186+
# }
187+
# }
188+
# }
155189

156190

157191

@@ -291,64 +325,7 @@ Describe "Last Agent Job Run" -Tag LastJobRunTime, Agent -ForEach $InstancesToTe
291325
# }
292326
# }
293327
# }
294-
# Describe "Long Running Agent Jobs" -Tags LongRunningJob, $filename {
295-
# $skip = Get-DbcConfigValue skip.agent.longrunningjobs
296-
# $runningjobpercentage = Get-DbcConfigValue agent.longrunningjob.percentage
297-
# if (-not $skip) {
298-
# $query = "SELECT
299-
# JobName,
300-
# AvgSec,
301-
# start_execution_date as StartDate,
302-
# RunningSeconds,
303-
# RunningSeconds - AvgSec AS Diff
304-
# FROM
305-
# (
306-
# SELECT
307-
# j.name AS JobName,
308-
# start_execution_date,
309-
# AVG(DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000'
310-
# + CONVERT(VARCHAR(6),jh.run_duration),6),5,0,':'),3,0,':'))) AS AvgSec,
311-
# ja.start_execution_date as startdate,
312-
# DATEDIFF(second, ja.start_execution_date, GetDate()) AS RunningSeconds
313-
# FROM msdb.dbo.sysjobactivity ja
314-
# JOIN msdb.dbo.sysjobs j
315-
# ON ja.job_id = j.job_id
316-
# JOIN msdb.dbo.sysjobhistory jh
317-
# ON jh.job_id = j.job_id
318-
# WHERE start_execution_date is not null
319-
# AND stop_execution_date is null
320-
# AND run_duration < 235959
321-
# AND run_duration >= 0
322-
# AND ja.start_execution_date > DATEADD(day,-1,GETDATE())
323-
# GROUP BY j.name,j.job_id,start_execution_date,stop_execution_date,ja.job_id
324-
# ) AS t
325-
# ORDER BY JobName;"
326-
# $runningjobs = Invoke-DbaQuery -SqlInstance $PSItem -Database msdb -Query $query
327-
# }
328-
# if ($NotContactable -contains $psitem) {
329-
# Context "Testing long running jobs on $psitem" {
330-
# It "Can't Connect to $Psitem" {
331-
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
332-
# }
333-
# }
334-
# }
335-
# else {
336-
# Context "Testing long running jobs on $psitem" {
337-
# if ($runningjobs) {
338-
# foreach ($runningjob in $runningjobs | Where-Object { $_.AvgSec -ne 0 }) {
339-
# It "Running job $($runningjob.JobName) duration should not be more than $runningjobpercentage % extra of the average run time on $psitem" -Skip:$skip {
340-
# Assert-LongRunningJobs -runningjob $runningjob -runningjobpercentage $runningjobpercentage
341-
# }
342-
# }
343-
# }
344-
# else {
345-
# It "There are no running jobs currently on $psitem" -Skip:$skip {
346-
# $True | SHould -BeTrue
347-
# }
348-
# }
349-
# }
350-
# }
351-
# }
328+
352329

353330

354331

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,49 @@ function Get-AllAgentInfo {
233233

234234
}
235235
'LongRunningJob' {
236+
$query = "SELECT
237+
JobName,
238+
AvgSec,
239+
start_execution_date as StartDate,
240+
RunningSeconds,
241+
RunningSeconds - AvgSec AS Diff
242+
FROM
243+
(
244+
SELECT
245+
j.name AS JobName,
246+
start_execution_date,
247+
AVG(DATEDIFF(SECOND, 0, STUFF(STUFF(RIGHT('000000'
248+
+ CONVERT(VARCHAR(6),jh.run_duration),6),5,0,':'),3,0,':'))) AS AvgSec,
249+
ja.start_execution_date as startdate,
250+
DATEDIFF(second, ja.start_execution_date, GetDate()) AS RunningSeconds
251+
FROM msdb.dbo.sysjobactivity ja
252+
JOIN msdb.dbo.sysjobs j
253+
ON ja.job_id = j.job_id
254+
JOIN msdb.dbo.sysjobhistory jh
255+
ON jh.job_id = j.job_id
256+
WHERE start_execution_date is not null
257+
AND stop_execution_date is null
258+
AND run_duration < 235959
259+
AND run_duration >= 0
260+
AND ja.start_execution_date > DATEADD(day,-1,GETDATE())
261+
GROUP BY j.name,j.job_id,start_execution_date,stop_execution_date,ja.job_id
262+
) AS t
263+
ORDER BY JobName;"
264+
$runningjobs = Invoke-DbaQuery -SqlInstance $Instance -Database msdb -Query $query
265+
266+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'LongRunningJob' -Value (Get-DbcConfigValue agent.longrunningjob.percentage)
236267

268+
$LongRunningJobs = $($runningjobs | Where-Object { $_.AvgSec -ne 0 }).ForEach{
269+
[PSCustomObject]@{
270+
InstanceName = $Instance.Name
271+
JobName = $PSItem.JobName
272+
RunningSeconds = $PSItem.RunningSeconds
273+
Average = $PSItem.AvgSec
274+
Diff = $PSItem.Diff
275+
ExpectedLongRunningJobPercentage = $ConfigValues.LongRunningJob
276+
ActualLongRunningJobPercentage = [math]::Round($PSItem.Diff / $PSItem.AvgSec * 100)
277+
}
278+
}
237279
}
238280
'LastJobRunTime' {
239281
$maxdays = Get-DbcConfigValue agent.failedjob.since
@@ -318,6 +360,7 @@ function Get-AllAgentInfo {
318360
JobOwner = $JobOwner
319361
InvalidJobOwner = $InvalidJobOwner
320362
LastJobRuns = $LastJobRuns
363+
LongRunningJobs = $LongRunningJobs
321364
}
322365
return $testInstanceObject
323366
}

0 commit comments

Comments
 (0)