Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Making sure running processes don't hang Unity when the domain reload…
Browse files Browse the repository at this point in the history
…s, part deux

Also ensuring that we get data from git commands that output progress
in stderr
  • Loading branch information
shana committed Jun 4, 2018
1 parent 43e1e78 commit 5a6a093
Show file tree
Hide file tree
Showing 35 changed files with 356 additions and 251 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ _NCrunch_GitHub.Unity
build/
TestResult.xml
submodules/
*.stackdump
*.stackdump
*.lastcodeanalysissucceeded
2 changes: 2 additions & 0 deletions src/GitHub.Api/Application/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace GitHub.Unity
public static class ApplicationConfiguration
{
public const int DefaultWebTimeout = 3000;
public const int DefaultGitTimeout = 5000;
public static int WebTimeout { get; set; } = DefaultWebTimeout;
public static int GitTimeout { get; set; } = DefaultGitTimeout;
}
}
45 changes: 9 additions & 36 deletions src/GitHub.Api/Application/ApplicationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace GitHub.Unity
{
abstract class ApplicationManagerBase : IApplicationManager
class ApplicationManagerBase : IApplicationManager
{
protected static ILogging Logger { get; } = LogHelper.GetLogger<IApplicationManager>();

Expand Down Expand Up @@ -45,9 +45,10 @@ protected void Initialize()
{
LogHelper.TracingEnabled = UserSettings.Get(Constants.TraceLoggingKey, false);
ApplicationConfiguration.WebTimeout = UserSettings.Get(Constants.WebTimeoutKey, ApplicationConfiguration.WebTimeout);
ApplicationConfiguration.GitTimeout = UserSettings.Get(Constants.GitTimeoutKey, ApplicationConfiguration.GitTimeout);
Platform.Initialize(ProcessManager, TaskManager);
progress.OnProgress += progressReporter.UpdateProgress;
UsageTracker = new UsageTracker(TaskManager, UserSettings, Environment, InstanceId.ToString());
UsageTracker = new UsageTracker(TaskManager, GitClient, ProcessManager, UserSettings, Environment, InstanceId.ToString());

#if ENABLE_METRICS
var metricsService = new MetricsService(ProcessManager,
Expand Down Expand Up @@ -191,7 +192,6 @@ public void SetupGit(GitInstaller.GitInstallationState state)
if (Environment.RepositoryPath.IsInitialized)
{
ConfigureMergeSettings();
CaptureRepoSize();

GitClient.LfsInstall()
.Catch(e =>
Expand Down Expand Up @@ -300,37 +300,6 @@ private void ConfigureMergeSettings()
}).RunSynchronously();
}

private void CaptureRepoSize()
{
GitClient.CountObjects()
.Finally((success, gitObjects) =>
{
if (success)
{
UsageTracker.UpdateRepoSize(gitObjects.kilobytes);
}
})
.Start();

var gitLfsDataPath = Environment.RepositoryPath.Combine(".git", "lfs");
if (gitLfsDataPath.Exists())
{
var diskUsageTask = Environment.IsWindows
? (IProcessTask<int>)new WindowsDiskUsageTask(gitLfsDataPath, TaskManager.Token)
: new LinuxDiskUsageTask(gitLfsDataPath, TaskManager.Token);

diskUsageTask
.Configure(ProcessManager)
.Finally((success, kilobytes) =>
{
if (success)
{
UsageTracker.UpdateLfsDiskUsage(kilobytes);
}
}).Start();
}
}

public void RestartRepository()
{
if (!Environment.RepositoryPath.IsInitialized)
Expand All @@ -346,8 +315,8 @@ public void RestartRepository()
Logger.Trace($"Got a repository? {(Environment.Repository != null ? Environment.Repository.LocalPath : "null")}");
}

protected abstract void InitializeUI();
protected abstract void InitializationComplete();
protected virtual void InitializeUI() {}
protected virtual void InitializationComplete() {}

private bool disposed = false;
protected virtual void Dispose(bool disposing)
Expand All @@ -356,6 +325,10 @@ protected virtual void Dispose(bool disposing)
{
if (disposed) return;
disposed = true;
if (ProcessManager != null)
{
ProcessManager.Stop();
}
if (TaskManager != null)
{
TaskManager.Dispose();
Expand Down
10 changes: 5 additions & 5 deletions src/GitHub.Api/Git/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface IGitClient
ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null);
ITask<TheVersion> Version(IOutputProcessor<TheVersion> processor = null);
ITask<TheVersion> LfsVersion(IOutputProcessor<TheVersion> processor = null);
ITask<GitCountObjects> CountObjects(IOutputProcessor<GitCountObjects> processor = null);
ITask<int> CountObjects(IOutputProcessor<int> processor = null);
ITask<GitUser> SetConfigNameAndEmail(string username, string email);
ITask<string> GetHead(IOutputProcessor<string> processor = null);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public ITask<TheVersion> LfsVersion(IOutputProcessor<TheVersion> processor = nul
.Configure(processManager);
}

public ITask<GitCountObjects> CountObjects(IOutputProcessor<GitCountObjects> processor = null)
public ITask<int> CountObjects(IOutputProcessor<int> processor = null)
{
return new GitCountObjectsTask(cancellationToken, processor)
.Configure(processManager);
Expand Down Expand Up @@ -151,7 +151,7 @@ public ITask<GitUser> SetConfigNameAndEmail(string username, string email)
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
{
return new GitListLocksTask(local, cancellationToken, processor)
.Configure(processManager);
.Configure(processManager, environment.GitLfsExecutablePath);
}

public ITask<string> Pull(string remote, string branch, IOutputProcessor<string> processor = null)
Expand Down Expand Up @@ -301,14 +301,14 @@ public ITask<string> Lock(NPath file,
IOutputProcessor<string> processor = null)
{
return new GitLockTask(file, cancellationToken, processor)
.Configure(processManager);
.Configure(processManager, environment.GitLfsExecutablePath);
}

public ITask<string> Unlock(NPath file, bool force,
IOutputProcessor<string> processor = null)
{
return new GitUnlockTask(file, force, cancellationToken, processor)
.Configure(processManager);
.Configure(processManager, environment.GitLfsExecutablePath);
}

public ITask<string> GetHead(IOutputProcessor<string> processor = null)
Expand Down
23 changes: 0 additions & 23 deletions src/GitHub.Api/Git/GitCountObjects.cs

This file was deleted.

6 changes: 1 addition & 5 deletions src/GitHub.Api/Git/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@ public void Refresh(CacheType cacheType)
{
var cache = cacheContainer.GetCache(cacheType);
cache.InvalidateData();

// Ensuring that the GitLock cache is kept up to date
cacheContainer.GetCache(CacheType.GitLocks).ValidateData();
}

private void CacheHasBeenInvalidated(CacheType cacheType)
Expand Down Expand Up @@ -309,7 +306,6 @@ private static GitBranch GetLocalGitBranch(string currentBranchName, ConfigBranc
{
var branchName = x.Name;
var trackingName = x.IsTracking ? x.Remote.Value.Name + "/" + branchName : "[None]";
var isActive = branchName == currentBranchName;
return new GitBranch(branchName, trackingName);
}

Expand Down Expand Up @@ -504,4 +500,4 @@ public string Email

protected static ILogging Logger { get; } = LogHelper.GetLogger<User>();
}
}
}
10 changes: 10 additions & 0 deletions src/GitHub.Api/Git/RepositoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public ITask CommitFiles(List<string> files, string message, string body)
public ITask Fetch(string remote)
{
var task = GitClient.Fetch(remote);
task.OnEnd += (_, __, success, ___) =>
{
if (success)
UpdateGitAheadBehindStatus().Start();
};
return HookupHandlers(task, false);
}

Expand All @@ -207,6 +212,11 @@ public ITask Pull(string remote, string branch)
public ITask Push(string remote, string branch)
{
var task = GitClient.Push(remote, branch);
task.OnEnd += (_, __, success, ___) =>
{
if (success)
UpdateGitAheadBehindStatus().Start();
};
return HookupHandlers(task, false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/GitHub.Api/Git/Tasks/GitCountObjectsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace GitHub.Unity
{
class GitCountObjectsTask : ProcessTask<GitCountObjects>
class GitCountObjectsTask : ProcessTask<int>
{
private const string TaskName = "git count-objects";

public GitCountObjectsTask(CancellationToken token, IOutputProcessor<GitCountObjects> processor = null)
public GitCountObjectsTask(CancellationToken token, IOutputProcessor<int> processor = null)
: base(token, processor ?? new GitCountObjectsProcessor())
{
Name = TaskName;
Expand All @@ -19,4 +19,4 @@ public override string ProcessArguments
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
public override string Message { get; set; } = "Counting git objects...";
}
}
}
5 changes: 3 additions & 2 deletions src/GitHub.Api/Git/Tasks/GitListLocksTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ namespace GitHub.Unity
{
class GitListLocksTask : ProcessTaskWithListOutput<GitLock>
{
private const string TaskName = "git lfs locks";
private readonly string args;

public GitListLocksTask(bool local,
CancellationToken token, BaseOutputListProcessor<GitLock> processor = null)
: base(token, processor ?? new LocksOutputProcessor())
{
args = "lfs locks --json";
Name = TaskName;
args = "locks --json";
if (local)
{
args += " --local";
}
Name = args;
}

public override string ProcessArguments => args;
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Git/Tasks/GitLockTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public GitLockTask(string path,
{
Name = TaskName;
Guard.ArgumentNotNullOrWhiteSpace(path, "path");
arguments = String.Format("lfs lock \"{0}\"", path);
arguments = String.Format("lock \"{0}\"", path);
}

public override string ProcessArguments => arguments;
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Git/Tasks/GitUnlockTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public GitUnlockTask(NPath path, bool force,
Guard.ArgumentNotNullOrWhiteSpace(path, "path");

Name = TaskName;
var stringBuilder = new StringBuilder("lfs unlock ");
var stringBuilder = new StringBuilder("unlock ");

if (force)
{
Expand Down
3 changes: 1 addition & 2 deletions src/GitHub.Api/GitHub.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<DefineConstants>DEBUG;TRACE;$(BuildDefs)</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
Expand Down Expand Up @@ -91,7 +91,6 @@
<Compile Include="Cache\CachingClasses.cs" />
<Compile Include="Extensions\ActionExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Git\GitCountObjects.cs" />
<Compile Include="Git\Tasks\GitCheckoutTask.cs" />
<Compile Include="Git\GitAheadBehindStatus.cs" />
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/GitHub.Api/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static class Constants
public const string GitInstallPathKey = "GitInstallPath";
public const string TraceLoggingKey = "EnableTraceLogging";
public const string WebTimeoutKey = "WebTimeout";
public const string GitTimeoutKey = "GitTimeout";
public const string Iso8601Format = @"yyyy-MM-dd\THH\:mm\:ss.fffzzz";
public const string Iso8601FormatZ = @"yyyy-MM-dd\THH\:mm\:ss\Z";
public static readonly string[] Iso8601Formats = {
Expand Down
39 changes: 37 additions & 2 deletions src/GitHub.Api/Metrics/UsageTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,21 @@ private void SendUsage()
}

// if we're here, success!
lock(_lock)
lock (_lock)
{
usageStore = usageLoader.Load(userId);
usageStore.LastSubmissionDate = currentTimeOffset;
usageStore.Model.RemoveReports(currentTimeOffset.Date);
usageLoader.Save(usageStore);
}

// update the repo size for the current report, while we're at it
CaptureRepoSize();
}

protected virtual void CaptureRepoSize()
{}

public virtual void IncrementNumberOfStartups()
{
lock (_lock)
Expand Down Expand Up @@ -336,13 +342,39 @@ public bool Enabled

class UsageTracker : UsageTrackerSync
{
public UsageTracker(ITaskManager taskManager, ISettings userSettings,
public UsageTracker(ITaskManager taskManager, IGitClient gitClient, IProcessManager processManager,
ISettings userSettings,
IEnvironment environment, string instanceId)
: base(userSettings,
new UsageLoader(environment.UserCachePath.Combine(Constants.UsageFile)),
environment.UnityVersion, instanceId)
{
TaskManager = taskManager;
Environment = environment;
GitClient = gitClient;
ProcessManager = processManager;
}

protected override void CaptureRepoSize()
{
try
{
var gitSize = GitClient.CountObjects()
.Catch(_ => true)
.RunSynchronously();
base.UpdateRepoSize(gitSize);

var gitLfsDataPath = Environment.RepositoryPath.Combine(".git", "lfs");
if (gitLfsDataPath.Exists())
{
var lfsSize = new LinuxDiskUsageTask(gitLfsDataPath, TaskManager.Token)
.Configure(ProcessManager)
.Catch(_ => true)
.RunSynchronously();
base.UpdateLfsDiskUsage(lfsSize);
}
}
catch {}
}

public override void IncrementApplicationMenuMenuItemCommandLine() => TaskManager.Run(base.IncrementApplicationMenuMenuItemCommandLine);
Expand All @@ -365,6 +397,9 @@ public UsageTracker(ITaskManager taskManager, ISettings userSettings,
public override void UpdateRepoSize(int kilobytes) => TaskManager.Run(() => base.UpdateRepoSize(kilobytes));

protected ITaskManager TaskManager { get; }
protected IEnvironment Environment { get; }
protected IGitClient GitClient { get; }
public IProcessManager ProcessManager { get; }
}

interface IUsageLoader
Expand Down
Loading

0 comments on commit 5a6a093

Please sign in to comment.