Skip to content

Commit 700ae2f

Browse files
authored
1703 fix logs file in verbose execution not saved on exception (#1608)
Closes SoftUni-Internal/exam-systems-issues#1703 **Summary of the changes made**: 1. {Describe your changes in the list} 2.
1 parent 0a746a9 commit 700ae2f

File tree

9 files changed

+42
-61
lines changed

9 files changed

+42
-61
lines changed

PubSub/OJS.PubSub.Worker.Models/Submissions/ProcessedSubmissionPubSubModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public ProcessedSubmissionPubSubModel()
2626

2727
public string? WorkerName { get; set; }
2828

29+
public byte[]? VerboseLogFile { get; set; }
30+
2931
public void SetExecutionResult(ExecutionResultServiceModel executionResultModel)
3032
{
3133
this.ExecutionResult = executionResultModel;

Servers/Worker/OJS.Servers.Worker/Consumers/SubmissionsForProcessingConsumer.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,48 @@ namespace OJS.Servers.Worker.Consumers;
22

33
using MassTransit;
44
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Options;
56
using OJS.Services.Common;
67
using OJS.Services.Worker.Business;
78
using OJS.Services.Infrastructure.Extensions;
89
using System;
910
using System.Threading.Tasks;
1011
using OJS.PubSub.Worker.Models.Submissions;
1112
using OJS.Services.Common.Extensions;
12-
using OJS.Services.Common.Models.Submissions;
1313
using OJS.Services.Common.Models.Submissions.ExecutionContext;
1414
using OJS.Services.Infrastructure;
1515
using OJS.Services.Infrastructure.Constants;
16+
using OJS.Services.Worker.Models.Configuration;
1617
using OJS.Workers.Common.Exceptions;
18+
using OJS.Workers.Common.Helpers;
1719
using OJS.Workers.Common.Models;
1820
using ConfigurationException = OJS.Workers.Common.Exceptions.ConfigurationException;
1921

20-
public class SubmissionsForProcessingConsumer : IConsumer<SubmissionForProcessingPubSubModel>
22+
public class SubmissionsForProcessingConsumer(
23+
ISubmissionsBusinessService submissionsBusiness,
24+
IPublisherService publisher,
25+
IHostInfoService hostInfoService,
26+
ILogger<SubmissionsForProcessingConsumer> logger,
27+
IDatesService dates,
28+
IOptions<SubmissionExecutionConfig> executionConfigAccessor)
29+
: IConsumer<SubmissionForProcessingPubSubModel>
2130
{
22-
private readonly ISubmissionsBusinessService submissionsBusiness;
23-
private readonly IPublisherService publisher;
24-
private readonly IHostInfoService hostInfoService;
25-
private readonly ILogger<SubmissionsForProcessingConsumer> logger;
26-
private readonly IDatesService dates;
27-
28-
public SubmissionsForProcessingConsumer(
29-
ISubmissionsBusinessService submissionsBusiness,
30-
IPublisherService publisher,
31-
IHostInfoService hostInfoService,
32-
ILogger<SubmissionsForProcessingConsumer> logger,
33-
IDatesService dates)
34-
{
35-
this.submissionsBusiness = submissionsBusiness;
36-
this.publisher = publisher;
37-
this.hostInfoService = hostInfoService;
38-
this.logger = logger;
39-
this.dates = dates;
40-
}
31+
private readonly SubmissionExecutionConfig executionConfig = executionConfigAccessor.Value;
4132

4233
public async Task Consume(ConsumeContext<SubmissionForProcessingPubSubModel> context)
4334
{
44-
var startedExecutionOn = this.dates.GetUtcNowOffset();
45-
var workerName = this.hostInfoService.GetHostIp();
35+
var startedExecutionOn = dates.GetUtcNowOffset();
36+
var workerName = hostInfoService.GetHostIp();
4637

47-
this.logger.LogStartingProcessingSubmission(context.Message.Id, workerName);
38+
logger.LogStartingProcessingSubmission(context.Message.Id, workerName);
4839

4940
var submissionStartedProcessingPubSubModel = new SubmissionStartedProcessingPubSubModel
5041
{
5142
SubmissionId = context.Message.Id,
5243
ProcessingStartedAt = startedExecutionOn,
5344
};
5445

55-
await this.publisher.Publish(submissionStartedProcessingPubSubModel);
46+
await publisher.Publish(submissionStartedProcessingPubSubModel);
5647

5748
var result = new ProcessedSubmissionPubSubModel(context.Message.Id)
5849
{
@@ -62,9 +53,9 @@ public async Task Consume(ConsumeContext<SubmissionForProcessingPubSubModel> con
6253
try
6354
{
6455
var submission = context.Message.Map<SubmissionServiceModel>();
65-
this.logger.LogExecutingSubmission(submission.Id, submission.TrimDetails());
66-
var executionResult = await this.submissionsBusiness.ExecuteSubmission(submission);
67-
this.logger.LogProducedExecutionResult(submission.Id, executionResult);
56+
logger.LogExecutingSubmission(submission.Id, submission.TrimDetails());
57+
var executionResult = await submissionsBusiness.ExecuteSubmission(submission);
58+
logger.LogProducedExecutionResult(submission.Id, executionResult);
6859

6960
result.SetExecutionResult(executionResult);
7061
}
@@ -75,19 +66,29 @@ public async Task Consume(ConsumeContext<SubmissionForProcessingPubSubModel> con
7566
StrategyException => ExceptionType.Strategy,
7667
SolutionException => ExceptionType.Solution,
7768
ConfigurationException => ExceptionType.Configuration,
78-
_ => ExceptionType.Other
69+
_ => ExceptionType.Other,
7970
};
8071

81-
this.logger.LogErrorProcessingSubmission(context.Message.Id, result.WorkerName, exception);
72+
logger.LogErrorProcessingSubmission(context.Message.Id, result.WorkerName, exception);
8273
result.SetException(exception, true, exceptionType);
8374
}
84-
8575
finally
8676
{
8777
result.SetStartedAndCompletedExecutionOn(startedExecutionOn.UtcDateTime, completedExecutionOn: DateTime.UtcNow);
78+
79+
if (context.Message.Verbosely)
80+
{
81+
// If the submission is marked as verbose, try to read the log file and attach it to the result
82+
var logFilePath = FileHelpers.BuildSubmissionLogFilePath(context.Message.Id);
83+
if (FileHelpers.FileExists(logFilePath))
84+
{
85+
result.VerboseLogFile = await FileHelpers.ReadFileUpToBytes(logFilePath, this.executionConfig.SubmissionVerboseLogFileMaxBytes);
86+
FileHelpers.DeleteFile(logFilePath);
87+
}
88+
}
8889
}
8990

90-
await this.publisher.Publish(result);
91-
this.logger.LogPublishedProcessedSubmission(context.Message.Id, result.WorkerName);
91+
await publisher.Publish(result);
92+
logger.LogPublishedProcessedSubmission(context.Message.Id, result.WorkerName);
9293
}
9394
}

Services/Common/OJS.Services.Common.Models/Submissions/ExecutionResultServiceModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public class ExecutionResultServiceModel : IMapExplicitly
1919

2020
public OutputResult? OutputResult { get; set; }
2121

22-
public byte[]? VerboseLogFile { get; set; }
23-
2422
public void RegisterMappings(IProfileExpression configuration)
2523
=> configuration
2624
.CreateMap(typeof(ExecutionResult<TestResult>), typeof(ExecutionResultServiceModel))

Services/Common/OJS.Services.Common.Models/Submissions/SubmissionExecutionResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public class SubmissionExecutionResult
1616
public DateTime? CompletedExecutionOn { get; set; }
1717

1818
public string? WorkerName { get; set; }
19+
20+
public byte[]? VerboseLogFile { get; set; }
1921
}
2022
}

Services/Common/OJS.Workers/OJS.Workers.Common/IExecutionContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public interface IExecutionContext<TInput>
2020

2121
int MemoryLimit { get; }
2222

23-
int VerboseLogFileMaxBytes { get; }
24-
2523
TInput Input { get; set; }
2624
}
2725
}

Services/Common/OJS.Workers/OJS.Workers.ExecutionStrategies/BaseExecutionStrategy.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,9 @@ public async Task<IExecutionResult<TResult>> SafeExecute<TInput, TResult>(
4646
try
4747
{
4848
executionContext.Code = this.PreprocessCode(executionContext);
49-
var result = await this.InternalExecute(executionContext, new ExecutionResult<TResult>());
49+
return await this.InternalExecute(executionContext, new ExecutionResult<TResult>());
5050

51-
if (submission.Verbosely)
52-
{
53-
// If the submission is marked as verbose, try read the log file and attach it to the result
54-
var logFilePath = FileHelpers.BuildSubmissionLogFilePath(submissionId);
55-
if (FileHelpers.FileExists(logFilePath))
56-
{
57-
result.VerboseLogFile = await FileHelpers.ReadFileUpToBytes(logFilePath, executionContext.VerboseLogFileMaxBytes);
58-
}
59-
else
60-
{
61-
result.ProcessingComment = $"No verbose log file found in {logFilePath}";
62-
}
63-
}
64-
65-
return result;
66-
67-
// Catch logic is handled by the caller
51+
// The caller handles catch logic
6852
}
6953
finally
7054
{
@@ -75,7 +59,6 @@ await Task.Run(() =>
7559
try
7660
{
7761
DirectoryHelpers.SafeDeleteDirectory(this.WorkingDirectory, true);
78-
FileHelpers.DeleteFile(FileHelpers.BuildSubmissionLogFilePath(submissionId));
7962
}
8063
catch (Exception ex)
8164
{

Services/Common/OJS.Workers/OJS.Workers.ExecutionStrategies/Models/ExecutionContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public class ExecutionContext<TInput> : IExecutionContext<TInput>
2222

2323
public int MemoryLimit { get; set; }
2424

25-
public int VerboseLogFileMaxBytes { get; set; }
26-
2725
public TInput Input { get; set; }
2826
}
2927
}

Services/UI/OJS.Services.Ui.Business/Implementations/SubmissionsBusinessService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ await this.transactionsProvider.ExecuteInTransaction(async () =>
505505
await this.submissionsForProcessingData.SetProcessingState(submissionForProcessing, SubmissionProcessingState.Processed);
506506
});
507507

508-
if (executionResult?.VerboseLogFile != null)
508+
if (submissionExecutionResult.VerboseLogFile != null)
509509
{
510-
await this.fileIo.SaveFile(this.GetLogFilePath(submission.Id), executionResult.VerboseLogFile);
510+
await this.fileIo.SaveFile(this.GetLogFilePath(submission.Id), submissionExecutionResult.VerboseLogFile);
511511
}
512512

513513
this.logger.LogSubmissionProcessedSuccessfully(submission.Id, submissionForProcessing);

Services/Worker/OJS.Services.Worker.Business/Implementations/SubmissionExecutor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ private IExecutionContext<TInput> CreateExecutionContext<TInput>(OjsSubmission<T
5757
MemoryLimit = submission.MemoryLimit,
5858
TimeLimit = submission.TimeLimit,
5959
Input = submission.Input,
60-
VerboseLogFileMaxBytes = this.executionConfig.SubmissionVerboseLogFileMaxBytes,
6160
};
6261
}
6362
catch (Exception ex)

0 commit comments

Comments
 (0)