More Stylecop WIP

This commit is contained in:
Jordan Brown
2018-11-28 12:57:13 -05:00
parent ca1b9d5f2c
commit 5c9c54c86f
3 changed files with 47 additions and 27 deletions
@@ -33,10 +33,12 @@ namespace Tgstation.Server.Host.Components.Compiler
/// The <see cref="IDatabaseContextFactory"/> for the <see cref="DmbFactory"/>
/// </summary>
readonly IDatabaseContextFactory databaseContextFactory;
/// <summary>
/// The <see cref="IIOManager"/> for the <see cref="DmbFactory"/>
/// </summary>
readonly IIOManager ioManager;
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="DmbFactory"/>
/// </summary>
@@ -46,6 +48,7 @@ namespace Tgstation.Server.Host.Components.Compiler
/// The <see cref="Api.Models.Instance"/> for the <see cref="DmbFactory"/>
/// </summary>
readonly Api.Models.Instance instance;
/// <summary>
/// The <see cref="CancellationTokenSource"/> for <see cref="cleanupTask"/>
/// </summary>
@@ -55,10 +58,12 @@ namespace Tgstation.Server.Host.Components.Compiler
/// <see cref="Task"/> representing calls to <see cref="CleanJob(CompileJob)"/>
/// </summary>
Task cleanupTask;
/// <summary>
/// <see cref="TaskCompletionSource{TResult}"/> resulting in the latest <see cref="DmbProvider"/> yet to exist
/// </summary>
TaskCompletionSource<object> newerDmbTcs;
/// <summary>
/// The latest <see cref="DmbProvider"/>
/// </summary>
@@ -87,7 +92,7 @@ namespace Tgstation.Server.Host.Components.Compiler
}
/// <inheritdoc />
public void Dispose() => cleanupCts.Dispose(); //we don't dispose nextDmbProvider here, since it might be the only thing we have
public void Dispose() => cleanupCts.Dispose(); // we don't dispose nextDmbProvider here, since it might be the only thing we have
/// <summary>
/// Delete the <see cref="Api.Models.Internal.CompileJob.DirectoryName"/> of <paramref name="job"/>
@@ -99,12 +104,13 @@ namespace Tgstation.Server.Host.Components.Compiler
{
var deleteJob = ioManager.DeleteDirectory(job.DirectoryName.ToString(), cleanupCts.Token);
Task otherTask;
//lock (this) //already locked below
// lock (this) //already locked below
otherTask = cleanupTask;
await Task.WhenAll(otherTask, deleteJob).ConfigureAwait(false);
}
lock (this)
{
if (!jobLockCounts.TryGetValue(job.Id, out var currentVal) || currentVal == 1)
{
jobLockCounts.Remove(job.Id);
@@ -116,7 +122,6 @@ namespace Tgstation.Server.Host.Components.Compiler
var decremented = --jobLockCounts[job.Id];
logger.LogTrace("Compile job {0} lock count now: {1}", job.Id, decremented);
}
}
}
/// <inheritdoc />
@@ -156,13 +161,14 @@ namespace Tgstation.Server.Host.Components.Compiler
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken) => databaseContextFactory.UseContext(async (db) =>
{
//where complete clause not necessary, only successful COMPILEjobs get in the db
// where complete clause not necessary, only successful COMPILEjobs get in the db
var cj = await db.CompileJobs.Where(x => x.Job.Instance.Id == instance.Id)
.OrderByDescending(x => x.Job.StoppedAt).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
if (cj == default(CompileJob))
return;
await LoadCompileJob(cj, cancellationToken).ConfigureAwait(false);
//we dont do CleanUnusedCompileJobs here because the watchdog may have plans for them yet
// we dont do CleanUnusedCompileJobs here because the watchdog may have plans for them yet
});
/// <inheritdoc />
@@ -178,12 +184,12 @@ namespace Tgstation.Server.Host.Components.Compiler
if (compileJob == null)
throw new ArgumentNullException(nameof(compileJob));
//ensure we have the entire compile job tree
// ensure we have the entire compile job tree
await databaseContextFactory.UseContext(async db => compileJob = await db.CompileJobs.Where(x => x.Id == compileJob.Id)
.Include(x => x.Job).ThenInclude(x => x.StartedBy)
.Include(x => x.RevisionInformation).ThenInclude(x => x.PrimaryTestMerge).ThenInclude(x => x.MergedBy)
.Include(x => x.RevisionInformation).ThenInclude(x => x.ActiveTestMerges).ThenInclude(x => x.TestMerge).ThenInclude(x => x.MergedBy)
.FirstAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false); //can't wait to see that query
.FirstAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false); // can't wait to see that query
logger.LogTrace("Loading compile job {0}...", compileJob.Id);
var providerSubmitted = false;
@@ -201,7 +207,7 @@ namespace Tgstation.Server.Host.Components.Compiler
if (!(await primaryCheckTask.ConfigureAwait(false) && await secondaryCheckTask.ConfigureAwait(false)))
{
logger.LogWarning("Error loading compile job, .dmb missing!");
return null; //omae wa mou shinderu
return null; // omae wa mou shinderu
}
lock (this)
@@ -231,23 +237,24 @@ namespace Tgstation.Server.Host.Components.Compiler
public async Task CleanUnusedCompileJobs(CompileJob exceptThisOne, CancellationToken cancellationToken)
{
List<long> jobIdsToSkip;
//don't clean locked directories
// don't clean locked directories
lock (this)
jobIdsToSkip = jobLockCounts.Select(x => x.Key).ToList();
List<string> jobUidsToNotErase = null;
//find the uids of locked directories
// find the uids of locked directories
await databaseContextFactory.UseContext(async db =>
{
jobUidsToNotErase = await db.CompileJobs.Where(x => x.Job.Instance.Id == instance.Id && jobIdsToSkip.Contains(x.Id)).Select(x => x.DirectoryName.Value.ToString().ToUpperInvariant()).ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
//add the other exemption
// add the other exemption
if (exceptThisOne != null)
jobUidsToNotErase.Add(exceptThisOne.DirectoryName.Value.ToString().ToUpperInvariant());
//cleanup
// cleanup
await ioManager.CreateDirectory(".", cancellationToken).ConfigureAwait(false);
var directories = await ioManager.GetDirectories(".", cancellationToken).ConfigureAwait(false);
int deleting = 0;
@@ -28,6 +28,7 @@ namespace Tgstation.Server.Host.Components.Compiler
/// The <see cref="IIOManager"/> for the <see cref="DmbProvider"/>
/// </summary>
readonly IIOManager ioManager;
/// <summary>
/// The <see cref="Action"/> to run when <see cref="Dispose"/> is called
/// </summary>
@@ -24,14 +24,17 @@ namespace Tgstation.Server.Host.Components.Compiler
/// Name of the primary directory used for compilation
/// </summary>
public const string ADirectoryName = "A";
/// <summary>
/// Name of the secondary directory used for compilation
/// </summary>
public const string BDirectoryName = "B";
/// <summary>
/// Extension for .dmbs
/// </summary>
public const string DmbExtension = ".dmb";
/// <summary>
/// Extension for .dmes
/// </summary>
@@ -41,42 +44,52 @@ namespace Tgstation.Server.Host.Components.Compiler
/// The <see cref="IByondManager"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IByondManager byond;
/// <summary>
/// The <see cref="IIOManager"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IIOManager ioManager;
/// <summary>
/// The <see cref="StaticFiles.IConfiguration"/> for <see cref="DreamMaker"/>
/// </summary>
readonly StaticFiles.IConfiguration configuration;
/// <summary>
/// The <see cref="ISessionControllerFactory"/> for <see cref="DreamMaker"/>
/// </summary>
readonly ISessionControllerFactory sessionControllerFactory;
/// <summary>
/// The <see cref="ICompileJobConsumer"/> for <see cref="DreamMaker"/>
/// </summary>
readonly ICompileJobConsumer compileJobConsumer;
/// <summary>
/// The <see cref="IApplication"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IApplication application;
/// <summary>
/// The <see cref="IEventConsumer"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IEventConsumer eventConsumer;
/// <summary>
/// The <see cref="IChat"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IChat chat;
/// <summary>
/// The <see cref="IProcessExecutor"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IProcessExecutor processExecutor;
/// <summary>
/// The <see cref="IWatchdog"/> for <see cref="DreamMaker"/>
/// </summary>
readonly IWatchdog watchdog;
/// <summary>
/// The <see cref="ILogger"/> for <see cref="DreamMaker"/>
/// </summary>
@@ -133,13 +146,13 @@ namespace Tgstation.Server.Host.Components.Compiler
{
AllowWebClient = false,
PrimaryPort = portToUse,
SecurityLevel = securityLevel, //all it needs to read the file and exit
SecurityLevel = securityLevel,
StartupTimeout = timeout
};
var dirA = ioManager.ConcatPath(job.DirectoryName.ToString(), ADirectoryName);
job.MinimumSecurityLevel = securityLevel; //needed for the TempDmbProvider
job.MinimumSecurityLevel = securityLevel; // needed for the TempDmbProvider
var provider = new TemporaryDmbProvider(ioManager.ResolvePath(dirA), String.Concat(job.DmeName, DmbExtension), job);
var timeoutAt = DateTimeOffset.Now.AddSeconds(timeout);
@@ -184,13 +197,13 @@ namespace Tgstation.Server.Host.Components.Compiler
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Session controller returned unexpected ApiValidationStatus: {0}", validationStatus));
}
}
throw new JobException("DMAPI validation timed out!");
}
}
/// <summary>
/// Compiles a .dme with DreamMaker
/// Compiles a .dme with DreamMaker
/// </summary>
/// <param name="dreamMakerPath">The path to the DreamMaker executable</param>
/// <param name="job">The <see cref="Models.CompileJob"/> for the operation</param>
@@ -327,8 +340,6 @@ namespace Tgstation.Server.Host.Components.Compiler
var progressTask = ProgressTask();
try
{
var commitInsert = revisionInformation.CommitSha.Substring(0, 7);
string remoteCommitInsert;
if (revisionInformation.CommitSha == revisionInformation.OriginCommitSha)
@@ -366,7 +377,7 @@ namespace Tgstation.Server.Host.Components.Compiler
}
await chatTask.ConfigureAwait(false);
};
}
try
{
@@ -375,18 +386,18 @@ namespace Tgstation.Server.Host.Components.Compiler
var dirA = ioManager.ConcatPath(job.DirectoryName.ToString(), ADirectoryName);
var dirB = ioManager.ConcatPath(job.DirectoryName.ToString(), BDirectoryName);
// copy the repository
logger.LogTrace("Copying repository to game directory...");
//copy the repository
var fullDirA = ioManager.ResolvePath(dirA);
var repoOrigin = repository.Origin;
using (repository)
await repository.CopyTo(fullDirA, cancellationToken).ConfigureAwait(false);
//run precompile scripts
// run precompile scripts
var resolvedGameDirectory = ioManager.ResolvePath(ioManager.ConcatPath(job.DirectoryName.ToString(), ADirectoryName));
await eventConsumer.HandleEvent(EventType.CompileStart, new List<string> { resolvedGameDirectory, repoOrigin }, cancellationToken).ConfigureAwait(false);
//determine the dme
// determine the dme
if (job.DmeName == null)
{
logger.LogTrace("Searching for available .dmes...");
@@ -403,11 +414,12 @@ namespace Tgstation.Server.Host.Components.Compiler
await ModifyDme(job, cancellationToken).ConfigureAwait(false);
//run compiler, verify api
// run compiler
job.ByondVersion = byondLock.Version.ToString();
var exitCode = await RunDreamMaker(byondLock.DreamMakerPath, job, cancellationToken).ConfigureAwait(false);
// verify api
try
{
if (exitCode != 0)
@@ -417,7 +429,7 @@ namespace Tgstation.Server.Host.Components.Compiler
}
catch (JobException)
{
//server never validated or compile failed
// DD never validated or compile failed
await eventConsumer.HandleEvent(EventType.CompileFailure, new List<string> { resolvedGameDirectory, exitCode == 0 ? "1" : "0" }, cancellationToken).ConfigureAwait(false);
throw;
}
@@ -427,12 +439,12 @@ namespace Tgstation.Server.Host.Components.Compiler
logger.LogTrace("Duplicating compiled game...");
//duplicate the dmb et al
// duplicate the dmb et al
await ioManager.CopyDirectory(dirA, dirB, null, cancellationToken).ConfigureAwait(false);
logger.LogTrace("Applying static game file symlinks...");
//symlink in the static data
// symlink in the static data
var symATask = configuration.SymlinkStaticFilesTo(fullDirA, cancellationToken);
var symBTask = configuration.SymlinkStaticFilesTo(ioManager.ResolvePath(dirB), cancellationToken);