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

Fixed formatting for Wrapper #52

Merged
merged 1 commit into from
Oct 21, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class JudgeProcessFailedException : Exception
{
public JudgeProcessFailedException(string message, Exception innerException)
: base(message, innerException: innerException) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class LinuxCommandFailedException : Exception
{
public LinuxCommandFailedException(string command, string arguments, string output, string error)
: base($"Linux command failed. Command: {command}, Arguments: {arguments}, Output: {output}, Error: {error}") { }
}
}
24 changes: 12 additions & 12 deletions src/Ilmhub.Judge.Wrapper/JudgeWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class JudgeWrapper : IJudgeWrapper
private readonly ILinuxCommandLine cli;

public JudgeWrapper(ILogger<JudgeWrapper> logger, ILinuxCommandLine cli)
{
{
this.logger = logger;
this.cli = cli;
}
Expand Down Expand Up @@ -47,7 +47,7 @@ public async ValueTask<IExecutionResult> ExecuteJudgerAsync(IExecutionRequest re
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
logger.LogInformation(
"Libjudger.so process finished. Exit code: {exitCode}, Output: {output}, Error: {error}.",
"Libjudger.so process finished. Exit code: {exitCode}, Output: {output}, Error: {error}.",
process.ExitCode,
output,
error);
Expand All @@ -57,12 +57,12 @@ public async ValueTask<IExecutionResult> ExecuteJudgerAsync(IExecutionRequest re
resultObject.Output = output;
return resultObject;
}
catch(JsonException jsonException)
catch (JsonException jsonException)
{
logger.LogError(jsonException, "Failed to deserialize Libjudger.so output.");
throw;
}
catch(Exception ex)
catch (Exception ex)
{
logger.LogWarning(ex, "Libjudger.so process faild while executing {executable}.", request.ExecutablePath);
throw new JudgeProcessFailedException($"Libjudger.so process faild while executing {request.ExecutablePath}", ex);
Expand All @@ -78,7 +78,7 @@ private string BuildArguments(IExecutionRequest request)
AppendSingleArgument(builder, "error_path", request.ErrorPath);
AppendSingleArgument(builder, "log_path", request.LogPath);
AppendSingleArgument(builder, "seccomp_rule_name", request.SeccompRuleName);

// below values have defualt value so we dont need to null-check
builder.Append($" --max_cpu_time={request.CpuTime}");
builder.Append($" --max_real_time={request.RealTime}");
Expand All @@ -92,22 +92,22 @@ private string BuildArguments(IExecutionRequest request)

builder.Append($" --env=\"PATH={Environment.GetEnvironmentVariable("PATH")}\"");

if(request.Environments?.Any() is true)
foreach(var env in request.Environments)
if (request.Environments?.Any() is true)
foreach (var env in request.Environments)
AppendSingleArgument(builder, "env", env);

if(request.Arguments?.Any() is true)
foreach(var arg in request.Arguments)
if (request.Arguments?.Any() is true)
foreach (var arg in request.Arguments)
AppendSingleArgument(builder, "args", arg);

logger.LogInformation("Libjudger arguments: {arguments}", builder.ToString());

return builder.ToString();
}

private void AppendSingleArgument(StringBuilder builder, string key, string value)
private void AppendSingleArgument(StringBuilder builder, string key, string value)
{
if(string.IsNullOrWhiteSpace(value) is false)
if (string.IsNullOrWhiteSpace(value) is false)
builder.Append($" --{key}={value}");
}
}
6 changes: 3 additions & 3 deletions src/Ilmhub.Judge.Wrapper/LinuxCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ValueTask ChangePathModeAsync(string mode, string path, bool recursive =

public ValueTask AddPathOwnerAsync(string owner, string path, bool recursive = false, CancellationToken cancellationToken = default)
=> RunCommandAsync("chown", $"{(recursive ? "-R " : "")}{owner} {path}");

// TODO: People assume this method never throws. Add try catch in the method body here
public async ValueTask<(bool IsSuccess, string Output, string ErrorMessage)> TryRunAsync(
string command,
Expand Down Expand Up @@ -49,7 +49,7 @@ public ValueTask AddPathOwnerAsync(string owner, string path, bool recursive = f
public async ValueTask RunCommandAsync(string command, string arguments, CancellationToken cancellationToken = default)
{
var processResult = await TryRunAsync(command, arguments, cancellationToken);
if(processResult.IsSuccess is false)
if (processResult.IsSuccess is false)
throw new LinuxCommandFailedException(command, arguments, processResult.Output, processResult.ErrorMessage);
}

Expand All @@ -69,4 +69,4 @@ public IEnumerable<string> SplitCommand(string command)

return arguments;
}
}
}
2 changes: 1 addition & 1 deletion src/Ilmhub.Judge.Wrapper/Models/ExecutionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public class ExecutionResult : IExecutionResult
public EExecutionError Error { get; set; }
public string Output { get; set; }
public string ErrorMessage { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Ilmhub.Judge.Wrapper/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public static IServiceCollection AddIlmhubJudgeWrapper(this IServiceCollection s
services.AddTransient<ILinuxCommandLine, LinuxCommandLine>();
return services;
}
}
}
Loading