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

Pause maintenance between repos during same task #303

Merged
merged 1 commit into from
Jan 23, 2020
Merged
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
62 changes: 44 additions & 18 deletions Scalar.Service/MaintenanceTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,15 @@ public void ScheduleRecurringTasks()
this.taskTimers.Add(new Timer(
(state) =>
{
if (!schedule.IgnorePause)
{
if (this.repoRegistry.TryGetMaintenanceDelayTime(out DateTime time))
{
if (time.CompareTo(DateTime.Now) > 0)
{
this.tracer.RelatedInfo($"Maintenance is paused until {time}.");
return;
}
else if (!this.repoRegistry.TryRemovePauseFile(out string error))
{
this.tracer.RelatedWarning($"Failed to remove pause file: {error}");
}
}
}
this.taskQueue.TryEnqueue(
new MaintenanceTask(
this.tracer,
this.fileSystem,
this.scalarVerb,
this.repoRegistry,
this,
schedule.Task));
schedule.Task,
schedule.IgnorePause));
},
state: null,
dueTime: schedule.DueTime,
Expand All @@ -157,21 +142,24 @@ internal class MaintenanceTask : IServiceTask
private readonly IScalarRepoRegistry repoRegistry;
private readonly IRegisteredUserStore registeredUserStore;
private readonly MaintenanceTasks.Task task;
private readonly bool ignorePause;

public MaintenanceTask(
ITracer tracer,
PhysicalFileSystem fileSystem,
IScalarVerbRunner scalarVerb,
IScalarRepoRegistry repoRegistry,
IRegisteredUserStore registeredUserStore,
MaintenanceTasks.Task task)
MaintenanceTasks.Task task,
bool ignorePause = true)
{
this.tracer = tracer;
this.fileSystem = fileSystem;
this.scalarVerb = scalarVerb;
this.repoRegistry = repoRegistry;
this.registeredUserStore = registeredUserStore;
this.task = task;
this.ignorePause = ignorePause;
}

public void Execute()
Expand Down Expand Up @@ -212,16 +200,27 @@ private void RunMaintenanceTaskForRepos(UserAndSession registeredUser)
int repoRemovalFailures = 0;
int reposMaintained = 0;
int reposInRegistryForUser = 0;
bool maintenancePaused = false;

string rootPath;
string errorMessage;
string traceMessage = null;

IEnumerable<ScalarRepoRegistration> reposForUser = this.repoRegistry.GetRegisteredRepos().Where(
x => x.UserId.Equals(registeredUser.UserId, StringComparison.InvariantCultureIgnoreCase));

foreach (ScalarRepoRegistration repoRegistration in reposForUser)
{
++reposInRegistryForUser;

if (maintenancePaused || this.IsMaintenancePaused(out traceMessage))
{
metadata[nameof(traceMessage)] = traceMessage;
maintenancePaused = true;
++reposSkipped;
continue;
}

rootPath = Path.GetPathRoot(repoRegistration.NormalizedRepoRoot);

metadata[nameof(repoRegistration.NormalizedRepoRoot)] = repoRegistration.NormalizedRepoRoot;
Expand Down Expand Up @@ -281,11 +280,38 @@ private void RunMaintenanceTaskForRepos(UserAndSession registeredUser)
metadata.Add(nameof(reposSuccessfullyRemoved), reposSuccessfullyRemoved);
metadata.Add(nameof(repoRemovalFailures), repoRemovalFailures);
metadata.Add(nameof(reposMaintained), reposMaintained);
metadata.Add(nameof(maintenancePaused), maintenancePaused);
this.tracer.RelatedEvent(
EventLevel.Informational,
$"{nameof(this.RunMaintenanceTaskForRepos)}_MaintenanceSummary",
metadata);
}

private bool IsMaintenancePaused(out string traceMessage)
{
if (this.ignorePause)
{
traceMessage = null;
return false;
}

if (this.repoRegistry.TryGetMaintenanceDelayTime(out DateTime time))
{
if (time.CompareTo(DateTime.Now) > 0)
{
traceMessage = $"Maintenance is paused until {time}.";
return true;
}
else if (!this.repoRegistry.TryRemovePauseFile(out string innerError))
{
traceMessage = $"Failed to remove pause file: {innerError}";
return false;
}
}

traceMessage = null;
return false;
}
}

private class MaintenanceSchedule
Expand Down