Skip to content

Commit

Permalink
Add Endtime and check for Terminating status
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Nov 21, 2024
1 parent 1644272 commit 1250005
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ private async Task<IEnumerable<RequestedSettlementReportDto>> GetSettlementRepor
{
if (settlementReportDto.Status != SettlementReportStatus.Completed)
{
var jobStatus = await _jobHelper.GetSettlementReportsJobStatusAsync(settlementReportDto.JobId!.Id)
var jobResult = await _jobHelper.GetSettlementReportsJobWithStatusAndEndTimeAsync(settlementReportDto.JobId!.Id)
.ConfigureAwait(false);
switch (jobStatus)
switch (jobResult.Status)
{
case JobRunStatus.Completed:
await MarkAsCompletedAsync(settlementReportDto).ConfigureAwait(false);
await MarkAsCompletedAsync(settlementReportDto, jobResult.EndTime).ConfigureAwait(false);
break;
case JobRunStatus.Canceled:
await MarkAsCanceledAsync(settlementReportDto).ConfigureAwait(false);
Expand All @@ -73,7 +73,7 @@ private async Task<IEnumerable<RequestedSettlementReportDto>> GetSettlementRepor
break;
}

results.Add(settlementReportDto with { Status = MapFromJobStatus(jobStatus) });
results.Add(settlementReportDto with { Status = MapFromJobStatus(jobResult.Status) });
}
else
{
Expand All @@ -84,7 +84,7 @@ private async Task<IEnumerable<RequestedSettlementReportDto>> GetSettlementRepor
return results;
}

private async Task MarkAsCompletedAsync(RequestedSettlementReportDto settlementReportDto)
private async Task MarkAsCompletedAsync(RequestedSettlementReportDto settlementReportDto, DateTimeOffset? endTime)
{
ArgumentNullException.ThrowIfNull(settlementReportDto);
ArgumentNullException.ThrowIfNull(settlementReportDto.JobId);
Expand All @@ -93,7 +93,7 @@ private async Task MarkAsCompletedAsync(RequestedSettlementReportDto settlementR
.GetAsync(settlementReportDto.JobId.Id)
.ConfigureAwait(false);

request.MarkAsCompleted(_clock, settlementReportDto.RequestId);
request.MarkAsCompleted(_clock, settlementReportDto.RequestId, endTime);

await _repository
.AddOrUpdateAsync(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public void MarkAsCompleted(IClock clock, GeneratedSettlementReportDto generated
EndedDateTime = clock.GetCurrentInstant();
}

public void MarkAsCompleted(IClock clock, SettlementReportRequestId requestId)
public void MarkAsCompleted(IClock clock, SettlementReportRequestId requestId, DateTimeOffset? endTime)
{
Status = SettlementReportStatus.Completed;
BlobFileName = requestId.Id + ".zip";
EndedDateTime = clock.GetCurrentInstant();
EndedDateTime = endTime?.ToInstant() ?? clock.GetCurrentInstant();
}

public void MarkAsFailed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SettlementReportUpdateStatusTimerTrigger(

[Function(nameof(UpdateStatusForSettlementReports))]
public async Task UpdateStatusForSettlementReports(
[TimerTrigger("0 */5 * * * *")] TimerInfo timer,
[TimerTrigger("0 */1 * * * *")] TimerInfo timer,
FunctionContext executionContext)
{
ArgumentNullException.ThrowIfNull(executionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public async Task<JobRunId> RunSettlementReportsJobAsync(
return new JobRunId(await _jobsApiClient.Jobs.RunNow(job.JobId, CreateParameters(request, marketRole, reportId, actorGln)).ConfigureAwait(false));
}

public async Task<JobRunStatus> GetSettlementReportsJobStatusAsync(long runId)
public async Task<JobRunWithStatusAndEndTime> GetSettlementReportsJobWithStatusAndEndTimeAsync(long runId)
{
var jobRun = await _jobsApiClient.Jobs.RunsGet(runId, false).ConfigureAwait(false);
return ConvertJobStatus(jobRun.Item1);
return new JobRunWithStatusAndEndTime(ConvertJobStatus(jobRun.Item1), jobRun.Item1.EndTime);
}

public Task CancelSettlementReportJobAsync(long runId)
Expand Down Expand Up @@ -136,17 +136,17 @@ private static JobRunStatus ConvertJobStatus(Run jobRun)
return JobRunStatus.Queued;
}

if (jobRun.Status.State is RunStatusState.TERMINATED && jobRun.IsCompleted && jobRun.Status.TerminationDetails.Code is RunTerminationCode.SUCCESS)
if (jobRun.Status.State is RunStatusState.TERMINATED or RunStatusState.TERMINATING && jobRun.IsCompleted && jobRun.Status.TerminationDetails.Code is RunTerminationCode.SUCCESS)
{
return JobRunStatus.Completed;
}

if (jobRun.Status.State is RunStatusState.TERMINATED && jobRun.Status.TerminationDetails.Code is RunTerminationCode.CANCELED or RunTerminationCode.USER_CANCELED)
if (jobRun.Status.State is RunStatusState.TERMINATED or RunStatusState.TERMINATING && jobRun.Status.TerminationDetails.Code is RunTerminationCode.CANCELED or RunTerminationCode.USER_CANCELED)
{
return JobRunStatus.Canceled;
}

if (jobRun.Status.State is RunStatusState.TERMINATED && jobRun.Status.TerminationDetails.Code is
if (jobRun.Status.State is RunStatusState.TERMINATED or RunStatusState.TERMINATING && jobRun.Status.TerminationDetails.Code is
RunTerminationCode.INTERNAL_ERROR
or RunTerminationCode.DRIVER_ERROR
or RunTerminationCode.CLOUD_FAILURE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Task<JobRunId> RunSettlementReportsJobAsync(
SettlementReportRequestId reportId,
string actorGln);

Task<JobRunStatus> GetSettlementReportsJobStatusAsync(long runId);
Task<JobRunWithStatusAndEndTime> GetSettlementReportsJobWithStatusAndEndTimeAsync(long runId);

Task CancelSettlementReportJobAsync(long runId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

public record JobRunWithStatusAndEndTime(JobRunStatus Status, DateTimeOffset? EndTime);

0 comments on commit 1250005

Please sign in to comment.