Skip to content

Commit 7e77a5f

Browse files
[SPDBT-4060] Schedule Job Error Handling improvements (#2557)
# Description This PR includes the following proposed change(s): - [SPDBT-4060] Schedule Job Error Handling improvements
1 parent a34f841 commit 7e77a5f

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/Spd.Manager.ScheduleJob/ScheduleJobManager.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,45 @@ public async Task<Unit> Handle(RunScheduleJobSessionCommand cmd, CancellationTok
7373
ScheduleJobSessionResp? resp = await _scheduleJobSessionRepository.GetAsync(cmd.JobSessionId, ct);
7474
if (resp == null)
7575
{
76-
throw new ApiException(HttpStatusCode.BadRequest, "The schedule job session does not exist.");
76+
string err = "The schedule job session does not exist.";
77+
_logger.LogError(err);
78+
throw new ApiException(HttpStatusCode.BadRequest, err);
7779
}
7880

79-
Stopwatch stopwatch = Stopwatch.StartNew();
80-
using var cts = new CancellationTokenSource(); // no timeout, this is the resolve for mysteriously cancelled requests
81-
//request will from bcgov_schedulejob
82-
//filterStr should be like "statecode eq 0 and spd_eligibleforcreditpayment eq 100000001"
83-
RunJobRequest request = new RunJobRequest
81+
try
8482
{
85-
PrimaryTypeName = resp.PrimaryEntity,
86-
PrimaryEntityActionStr = resp.EndPoint,
87-
PrimaryEntityName = resp.PrimaryEntity + "s",
88-
PrimaryEntityFilterStr = resp.FilterStr.Trim(),
89-
PrimaryEntityIdName = resp.PrimaryEntity + "id",
90-
};
91-
var result = await _generalizeScheduleJobRepository.RunJobsAsync(request, cmd.ConcurrentRequests, cts.Token);
92-
stopwatch.Stop();
83+
Stopwatch stopwatch = Stopwatch.StartNew();
84+
using var cts = new CancellationTokenSource(); // no timeout, this is the resolve for mysteriously cancelled requests
85+
//request will from bcgov_schedulejob
86+
//filterStr should be like "statecode eq 0 and spd_eligibleforcreditpayment eq 100000001"
87+
RunJobRequest request = new RunJobRequest
88+
{
89+
PrimaryTypeName = resp.PrimaryEntity,
90+
PrimaryEntityActionStr = resp.EndPoint,
91+
PrimaryEntityName = resp.PrimaryEntity + "s",
92+
PrimaryEntityFilterStr = resp.FilterStr.Trim(),
93+
PrimaryEntityIdName = resp.PrimaryEntity + "id",
94+
};
95+
var result = await _generalizeScheduleJobRepository.RunJobsAsync(request, cmd.ConcurrentRequests, cts.Token);
96+
stopwatch.Stop();
9397

94-
//update result in JobSession
95-
UpdateScheduleJobSessionCmd updateResultCmd = CreateUpdateScheduleJobSessionCmd(cmd.JobSessionId, result, Decimal.Round((decimal)(stopwatch.ElapsedMilliseconds / 1000), 2));
96-
await _scheduleJobSessionRepository.ManageAsync(updateResultCmd, cts.Token);
98+
//update result in JobSession
99+
UpdateScheduleJobSessionCmd updateResultCmd = CreateUpdateScheduleJobSessionCmd(cmd.JobSessionId, result, Decimal.Round((decimal)(stopwatch.ElapsedMilliseconds / 1000), 2));
100+
await _scheduleJobSessionRepository.ManageAsync(updateResultCmd, cts.Token);
101+
}
102+
catch (Exception ex)
103+
{
104+
string err = ex.Message;
105+
if (ex.InnerException != null)
106+
err += ex.InnerException.Message;
107+
_logger.LogError(err);
108+
UpdateScheduleJobSessionCmd updateCmd = new UpdateScheduleJobSessionCmd();
109+
updateCmd.ScheduleJobSessionId = cmd.JobSessionId;
110+
updateCmd.JobSessionStatusCode = JobSessionStatusCode.Failed;
111+
updateCmd.ErrorMsg = err;
112+
//update result in JobSession
113+
await _scheduleJobSessionRepository.ManageAsync(updateCmd, ct);
114+
}
97115

98116
return default;
99117
}

0 commit comments

Comments
 (0)