Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Agent Jobs: Handle case where SQL Server is stopped whilst a job is running #265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Opserver.Core/Data/SQL/SQLInstance.Jobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ public MonitorStatus LastRunMonitorStatus
{
get
{
if (!LastRunStatus.HasValue) return MonitorStatus.Unknown;
// If there isn't a last run status, it's unknown unless it isn't running, has
// a last start date and no last stop date, in which case SQL Server was stopped
// whilst the job was running.
if (!LastRunStatus.HasValue)
return !IsRunning && LastStartDate.HasValue && !LastStopDate.HasValue
? MonitorStatus.Warning
: MonitorStatus.Unknown;
switch (LastRunStatus.Value)
{
case JobStatuses.Succeeded:
Expand Down Expand Up @@ -131,18 +137,22 @@ public string GetFetchSQL(Version v) => @"
j.date_modified DateModified,
j.version_number Version,
Cast(j.enabled as bit) IsEnabled,
Cast(Case When ja.run_requested_date Is Not Null and ja.stop_execution_date Is Null Then 1
Else 0
Cast(Case When ja.run_requested_date Is Not Null
and ja.stop_execution_date Is Null
AND ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC) Then 1
Else 0
End as Bit) IsRunning,
c.name as Category,
jh.run_status LastRunStatus,
jh.message LastRunMessage,
Cast(ja.run_requested_source as int) LastRunRequestedSource,
ja.run_requested_date LastRunRequestedDate,
Coalesce(ja.start_execution_date, msdb.dbo.agent_datetime(jh.run_date, jh.run_time)) LastStartDate,
(Case When ja.run_requested_date Is Not Null and ja.stop_execution_date Is Null
Then DateDiff(Second, ja.run_requested_date, GETDATE())
Else jh.run_duration % 100 + ROUND((jh.run_duration % 10000)/100,0,0)*60 + ROUND((jh.run_duration%1000000)/10000,0,0)*3600
(Case When ja.run_requested_date Is Not Null
and ja.stop_execution_date Is Null
AND ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)
Then DateDiff(Second, ja.run_requested_date, GETDATE())
Else jh.run_duration % 100 + ROUND((jh.run_duration % 10000)/100,0,0)*60 + ROUND((jh.run_duration%1000000)/10000,0,0)*3600
End) LastRunDurationSeconds,
ja.stop_execution_date LastStopDate,
ja.job_history_id LastRunInstanceId,
Expand Down
9 changes: 8 additions & 1 deletion Opserver/Views/SQL/Instance.Jobs.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ Last Run Message: @j.LastRunMessage">
<td>@(j.LastStopDate?.ToRelativeTimeSpan())</td>
<td>@(j.LastRunDuration.HasValue ? j.LastRunDuration.Value.ToTimeStringMini() : "")</td>
}
else if (j.LastStartDate.HasValue && !j.LastStopDate.HasValue)
{
<td>@j.LastRunMonitorStatus.IconSpan() @j.LastRunMonitorStatus.Span("Server Stopped whilst Running") (@lastInitiator)</td>
<td>@(j.LastStartDate?.ToRelativeTimeSpan())</td>
<td></td>
<td></td>
}
else
{
<td colspan="4" class="text-muted">no run history found</td>
<td colspan="4" class="text-muted">no run history found</td>
}
<td class="@(j.IsEnabled ? "" : "text-warning")">@(j.IsEnabled ? "Yes" : "No")</td>
@if (Current.IsInRole(Roles.SQLAdmin))
Expand Down